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  *
27cce0e03bSab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31ba2be530Sab /* Get the x86 version of the relocation engine */
32ba2be530Sab #define	DO_RELOC_LIBLD_X86
33ba2be530Sab 
347c478bd9Sstevel@tonic-gate #include	<string.h>
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
375aefb655Srie #include	<debug.h>
385aefb655Srie #include	<reloc.h>
39ba2be530Sab #include	<i386/machdep_x86.h>
407c478bd9Sstevel@tonic-gate #include	"msg.h"
417c478bd9Sstevel@tonic-gate #include	"_libld.h"
427c478bd9Sstevel@tonic-gate 
43ba2be530Sab /* Forward declarations */
44ba2be530Sab static Gotndx	*ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *);
45ba2be530Sab static Xword	ld_calc_got_offset(Rel_desc *, Ofl_desc *);
46ba2be530Sab 
47ba2be530Sab 
48ba2be530Sab static Word
495aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	Rel *	rel = (Rel *)reloc;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	/* LINTED */
54ba2be530Sab 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
557c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
567c478bd9Sstevel@tonic-gate 	reld->rel_raddend = 0;
577c478bd9Sstevel@tonic-gate 	reld->rel_typedata = 0;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
62ba2be530Sab static void
635aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
647c478bd9Sstevel@tonic-gate {
655aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
68ba2be530Sab static void
695aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
727c478bd9Sstevel@tonic-gate 		/*
737c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
747c478bd9Sstevel@tonic-gate 		 */
757c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
767c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
80ba2be530Sab static void
81d326b23bSrie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
827c478bd9Sstevel@tonic-gate {
835aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
845aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
855aefb655Srie 		if (ofl->ofl_osgot)
865aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
875aefb655Srie 		else
885aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
895aefb655Srie 		(*dyn)++;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
93ba2be530Sab static Xword
945aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	Xword	value;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
997c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1007c478bd9Sstevel@tonic-gate 	return (value);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
1057c478bd9Sstevel@tonic-gate  *	if (building a.out)
1067c478bd9Sstevel@tonic-gate  *		JMP	*got_off
1077c478bd9Sstevel@tonic-gate  *	else
1087c478bd9Sstevel@tonic-gate  *		JMP	*got_off@GOT(%ebx)
1097c478bd9Sstevel@tonic-gate  *	PUSHL	&rel_off
1107c478bd9Sstevel@tonic-gate  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1137c478bd9Sstevel@tonic-gate  *	so the first pass through the plt jumps back here, jumping
1147c478bd9Sstevel@tonic-gate  *	in turn to the first plt entry, which jumps to the dynamic
1157c478bd9Sstevel@tonic-gate  *	linker.	 The dynamic linker then patches the GOT, rerouting
1167c478bd9Sstevel@tonic-gate  *	future plt calls to the proper destination.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static void
1197c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1207c478bd9Sstevel@tonic-gate {
121b3fbe5e6Sseizo 	uchar_t		*pltent, *gotent;
1227c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1237c478bd9Sstevel@tonic-gate 	Word		got_off;
124ba2be530Sab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1277c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1287c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
129b3fbe5e6Sseizo 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
130b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	/* LINTED */
1367c478bd9Sstevel@tonic-gate 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
137b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
138ba2be530Sab 	if (bswap)
139ba2be530Sab 		/* LINTED */
140ba2be530Sab 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1437c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1447c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_DISP_IND;
1457c478bd9Sstevel@tonic-gate 		pltent += 2;
1467c478bd9Sstevel@tonic-gate 		/* LINTED */
1477c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
148de777a60Sab 		    got_off);
1497c478bd9Sstevel@tonic-gate 	} else {
1507c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1517c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_REG_DISP_IND;
1527c478bd9Sstevel@tonic-gate 		pltent += 2;
1537c478bd9Sstevel@tonic-gate 		/* LINTED */
1547c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)got_off;
1557c478bd9Sstevel@tonic-gate 	}
156ba2be530Sab 	if (bswap)
157ba2be530Sab 		/* LINTED */
158ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1597c478bd9Sstevel@tonic-gate 	pltent += 4;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_PUSHL;
1627c478bd9Sstevel@tonic-gate 	pltent++;
1637c478bd9Sstevel@tonic-gate 	/* LINTED */
1647c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)rel_off;
165ba2be530Sab 	if (bswap)
166ba2be530Sab 		/* LINTED */
167ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1687c478bd9Sstevel@tonic-gate 	pltent += 4;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
1717c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_JMP;
1727c478bd9Sstevel@tonic-gate 	pltent++;
1737c478bd9Sstevel@tonic-gate 	/* LINTED */
1747c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)plt_off;
175ba2be530Sab 	if (bswap)
176ba2be530Sab 		/* LINTED */
177ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
180ba2be530Sab static uintptr_t
1815aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
1847c478bd9Sstevel@tonic-gate 	Word		ndx, roffset, value;
1857c478bd9Sstevel@tonic-gate 	Rel		rea;
1867c478bd9Sstevel@tonic-gate 	char		*relbits;
1877c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
1887c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
1947c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
1977c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
1987c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
1995aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2007c478bd9Sstevel@tonic-gate 		return (1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
2057c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	if (orsp->rel_move)
2085aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section using a partial initialized
2127c478bd9Sstevel@tonic-gate 	 * symbol, adjust the embedded symbol info.
2137c478bd9Sstevel@tonic-gate 	 *
2147c478bd9Sstevel@tonic-gate 	 * The second argument of the am_I_partial() is the value stored at the
2157c478bd9Sstevel@tonic-gate 	 * target address relocation is going to be applied.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2187c478bd9Sstevel@tonic-gate 		if (ofl->ofl_parsym.head &&
2197c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2207c478bd9Sstevel@tonic-gate 		    /* LINTED */
2215aefb655Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
222b3fbe5e6Sseizo 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2237c478bd9Sstevel@tonic-gate 		    orsp->rel_roffset)))) {
2245aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2257c478bd9Sstevel@tonic-gate 			sectmoved = 1;
226de777a60Sab 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
2327c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
2335aefb655Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2345aefb655Srie 
2357c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2367c478bd9Sstevel@tonic-gate 		/*
2377c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
2387c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
2397c478bd9Sstevel@tonic-gate 		 */
2407c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
2417c478bd9Sstevel@tonic-gate 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2427c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2477c478bd9Sstevel@tonic-gate 		/*
2487c478bd9Sstevel@tonic-gate 		 * This must be a R_386_COPY.  For these set the roffset to
2497c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
2527c478bd9Sstevel@tonic-gate 		roffset = (Word)value;
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 		osp = orsp->rel_osdesc;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		/*
2577c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
2587c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
2597c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
2627c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
2637c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2647c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
2657c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
2697c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
2737c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
2747c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
2757c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_RELATIVE)
2787c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
2797c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
2807c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2817c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
2847c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
2857c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
2867c478bd9Sstevel@tonic-gate 			 */
2877c478bd9Sstevel@tonic-gate 			if ((sdp->sd_isc != 0) &&
2887c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_osdesc != 0))
2897c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
2907c478bd9Sstevel@tonic-gate 			else
2917c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
2927c478bd9Sstevel@tonic-gate 		} else
29335450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
2947c478bd9Sstevel@tonic-gate 	} else
2957c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
2967c478bd9Sstevel@tonic-gate 
297cce0e03bSab 	/*
298cce0e03bSab 	 * If we have a replacement value for the relocation
299cce0e03bSab 	 * target, put it in place now.
300cce0e03bSab 	 */
301cce0e03bSab 	if (orsp->rel_flags & FLG_REL_NADDEND) {
302cce0e03bSab 		Xword	addend = orsp->rel_raddend;
303cce0e03bSab 		uchar_t	*addr;
304cce0e03bSab 
305cce0e03bSab 		/*
306cce0e03bSab 		 * Get the address of the data item we need to modify.
307cce0e03bSab 		 */
308cce0e03bSab 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
309cce0e03bSab 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
310cce0e03bSab 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
311cce0e03bSab 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
312cce0e03bSab 			return (S_ERROR);
313cce0e03bSab 	}
314cce0e03bSab 
3157c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3187c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
3195aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
3205aefb655Srie 	    orsp->rel_sname));
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3287c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rel));
3297c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += sizeof (Rel);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3337c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3347c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3357c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3387c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3397c478bd9Sstevel@tonic-gate 
3405aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3417c478bd9Sstevel@tonic-gate 	return (1);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * i386 Instructions for TLS processing
3467c478bd9Sstevel@tonic-gate  */
347b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * 0x00	movl %gs:0x0, %eax
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * 0x06	addl x(%eax), %eax
3547c478bd9Sstevel@tonic-gate 	 * 0x0c ...
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
3577c478bd9Sstevel@tonic-gate };
3587c478bd9Sstevel@tonic-gate 
359b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * 0x00 movl %gs:0x0, %eax
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * 0x06 addl $0x0, %eax
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	0x05, 0x00, 0x00, 0x00, 0x00,
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * 0x0b nop
3707c478bd9Sstevel@tonic-gate 	 * 0x0c
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	0x90
3737c478bd9Sstevel@tonic-gate };
3747c478bd9Sstevel@tonic-gate 
375b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie_movgs[] = {
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 *	movl %gs:0x0,%eax
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
3807c478bd9Sstevel@tonic-gate };
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
3837c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_POP	0x58	/* popl + reg */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate #define	TLS_NOP		0x90	/* NOP instruction */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate #define	MODRM_MSK_MOD	0xc0
3907c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RO	0x38
3917c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RM	0x07
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate #define	SIB_MSK_SS	0xc0
3947c478bd9Sstevel@tonic-gate #define	SIB_MSK_IND	0x38
3957c478bd9Sstevel@tonic-gate #define	SIB_MSK_BS	0x07
3967c478bd9Sstevel@tonic-gate 
3975aefb655Srie static Fixupret
3985aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4017c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
402b3fbe5e6Sseizo 	uchar_t		*offset, r1, r2;
4037c478bd9Sstevel@tonic-gate 
404b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
405b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
406b3fbe5e6Sseizo 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * IE reference model
4117c478bd9Sstevel@tonic-gate 		 */
4127c478bd9Sstevel@tonic-gate 		switch (rtype) {
4137c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD:
4147c478bd9Sstevel@tonic-gate 			/*
4157c478bd9Sstevel@tonic-gate 			 * Transition:
4167c478bd9Sstevel@tonic-gate 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4177c478bd9Sstevel@tonic-gate 			 *	0x7 call ___tls_get_addr
4187c478bd9Sstevel@tonic-gate 			 *	0xc
4197c478bd9Sstevel@tonic-gate 			 * To:
4207c478bd9Sstevel@tonic-gate 			 *	0x0 movl %gs:0, %eax
4217c478bd9Sstevel@tonic-gate 			 *	0x6 addl x@gotntpoff(r1), %eax
4227c478bd9Sstevel@tonic-gate 			 */
4235aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
424051d39bbSrie 			    R_386_TLS_GOTIE, arsp));
4257c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_386_TLS_GOTIE;
4267c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 5;
4275aefb655Srie 
4287c478bd9Sstevel@tonic-gate 			/*
429051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4307c478bd9Sstevel@tonic-gate 			 * sequence.
4317c478bd9Sstevel@tonic-gate 			 */
4327c478bd9Sstevel@tonic-gate 			offset -= 3;
4337c478bd9Sstevel@tonic-gate 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4347c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4355aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4365aefb655Srie 
4377c478bd9Sstevel@tonic-gate 			/*
4387c478bd9Sstevel@tonic-gate 			 * set register %r1 into the addl
4397c478bd9Sstevel@tonic-gate 			 * instruction.
4407c478bd9Sstevel@tonic-gate 			 */
4417c478bd9Sstevel@tonic-gate 			offset[0x7] |= r1;
4427c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4435aefb655Srie 
4447c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD_PLT:
4457c478bd9Sstevel@tonic-gate 			/*
4467c478bd9Sstevel@tonic-gate 			 * Fixup done via the TLS_GD relocation
4477c478bd9Sstevel@tonic-gate 			 */
4485aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
449051d39bbSrie 			    R_386_NONE, arsp));
4507c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * LE reference model
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	switch (rtype) {
4587c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD:
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Transition:
4617c478bd9Sstevel@tonic-gate 		 *	0x0 leal x@tlsgd(,r1,1), %eax
4627c478bd9Sstevel@tonic-gate 		 *	0x7 call ___tls_get_addr
4637c478bd9Sstevel@tonic-gate 		 *	0xc
4647c478bd9Sstevel@tonic-gate 		 * To:
4657c478bd9Sstevel@tonic-gate 		 *	0x0 movl %gs:0, %eax
4667c478bd9Sstevel@tonic-gate 		 *	0x6 addl $x@ntpoff, %eax
4677c478bd9Sstevel@tonic-gate 		 *	0xb nop
4687c478bd9Sstevel@tonic-gate 		 *	0xc
4697c478bd9Sstevel@tonic-gate 		 */
4705aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
471051d39bbSrie 		    R_386_TLS_LE, arsp));
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
4747c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 4;
4755aefb655Srie 
4767c478bd9Sstevel@tonic-gate 		/*
477051d39bbSrie 		 * Adjust 'offset' to beginning of instruction
4787c478bd9Sstevel@tonic-gate 		 * sequence.
4797c478bd9Sstevel@tonic-gate 		 */
4807c478bd9Sstevel@tonic-gate 		offset -= 3;
4817c478bd9Sstevel@tonic-gate 		(void) memcpy(offset, tlsinstr_gd_le,
4825aefb655Srie 		    sizeof (tlsinstr_gd_le));
4837c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
4845aefb655Srie 
4857c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD_PLT:
4867c478bd9Sstevel@tonic-gate 	case R_386_PLT32:
4877c478bd9Sstevel@tonic-gate 		/*
4887c478bd9Sstevel@tonic-gate 		 * Fixup done via the TLS_GD relocation
4897c478bd9Sstevel@tonic-gate 		 */
4905aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
491051d39bbSrie 		    R_386_NONE, arsp));
4927c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
4935aefb655Srie 
4947c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM_PLT:
4955aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
496051d39bbSrie 		    R_386_NONE, arsp));
4975aefb655Srie 
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * Transition:
5007c478bd9Sstevel@tonic-gate 		 *	call __tls_get_addr()
5017c478bd9Sstevel@tonic-gate 		 * to:
5027c478bd9Sstevel@tonic-gate 		 *	nop
5037c478bd9Sstevel@tonic-gate 		 *	nop
5047c478bd9Sstevel@tonic-gate 		 *	nop
5057c478bd9Sstevel@tonic-gate 		 *	nop
5067c478bd9Sstevel@tonic-gate 		 *	nop
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		*(offset - 1) = TLS_NOP;
5097c478bd9Sstevel@tonic-gate 		*(offset) = TLS_NOP;
5107c478bd9Sstevel@tonic-gate 		*(offset + 1) = TLS_NOP;
5117c478bd9Sstevel@tonic-gate 		*(offset + 2) = TLS_NOP;
5127c478bd9Sstevel@tonic-gate 		*(offset + 3) = TLS_NOP;
5137c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5145aefb655Srie 
5157c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM:
5165aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
517051d39bbSrie 		    R_386_NONE, arsp));
5185aefb655Srie 
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * Transition:
5217c478bd9Sstevel@tonic-gate 		 *
5227c478bd9Sstevel@tonic-gate 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5237c478bd9Sstevel@tonic-gate 		 *  0x06 call ___tls_get_addr
5247c478bd9Sstevel@tonic-gate 		 *
5257c478bd9Sstevel@tonic-gate 		 * to:
5267c478bd9Sstevel@tonic-gate 		 *
5277c478bd9Sstevel@tonic-gate 		 *  0x00 movl %gs:0, %eax
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5305aefb655Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5317c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5325aefb655Srie 
5337c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDO_32:
5347c478bd9Sstevel@tonic-gate 		/*
5357c478bd9Sstevel@tonic-gate 		 *  Instructions:
5367c478bd9Sstevel@tonic-gate 		 *
5377c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5387c478bd9Sstevel@tonic-gate 		 *		to
5397c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5407c478bd9Sstevel@tonic-gate 		 *
5417c478bd9Sstevel@tonic-gate 		 */
5427c478bd9Sstevel@tonic-gate 		offset -= 2;
5437c478bd9Sstevel@tonic-gate 
5445aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
545051d39bbSrie 		    R_386_TLS_LE, arsp));
5467c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5477c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5485aefb655Srie 
5497c478bd9Sstevel@tonic-gate 	case R_386_TLS_GOTIE:
5507c478bd9Sstevel@tonic-gate 		/*
5517c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
5527c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
5537c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
5547c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
5557c478bd9Sstevel@tonic-gate 		 *
5567c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
5577c478bd9Sstevel@tonic-gate 		 *
5587c478bd9Sstevel@tonic-gate 		 *  1) movl foo@gotntpoff(%reg1), %reg2
5597c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5607c478bd9Sstevel@tonic-gate 		 *
5617c478bd9Sstevel@tonic-gate 		 *  2) addl foo@gotntpoff(%reg1), %reg2
5627c478bd9Sstevel@tonic-gate 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5637c478bd9Sstevel@tonic-gate 		 *
5647c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
5657c478bd9Sstevel@tonic-gate 		 *
5667c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %reg2
5677c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
5687c478bd9Sstevel@tonic-gate 		 *
5697c478bd9Sstevel@tonic-gate 		 *  2) addl $foo@ntpoff, %reg2
5707c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg2, foo@ntpoff
5717c478bd9Sstevel@tonic-gate 		 *
5727c478bd9Sstevel@tonic-gate 		 * Note: reg1 != 4 (%esp)
5737c478bd9Sstevel@tonic-gate 		 */
5745aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
575051d39bbSrie 		    R_386_TLS_LE, arsp));
5767c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5775aefb655Srie 
5787c478bd9Sstevel@tonic-gate 		offset -= 2;
5797c478bd9Sstevel@tonic-gate 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
5807c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
5817c478bd9Sstevel@tonic-gate 			/* case 1 above */
5827c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
5837c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
5847c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
5887c478bd9Sstevel@tonic-gate 			/* case 2 above */
5897c478bd9Sstevel@tonic-gate 			assert(offset[0] == 0x03);
5907c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
5917c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
5927c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
5977c478bd9Sstevel@tonic-gate 		 */
598de777a60Sab 		{
599de777a60Sab 			Conv_inv_buf_t	inv_buf;
600de777a60Sab 
601de777a60Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
602de777a60Sab 			    MSG_INTL(MSG_REL_BADTLSINS),
603de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
604de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
605de777a60Sab 			    demangle(arsp->rel_sname),
606de777a60Sab 			    arsp->rel_isdesc->is_name,
607de777a60Sab 			    EC_OFF(arsp->rel_roffset));
608de777a60Sab 		}
6097c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
6105aefb655Srie 
6117c478bd9Sstevel@tonic-gate 	case R_386_TLS_IE:
6127c478bd9Sstevel@tonic-gate 		/*
6137c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
6147c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
6157c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
6167c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
6177c478bd9Sstevel@tonic-gate 		 *
6187c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
6197c478bd9Sstevel@tonic-gate 		 *  1) movl foo@indntpoff, %eax
6207c478bd9Sstevel@tonic-gate 		 *	0xa1, foo@indntpoff
6217c478bd9Sstevel@tonic-gate 		 *
6227c478bd9Sstevel@tonic-gate 		 *  2) movl foo@indntpoff, %eax
6237c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6247c478bd9Sstevel@tonic-gate 		 *
6257c478bd9Sstevel@tonic-gate 		 *  3) addl foo@indntpoff, %eax
6267c478bd9Sstevel@tonic-gate 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6277c478bd9Sstevel@tonic-gate 		 *
6287c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
6297c478bd9Sstevel@tonic-gate 		 *
6307c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %eax
6317c478bd9Sstevel@tonic-gate 		 *	0xb8, foo@ntpoff
6327c478bd9Sstevel@tonic-gate 		 *
6337c478bd9Sstevel@tonic-gate 		 *  2) movl $foo@ntpoff, %reg
6347c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6357c478bd9Sstevel@tonic-gate 		 *
6367c478bd9Sstevel@tonic-gate 		 *  3) addl $foo@ntpoff, %reg
6377c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg, foo@ntpoff
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6407c478bd9Sstevel@tonic-gate 		offset--;
6417c478bd9Sstevel@tonic-gate 		if (offset[0] == 0xa1) {
6427c478bd9Sstevel@tonic-gate 			/* case 1 above */
6437c478bd9Sstevel@tonic-gate 			offset[0] = 0xb8;	/*  movl */
6447c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		offset--;
6487c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
6497c478bd9Sstevel@tonic-gate 			/* case 2 above */
6507c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6517c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
6527c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6537c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
6567c478bd9Sstevel@tonic-gate 			/* case 3 above */
6577c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6587c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
6597c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6607c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
6647c478bd9Sstevel@tonic-gate 		 */
665de777a60Sab 		{
666de777a60Sab 			Conv_inv_buf_t	inv_buf;
667de777a60Sab 
668de777a60Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
669de777a60Sab 			    MSG_INTL(MSG_REL_BADTLSINS),
670de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
671de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
672de777a60Sab 			    demangle(arsp->rel_sname),
673de777a60Sab 			    arsp->rel_isdesc->is_name,
674de777a60Sab 			    EC_OFF(arsp->rel_roffset));
675de777a60Sab 		}
6767c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
681ba2be530Sab static uintptr_t
6825aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6837c478bd9Sstevel@tonic-gate {
684141040e8Srie 	Rel_desc	*arsp;
685141040e8Srie 	Rel_cache	*rcp;
686141040e8Srie 	Listnode	*lnp;
6877c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6881d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
6897c478bd9Sstevel@tonic-gate 
6907010c12aSrie 	if (ofl->ofl_actrels.head)
6917010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6927010c12aSrie 
6937c478bd9Sstevel@tonic-gate 	/*
694141040e8Srie 	 * Process active relocations.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6977c478bd9Sstevel@tonic-gate 		/* LINTED */
6987c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6997c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
700b3fbe5e6Sseizo 			uchar_t		*addr;
701141040e8Srie 			Xword 		value;
7027c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
7037c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
704141040e8Srie 			Xword		refaddr;
7057c478bd9Sstevel@tonic-gate 			int		moved = 0;
7067c478bd9Sstevel@tonic-gate 			Gotref		gref;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 			/*
7097c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
7107c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
7117c478bd9Sstevel@tonic-gate 			 * relocation itself.
7127c478bd9Sstevel@tonic-gate 			 */
7137c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
7147c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
7157c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
7167c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
7175aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
7185aefb655Srie 				    M_MACH, arsp));
7197c478bd9Sstevel@tonic-gate 				continue;
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			/*
723d2d5cf7cSAli Bahrami 			 * We determine what the 'got reference'
7247c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
7257c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
7267c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
7277c478bd9Sstevel@tonic-gate 			 *
7287c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
7297c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
7307c478bd9Sstevel@tonic-gate 			 */
7317c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
7327c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
7337c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
7347c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
7357c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
7367c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
7377c478bd9Sstevel@tonic-gate 			else
7387c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 			/*
7417c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
7427c478bd9Sstevel@tonic-gate 			 */
7437c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7447c478bd9Sstevel@tonic-gate 				Fixupret	ret;
7457c478bd9Sstevel@tonic-gate 
7465aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7477c478bd9Sstevel@tonic-gate 					return (S_ERROR);
7487c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
7497c478bd9Sstevel@tonic-gate 					continue;
7507c478bd9Sstevel@tonic-gate 			}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 			/*
7537c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
7547c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
7557c478bd9Sstevel@tonic-gate 			 */
7567c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
7575aefb655Srie 				ld_adj_movereloc(ofl, arsp);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
7607c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
7617c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_CLVAL)
7647c478bd9Sstevel@tonic-gate 				value = 0;
7657c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7667c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
7677c478bd9Sstevel@tonic-gate 				/*
7687c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
7697c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
7707c478bd9Sstevel@tonic-gate 				 */
771*b26cc8daSAli Bahrami 				if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
772*b26cc8daSAli Bahrami 					Sym_desc	*sym;
773*b26cc8daSAli Bahrami 					Xword		radd;
774*b26cc8daSAli Bahrami 					uchar_t		*raddr = (uchar_t *)
775*b26cc8daSAli Bahrami 					    arsp->rel_isdesc->is_indata->d_buf +
776*b26cc8daSAli Bahrami 					    arsp->rel_roffset;
777*b26cc8daSAli Bahrami 
7787c478bd9Sstevel@tonic-gate 					/*
779*b26cc8daSAli Bahrami 					 * This is a REL platform. Hence, the
780*b26cc8daSAli Bahrami 					 * second argument of ld_am_I_partial()
781*b26cc8daSAli Bahrami 					 * is the value stored at the target
782*b26cc8daSAli Bahrami 					 * address where the relocation is
783*b26cc8daSAli Bahrami 					 * going to be applied.
7847c478bd9Sstevel@tonic-gate 					 */
785*b26cc8daSAli Bahrami 					if (ld_reloc_targval_get(ofl, arsp,
786*b26cc8daSAli Bahrami 					    raddr, &radd) == 0)
787*b26cc8daSAli Bahrami 						return (S_ERROR);
788*b26cc8daSAli Bahrami 					sym = ld_am_I_partial(arsp, radd);
789*b26cc8daSAli Bahrami 					if (sym) {
790*b26cc8daSAli Bahrami 						Sym	*osym = sym->sd_osym;
791*b26cc8daSAli Bahrami 
792*b26cc8daSAli Bahrami 						/*
793*b26cc8daSAli Bahrami 						 * The symbol was moved, so
794*b26cc8daSAli Bahrami 						 * adjust the value relative
795*b26cc8daSAli Bahrami 						 * to the new section.
796*b26cc8daSAli Bahrami 						 */
797*b26cc8daSAli Bahrami 						value = sym->sd_sym->st_value;
798*b26cc8daSAli Bahrami 						moved = 1;
799*b26cc8daSAli Bahrami 
800*b26cc8daSAli Bahrami 						/*
801*b26cc8daSAli Bahrami 						 * The original raddend covers
802*b26cc8daSAli Bahrami 						 * the displacement from the
803*b26cc8daSAli Bahrami 						 * section start to the desired
804*b26cc8daSAli Bahrami 						 * address. The value computed
805*b26cc8daSAli Bahrami 						 * above gets us from the
806*b26cc8daSAli Bahrami 						 * section start to the start
807*b26cc8daSAli Bahrami 						 * of the symbol range. Adjust
808*b26cc8daSAli Bahrami 						 * the old raddend to remove the
809*b26cc8daSAli Bahrami 						 * offset from section start to
810*b26cc8daSAli Bahrami 						 * symbol start, leaving the
811*b26cc8daSAli Bahrami 						 * displacement within the
812*b26cc8daSAli Bahrami 						 * range of the symbol.
813*b26cc8daSAli Bahrami 						 */
814*b26cc8daSAli Bahrami 						if (osym->st_value != 0) {
815*b26cc8daSAli Bahrami 							radd -= osym->st_value;
816*b26cc8daSAli Bahrami 							if (ld_reloc_targval_set
817*b26cc8daSAli Bahrami 							    (ofl, arsp, raddr,
818*b26cc8daSAli Bahrami 							    radd) == 0)
819*b26cc8daSAli Bahrami 								return (
820*b26cc8daSAli Bahrami 								    S_ERROR);
821*b26cc8daSAli Bahrami 						}
822*b26cc8daSAli Bahrami 					}
823*b26cc8daSAli Bahrami 				}
824*b26cc8daSAli Bahrami 				if (!moved) {
825141040e8Srie 					value = _elf_getxoff(
826141040e8Srie 					    sdp->sd_isc->is_indata);
8277c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
8287c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
829141040e8Srie 						value += sdp->sd_isc->
830141040e8Srie 						    is_osdesc->os_shdr->sh_addr;
8317c478bd9Sstevel@tonic-gate 				}
8327c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8337c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
8342926dd2eSrie 
8352926dd2eSrie 			} else if (IS_SIZE(arsp->rel_rtype)) {
8362926dd2eSrie 				/*
8372926dd2eSrie 				 * Size relocations require the symbols size.
8382926dd2eSrie 				 */
8392926dd2eSrie 				value = sdp->sd_sym->st_size;
840141040e8Srie 			} else {
8417c478bd9Sstevel@tonic-gate 				/*
8427010c12aSrie 				 * Else the value is the symbols value.
8437c478bd9Sstevel@tonic-gate 				 */
8447c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
845141040e8Srie 			}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 			/*
8487c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
8497c478bd9Sstevel@tonic-gate 			 */
8507c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
8517c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 			/*
8547c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
8557c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
8567c478bd9Sstevel@tonic-gate 			 * address.
8577c478bd9Sstevel@tonic-gate 			 */
8587c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
859141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8607c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
8617c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 			/*
8647c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
8657c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
8667c478bd9Sstevel@tonic-gate 			 * not the address of the function).
8677c478bd9Sstevel@tonic-gate 			 */
8687c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
8697c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8705aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 
873141040e8Srie 			/*
874141040e8Srie 			 * Determine whether the value needs further adjustment.
875141040e8Srie 			 * Filter through the attributes of the relocation to
876141040e8Srie 			 * determine what adjustment is required.  Note, many
877141040e8Srie 			 * of the following cases are only applicable when a
878141040e8Srie 			 * .got is present.  As a .got is not generated when a
879141040e8Srie 			 * relocatable object is being built, any adjustments
880141040e8Srie 			 * that require a .got need to be skipped.
881141040e8Srie 			 */
882141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
883141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8847c478bd9Sstevel@tonic-gate 				Xword		R1addr;
8857c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
8867c478bd9Sstevel@tonic-gate 				Word		gotndx;
8877c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 				/*
8907c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
8917c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
8927c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
8937c478bd9Sstevel@tonic-gate 				 * directly
8947c478bd9Sstevel@tonic-gate 				 *
8957c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
8967c478bd9Sstevel@tonic-gate 				 * the relocation.
8977c478bd9Sstevel@tonic-gate 				 */
8985aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8997c478bd9Sstevel@tonic-gate 				    ofl, 0);
9007c478bd9Sstevel@tonic-gate 				assert(gnp);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
9037c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
9047c478bd9Sstevel@tonic-gate 				else
9057c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 				/*
9107c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
9117c478bd9Sstevel@tonic-gate 				 */
9127c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
9137c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
9147c478bd9Sstevel@tonic-gate 
9155aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
9165aefb655Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
9177c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
9187c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 				/*
9217c478bd9Sstevel@tonic-gate 				 * And do it.
9227c478bd9Sstevel@tonic-gate 				 */
923f3324781Sab 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
924f3324781Sab 					*(Xword *)R2addr =
925ba2be530Sab 					    ld_bswap_Xword(value);
926f3324781Sab 				else
927f3324781Sab 					*(Xword *)R2addr = value;
9287c478bd9Sstevel@tonic-gate 				continue;
9297c478bd9Sstevel@tonic-gate 
930141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
931141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9327c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
933141040e8Srie 
934141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
935141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
936141040e8Srie 				value =
937141040e8Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
938141040e8Srie 				    refaddr;
939141040e8Srie 
9407c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
941141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9427c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9437c478bd9Sstevel@tonic-gate 				value -= refaddr;
944141040e8Srie 
9457c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
946141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
947141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9487c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
9497c478bd9Sstevel@tonic-gate 
9505aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
9517c478bd9Sstevel@tonic-gate 				    ofl, 0);
9527c478bd9Sstevel@tonic-gate 				assert(gnp);
953141040e8Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9547c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_386_TLS_IE) {
955141040e8Srie 					value +=
956141040e8Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9577c478bd9Sstevel@tonic-gate 				}
958141040e8Srie 
959141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
960141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
961141040e8Srie 				Gotndx *gnp;
9627c478bd9Sstevel@tonic-gate 
9635aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9647c478bd9Sstevel@tonic-gate 				    GOT_REF_GENERIC, ofl, 0);
9657c478bd9Sstevel@tonic-gate 				assert(gnp);
966141040e8Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
967141040e8Srie 
968141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
969141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9707c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
971141040e8Srie 
9727c478bd9Sstevel@tonic-gate 				/*
9737c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
9747c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
9757c478bd9Sstevel@tonic-gate 				 */
976141040e8Srie 				tlsstatsize =
977141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9787c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
9797c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
980141040e8Srie 
9817c478bd9Sstevel@tonic-gate 				/*
982141040e8Srie 				 * Since this code is fixed up, it assumes a
983141040e8Srie 				 * negative offset that can be added to the
984141040e8Srie 				 * thread pointer.
9857c478bd9Sstevel@tonic-gate 				 */
9867c478bd9Sstevel@tonic-gate 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9877c478bd9Sstevel@tonic-gate 				    (arsp->rel_rtype == R_386_TLS_LE))
9887c478bd9Sstevel@tonic-gate 					value = -value;
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
9927c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9937c478bd9Sstevel@tonic-gate 			else
9947c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 			/*
9977c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9987c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9997c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
10007c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
10017c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
10027c478bd9Sstevel@tonic-gate 			 * see this.
10037c478bd9Sstevel@tonic-gate 			 */
10047c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1005de777a60Sab 				Conv_inv_buf_t	inv_buf;
1006de777a60Sab 
10075aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
10085aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
1009de777a60Sab 				    conv_reloc_386_type(arsp->rel_rtype,
1010de777a60Sab 				    0, &inv_buf),
10117c478bd9Sstevel@tonic-gate 				    ifl_name, demangle(arsp->rel_sname),
10127c478bd9Sstevel@tonic-gate 				    arsp->rel_isdesc->is_name);
10137c478bd9Sstevel@tonic-gate 				return (S_ERROR);
10147c478bd9Sstevel@tonic-gate 			}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			/*
10177c478bd9Sstevel@tonic-gate 			 * Get the address of the data item we need to modify.
10187c478bd9Sstevel@tonic-gate 			 */
1019b3fbe5e6Sseizo 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1020b3fbe5e6Sseizo 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1021b3fbe5e6Sseizo 			    is_indata));
10227c478bd9Sstevel@tonic-gate 
10235aefb655Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
10245aefb655Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
10255aefb655Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
10267c478bd9Sstevel@tonic-gate 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
10277c478bd9Sstevel@tonic-gate 
10285aefb655Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1029b3fbe5e6Sseizo 			    ofl->ofl_size) || (arsp->rel_roffset >
10307c478bd9Sstevel@tonic-gate 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1031de777a60Sab 				Conv_inv_buf_t	inv_buf;
1032de777a60Sab 				int		class;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 				if (((uintptr_t)addr -
10355aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10367c478bd9Sstevel@tonic-gate 					class = ERR_FATAL;
10377c478bd9Sstevel@tonic-gate 				else
10387c478bd9Sstevel@tonic-gate 					class = ERR_WARNING;
10397c478bd9Sstevel@tonic-gate 
10405aefb655Srie 				eprintf(ofl->ofl_lml, class,
10415aefb655Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
1042de777a60Sab 				    conv_reloc_386_type(arsp->rel_rtype,
1043de777a60Sab 				    0, &inv_buf),
10447c478bd9Sstevel@tonic-gate 				    ifl_name, arsp->rel_isdesc->is_name,
10457c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
10467c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
10475aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
10507c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
10517c478bd9Sstevel@tonic-gate 					continue;
10527c478bd9Sstevel@tonic-gate 				}
10537c478bd9Sstevel@tonic-gate 			}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 			/*
10567c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
10577c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
10587c478bd9Sstevel@tonic-gate 			 * expanded.
10597c478bd9Sstevel@tonic-gate 			 */
10607c478bd9Sstevel@tonic-gate 			if (moved)
10617c478bd9Sstevel@tonic-gate 				value -= *addr;
10627c478bd9Sstevel@tonic-gate 
1063cce0e03bSab 			/*
1064cce0e03bSab 			 * If we have a replacement value for the relocation
1065cce0e03bSab 			 * target, put it in place now.
1066cce0e03bSab 			 */
1067cce0e03bSab 			if (arsp->rel_flags & FLG_REL_NADDEND) {
1068cce0e03bSab 				Xword addend = arsp->rel_raddend;
1069cce0e03bSab 
1070cce0e03bSab 				if (ld_reloc_targval_set(ofl, arsp,
1071cce0e03bSab 				    addr, addend) == 0)
1072cce0e03bSab 					return (S_ERROR);
1073cce0e03bSab 			}
1074cce0e03bSab 
10757c478bd9Sstevel@tonic-gate 			/*
1076f3324781Sab 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10777c478bd9Sstevel@tonic-gate 			 * stage.
10787c478bd9Sstevel@tonic-gate 			 */
1079f3324781Sab 			if (OFL_DO_RELOC(ofl)) {
1080f3324781Sab 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10815aefb655Srie 				    &value, arsp->rel_sname, ifl_name,
1082f3324781Sab 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10835aefb655Srie 				    ofl->ofl_lml) == 0)
10847c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 	return (return_code);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
1091141040e8Srie /*
1092141040e8Srie  * Add an output relocation record.
1093141040e8Srie  */
1094ba2be530Sab static uintptr_t
10955aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10967c478bd9Sstevel@tonic-gate {
1097141040e8Srie 	Rel_desc	*orsp;
1098141040e8Srie 	Rel_cache	*rcp;
1099141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
11037c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11047c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
11057c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
11087c478bd9Sstevel@tonic-gate 	    (FLG_OF_STATIC | FLG_OF_EXEC))
11097c478bd9Sstevel@tonic-gate 		return (1);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * If no relocation cache structures are available allocate
11137c478bd9Sstevel@tonic-gate 	 * a new one and link it into the cache list.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_outrels.tail == 0) ||
11167c478bd9Sstevel@tonic-gate 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
11177c478bd9Sstevel@tonic-gate 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
11187c478bd9Sstevel@tonic-gate 		static size_t	nextsize = 0;
11197c478bd9Sstevel@tonic-gate 		size_t		size;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		/*
11227c478bd9Sstevel@tonic-gate 		 * Output relocation numbers can vary considerably between
11237c478bd9Sstevel@tonic-gate 		 * building executables or shared objects (pic vs. non-pic),
11247c478bd9Sstevel@tonic-gate 		 * etc.  But, they typically aren't very large, so for these
11257c478bd9Sstevel@tonic-gate 		 * objects use a standard bucket size.  For building relocatable
11267c478bd9Sstevel@tonic-gate 		 * objects, typically there will be an output relocation for
11277c478bd9Sstevel@tonic-gate 		 * every input relocation.
11287c478bd9Sstevel@tonic-gate 		 */
11297c478bd9Sstevel@tonic-gate 		if (nextsize == 0) {
11307c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
11317c478bd9Sstevel@tonic-gate 				if ((size = ofl->ofl_relocincnt) == 0)
11327c478bd9Sstevel@tonic-gate 					size = REL_LOIDESCNO;
11337c478bd9Sstevel@tonic-gate 				if (size > REL_HOIDESCNO)
11347c478bd9Sstevel@tonic-gate 					nextsize = REL_HOIDESCNO;
11357c478bd9Sstevel@tonic-gate 				else
11367c478bd9Sstevel@tonic-gate 					nextsize = REL_LOIDESCNO;
11377c478bd9Sstevel@tonic-gate 			} else
11387c478bd9Sstevel@tonic-gate 				nextsize = size = REL_HOIDESCNO;
11397c478bd9Sstevel@tonic-gate 		} else
11407c478bd9Sstevel@tonic-gate 			size = nextsize;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		size = size * sizeof (Rel_desc);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
11457c478bd9Sstevel@tonic-gate 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
11467c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		/* LINTED */
11497c478bd9Sstevel@tonic-gate 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
11507c478bd9Sstevel@tonic-gate 		/* LINTED */
11517c478bd9Sstevel@tonic-gate 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
11557c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
11567c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
11577c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11607c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
11617c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		/*
11647c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
11657c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
11667c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
11677c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
11687c478bd9Sstevel@tonic-gate 		 */
11697c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11700bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11717c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11727c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11737c478bd9Sstevel@tonic-gate 			else
11747c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1175141040e8Srie 		} else {
1176141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11777c478bd9Sstevel@tonic-gate 
1178c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11797c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11807c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11817c478bd9Sstevel@tonic-gate 			}
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
11867c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
11897c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
11927c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11937c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
11947c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11957c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
11967c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11977c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
11987c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11997c478bd9Sstevel@tonic-gate 	else
12007c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
12037c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	/*
12067c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
12077c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
12087c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
12117c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/*
1214141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
12157c478bd9Sstevel@tonic-gate 	 */
1216141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12177c478bd9Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	/*
12207c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
12217c478bd9Sstevel@tonic-gate 	 */
12227c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
12237c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12265aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12277c478bd9Sstevel@tonic-gate 	}
12285aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
12295aefb655Srie 	    M_MACH, orsp));
12307c478bd9Sstevel@tonic-gate 	return (1);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
12357c478bd9Sstevel@tonic-gate  */
1236ba2be530Sab static uintptr_t
12375aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12387c478bd9Sstevel@tonic-gate {
12391d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12407c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12410bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
12457c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
12467c478bd9Sstevel@tonic-gate 	 * then
12477c478bd9Sstevel@tonic-gate 	 *	build R_386_RELATIVE
12487c478bd9Sstevel@tonic-gate 	 * fi
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12512926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12527c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12537c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
12547c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12557c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12567c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12577c478bd9Sstevel@tonic-gate 		Word	ortype = rsp->rel_rtype;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_RELATIVE;
12605aefb655Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12617c478bd9Sstevel@tonic-gate 			return (S_ERROR);
12627c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * If the relocation is against a 'non-allocatable' section
12677c478bd9Sstevel@tonic-gate 	 * and we can not resolve it now - then give a warning
12687c478bd9Sstevel@tonic-gate 	 * message.
12697c478bd9Sstevel@tonic-gate 	 *
12707c478bd9Sstevel@tonic-gate 	 * We can not resolve the symbol if either:
12717c478bd9Sstevel@tonic-gate 	 *	a) it's undefined
12727c478bd9Sstevel@tonic-gate 	 *	b) it's defined in a shared library and a
12737c478bd9Sstevel@tonic-gate 	 *	   COPY relocation hasn't moved it to the executable
12747c478bd9Sstevel@tonic-gate 	 *
12757c478bd9Sstevel@tonic-gate 	 * Note: because we process all of the relocations against the
12767c478bd9Sstevel@tonic-gate 	 *	text segment before any others - we know whether
12777c478bd9Sstevel@tonic-gate 	 *	or not a copy relocation will be generated before
12787c478bd9Sstevel@tonic-gate 	 *	we get here (see reloc_init()->reloc_segments()).
12797c478bd9Sstevel@tonic-gate 	 */
12807c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12817c478bd9Sstevel@tonic-gate 	    ((shndx == SHN_UNDEF) ||
12827c478bd9Sstevel@tonic-gate 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12837c478bd9Sstevel@tonic-gate 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1284de777a60Sab 		Conv_inv_buf_t inv_buf;
1285de777a60Sab 
12867c478bd9Sstevel@tonic-gate 		/*
12877c478bd9Sstevel@tonic-gate 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12887c478bd9Sstevel@tonic-gate 		 * section - then silently ignore that the relocation
12897c478bd9Sstevel@tonic-gate 		 * can not be resolved.
12907c478bd9Sstevel@tonic-gate 		 */
12917c478bd9Sstevel@tonic-gate 		if (rsp->rel_osdesc &&
12927c478bd9Sstevel@tonic-gate 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12937c478bd9Sstevel@tonic-gate 			return (0);
12945aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1295de777a60Sab 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12967c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12977c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12987c478bd9Sstevel@tonic-gate 		return (1);
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * Perform relocation.
13037c478bd9Sstevel@tonic-gate 	 */
13045aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
1307ba2be530Sab static uintptr_t
13085aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
13117c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
13121d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
13137c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	/*
1316d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1317d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
13187c478bd9Sstevel@tonic-gate 	 */
1319d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13207c478bd9Sstevel@tonic-gate 		/*
1321d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13247c478bd9Sstevel@tonic-gate 
1325d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13267c478bd9Sstevel@tonic-gate 			/*
1327d326b23bSrie 			 * Assign a GOT entry for static TLS references.
13287c478bd9Sstevel@tonic-gate 			 */
1329d326b23bSrie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1330d326b23bSrie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
13317c478bd9Sstevel@tonic-gate 
1332d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1333d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1334d326b23bSrie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
1335d326b23bSrie 					return (S_ERROR);
1336d326b23bSrie 			}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 			/*
1339d326b23bSrie 			 * IE access model.
13407c478bd9Sstevel@tonic-gate 			 */
1341d326b23bSrie 			if (IS_TLS_IE(rtype)) {
1342d326b23bSrie 				if (ld_add_actrel(FLG_REL_STLS,
13437c478bd9Sstevel@tonic-gate 				    rsp, ofl) == S_ERROR)
13447c478bd9Sstevel@tonic-gate 					return (S_ERROR);
1345d326b23bSrie 
1346d326b23bSrie 				/*
1347d326b23bSrie 				 * A non-pic shared object needs to adjust the
1348d326b23bSrie 				 * active relocation (indntpoff).
1349d326b23bSrie 				 */
1350d326b23bSrie 				if (((flags & FLG_OF_EXEC) == 0) &&
1351d326b23bSrie 				    (rtype == R_386_TLS_IE)) {
1352d326b23bSrie 					rsp->rel_rtype = R_386_RELATIVE;
1353d326b23bSrie 					return (ld_add_outrel(NULL, rsp, ofl));
1354d326b23bSrie 				}
1355d326b23bSrie 				return (1);
13567c478bd9Sstevel@tonic-gate 			}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 			/*
1359d326b23bSrie 			 * Fixups are required for other executable models.
13607c478bd9Sstevel@tonic-gate 			 */
13615aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13627c478bd9Sstevel@tonic-gate 			    rsp, ofl));
13637c478bd9Sstevel@tonic-gate 		}
1364d326b23bSrie 
13657c478bd9Sstevel@tonic-gate 		/*
1366d326b23bSrie 		 * LE access model.
13677c478bd9Sstevel@tonic-gate 		 */
13687c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13695aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13707c478bd9Sstevel@tonic-gate 
13715aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13725aefb655Srie 		    rsp, ofl));
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*
1376d326b23bSrie 	 * Building a shared object.
1377d326b23bSrie 	 *
1378d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13797c478bd9Sstevel@tonic-gate 	 */
13805aefb655Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13817c478bd9Sstevel@tonic-gate 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
1382d326b23bSrie 
1383d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1384d326b23bSrie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13857c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1386d326b23bSrie 
13875aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
13885aefb655Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13895aefb655Srie 	    ofl, 0)) == 0)) {
1390d326b23bSrie 
1391d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1392d326b23bSrie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1393d326b23bSrie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13947c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13957c478bd9Sstevel@tonic-gate 	}
1396d326b23bSrie 
13977c478bd9Sstevel@tonic-gate 	/*
13987c478bd9Sstevel@tonic-gate 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1399d326b23bSrie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1400d326b23bSrie 	 * symbol now, and prepare for the PLT magic.
14017c478bd9Sstevel@tonic-gate 	 */
14027c478bd9Sstevel@tonic-gate 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1403f5a18a30Srie 		Sym_desc	*tlsgetsym;
14047c478bd9Sstevel@tonic-gate 
14055aefb655Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1406f5a18a30Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
14077c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1408d326b23bSrie 
14097c478bd9Sstevel@tonic-gate 		rsp->rel_sym = tlsgetsym;
14107c478bd9Sstevel@tonic-gate 		rsp->rel_sname = tlsgetsym->sd_name;
14117c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_PLT32;
1412d326b23bSrie 
14135aefb655Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
14147c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1415d326b23bSrie 
14167c478bd9Sstevel@tonic-gate 		rsp->rel_sym = sdp;
14177c478bd9Sstevel@tonic-gate 		rsp->rel_sname = sdp->sd_name;
14187c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = rtype;
14197c478bd9Sstevel@tonic-gate 		return (1);
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
14235aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14247c478bd9Sstevel@tonic-gate 
14255aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
1429ba2be530Sab static Gotndx *
14305aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
14317c478bd9Sstevel@tonic-gate {
14327c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
14337c478bd9Sstevel@tonic-gate 	Gotndx *	gnp;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14367c478bd9Sstevel@tonic-gate 		return (ofl->ofl_tlsldgotndx);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14397c478bd9Sstevel@tonic-gate 		if (gnp->gn_gotref == gref)
14407c478bd9Sstevel@tonic-gate 			return (gnp);
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 	return ((Gotndx *)0);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
1445ba2be530Sab static Xword
14465aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	Os_desc		*osp = ofl->ofl_osgot;
14497c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rdesc->rel_sym;
14507c478bd9Sstevel@tonic-gate 	Xword		gotndx;
14517c478bd9Sstevel@tonic-gate 	Gotref		gref;
14527c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (rdesc->rel_flags & FLG_REL_DTLS)
14557c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSGD;
14567c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14577c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSLD;
14587c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_STLS)
14597c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSIE;
14607c478bd9Sstevel@tonic-gate 	else
14617c478bd9Sstevel@tonic-gate 		gref = GOT_REF_GENERIC;
14627c478bd9Sstevel@tonic-gate 
14635aefb655Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14647c478bd9Sstevel@tonic-gate 	assert(gnp);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	gotndx = (Xword)gnp->gn_gotndx;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14697c478bd9Sstevel@tonic-gate 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14707c478bd9Sstevel@tonic-gate 		gotndx++;
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
1477ba2be530Sab static uintptr_t
1478d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14797c478bd9Sstevel@tonic-gate     Rel_desc * rsp, Sym_desc * sdp)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	Gotndx	*gnp;
14827c478bd9Sstevel@tonic-gate 	uint_t	gotents;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (pgnp)
14857c478bd9Sstevel@tonic-gate 		return (1);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14887c478bd9Sstevel@tonic-gate 		gotents = 2;
14897c478bd9Sstevel@tonic-gate 	else
14907c478bd9Sstevel@tonic-gate 		gotents = 1;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14937c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14947c478bd9Sstevel@tonic-gate 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14957c478bd9Sstevel@tonic-gate 	gnp->gn_gotref = gref;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
15007c478bd9Sstevel@tonic-gate 		ofl->ofl_tlsldgotndx = gnp;
15017c478bd9Sstevel@tonic-gate 		return (1);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (list_appendc(lst, (void *)gnp) == 0)
15057c478bd9Sstevel@tonic-gate 		return (S_ERROR);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	return (1);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
1510ba2be530Sab static void
15115aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
15147c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1515141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /*
15197c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
15207c478bd9Sstevel@tonic-gate  */
1521ba2be530Sab static uintptr_t
1522d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
15237c478bd9Sstevel@tonic-gate {
15241d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
15251d9df23bSab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1526d326b23bSrie 
15277c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1528d326b23bSrie 		Sym_desc	*sdp;
15297c478bd9Sstevel@tonic-gate 
15305aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
15317c478bd9Sstevel@tonic-gate 		    SYM_NOHASH, 0, ofl)) != NULL) {
1532d326b23bSrie 			uchar_t	*genptr;
1533d326b23bSrie 
1534d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15357c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15367c478bd9Sstevel@tonic-gate 			/* LINTED */
15377c478bd9Sstevel@tonic-gate 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1538ba2be530Sab 			if (bswap)
1539ba2be530Sab 				/* LINTED */
1540ba2be530Sab 				*(Word *)genptr =
1541ba2be530Sab 				    /* LINTED */
1542ba2be530Sab 				    ld_bswap_Word(*(Word *)genptr);
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
15487c478bd9Sstevel@tonic-gate 	 * entry is:
15497c478bd9Sstevel@tonic-gate 	 *  if (building a.out) {
15507c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]		    # the address of the link map entry
15517c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]		    # the address of rtbinder
15527c478bd9Sstevel@tonic-gate 	 *  } else {
15537c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15547c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15557c478bd9Sstevel@tonic-gate 	 *  }
15567c478bd9Sstevel@tonic-gate 	 */
1557d326b23bSrie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1558b3fbe5e6Sseizo 		uchar_t *pltent;
15597c478bd9Sstevel@tonic-gate 
1560b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1561d326b23bSrie 		if (!(flags & FLG_OF_SHAROBJ)) {
15627c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15637c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_DISP;
15647c478bd9Sstevel@tonic-gate 			pltent += 2;
15657c478bd9Sstevel@tonic-gate 			/* LINTED */
15667c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1567de777a60Sab 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1568ba2be530Sab 			if (bswap)
1569ba2be530Sab 				/* LINTED */
1570ba2be530Sab 				*(Word *)pltent =
1571ba2be530Sab 				    /* LINTED */
1572ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15737c478bd9Sstevel@tonic-gate 			pltent += 4;
15747c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15757c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_DISP_IND;
15767c478bd9Sstevel@tonic-gate 			pltent += 2;
15777c478bd9Sstevel@tonic-gate 			/* LINTED */
15787c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1579de777a60Sab 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1580ba2be530Sab 			if (bswap)
1581ba2be530Sab 				/* LINTED */
1582ba2be530Sab 				*(Word *)pltent =
1583ba2be530Sab 				    /* LINTED */
1584ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15857c478bd9Sstevel@tonic-gate 		} else {
15867c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15877c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_REG_DISP;
15887c478bd9Sstevel@tonic-gate 			pltent += 2;
15897c478bd9Sstevel@tonic-gate 			/* LINTED */
15907c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1591de777a60Sab 			    M_GOT_ENTSIZE);
1592ba2be530Sab 			if (bswap)
1593ba2be530Sab 				/* LINTED */
1594ba2be530Sab 				*(Word *)pltent =
1595ba2be530Sab 				    /* LINTED */
1596ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15977c478bd9Sstevel@tonic-gate 			pltent += 4;
15987c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15997c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_REG_DISP_IND;
16007c478bd9Sstevel@tonic-gate 			pltent += 2;
16017c478bd9Sstevel@tonic-gate 			/* LINTED */
16027c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1603de777a60Sab 			    M_GOT_ENTSIZE);
1604ba2be530Sab 			if (bswap)
1605ba2be530Sab 				/* LINTED */
1606ba2be530Sab 				*(Word *)pltent =
1607ba2be530Sab 				    /* LINTED */
1608ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 	return (1);
16127c478bd9Sstevel@tonic-gate }
1613ba2be530Sab 
1614ba2be530Sab 
1615ba2be530Sab 
1616ba2be530Sab /*
1617ba2be530Sab  * Template for generating "void (*)(void)" function
1618ba2be530Sab  */
1619ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1620ba2be530Sab /* 0x00 */	0xc3				/* ret */
1621ba2be530Sab };
1622ba2be530Sab 
1623ba2be530Sab 
1624ba2be530Sab 
1625ba2be530Sab /*
1626ba2be530Sab  * Return the ld_targ definition for this target.
1627ba2be530Sab  */
1628ba2be530Sab const Target *
1629ba2be530Sab ld_targ_init_x86(void)
1630ba2be530Sab {
1631ba2be530Sab 	static const Target _ld_targ = {
1632ba2be530Sab 		{			/* Target_mach */
1633ba2be530Sab 			M_MACH,			/* m_mach */
1634ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1635ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1636ba2be530Sab 			M_CLASS,		/* m_class */
1637ba2be530Sab 			M_DATA,			/* m_data */
1638ba2be530Sab 
1639ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1640ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1641ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
1642ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1643ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1644ba2be530Sab 
1645ba2be530Sab 			/* Relocation type codes */
1646ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1647ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1648ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1649ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1650ba2be530Sab 			M_R_NUM,		/* m_r_num */
1651ba2be530Sab 			M_R_NONE,		/* m_r_none */
1652ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1653ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1654ba2be530Sab 
1655ba2be530Sab 			/* Relocation related constants */
1656ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1657ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1658ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1659ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1660ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1661ba2be530Sab 
1662ba2be530Sab 			/* GOT related constants */
1663ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1664ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1665ba2be530Sab 
1666ba2be530Sab 			/* PLT related constants */
1667ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1668ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1669ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1670ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1671ba2be530Sab 
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 */
1678ba2be530Sab 			M_ID_DATA,		/* id_data */
1679ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1680ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1681ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1682ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1683ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1684ba2be530Sab 			M_ID_GOT,		/* id_got */
1685ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1686ba2be530Sab 			M_ID_HASH,		/* id_hash */
1687ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1688ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1689ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1690ba2be530Sab 			M_ID_NOTE,		/* id_note */
1691ba2be530Sab 			M_ID_NULL,		/* id_null */
1692ba2be530Sab 			M_ID_PLT,		/* id_plt */
1693ba2be530Sab 			M_ID_REL,		/* id_rel */
1694ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1695ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1696ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1697ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1698ba2be530Sab 			M_ID_TEXT,		/* id_text */
1699ba2be530Sab 			M_ID_TLS,		/* id_tls */
1700ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1701ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1702ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
1703ba2be530Sab 			M_ID_USER,		/* id_user */
1704ba2be530Sab 			M_ID_VERSION,		/* id_version */
1705ba2be530Sab 		},
1706ba2be530Sab 		{			/* Target_nullfunc */
1707ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1708ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1709ba2be530Sab 		},
1710ba2be530Sab 		{			/* Target_machrel */
1711ba2be530Sab 			reloc_table,
1712ba2be530Sab 
1713ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1714ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1715ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1716ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1717ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1718ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1719ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1720ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1721ba2be530Sab 			NULL,			/* mr_reloc_register */
1722ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1723ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1724ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1725ba2be530Sab 			NULL,			/* mr_assign_got */
1726ba2be530Sab 			ld_find_gotndx,		/* mr_find_gotndx */
1727ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1728ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1729ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1730ba2be530Sab 			NULL,			/* mr_allocate_got */
1731ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1732ba2be530Sab 		},
1733ba2be530Sab 		{			/* Target_machsym */
1734ba2be530Sab 			NULL,			/* ms_reg_check */
1735ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1736ba2be530Sab 			NULL,			/* ms_is_regsym */
1737ba2be530Sab 			NULL,			/* ms_reg_find */
1738ba2be530Sab 			NULL			/* ms_reg_enter */
1739ba2be530Sab 		},
1740ba2be530Sab 		{			/* Target_unwind */
1741ba2be530Sab 			NULL,		/* uw_make_unwindhdr */
1742ba2be530Sab 			NULL,		/* uw_populate_unwindhdr */
1743ba2be530Sab 			NULL,		/* uw_append_unwind */
1744ba2be530Sab 		}
1745ba2be530Sab 	};
1746ba2be530Sab 
1747ba2be530Sab 	return (&_ld_targ);
1748ba2be530Sab }
1749