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 *
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
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
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
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
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
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
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:
1497c478bd9Sstevel@tonic-gate  *	if (building a.out)
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
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
225*1007fd6fSAli 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 
3587c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3617c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
3625aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
363bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3717c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rel));
3727c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += sizeof (Rel);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3767c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3777c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3787c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3817c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3827c478bd9Sstevel@tonic-gate 
383*1007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
3847c478bd9Sstevel@tonic-gate 	return (1);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * i386 Instructions for TLS processing
3897c478bd9Sstevel@tonic-gate  */
390b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * 0x00	movl %gs:0x0, %eax
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * 0x06	addl x(%eax), %eax
3977c478bd9Sstevel@tonic-gate 	 * 0x0c ...
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4007c478bd9Sstevel@tonic-gate };
4017c478bd9Sstevel@tonic-gate 
402b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * 0x00 movl %gs:0x0, %eax
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * 0x06 addl $0x0, %eax
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	0x05, 0x00, 0x00, 0x00, 0x00,
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * 0x0b nop
4137c478bd9Sstevel@tonic-gate 	 * 0x0c
4147c478bd9Sstevel@tonic-gate 	 */
4157c478bd9Sstevel@tonic-gate 	0x90
4167c478bd9Sstevel@tonic-gate };
4177c478bd9Sstevel@tonic-gate 
418b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie_movgs[] = {
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 *	movl %gs:0x0,%eax
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
4237c478bd9Sstevel@tonic-gate };
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
4267c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_POP	0x58	/* popl + reg */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate #define	TLS_NOP		0x90	/* NOP instruction */
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate #define	MODRM_MSK_MOD	0xc0
4337c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RO	0x38
4347c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RM	0x07
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate #define	SIB_MSK_SS	0xc0
4377c478bd9Sstevel@tonic-gate #define	SIB_MSK_IND	0x38
4387c478bd9Sstevel@tonic-gate #define	SIB_MSK_BS	0x07
4397c478bd9Sstevel@tonic-gate 
4405aefb655Srie static Fixupret
4415aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4447c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
445b3fbe5e6Sseizo 	uchar_t		*offset, r1, r2;
4467c478bd9Sstevel@tonic-gate 
447b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
448b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
449bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * IE reference model
4547c478bd9Sstevel@tonic-gate 		 */
4557c478bd9Sstevel@tonic-gate 		switch (rtype) {
4567c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD:
4577c478bd9Sstevel@tonic-gate 			/*
4587c478bd9Sstevel@tonic-gate 			 * Transition:
4597c478bd9Sstevel@tonic-gate 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4607c478bd9Sstevel@tonic-gate 			 *	0x7 call ___tls_get_addr
4617c478bd9Sstevel@tonic-gate 			 *	0xc
4627c478bd9Sstevel@tonic-gate 			 * To:
4637c478bd9Sstevel@tonic-gate 			 *	0x0 movl %gs:0, %eax
4647c478bd9Sstevel@tonic-gate 			 *	0x6 addl x@gotntpoff(r1), %eax
4657c478bd9Sstevel@tonic-gate 			 */
4665aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
467bf994817SAli Bahrami 			    R_386_TLS_GOTIE, arsp, ld_reloc_sym_name));
4687c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_386_TLS_GOTIE;
4697c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 5;
4705aefb655Srie 
4717c478bd9Sstevel@tonic-gate 			/*
472051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4737c478bd9Sstevel@tonic-gate 			 * sequence.
4747c478bd9Sstevel@tonic-gate 			 */
4757c478bd9Sstevel@tonic-gate 			offset -= 3;
4767c478bd9Sstevel@tonic-gate 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4777c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4785aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4795aefb655Srie 
4807c478bd9Sstevel@tonic-gate 			/*
4817c478bd9Sstevel@tonic-gate 			 * set register %r1 into the addl
4827c478bd9Sstevel@tonic-gate 			 * instruction.
4837c478bd9Sstevel@tonic-gate 			 */
4847c478bd9Sstevel@tonic-gate 			offset[0x7] |= r1;
4857c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4865aefb655Srie 
4877c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD_PLT:
4887c478bd9Sstevel@tonic-gate 			/*
4897c478bd9Sstevel@tonic-gate 			 * Fixup done via the TLS_GD relocation
4907c478bd9Sstevel@tonic-gate 			 */
4915aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
492bf994817SAli Bahrami 			    R_386_NONE, arsp, ld_reloc_sym_name));
4937c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * LE reference model
4997c478bd9Sstevel@tonic-gate 	 */
5007c478bd9Sstevel@tonic-gate 	switch (rtype) {
5017c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD:
5027c478bd9Sstevel@tonic-gate 		/*
5037c478bd9Sstevel@tonic-gate 		 * Transition:
5047c478bd9Sstevel@tonic-gate 		 *	0x0 leal x@tlsgd(,r1,1), %eax
5057c478bd9Sstevel@tonic-gate 		 *	0x7 call ___tls_get_addr
5067c478bd9Sstevel@tonic-gate 		 *	0xc
5077c478bd9Sstevel@tonic-gate 		 * To:
5087c478bd9Sstevel@tonic-gate 		 *	0x0 movl %gs:0, %eax
5097c478bd9Sstevel@tonic-gate 		 *	0x6 addl $x@ntpoff, %eax
5107c478bd9Sstevel@tonic-gate 		 *	0xb nop
5117c478bd9Sstevel@tonic-gate 		 *	0xc
5127c478bd9Sstevel@tonic-gate 		 */
5135aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
514bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5177c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 4;
5185aefb655Srie 
5197c478bd9Sstevel@tonic-gate 		/*
520051d39bbSrie 		 * Adjust 'offset' to beginning of instruction
5217c478bd9Sstevel@tonic-gate 		 * sequence.
5227c478bd9Sstevel@tonic-gate 		 */
5237c478bd9Sstevel@tonic-gate 		offset -= 3;
5247c478bd9Sstevel@tonic-gate 		(void) memcpy(offset, tlsinstr_gd_le,
5255aefb655Srie 		    sizeof (tlsinstr_gd_le));
5267c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5275aefb655Srie 
5287c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD_PLT:
5297c478bd9Sstevel@tonic-gate 	case R_386_PLT32:
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * Fixup done via the TLS_GD relocation
5327c478bd9Sstevel@tonic-gate 		 */
5335aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
534bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5357c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5365aefb655Srie 
5377c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM_PLT:
5385aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
539bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5405aefb655Srie 
5417c478bd9Sstevel@tonic-gate 		/*
5427c478bd9Sstevel@tonic-gate 		 * Transition:
5437c478bd9Sstevel@tonic-gate 		 *	call __tls_get_addr()
5447c478bd9Sstevel@tonic-gate 		 * to:
5457c478bd9Sstevel@tonic-gate 		 *	nop
5467c478bd9Sstevel@tonic-gate 		 *	nop
5477c478bd9Sstevel@tonic-gate 		 *	nop
5487c478bd9Sstevel@tonic-gate 		 *	nop
5497c478bd9Sstevel@tonic-gate 		 *	nop
5507c478bd9Sstevel@tonic-gate 		 */
5517c478bd9Sstevel@tonic-gate 		*(offset - 1) = TLS_NOP;
5527c478bd9Sstevel@tonic-gate 		*(offset) = TLS_NOP;
5537c478bd9Sstevel@tonic-gate 		*(offset + 1) = TLS_NOP;
5547c478bd9Sstevel@tonic-gate 		*(offset + 2) = TLS_NOP;
5557c478bd9Sstevel@tonic-gate 		*(offset + 3) = TLS_NOP;
5567c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5575aefb655Srie 
5587c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM:
5595aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
560bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5615aefb655Srie 
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * Transition:
5647c478bd9Sstevel@tonic-gate 		 *
5657c478bd9Sstevel@tonic-gate 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5667c478bd9Sstevel@tonic-gate 		 *  0x06 call ___tls_get_addr
5677c478bd9Sstevel@tonic-gate 		 *
5687c478bd9Sstevel@tonic-gate 		 * to:
5697c478bd9Sstevel@tonic-gate 		 *
5707c478bd9Sstevel@tonic-gate 		 *  0x00 movl %gs:0, %eax
5717c478bd9Sstevel@tonic-gate 		 */
5727c478bd9Sstevel@tonic-gate 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5735aefb655Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5747c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5755aefb655Srie 
5767c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDO_32:
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 *  Instructions:
5797c478bd9Sstevel@tonic-gate 		 *
5807c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5817c478bd9Sstevel@tonic-gate 		 *		to
5827c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5837c478bd9Sstevel@tonic-gate 		 *
5847c478bd9Sstevel@tonic-gate 		 */
5857c478bd9Sstevel@tonic-gate 		offset -= 2;
5867c478bd9Sstevel@tonic-gate 
5875aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
588bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5897c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5907c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5915aefb655Srie 
5927c478bd9Sstevel@tonic-gate 	case R_386_TLS_GOTIE:
5937c478bd9Sstevel@tonic-gate 		/*
5947c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
5957c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
5967c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
5977c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
5987c478bd9Sstevel@tonic-gate 		 *
5997c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
6007c478bd9Sstevel@tonic-gate 		 *
6017c478bd9Sstevel@tonic-gate 		 *  1) movl foo@gotntpoff(%reg1), %reg2
6027c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6037c478bd9Sstevel@tonic-gate 		 *
6047c478bd9Sstevel@tonic-gate 		 *  2) addl foo@gotntpoff(%reg1), %reg2
6057c478bd9Sstevel@tonic-gate 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6067c478bd9Sstevel@tonic-gate 		 *
6077c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
6087c478bd9Sstevel@tonic-gate 		 *
6097c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %reg2
6107c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
6117c478bd9Sstevel@tonic-gate 		 *
6127c478bd9Sstevel@tonic-gate 		 *  2) addl $foo@ntpoff, %reg2
6137c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg2, foo@ntpoff
6147c478bd9Sstevel@tonic-gate 		 *
6157c478bd9Sstevel@tonic-gate 		 * Note: reg1 != 4 (%esp)
6167c478bd9Sstevel@tonic-gate 		 */
6175aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
618bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
6197c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6205aefb655Srie 
6217c478bd9Sstevel@tonic-gate 		offset -= 2;
6227c478bd9Sstevel@tonic-gate 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6237c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
6247c478bd9Sstevel@tonic-gate 			/* case 1 above */
6257c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
6267c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6277c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
6317c478bd9Sstevel@tonic-gate 			/* case 2 above */
6327c478bd9Sstevel@tonic-gate 			assert(offset[0] == 0x03);
6337c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
6347c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6357c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
6407c478bd9Sstevel@tonic-gate 		 */
641de777a60Sab 		{
642de777a60Sab 			Conv_inv_buf_t	inv_buf;
643de777a60Sab 
644*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
645de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
646de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
647bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
648de777a60Sab 			    arsp->rel_isdesc->is_name,
649de777a60Sab 			    EC_OFF(arsp->rel_roffset));
650de777a60Sab 		}
6517c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
6525aefb655Srie 
6537c478bd9Sstevel@tonic-gate 	case R_386_TLS_IE:
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
6567c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
6577c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
6587c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
6597c478bd9Sstevel@tonic-gate 		 *
6607c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
6617c478bd9Sstevel@tonic-gate 		 *  1) movl foo@indntpoff, %eax
6627c478bd9Sstevel@tonic-gate 		 *	0xa1, foo@indntpoff
6637c478bd9Sstevel@tonic-gate 		 *
6647c478bd9Sstevel@tonic-gate 		 *  2) movl foo@indntpoff, %eax
6657c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6667c478bd9Sstevel@tonic-gate 		 *
6677c478bd9Sstevel@tonic-gate 		 *  3) addl foo@indntpoff, %eax
6687c478bd9Sstevel@tonic-gate 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6697c478bd9Sstevel@tonic-gate 		 *
6707c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
6717c478bd9Sstevel@tonic-gate 		 *
6727c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %eax
6737c478bd9Sstevel@tonic-gate 		 *	0xb8, foo@ntpoff
6747c478bd9Sstevel@tonic-gate 		 *
6757c478bd9Sstevel@tonic-gate 		 *  2) movl $foo@ntpoff, %reg
6767c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6777c478bd9Sstevel@tonic-gate 		 *
6787c478bd9Sstevel@tonic-gate 		 *  3) addl $foo@ntpoff, %reg
6797c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg, foo@ntpoff
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6827c478bd9Sstevel@tonic-gate 		offset--;
6837c478bd9Sstevel@tonic-gate 		if (offset[0] == 0xa1) {
6847c478bd9Sstevel@tonic-gate 			/* case 1 above */
6857c478bd9Sstevel@tonic-gate 			offset[0] = 0xb8;	/*  movl */
6867c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		offset--;
6907c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
6917c478bd9Sstevel@tonic-gate 			/* case 2 above */
6927c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6937c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
6947c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6957c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
6987c478bd9Sstevel@tonic-gate 			/* case 3 above */
6997c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7007c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
7017c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
7027c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		/*
7057c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
7067c478bd9Sstevel@tonic-gate 		 */
707de777a60Sab 		{
708de777a60Sab 			Conv_inv_buf_t	inv_buf;
709de777a60Sab 
710*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
711de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
712de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
713bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
714de777a60Sab 			    arsp->rel_isdesc->is_name,
715de777a60Sab 			    EC_OFF(arsp->rel_roffset));
716de777a60Sab 		}
7177c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
722ba2be530Sab static uintptr_t
7235aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7247c478bd9Sstevel@tonic-gate {
725141040e8Srie 	Rel_desc	*arsp;
726bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
72757ef7aa9SRod Evans 	Aliste		idx;
7287c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
7291d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
7307c478bd9Sstevel@tonic-gate 
731bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7327010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7337010c12aSrie 
7347c478bd9Sstevel@tonic-gate 	/*
735141040e8Srie 	 * Process active relocations.
7367c478bd9Sstevel@tonic-gate 	 */
737bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
738bf994817SAli Bahrami 		uchar_t		*addr;
739bf994817SAli Bahrami 		Xword 		value;
740bf994817SAli Bahrami 		Sym_desc	*sdp;
741bf994817SAli Bahrami 		const char	*ifl_name;
742bf994817SAli Bahrami 		Xword		refaddr;
743bf994817SAli Bahrami 		int		moved = 0;
744bf994817SAli Bahrami 		Gotref		gref;
745bf994817SAli Bahrami 		Os_desc		*osp;
7467c478bd9Sstevel@tonic-gate 
747bf994817SAli Bahrami 		/*
748bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
749bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
750bf994817SAli Bahrami 		 */
751bf994817SAli Bahrami 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
752bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
753bf994817SAli Bahrami 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
754bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
755bf994817SAli Bahrami 			continue;
756bf994817SAli Bahrami 		}
7577c478bd9Sstevel@tonic-gate 
758bf994817SAli Bahrami 		/*
759bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
760bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
761bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
762bf994817SAli Bahrami 		 *
763bf994817SAli Bahrami 		 * The got table entries have already been assigned,
764bf994817SAli Bahrami 		 * and we bind to those initial entries.
765bf994817SAli Bahrami 		 */
766bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_DTLS)
767bf994817SAli Bahrami 			gref = GOT_REF_TLSGD;
768bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_MTLS)
769bf994817SAli Bahrami 			gref = GOT_REF_TLSLD;
770bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_STLS)
771bf994817SAli Bahrami 			gref = GOT_REF_TLSIE;
772bf994817SAli Bahrami 		else
773bf994817SAli Bahrami 			gref = GOT_REF_GENERIC;
7747c478bd9Sstevel@tonic-gate 
775bf994817SAli Bahrami 		/*
776bf994817SAli Bahrami 		 * Perform any required TLS fixups.
777bf994817SAli Bahrami 		 */
778bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
779bf994817SAli Bahrami 			Fixupret	ret;
7807c478bd9Sstevel@tonic-gate 
781bf994817SAli Bahrami 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
782bf994817SAli Bahrami 				return (S_ERROR);
783bf994817SAli Bahrami 			if (ret == FIX_DONE)
784bf994817SAli Bahrami 				continue;
785bf994817SAli Bahrami 		}
7867c478bd9Sstevel@tonic-gate 
787bf994817SAli Bahrami 		/*
788bf994817SAli Bahrami 		 * If this is a relocation against a move table, or
789bf994817SAli Bahrami 		 * expanded move table, adjust the relocation entries.
790bf994817SAli Bahrami 		 */
791bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
792bf994817SAli Bahrami 			ld_adj_movereloc(ofl, arsp);
793bf994817SAli Bahrami 
794bf994817SAli Bahrami 		sdp = arsp->rel_sym;
795bf994817SAli Bahrami 		refaddr = arsp->rel_roffset +
796bf994817SAli Bahrami 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
797bf994817SAli Bahrami 
798bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_CLVAL)
799bf994817SAli Bahrami 			value = 0;
800bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8017c478bd9Sstevel@tonic-gate 			/*
802bf994817SAli Bahrami 			 * The value for a symbol pointing to a SECTION
803bf994817SAli Bahrami 			 * is based off of that sections position.
8047c478bd9Sstevel@tonic-gate 			 */
805bf994817SAli Bahrami 			if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
806bf994817SAli Bahrami 				Sym_desc	*sym;
807bf994817SAli Bahrami 				Xword		radd;
808bf994817SAli Bahrami 				uchar_t		*raddr = (uchar_t *)
809bf994817SAli Bahrami 				    arsp->rel_isdesc->is_indata->d_buf +
810bf994817SAli Bahrami 				    arsp->rel_roffset;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 				/*
813bf994817SAli Bahrami 				 * This is a REL platform. Hence, the second
814bf994817SAli Bahrami 				 * argument of ld_am_I_partial() is the value
815bf994817SAli Bahrami 				 * stored at the target address where the
816bf994817SAli Bahrami 				 * relocation is going to be applied.
8177c478bd9Sstevel@tonic-gate 				 */
818bf994817SAli Bahrami 				if (ld_reloc_targval_get(ofl, arsp, raddr,
819bf994817SAli Bahrami 				    &radd) == 0)
820bf994817SAli Bahrami 					return (S_ERROR);
821bf994817SAli Bahrami 				sym = ld_am_I_partial(arsp, radd);
822bf994817SAli Bahrami 				if (sym) {
823bf994817SAli Bahrami 					Sym	*osym = sym->sd_osym;
824b26cc8daSAli Bahrami 
8257c478bd9Sstevel@tonic-gate 					/*
826bf994817SAli Bahrami 					 * The symbol was moved, so adjust the
827bf994817SAli Bahrami 					 * value relative to the new section.
8287c478bd9Sstevel@tonic-gate 					 */
829bf994817SAli Bahrami 					value = sym->sd_sym->st_value;
830bf994817SAli Bahrami 					moved = 1;
831bf994817SAli Bahrami 
832bf994817SAli Bahrami 					/*
833bf994817SAli Bahrami 					 * The original raddend covers the
834bf994817SAli Bahrami 					 * displacement from the section start
835bf994817SAli Bahrami 					 * to the desired address. The value
836bf994817SAli Bahrami 					 * computed above gets us from the
837bf994817SAli Bahrami 					 * section start to the start of the
838bf994817SAli Bahrami 					 * symbol range. Adjust the old raddend
839bf994817SAli Bahrami 					 * to remove the offset from section
840bf994817SAli Bahrami 					 * start to symbol start, leaving the
841bf994817SAli Bahrami 					 * displacement within the range of
842bf994817SAli Bahrami 					 * the symbol.
843bf994817SAli Bahrami 					 */
844bf994817SAli Bahrami 					if (osym->st_value != 0) {
845bf994817SAli Bahrami 						radd -= osym->st_value;
846bf994817SAli Bahrami 						if (ld_reloc_targval_set(ofl,
847bf994817SAli Bahrami 						    arsp, raddr, radd) == 0)
848bf994817SAli Bahrami 							return (S_ERROR);
849b26cc8daSAli Bahrami 					}
850b26cc8daSAli Bahrami 				}
851bf994817SAli Bahrami 			}
852bf994817SAli Bahrami 			if (!moved) {
853bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
854bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
855bf994817SAli Bahrami 					value += sdp->sd_isc->
856bf994817SAli Bahrami 					    is_osdesc->os_shdr->sh_addr;
857bf994817SAli Bahrami 			}
858bf994817SAli Bahrami 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
859bf994817SAli Bahrami 				value -= ofl->ofl_tlsphdr->p_vaddr;
8602926dd2eSrie 
861bf994817SAli Bahrami 		} else if (IS_SIZE(arsp->rel_rtype)) {
862bf994817SAli Bahrami 			/*
863bf994817SAli Bahrami 			 * Size relocations require the symbols size.
864bf994817SAli Bahrami 			 */
865bf994817SAli Bahrami 			value = sdp->sd_sym->st_size;
86608278a5eSRod Evans 
867bf994817SAli Bahrami 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
868bf994817SAli Bahrami 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
869bf994817SAli Bahrami 			/*
870bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
871bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
872bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
873bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
874bf994817SAli Bahrami 			 */
875bf994817SAli Bahrami 			value = ld_calc_plt_addr(sdp, ofl);
876bf994817SAli Bahrami 
877bf994817SAli Bahrami 		} else
878bf994817SAli Bahrami 			value = sdp->sd_sym->st_value;
879bf994817SAli Bahrami 
880bf994817SAli Bahrami 		/*
881bf994817SAli Bahrami 		 * Relocation against the GLOBAL_OFFSET_TABLE.
882bf994817SAli Bahrami 		 */
883bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
884bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
885bf994817SAli Bahrami 			return (S_ERROR);
886bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
887bf994817SAli Bahrami 
888bf994817SAli Bahrami 		/*
889bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
890bf994817SAli Bahrami 		 * sections virtual address to the reference address.
891bf994817SAli Bahrami 		 */
892bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
893bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0))
894bf994817SAli Bahrami 			refaddr +=
895bf994817SAli Bahrami 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
896bf994817SAli Bahrami 
897bf994817SAli Bahrami 		/*
898bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
899bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
900bf994817SAli Bahrami 		 */
901bf994817SAli Bahrami 		if (IS_PLT(arsp->rel_rtype)) {
902bf994817SAli Bahrami 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
90308278a5eSRod Evans 				value = ld_calc_plt_addr(sdp, ofl);
904bf994817SAli Bahrami 		}
90508278a5eSRod Evans 
906bf994817SAli Bahrami 		/*
907bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
908bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
909bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
910bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
911bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
912bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
913bf994817SAli Bahrami 		 */
914bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
915bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
916bf994817SAli Bahrami 			Xword		R1addr;
917bf994817SAli Bahrami 			uintptr_t	R2addr;
918bf994817SAli Bahrami 			Word		gotndx;
919bf994817SAli Bahrami 			Gotndx		*gnp;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 			/*
922bf994817SAli Bahrami 			 * Perform relocation against GOT table.  Since this
923bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
924bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
925bf994817SAli Bahrami 			 *
926bf994817SAli Bahrami 			 * Calculate offset into GOT at which to apply
927bf994817SAli Bahrami 			 * the relocation.
9287c478bd9Sstevel@tonic-gate 			 */
929bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
930bf994817SAli Bahrami 			assert(gnp);
9317c478bd9Sstevel@tonic-gate 
932bf994817SAli Bahrami 			if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
933bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx + 1;
934bf994817SAli Bahrami 			else
935bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx;
936bf994817SAli Bahrami 
937bf994817SAli Bahrami 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 			/*
940bf994817SAli Bahrami 			 * Add the GOTs data's offset.
9417c478bd9Sstevel@tonic-gate 			 */
942bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
943bf994817SAli Bahrami 
944bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
945bf994817SAli Bahrami 			    M_MACH, SHT_REL, arsp, R1addr, value,
946bf994817SAli Bahrami 			    ld_reloc_sym_name));
9477c478bd9Sstevel@tonic-gate 
948141040e8Srie 			/*
949bf994817SAli Bahrami 			 * And do it.
950141040e8Srie 			 */
951bf994817SAli Bahrami 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
952bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
953bf994817SAli Bahrami 			else
954bf994817SAli Bahrami 				*(Xword *)R2addr = value;
955bf994817SAli Bahrami 			continue;
956bf994817SAli Bahrami 
957bf994817SAli Bahrami 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
958bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
959bf994817SAli Bahrami 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
960bf994817SAli Bahrami 
961bf994817SAli Bahrami 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
962bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
963bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
964bf994817SAli Bahrami 			    refaddr;
965bf994817SAli Bahrami 
966bf994817SAli Bahrami 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
967bf994817SAli Bahrami 		    (((flags & FLG_OF_RELOBJ) == 0) ||
968bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
969bf994817SAli Bahrami 			value -= refaddr;
970bf994817SAli Bahrami 
971bf994817SAli Bahrami 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
972bf994817SAli Bahrami 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
973bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
974bf994817SAli Bahrami 			Gotndx	*gnp;
975bf994817SAli Bahrami 
976bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
977bf994817SAli Bahrami 			assert(gnp);
978bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
979bf994817SAli Bahrami 			if (arsp->rel_rtype == R_386_TLS_IE) {
980bf994817SAli Bahrami 				value += ofl->ofl_osgot->os_shdr->sh_addr;
981bf994817SAli Bahrami 			}
9827c478bd9Sstevel@tonic-gate 
983bf994817SAli Bahrami 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
984bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
985bf994817SAli Bahrami 			Gotndx *gnp;
9867c478bd9Sstevel@tonic-gate 
987bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
988bf994817SAli Bahrami 			    GOT_REF_GENERIC, ofl, NULL);
989bf994817SAli Bahrami 			assert(gnp);
990bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9917c478bd9Sstevel@tonic-gate 
992bf994817SAli Bahrami 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
993bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
994bf994817SAli Bahrami 			Xword	tlsstatsize;
9957c478bd9Sstevel@tonic-gate 
996bf994817SAli Bahrami 			/*
997bf994817SAli Bahrami 			 * This is the LE TLS reference model.  Static
998bf994817SAli Bahrami 			 * offset is hard-coded.
999bf994817SAli Bahrami 			 */
1000bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
1001bf994817SAli Bahrami 			    M_TLSSTATALIGN);
1002bf994817SAli Bahrami 			value = tlsstatsize - value;
10037c478bd9Sstevel@tonic-gate 
1004bf994817SAli Bahrami 			/*
1005bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a
1006bf994817SAli Bahrami 			 * negative offset that can be added to the
1007bf994817SAli Bahrami 			 * thread pointer.
1008bf994817SAli Bahrami 			 */
1009bf994817SAli Bahrami 			if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
1010bf994817SAli Bahrami 			    (arsp->rel_rtype == R_386_TLS_LE))
1011bf994817SAli Bahrami 				value = -value;
1012bf994817SAli Bahrami 		}
10137c478bd9Sstevel@tonic-gate 
1014bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_file)
1015bf994817SAli Bahrami 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1016bf994817SAli Bahrami 		else
1017bf994817SAli Bahrami 			ifl_name = MSG_INTL(MSG_STR_NULL);
1018141040e8Srie 
1019bf994817SAli Bahrami 		/*
1020bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1021bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1022bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1023bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1024bf994817SAli Bahrami 		 * End users should never see this.
1025bf994817SAli Bahrami 		 */
1026bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1027bf994817SAli Bahrami 			Conv_inv_buf_t	inv_buf;
10287c478bd9Sstevel@tonic-gate 
1029*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1030bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1031bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
1032bf994817SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
1033bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name);
1034bf994817SAli Bahrami 			return (S_ERROR);
1035bf994817SAli Bahrami 		}
1036141040e8Srie 
1037bf994817SAli Bahrami 		/*
1038bf994817SAli Bahrami 		 * Get the address of the data item we need to modify.
1039bf994817SAli Bahrami 		 */
1040bf994817SAli Bahrami 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1041bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1042141040e8Srie 
1043bf994817SAli Bahrami 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1044bf994817SAli Bahrami 		    M_MACH, SHT_REL, arsp, EC_NATPTR(addr), value,
1045bf994817SAli Bahrami 		    ld_reloc_sym_name));
1046bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
1047141040e8Srie 
1048bf994817SAli Bahrami 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1049bf994817SAli Bahrami 		    ofl->ofl_size) || (arsp->rel_roffset >
1050bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
1051bf994817SAli Bahrami 			Conv_inv_buf_t	inv_buf;
1052bf994817SAli Bahrami 			int		class;
10537c478bd9Sstevel@tonic-gate 
1054bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1055bf994817SAli Bahrami 			    ofl->ofl_size)
1056bf994817SAli Bahrami 				class = ERR_FATAL;
10577c478bd9Sstevel@tonic-gate 			else
1058bf994817SAli Bahrami 				class = ERR_WARNING;
10597c478bd9Sstevel@tonic-gate 
1060*1007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1061bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1062bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1063bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1064bf994817SAli Bahrami 			    EC_ADDR((uintptr_t)addr -
1065bf994817SAli Bahrami 			    (uintptr_t)ofl->ofl_nehdr));
10667c478bd9Sstevel@tonic-gate 
1067bf994817SAli Bahrami 			if (class == ERR_FATAL) {
1068bf994817SAli Bahrami 				return_code = S_ERROR;
1069bf994817SAli Bahrami 				continue;
10707c478bd9Sstevel@tonic-gate 			}
1071bf994817SAli Bahrami 		}
10727c478bd9Sstevel@tonic-gate 
1073bf994817SAli Bahrami 		/*
1074bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1075bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
1076bf994817SAli Bahrami 		 */
1077bf994817SAli Bahrami 		if (moved)
1078bf994817SAli Bahrami 			value -= *addr;
10797c478bd9Sstevel@tonic-gate 
1080bf994817SAli Bahrami 		/*
1081bf994817SAli Bahrami 		 * If we have a replacement value for the relocation
1082bf994817SAli Bahrami 		 * target, put it in place now.
1083bf994817SAli Bahrami 		 */
1084bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_NADDEND) {
1085bf994817SAli Bahrami 			Xword addend = arsp->rel_raddend;
1086cce0e03bSab 
1087bf994817SAli Bahrami 			if (ld_reloc_targval_set(ofl, arsp, addr, addend) == 0)
1088bf994817SAli Bahrami 				return (S_ERROR);
1089bf994817SAli Bahrami 		}
1090cce0e03bSab 
1091bf994817SAli Bahrami 		/*
1092bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1093bf994817SAli Bahrami 		 */
1094bf994817SAli Bahrami 		if (OFL_DO_RELOC(ofl)) {
1095bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1096bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1097*1007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
1098*1007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
1099bf994817SAli Bahrami 				return_code = S_ERROR;
1100*1007fd6fSAli Bahrami 			}
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	return (return_code);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
1106141040e8Srie /*
1107141040e8Srie  * Add an output relocation record.
1108141040e8Srie  */
1109ba2be530Sab static uintptr_t
11105aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11117c478bd9Sstevel@tonic-gate {
1112141040e8Srie 	Rel_desc	*orsp;
1113141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
11177c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11187c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
11197c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
11207c478bd9Sstevel@tonic-gate 	 */
1121635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11227c478bd9Sstevel@tonic-gate 		return (1);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
11267c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
11277c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11307c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
11317c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		/*
11347c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
11357c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
11367c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
11377c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
11387c478bd9Sstevel@tonic-gate 		 */
11397c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11400bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11417c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11427c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11437c478bd9Sstevel@tonic-gate 			else
11447c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1145141040e8Srie 		} else {
114608278a5eSRod Evans 			Os_desc *osp;
114708278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11487c478bd9Sstevel@tonic-gate 
114908278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
115008278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11517c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11527c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11537c478bd9Sstevel@tonic-gate 			}
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
1157bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1158bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1159bf994817SAli Bahrami 		return (S_ERROR);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
11627c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11637c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
11647c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11657c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
11667c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11677c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
11687c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11697c478bd9Sstevel@tonic-gate 	else
1170bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rel);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
11737c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	/*
11767c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
11777c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
11787c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
11797c478bd9Sstevel@tonic-gate 	 */
11807c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
11817c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	/*
1184141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11857c478bd9Sstevel@tonic-gate 	 */
1186141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11877c478bd9Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	/*
11907c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11917c478bd9Sstevel@tonic-gate 	 */
11927c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11937c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11965aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11977c478bd9Sstevel@tonic-gate 	}
11985aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11995aefb655Srie 	    M_MACH, orsp));
12007c478bd9Sstevel@tonic-gate 	return (1);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
12057c478bd9Sstevel@tonic-gate  */
1206ba2be530Sab static uintptr_t
12075aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12087c478bd9Sstevel@tonic-gate {
12091d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12107c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12110bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/*
12147c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
12157c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
12167c478bd9Sstevel@tonic-gate 	 * then
12177c478bd9Sstevel@tonic-gate 	 *	build R_386_RELATIVE
12187c478bd9Sstevel@tonic-gate 	 * fi
12197c478bd9Sstevel@tonic-gate 	 */
12207c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12212926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12227c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12237c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
12247c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12257c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12267c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12277c478bd9Sstevel@tonic-gate 		Word	ortype = rsp->rel_rtype;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_RELATIVE;
12305aefb655Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12317c478bd9Sstevel@tonic-gate 			return (S_ERROR);
12327c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
12367c478bd9Sstevel@tonic-gate 	 * If the relocation is against a 'non-allocatable' section
12377c478bd9Sstevel@tonic-gate 	 * and we can not resolve it now - then give a warning
12387c478bd9Sstevel@tonic-gate 	 * message.
12397c478bd9Sstevel@tonic-gate 	 *
12407c478bd9Sstevel@tonic-gate 	 * We can not resolve the symbol if either:
12417c478bd9Sstevel@tonic-gate 	 *	a) it's undefined
12427c478bd9Sstevel@tonic-gate 	 *	b) it's defined in a shared library and a
12437c478bd9Sstevel@tonic-gate 	 *	   COPY relocation hasn't moved it to the executable
12447c478bd9Sstevel@tonic-gate 	 *
12457c478bd9Sstevel@tonic-gate 	 * Note: because we process all of the relocations against the
12467c478bd9Sstevel@tonic-gate 	 *	text segment before any others - we know whether
12477c478bd9Sstevel@tonic-gate 	 *	or not a copy relocation will be generated before
12487c478bd9Sstevel@tonic-gate 	 *	we get here (see reloc_init()->reloc_segments()).
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12517c478bd9Sstevel@tonic-gate 	    ((shndx == SHN_UNDEF) ||
12527c478bd9Sstevel@tonic-gate 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12537c478bd9Sstevel@tonic-gate 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1254bf994817SAli Bahrami 		Conv_inv_buf_t	inv_buf;
1255bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1256de777a60Sab 
12577c478bd9Sstevel@tonic-gate 		/*
12587c478bd9Sstevel@tonic-gate 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12597c478bd9Sstevel@tonic-gate 		 * section - then silently ignore that the relocation
12607c478bd9Sstevel@tonic-gate 		 * can not be resolved.
12617c478bd9Sstevel@tonic-gate 		 */
1262bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12637c478bd9Sstevel@tonic-gate 			return (0);
1264*1007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1265de777a60Sab 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12667c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
1267bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
12687c478bd9Sstevel@tonic-gate 		return (1);
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12737c478bd9Sstevel@tonic-gate 	 */
12745aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
1277ba2be530Sab static uintptr_t
12785aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12817c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12821d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12837c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
1286d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1287d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12887c478bd9Sstevel@tonic-gate 	 */
1289d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12907c478bd9Sstevel@tonic-gate 		/*
1291d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12927c478bd9Sstevel@tonic-gate 		 */
12937c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12947c478bd9Sstevel@tonic-gate 
1295d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12967c478bd9Sstevel@tonic-gate 			/*
1297d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12987c478bd9Sstevel@tonic-gate 			 */
129957ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
130057ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
13017c478bd9Sstevel@tonic-gate 
1302d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1303d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
130457ef7aa9SRod Evans 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
1305d326b23bSrie 					return (S_ERROR);
1306d326b23bSrie 			}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 			/*
1309d326b23bSrie 			 * IE access model.
13107c478bd9Sstevel@tonic-gate 			 */
1311d326b23bSrie 			if (IS_TLS_IE(rtype)) {
1312d326b23bSrie 				if (ld_add_actrel(FLG_REL_STLS,
13137c478bd9Sstevel@tonic-gate 				    rsp, ofl) == S_ERROR)
13147c478bd9Sstevel@tonic-gate 					return (S_ERROR);
1315d326b23bSrie 
1316d326b23bSrie 				/*
1317d326b23bSrie 				 * A non-pic shared object needs to adjust the
1318d326b23bSrie 				 * active relocation (indntpoff).
1319d326b23bSrie 				 */
1320d326b23bSrie 				if (((flags & FLG_OF_EXEC) == 0) &&
1321d326b23bSrie 				    (rtype == R_386_TLS_IE)) {
1322d326b23bSrie 					rsp->rel_rtype = R_386_RELATIVE;
1323d326b23bSrie 					return (ld_add_outrel(NULL, rsp, ofl));
1324d326b23bSrie 				}
1325d326b23bSrie 				return (1);
13267c478bd9Sstevel@tonic-gate 			}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 			/*
1329d326b23bSrie 			 * Fixups are required for other executable models.
13307c478bd9Sstevel@tonic-gate 			 */
13315aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13327c478bd9Sstevel@tonic-gate 			    rsp, ofl));
13337c478bd9Sstevel@tonic-gate 		}
1334d326b23bSrie 
13357c478bd9Sstevel@tonic-gate 		/*
1336d326b23bSrie 		 * LE access model.
13377c478bd9Sstevel@tonic-gate 		 */
13387c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13395aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13407c478bd9Sstevel@tonic-gate 
13415aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13425aefb655Srie 		    rsp, ofl));
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/*
1346d326b23bSrie 	 * Building a shared object.
1347d326b23bSrie 	 *
1348d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13497c478bd9Sstevel@tonic-gate 	 */
135057ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135157ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
1352d326b23bSrie 
1353d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
135457ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
13557c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1356d326b23bSrie 
135757ef7aa9SRod Evans 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135857ef7aa9SRod Evans 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
1359d326b23bSrie 
1360d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1361d326b23bSrie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1362d326b23bSrie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13637c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13647c478bd9Sstevel@tonic-gate 	}
1365d326b23bSrie 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1368d326b23bSrie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1369d326b23bSrie 	 * symbol now, and prepare for the PLT magic.
13707c478bd9Sstevel@tonic-gate 	 */
13717c478bd9Sstevel@tonic-gate 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1372f5a18a30Srie 		Sym_desc	*tlsgetsym;
13737c478bd9Sstevel@tonic-gate 
13745aefb655Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1375f5a18a30Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13767c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1377d326b23bSrie 
13787c478bd9Sstevel@tonic-gate 		rsp->rel_sym = tlsgetsym;
13797c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_PLT32;
1380d326b23bSrie 
13815aefb655Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13827c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1383d326b23bSrie 
13847c478bd9Sstevel@tonic-gate 		rsp->rel_sym = sdp;
13857c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = rtype;
13867c478bd9Sstevel@tonic-gate 		return (1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13905aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13917c478bd9Sstevel@tonic-gate 
13925aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
1396ba2be530Sab static uintptr_t
139757ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
139857ef7aa9SRod Evans     Rel_desc *rsp, Sym_desc *sdp)
13997c478bd9Sstevel@tonic-gate {
140057ef7aa9SRod Evans 	Gotndx	gn, *gnp;
14017c478bd9Sstevel@tonic-gate 	uint_t	gotents;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (pgnp)
14047c478bd9Sstevel@tonic-gate 		return (1);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14077c478bd9Sstevel@tonic-gate 		gotents = 2;
14087c478bd9Sstevel@tonic-gate 	else
14097c478bd9Sstevel@tonic-gate 		gotents = 1;
14107c478bd9Sstevel@tonic-gate 
141157ef7aa9SRod Evans 	gn.gn_addend = 0;
141257ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
141357ef7aa9SRod Evans 	gn.gn_gotref = gref;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
141857ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
141957ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
142057ef7aa9SRod Evans 				return (S_ERROR);
142157ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
142257ef7aa9SRod Evans 			ofl->ofl_tlsldgotndx = gnp;
142357ef7aa9SRod Evans 		}
14247c478bd9Sstevel@tonic-gate 		return (1);
14257c478bd9Sstevel@tonic-gate 	}
14267c478bd9Sstevel@tonic-gate 
142757ef7aa9SRod Evans 	/*
142857ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
142957ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
143057ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
143157ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
143257ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
143357ef7aa9SRod Evans 	 */
143457ef7aa9SRod Evans 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
14357c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	return (1);
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
1440ba2be530Sab static void
14415aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14427c478bd9Sstevel@tonic-gate {
14437c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14447c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1445141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate /*
14497c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14507c478bd9Sstevel@tonic-gate  */
1451ba2be530Sab static uintptr_t
1452d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14537c478bd9Sstevel@tonic-gate {
14541d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
14551d9df23bSab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1456d326b23bSrie 
14577c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1458d326b23bSrie 		Sym_desc	*sdp;
14597c478bd9Sstevel@tonic-gate 
14605aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1461635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1462d326b23bSrie 			uchar_t	*genptr;
1463d326b23bSrie 
1464d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14657c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14667c478bd9Sstevel@tonic-gate 			/* LINTED */
14677c478bd9Sstevel@tonic-gate 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1468ba2be530Sab 			if (bswap)
1469ba2be530Sab 				/* LINTED */
1470ba2be530Sab 				*(Word *)genptr =
1471ba2be530Sab 				    /* LINTED */
1472ba2be530Sab 				    ld_bswap_Word(*(Word *)genptr);
14737c478bd9Sstevel@tonic-gate 		}
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14787c478bd9Sstevel@tonic-gate 	 * entry is:
14797c478bd9Sstevel@tonic-gate 	 *  if (building a.out) {
14807c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]		    # the address of the link map entry
14817c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]		    # the address of rtbinder
14827c478bd9Sstevel@tonic-gate 	 *  } else {
14837c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
14847c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
14857c478bd9Sstevel@tonic-gate 	 *  }
14867c478bd9Sstevel@tonic-gate 	 */
1487d326b23bSrie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1488b3fbe5e6Sseizo 		uchar_t *pltent;
14897c478bd9Sstevel@tonic-gate 
1490b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1491d326b23bSrie 		if (!(flags & FLG_OF_SHAROBJ)) {
14927c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
14937c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_DISP;
14947c478bd9Sstevel@tonic-gate 			pltent += 2;
14957c478bd9Sstevel@tonic-gate 			/* LINTED */
14967c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1497de777a60Sab 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1498ba2be530Sab 			if (bswap)
1499ba2be530Sab 				/* LINTED */
1500ba2be530Sab 				*(Word *)pltent =
1501ba2be530Sab 				    /* LINTED */
1502ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15037c478bd9Sstevel@tonic-gate 			pltent += 4;
15047c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15057c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_DISP_IND;
15067c478bd9Sstevel@tonic-gate 			pltent += 2;
15077c478bd9Sstevel@tonic-gate 			/* LINTED */
15087c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1509de777a60Sab 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1510ba2be530Sab 			if (bswap)
1511ba2be530Sab 				/* LINTED */
1512ba2be530Sab 				*(Word *)pltent =
1513ba2be530Sab 				    /* LINTED */
1514ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15157c478bd9Sstevel@tonic-gate 		} else {
15167c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15177c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_REG_DISP;
15187c478bd9Sstevel@tonic-gate 			pltent += 2;
15197c478bd9Sstevel@tonic-gate 			/* LINTED */
15207c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1521de777a60Sab 			    M_GOT_ENTSIZE);
1522ba2be530Sab 			if (bswap)
1523ba2be530Sab 				/* LINTED */
1524ba2be530Sab 				*(Word *)pltent =
1525ba2be530Sab 				    /* LINTED */
1526ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15277c478bd9Sstevel@tonic-gate 			pltent += 4;
15287c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15297c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_REG_DISP_IND;
15307c478bd9Sstevel@tonic-gate 			pltent += 2;
15317c478bd9Sstevel@tonic-gate 			/* LINTED */
15327c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1533de777a60Sab 			    M_GOT_ENTSIZE);
1534ba2be530Sab 			if (bswap)
1535ba2be530Sab 				/* LINTED */
1536ba2be530Sab 				*(Word *)pltent =
1537ba2be530Sab 				    /* LINTED */
1538ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	return (1);
15427c478bd9Sstevel@tonic-gate }
1543ba2be530Sab 
1544ba2be530Sab 
1545ba2be530Sab 
1546ba2be530Sab /*
1547ba2be530Sab  * Template for generating "void (*)(void)" function
1548ba2be530Sab  */
1549ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1550ba2be530Sab /* 0x00 */	0xc3				/* ret */
1551ba2be530Sab };
1552ba2be530Sab 
1553ba2be530Sab 
1554ba2be530Sab 
15553c573fccSAli Bahrami /*
15563c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15573c573fccSAli Bahrami  *
15583c573fccSAli Bahrami  * entry:
15593c573fccSAli Bahrami  *
15603c573fccSAli Bahrami  *	base - base address of section being filled
15613c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15623c573fccSAli Bahrami  *	cnt - # bytes to be filled
15633c573fccSAli Bahrami  *
15643c573fccSAli Bahrami  * exit:
15653c573fccSAli Bahrami  *	The fill has been completed.
15663c573fccSAli Bahrami  */
15673c573fccSAli Bahrami static void
15683c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
15693c573fccSAli Bahrami {
15703c573fccSAli Bahrami 	/*
15713c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
15723c573fccSAli Bahrami 	 * There are no alignment constraints.
15733c573fccSAli Bahrami 	 */
15743c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
15753c573fccSAli Bahrami }
15763c573fccSAli Bahrami 
15773c573fccSAli Bahrami 
1578ba2be530Sab /*
1579ba2be530Sab  * Return the ld_targ definition for this target.
1580ba2be530Sab  */
1581ba2be530Sab const Target *
1582ba2be530Sab ld_targ_init_x86(void)
1583ba2be530Sab {
1584ba2be530Sab 	static const Target _ld_targ = {
1585ba2be530Sab 		{			/* Target_mach */
1586ba2be530Sab 			M_MACH,			/* m_mach */
1587ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1588ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1589ba2be530Sab 			M_CLASS,		/* m_class */
1590ba2be530Sab 			M_DATA,			/* m_data */
1591ba2be530Sab 
1592ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1593ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1594bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1595ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
159669112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1597ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1598ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1599ba2be530Sab 
1600ba2be530Sab 			/* Relocation type codes */
1601ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1602ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1603ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1604ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1605ba2be530Sab 			M_R_NUM,		/* m_r_num */
1606ba2be530Sab 			M_R_NONE,		/* m_r_none */
1607ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1608ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1609ba2be530Sab 
1610ba2be530Sab 			/* Relocation related constants */
1611ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1612ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1613ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1614ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1615ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1616ba2be530Sab 
1617ba2be530Sab 			/* GOT related constants */
1618ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1619ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1620ba2be530Sab 
1621ba2be530Sab 			/* PLT related constants */
1622ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1623ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1624ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1625ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1626ba2be530Sab 
16277e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16287e16fca0SAli Bahrami 			SHT_PROGBITS,		/* m_sht_unwind */
16297e16fca0SAli Bahrami 
1630ba2be530Sab 			M_DT_REGISTER,		/* m_dt_register */
1631ba2be530Sab 		},
1632ba2be530Sab 		{			/* Target_machid */
1633ba2be530Sab 			M_ID_ARRAY,		/* id_array */
1634ba2be530Sab 			M_ID_BSS,		/* id_bss */
1635ba2be530Sab 			M_ID_CAP,		/* id_cap */
163608278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
163708278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1638ba2be530Sab 			M_ID_DATA,		/* id_data */
1639ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1640ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1641ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1642ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1643ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1644ba2be530Sab 			M_ID_GOT,		/* id_got */
1645ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1646ba2be530Sab 			M_ID_HASH,		/* id_hash */
1647ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1648ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1649ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1650ba2be530Sab 			M_ID_NOTE,		/* id_note */
1651ba2be530Sab 			M_ID_NULL,		/* id_null */
1652ba2be530Sab 			M_ID_PLT,		/* id_plt */
1653ba2be530Sab 			M_ID_REL,		/* id_rel */
1654ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1655ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1656ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1657ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1658ba2be530Sab 			M_ID_TEXT,		/* id_text */
1659ba2be530Sab 			M_ID_TLS,		/* id_tls */
1660ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1661ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1662ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
16637e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1664ba2be530Sab 			M_ID_USER,		/* id_user */
1665ba2be530Sab 			M_ID_VERSION,		/* id_version */
1666ba2be530Sab 		},
1667ba2be530Sab 		{			/* Target_nullfunc */
1668ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1669ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1670ba2be530Sab 		},
16713c573fccSAli Bahrami 		{			/* Target_fillfunc */
16723c573fccSAli Bahrami 			execfill		/* ff_execfill */
16733c573fccSAli Bahrami 		},
1674ba2be530Sab 		{			/* Target_machrel */
1675ba2be530Sab 			reloc_table,
1676ba2be530Sab 
1677ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1678ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1679ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1680ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1681ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1682ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1683ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1684ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1685ba2be530Sab 			NULL,			/* mr_reloc_register */
1686ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1687ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1688ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1689ba2be530Sab 			NULL,			/* mr_assign_got */
169057ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1691ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1692ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1693ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1694ba2be530Sab 			NULL,			/* mr_allocate_got */
1695ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1696ba2be530Sab 		},
1697ba2be530Sab 		{			/* Target_machsym */
1698ba2be530Sab 			NULL,			/* ms_reg_check */
1699ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1700ba2be530Sab 			NULL,			/* ms_is_regsym */
1701ba2be530Sab 			NULL,			/* ms_reg_find */
1702ba2be530Sab 			NULL			/* ms_reg_enter */
1703ba2be530Sab 		}
1704ba2be530Sab 	};
1705ba2be530Sab 
1706ba2be530Sab 	return (&_ld_targ);
1707ba2be530Sab }
1708