11618Srie /*
21618Srie  * CDDL HEADER START
31618Srie  *
41618Srie  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
71618Srie  *
81618Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91618Srie  * or http://www.opensolaris.org/os/licensing.
101618Srie  * See the License for the specific language governing permissions
111618Srie  * and limitations under the License.
121618Srie  *
131618Srie  * When distributing Covered Code, include this CDDL HEADER in each
141618Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151618Srie  * If applicable, add the following below this CDDL HEADER, with the
161618Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171618Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181618Srie  *
191618Srie  * CDDL HEADER END
201618Srie  */
211618Srie 
221618Srie /*
231618Srie  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
241618Srie  *	Copyright (c) 1988 AT&T
251618Srie  *	  All Rights Reserved
261618Srie  *
275892Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
281618Srie  * Use is subject to license terms.
291618Srie  */
301618Srie 
316206Sab196087 /* Get the x86 version of the relocation engine */
326206Sab196087 #define	DO_RELOC_LIBLD_X86
336206Sab196087 
341618Srie #include	<string.h>
351618Srie #include	<stdio.h>
361618Srie #include	<sys/elf_386.h>
371618Srie #include	<debug.h>
381618Srie #include	<reloc.h>
396206Sab196087 #include	<i386/machdep_x86.h>
401618Srie #include	"msg.h"
411618Srie #include	"_libld.h"
421618Srie 
436206Sab196087 /* Forward declarations */
446206Sab196087 static Gotndx	*ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *);
456206Sab196087 static Xword	ld_calc_got_offset(Rel_desc *, Ofl_desc *);
466206Sab196087 
476206Sab196087 
486206Sab196087 static Word
491618Srie ld_init_rel(Rel_desc *reld, void *reloc)
501618Srie {
511618Srie 	Rel *	rel = (Rel *)reloc;
521618Srie 
531618Srie 	/* LINTED */
546206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
551618Srie 	reld->rel_roffset = rel->r_offset;
561618Srie 	reld->rel_raddend = 0;
571618Srie 	reld->rel_typedata = 0;
581618Srie 
591618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
601618Srie }
611618Srie 
626206Sab196087 static void
631618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
641618Srie {
651618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
661618Srie }
671618Srie 
686206Sab196087 static void
691618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
701618Srie {
711618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
721618Srie 		/*
731618Srie 		 * Create this entry if we are going to create a PLT table.
741618Srie 		 */
751618Srie 		if (ofl->ofl_pltcnt)
761618Srie 			(*cnt)++;		/* DT_PLTGOT */
771618Srie 	}
781618Srie }
791618Srie 
806206Sab196087 static void
812145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
821618Srie {
831618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
841618Srie 		(*dyn)->d_tag = DT_PLTGOT;
851618Srie 		if (ofl->ofl_osgot)
861618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
871618Srie 		else
881618Srie 			(*dyn)->d_un.d_ptr = 0;
891618Srie 		(*dyn)++;
901618Srie 	}
911618Srie }
921618Srie 
936206Sab196087 static Xword
941618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
951618Srie {
961618Srie 	Xword	value;
971618Srie 
981618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
991618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1001618Srie 	return (value);
1011618Srie }
1021618Srie 
1031618Srie /*
1041618Srie  *  Build a single plt entry - code is:
1051618Srie  *	if (building a.out)
1061618Srie  *		JMP	*got_off
1071618Srie  *	else
1081618Srie  *		JMP	*got_off@GOT(%ebx)
1091618Srie  *	PUSHL	&rel_off
1101618Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1111618Srie  *
1121618Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1131618Srie  *	so the first pass through the plt jumps back here, jumping
1141618Srie  *	in turn to the first plt entry, which jumps to the dynamic
1151618Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1161618Srie  *	future plt calls to the proper destination.
1171618Srie  */
1181618Srie static void
1191618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1201618Srie {
1211618Srie 	uchar_t		*pltent, *gotent;
1221618Srie 	Sword		plt_off;
1231618Srie 	Word		got_off;
1246206Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1251618Srie 
1261618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1271618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1281618Srie 	    M_PLT_ENTSIZE);
1291618Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1301618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1311618Srie 
1321618Srie 	/*
1331618Srie 	 * Fill in the got entry with the address of the next instruction.
1341618Srie 	 */
1351618Srie 	/* LINTED */
1361618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1371618Srie 	    M_PLT_INSSIZE;
1386206Sab196087 	if (bswap)
1396206Sab196087 		/* LINTED */
1406206Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1411618Srie 
1421618Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1431618Srie 		pltent[0] = M_SPECIAL_INST;
1441618Srie 		pltent[1] = M_JMP_DISP_IND;
1451618Srie 		pltent += 2;
1461618Srie 		/* LINTED */
1471618Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1484734Sab196087 		    got_off);
1491618Srie 	} else {
1501618Srie 		pltent[0] = M_SPECIAL_INST;
1511618Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1521618Srie 		pltent += 2;
1531618Srie 		/* LINTED */
1541618Srie 		*(Word *)pltent = (Word)got_off;
1551618Srie 	}
1566206Sab196087 	if (bswap)
1576206Sab196087 		/* LINTED */
1586206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1591618Srie 	pltent += 4;
1601618Srie 
1611618Srie 	pltent[0] = M_INST_PUSHL;
1621618Srie 	pltent++;
1631618Srie 	/* LINTED */
1641618Srie 	*(Word *)pltent = (Word)rel_off;
1656206Sab196087 	if (bswap)
1666206Sab196087 		/* LINTED */
1676206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1681618Srie 	pltent += 4;
1691618Srie 
1701618Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
1711618Srie 	pltent[0] = M_INST_JMP;
1721618Srie 	pltent++;
1731618Srie 	/* LINTED */
1741618Srie 	*(Word *)pltent = (Word)plt_off;
1756206Sab196087 	if (bswap)
1766206Sab196087 		/* LINTED */
1776206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1781618Srie }
1791618Srie 
1806206Sab196087 static uintptr_t
1811618Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
1821618Srie {
1831618Srie 	Os_desc *	relosp, * osp = 0;
1841618Srie 	Word		ndx, roffset, value;
1851618Srie 	Rel		rea;
1861618Srie 	char		*relbits;
1871618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
1881618Srie 	int		sectmoved = 0;
1891618Srie 
1901618Srie 	sdp = orsp->rel_sym;
1911618Srie 
1921618Srie 	/*
1931618Srie 	 * If the section this relocation is against has been discarded
1941618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
1951618Srie 	 */
1961618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
1971618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
1981618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
1991618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2001618Srie 		return (1);
2011618Srie 	}
2021618Srie 
2031618Srie 	/*
2041618Srie 	 * If this is a relocation against a move table, or expanded move
2051618Srie 	 * table, adjust the relocation entries.
2061618Srie 	 */
2071618Srie 	if (orsp->rel_move)
2081618Srie 		ld_adj_movereloc(ofl, orsp);
2091618Srie 
2101618Srie 	/*
2111618Srie 	 * If this is a relocation against a section using a partial initialized
2121618Srie 	 * symbol, adjust the embedded symbol info.
2131618Srie 	 *
2141618Srie 	 * The second argument of the am_I_partial() is the value stored at the
2151618Srie 	 * target address relocation is going to be applied.
2161618Srie 	 */
2171618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2181618Srie 		if (ofl->ofl_parsym.head &&
2191618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2201618Srie 		    /* LINTED */
2211618Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2221618Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2231618Srie 		    orsp->rel_roffset)))) {
2241618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2251618Srie 			sectmoved = 1;
2264734Sab196087 		}
2271618Srie 	}
2281618Srie 
2291618Srie 	value = sdp->sd_sym->st_value;
2301618Srie 
2311618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2321618Srie 		osp = ofl->ofl_osgot;
2331618Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2341618Srie 
2351618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2361618Srie 		/*
2371618Srie 		 * Note that relocations for PLT's actually
2381618Srie 		 * cause a relocation againt the GOT.
2391618Srie 		 */
2401618Srie 		osp = ofl->ofl_osplt;
2411618Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2421618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2431618Srie 
2441618Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2451618Srie 
2461618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2471618Srie 		/*
2481618Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2491618Srie 		 * point to the new symbols location.
2501618Srie 		 */
2511618Srie 		osp = ofl->ofl_isbss->is_osdesc;
2521618Srie 		roffset = (Word)value;
2531618Srie 	} else {
2541618Srie 		osp = orsp->rel_osdesc;
2551618Srie 
2561618Srie 		/*
2571618Srie 		 * Calculate virtual offset of reference point; equals offset
2581618Srie 		 * into section + vaddr of section for loadable sections, or
2591618Srie 		 * offset plus section displacement for nonloadable sections.
2601618Srie 		 */
2611618Srie 		roffset = orsp->rel_roffset +
2621618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
2631618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2641618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
2651618Srie 			    os_shdr->sh_addr;
2661618Srie 	}
2671618Srie 
2681618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
2691618Srie 		relosp = ofl->ofl_osrel;
2701618Srie 
2711618Srie 	/*
2721618Srie 	 * Assign the symbols index for the output relocation.  If the
2731618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
2741618Srie 	 * the output sections symbols index.  Otherwise the index can be
2751618Srie 	 * derived from the symbols index itself.
2761618Srie 	 */
2771618Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
2781618Srie 		ndx = STN_UNDEF;
2791618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
2801618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2811618Srie 		if (sectmoved == 0) {
2821618Srie 			/*
2831618Srie 			 * Check for a null input section. This can
2841618Srie 			 * occur if this relocation references a symbol
2851618Srie 			 * generated by sym_add_sym().
2861618Srie 			 */
2871618Srie 			if ((sdp->sd_isc != 0) &&
2881618Srie 			    (sdp->sd_isc->is_osdesc != 0))
2891618Srie 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
2901618Srie 			else
2911618Srie 				ndx = sdp->sd_shndx;
2921618Srie 		} else
293*8159SAli.Bahrami@Sun.COM 			ndx = ofl->ofl_parexpnndx;
2941618Srie 	} else
2951618Srie 		ndx = sdp->sd_symndx;
2961618Srie 
2975892Sab196087 	/*
2985892Sab196087 	 * If we have a replacement value for the relocation
2995892Sab196087 	 * target, put it in place now.
3005892Sab196087 	 */
3015892Sab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
3025892Sab196087 		Xword	addend = orsp->rel_raddend;
3035892Sab196087 		uchar_t	*addr;
3045892Sab196087 
3055892Sab196087 		/*
3065892Sab196087 		 * Get the address of the data item we need to modify.
3075892Sab196087 		 */
3085892Sab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
3095892Sab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
3105892Sab196087 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
3115892Sab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
3125892Sab196087 			return (S_ERROR);
3135892Sab196087 	}
3145892Sab196087 
3151618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
3161618Srie 
3171618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3181618Srie 	rea.r_offset = roffset;
3191618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
3201618Srie 	    orsp->rel_sname));
3211618Srie 
3221618Srie 	/*
3231618Srie 	 * Assert we haven't walked off the end of our relocation table.
3241618Srie 	 */
3251618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3261618Srie 
3271618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3281618Srie 	    (char *)&rea, sizeof (Rel));
3291618Srie 	relosp->os_szoutrels += sizeof (Rel);
3301618Srie 
3311618Srie 	/*
3321618Srie 	 * Determine if this relocation is against a non-writable, allocatable
3331618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3341618Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3351618Srie 	 * result in modifications to the .got.
3361618Srie 	 */
3371618Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3381618Srie 		osp = ofl->ofl_osgot;
3391618Srie 
3401618Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3411618Srie 	return (1);
3421618Srie }
3431618Srie 
3441618Srie /*
3451618Srie  * i386 Instructions for TLS processing
3461618Srie  */
3471618Srie static uchar_t tlsinstr_gd_ie[] = {
3481618Srie 	/*
3491618Srie 	 * 0x00	movl %gs:0x0, %eax
3501618Srie 	 */
3511618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3521618Srie 	/*
3531618Srie 	 * 0x06	addl x(%eax), %eax
3541618Srie 	 * 0x0c ...
3551618Srie 	 */
3561618Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
3571618Srie };
3581618Srie 
3591618Srie static uchar_t tlsinstr_gd_le[] = {
3601618Srie 	/*
3611618Srie 	 * 0x00 movl %gs:0x0, %eax
3621618Srie 	 */
3631618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3641618Srie 	/*
3651618Srie 	 * 0x06 addl $0x0, %eax
3661618Srie 	 */
3671618Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
3681618Srie 	/*
3691618Srie 	 * 0x0b nop
3701618Srie 	 * 0x0c
3711618Srie 	 */
3721618Srie 	0x90
3731618Srie };
3741618Srie 
3751618Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
3761618Srie 	/*
3771618Srie 	 *	movl %gs:0x0,%eax
3781618Srie 	 */
3791618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
3801618Srie };
3811618Srie 
3821618Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
3831618Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
3841618Srie 
3851618Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
3861618Srie 
3871618Srie #define	TLS_NOP		0x90	/* NOP instruction */
3881618Srie 
3891618Srie #define	MODRM_MSK_MOD	0xc0
3901618Srie #define	MODRM_MSK_RO	0x38
3911618Srie #define	MODRM_MSK_RM	0x07
3921618Srie 
3931618Srie #define	SIB_MSK_SS	0xc0
3941618Srie #define	SIB_MSK_IND	0x38
3951618Srie #define	SIB_MSK_BS	0x07
3961618Srie 
3971618Srie static Fixupret
3981618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
3991618Srie {
4001618Srie 	Sym_desc	*sdp = arsp->rel_sym;
4011618Srie 	Word		rtype = arsp->rel_rtype;
4021618Srie 	uchar_t		*offset, r1, r2;
4031618Srie 
4041618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
4051618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
4061618Srie 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4071618Srie 
4081618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
4091618Srie 		/*
4101618Srie 		 * IE reference model
4111618Srie 		 */
4121618Srie 		switch (rtype) {
4131618Srie 		case R_386_TLS_GD:
4141618Srie 			/*
4151618Srie 			 * Transition:
4161618Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4171618Srie 			 *	0x7 call ___tls_get_addr
4181618Srie 			 *	0xc
4191618Srie 			 * To:
4201618Srie 			 *	0x0 movl %gs:0, %eax
4211618Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4221618Srie 			 */
4231618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4243304Srie 			    R_386_TLS_GOTIE, arsp));
4251618Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4261618Srie 			arsp->rel_roffset += 5;
4271618Srie 
4281618Srie 			/*
4293304Srie 			 * Adjust 'offset' to beginning of instruction
4301618Srie 			 * sequence.
4311618Srie 			 */
4321618Srie 			offset -= 3;
4331618Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4341618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4351618Srie 			    sizeof (tlsinstr_gd_ie));
4361618Srie 
4371618Srie 			/*
4381618Srie 			 * set register %r1 into the addl
4391618Srie 			 * instruction.
4401618Srie 			 */
4411618Srie 			offset[0x7] |= r1;
4421618Srie 			return (FIX_RELOC);
4431618Srie 
4441618Srie 		case R_386_TLS_GD_PLT:
4451618Srie 			/*
4461618Srie 			 * Fixup done via the TLS_GD relocation
4471618Srie 			 */
4481618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4493304Srie 			    R_386_NONE, arsp));
4501618Srie 			return (FIX_DONE);
4511618Srie 		}
4521618Srie 	}
4531618Srie 
4541618Srie 	/*
4551618Srie 	 * LE reference model
4561618Srie 	 */
4571618Srie 	switch (rtype) {
4581618Srie 	case R_386_TLS_GD:
4591618Srie 		/*
4601618Srie 		 * Transition:
4611618Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
4621618Srie 		 *	0x7 call ___tls_get_addr
4631618Srie 		 *	0xc
4641618Srie 		 * To:
4651618Srie 		 *	0x0 movl %gs:0, %eax
4661618Srie 		 *	0x6 addl $x@ntpoff, %eax
4671618Srie 		 *	0xb nop
4681618Srie 		 *	0xc
4691618Srie 		 */
4701618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4713304Srie 		    R_386_TLS_LE, arsp));
4721618Srie 
4731618Srie 		arsp->rel_rtype = R_386_TLS_LE;
4741618Srie 		arsp->rel_roffset += 4;
4751618Srie 
4761618Srie 		/*
4773304Srie 		 * Adjust 'offset' to beginning of instruction
4781618Srie 		 * sequence.
4791618Srie 		 */
4801618Srie 		offset -= 3;
4811618Srie 		(void) memcpy(offset, tlsinstr_gd_le,
4821618Srie 		    sizeof (tlsinstr_gd_le));
4831618Srie 		return (FIX_RELOC);
4841618Srie 
4851618Srie 	case R_386_TLS_GD_PLT:
4861618Srie 	case R_386_PLT32:
4871618Srie 		/*
4881618Srie 		 * Fixup done via the TLS_GD relocation
4891618Srie 		 */
4901618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4913304Srie 		    R_386_NONE, arsp));
4921618Srie 		return (FIX_DONE);
4931618Srie 
4941618Srie 	case R_386_TLS_LDM_PLT:
4951618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4963304Srie 		    R_386_NONE, arsp));
4971618Srie 
4981618Srie 		/*
4991618Srie 		 * Transition:
5001618Srie 		 *	call __tls_get_addr()
5011618Srie 		 * to:
5021618Srie 		 *	nop
5031618Srie 		 *	nop
5041618Srie 		 *	nop
5051618Srie 		 *	nop
5061618Srie 		 *	nop
5071618Srie 		 */
5081618Srie 		*(offset - 1) = TLS_NOP;
5091618Srie 		*(offset) = TLS_NOP;
5101618Srie 		*(offset + 1) = TLS_NOP;
5111618Srie 		*(offset + 2) = TLS_NOP;
5121618Srie 		*(offset + 3) = TLS_NOP;
5131618Srie 		return (FIX_DONE);
5141618Srie 
5151618Srie 	case R_386_TLS_LDM:
5161618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5173304Srie 		    R_386_NONE, arsp));
5181618Srie 
5191618Srie 		/*
5201618Srie 		 * Transition:
5211618Srie 		 *
5221618Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5231618Srie 		 *  0x06 call ___tls_get_addr
5241618Srie 		 *
5251618Srie 		 * to:
5261618Srie 		 *
5271618Srie 		 *  0x00 movl %gs:0, %eax
5281618Srie 		 */
5291618Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5301618Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5311618Srie 		return (FIX_DONE);
5321618Srie 
5331618Srie 	case R_386_TLS_LDO_32:
5341618Srie 		/*
5351618Srie 		 *  Instructions:
5361618Srie 		 *
5371618Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5381618Srie 		 *		to
5391618Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5401618Srie 		 *
5411618Srie 		 */
5421618Srie 		offset -= 2;
5431618Srie 
5441618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5453304Srie 		    R_386_TLS_LE, arsp));
5461618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5471618Srie 		return (FIX_RELOC);
5481618Srie 
5491618Srie 	case R_386_TLS_GOTIE:
5501618Srie 		/*
5511618Srie 		 * These transitions are a little different than the
5521618Srie 		 * others, in that we could have multiple instructions
5531618Srie 		 * pointed to by a single relocation.  Depending upon the
5541618Srie 		 * instruction, we perform a different code transition.
5551618Srie 		 *
5561618Srie 		 * Here's the known transitions:
5571618Srie 		 *
5581618Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
5591618Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5601618Srie 		 *
5611618Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
5621618Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5631618Srie 		 *
5641618Srie 		 *  Transitions IE -> LE
5651618Srie 		 *
5661618Srie 		 *  1) movl $foo@ntpoff, %reg2
5671618Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
5681618Srie 		 *
5691618Srie 		 *  2) addl $foo@ntpoff, %reg2
5701618Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
5711618Srie 		 *
5721618Srie 		 * Note: reg1 != 4 (%esp)
5731618Srie 		 */
5741618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5753304Srie 		    R_386_TLS_LE, arsp));
5761618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5771618Srie 
5781618Srie 		offset -= 2;
5791618Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
5801618Srie 		if (offset[0] == 0x8b) {
5811618Srie 			/* case 1 above */
5821618Srie 			offset[0] = 0xc7;	/* movl */
5831618Srie 			offset[1] = 0xc0 | r2;
5841618Srie 			return (FIX_RELOC);
5851618Srie 		}
5861618Srie 
5871618Srie 		if (offset[0] == 0x03) {
5881618Srie 			/* case 2 above */
5891618Srie 			assert(offset[0] == 0x03);
5901618Srie 			offset[0] = 0x81;	/* addl */
5911618Srie 			offset[1] = 0xc0 | r2;
5921618Srie 			return (FIX_RELOC);
5931618Srie 		}
5941618Srie 
5951618Srie 		/*
5961618Srie 		 * Unexpected instruction sequence - fatal error.
5971618Srie 		 */
5984734Sab196087 		{
5994734Sab196087 			Conv_inv_buf_t	inv_buf;
6004734Sab196087 
6014734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6024734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6034734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6044734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6054734Sab196087 			    demangle(arsp->rel_sname),
6064734Sab196087 			    arsp->rel_isdesc->is_name,
6074734Sab196087 			    EC_OFF(arsp->rel_roffset));
6084734Sab196087 		}
6091618Srie 		return (FIX_ERROR);
6101618Srie 
6111618Srie 	case R_386_TLS_IE:
6121618Srie 		/*
6131618Srie 		 * These transitions are a little different than the
6141618Srie 		 * others, in that we could have multiple instructions
6151618Srie 		 * pointed to by a single relocation.  Depending upon the
6161618Srie 		 * instruction, we perform a different code transition.
6171618Srie 		 *
6181618Srie 		 * Here's the known transitions:
6191618Srie 		 *  1) movl foo@indntpoff, %eax
6201618Srie 		 *	0xa1, foo@indntpoff
6211618Srie 		 *
6221618Srie 		 *  2) movl foo@indntpoff, %eax
6231618Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6241618Srie 		 *
6251618Srie 		 *  3) addl foo@indntpoff, %eax
6261618Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6271618Srie 		 *
6281618Srie 		 *  Transitions IE -> LE
6291618Srie 		 *
6301618Srie 		 *  1) movl $foo@ntpoff, %eax
6311618Srie 		 *	0xb8, foo@ntpoff
6321618Srie 		 *
6331618Srie 		 *  2) movl $foo@ntpoff, %reg
6341618Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6351618Srie 		 *
6361618Srie 		 *  3) addl $foo@ntpoff, %reg
6371618Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6381618Srie 		 */
6391618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6401618Srie 		offset--;
6411618Srie 		if (offset[0] == 0xa1) {
6421618Srie 			/* case 1 above */
6431618Srie 			offset[0] = 0xb8;	/*  movl */
6441618Srie 			return (FIX_RELOC);
6451618Srie 		}
6461618Srie 
6471618Srie 		offset--;
6481618Srie 		if (offset[0] == 0x8b) {
6491618Srie 			/* case 2 above */
6501618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6511618Srie 			offset[0] = 0xc7;	/* movl */
6521618Srie 			offset[1] = 0xc0 | r2;
6531618Srie 			return (FIX_RELOC);
6541618Srie 		}
6551618Srie 		if (offset[0] == 0x03) {
6561618Srie 			/* case 3 above */
6571618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6581618Srie 			offset[0] = 0x81;	/* addl */
6591618Srie 			offset[1] = 0xc0 | r2;
6601618Srie 			return (FIX_RELOC);
6611618Srie 		}
6621618Srie 		/*
6631618Srie 		 * Unexpected instruction sequence - fatal error.
6641618Srie 		 */
6654734Sab196087 		{
6664734Sab196087 			Conv_inv_buf_t	inv_buf;
6674734Sab196087 
6684734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6694734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6704734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6714734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6724734Sab196087 			    demangle(arsp->rel_sname),
6734734Sab196087 			    arsp->rel_isdesc->is_name,
6744734Sab196087 			    EC_OFF(arsp->rel_roffset));
6754734Sab196087 		}
6761618Srie 		return (FIX_ERROR);
6771618Srie 	}
6781618Srie 	return (FIX_RELOC);
6791618Srie }
6801618Srie 
6816206Sab196087 static uintptr_t
6821618Srie ld_do_activerelocs(Ofl_desc *ofl)
6831618Srie {
6841618Srie 	Rel_desc	*arsp;
6851618Srie 	Rel_cache	*rcp;
6861618Srie 	Listnode	*lnp;
6871618Srie 	uintptr_t	return_code = 1;
6886299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
6891618Srie 
6902647Srie 	if (ofl->ofl_actrels.head)
6912647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6922647Srie 
6931618Srie 	/*
6941618Srie 	 * Process active relocations.
6951618Srie 	 */
6961618Srie 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6971618Srie 		/* LINTED */
6981618Srie 		for (arsp = (Rel_desc *)(rcp + 1);
6991618Srie 		    arsp < rcp->rc_free; arsp++) {
7001618Srie 			uchar_t		*addr;
7011618Srie 			Xword 		value;
7021618Srie 			Sym_desc	*sdp;
7031618Srie 			const char	*ifl_name;
7041618Srie 			Xword		refaddr;
7051618Srie 			int		moved = 0;
7061618Srie 			Gotref		gref;
7071618Srie 
7081618Srie 			/*
7091618Srie 			 * If the section this relocation is against has been
7101618Srie 			 * discarded (-zignore), then discard (skip) the
7111618Srie 			 * relocation itself.
7121618Srie 			 */
7131618Srie 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
7141618Srie 			    ((arsp->rel_flags &
7151618Srie 			    (FLG_REL_GOT | FLG_REL_BSS |
7161618Srie 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
7171618Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
7181618Srie 				    M_MACH, arsp));
7191618Srie 				continue;
7201618Srie 			}
7211618Srie 
7221618Srie 			/*
7231618Srie 			 * We deteremine what the 'got reference'
7241618Srie 			 * model (if required) is at this point.  This
7251618Srie 			 * needs to be done before tls_fixup() since
7261618Srie 			 * it may 'transition' our instructions.
7271618Srie 			 *
7281618Srie 			 * The got table entries have already been assigned,
7291618Srie 			 * and we bind to those initial entries.
7301618Srie 			 */
7311618Srie 			if (arsp->rel_flags & FLG_REL_DTLS)
7321618Srie 				gref = GOT_REF_TLSGD;
7331618Srie 			else if (arsp->rel_flags & FLG_REL_MTLS)
7341618Srie 				gref = GOT_REF_TLSLD;
7351618Srie 			else if (arsp->rel_flags & FLG_REL_STLS)
7361618Srie 				gref = GOT_REF_TLSIE;
7371618Srie 			else
7381618Srie 				gref = GOT_REF_GENERIC;
7391618Srie 
7401618Srie 			/*
7411618Srie 			 * Perform any required TLS fixups.
7421618Srie 			 */
7431618Srie 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7441618Srie 				Fixupret	ret;
7451618Srie 
7461618Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7471618Srie 					return (S_ERROR);
7481618Srie 				if (ret == FIX_DONE)
7491618Srie 					continue;
7501618Srie 			}
7511618Srie 
7521618Srie 			/*
7531618Srie 			 * If this is a relocation against a move table, or
7541618Srie 			 * expanded move table, adjust the relocation entries.
7551618Srie 			 */
7561618Srie 			if (arsp->rel_move)
7571618Srie 				ld_adj_movereloc(ofl, arsp);
7581618Srie 
7591618Srie 			sdp = arsp->rel_sym;
7601618Srie 			refaddr = arsp->rel_roffset +
7611618Srie 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7621618Srie 
7631618Srie 			if (arsp->rel_flags & FLG_REL_CLVAL)
7641618Srie 				value = 0;
7651618Srie 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7661618Srie 			    STT_SECTION) {
7671618Srie 				Sym_desc	*sym;
7681618Srie 
7691618Srie 				/*
7701618Srie 				 * The value for a symbol pointing to a SECTION
7711618Srie 				 * is based off of that sections position.
7721618Srie 				 *
7731618Srie 				 * The second argument of the ld_am_I_partial()
7741618Srie 				 * is the value stored at the target address
7751618Srie 				 * relocation is going to be applied.
7761618Srie 				 */
7771618Srie 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7781618Srie 				    /* LINTED */
7791618Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
7801618Srie 				    ((uchar_t *)
7811618Srie 				    arsp->rel_isdesc->is_indata->d_buf +
7821618Srie 				    arsp->rel_roffset)))) {
7831618Srie 					/*
7841618Srie 					 * If the symbol is moved,
7851618Srie 					 * adjust the value
7861618Srie 					 */
7871618Srie 					value = sym->sd_sym->st_value;
7881618Srie 					moved = 1;
7891618Srie 				} else {
7901618Srie 					value = _elf_getxoff(
7911618Srie 					    sdp->sd_isc->is_indata);
7921618Srie 					if (sdp->sd_isc->is_shdr->sh_flags &
7931618Srie 					    SHF_ALLOC)
7941618Srie 						value += sdp->sd_isc->
7951618Srie 						    is_osdesc->os_shdr->sh_addr;
7961618Srie 				}
7971618Srie 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7981618Srie 					value -= ofl->ofl_tlsphdr->p_vaddr;
7992850Srie 
8002850Srie 			} else if (IS_SIZE(arsp->rel_rtype)) {
8012850Srie 				/*
8022850Srie 				 * Size relocations require the symbols size.
8032850Srie 				 */
8042850Srie 				value = sdp->sd_sym->st_size;
8051618Srie 			} else {
8061618Srie 				/*
8072647Srie 				 * Else the value is the symbols value.
8081618Srie 				 */
8091618Srie 				value = sdp->sd_sym->st_value;
8101618Srie 			}
8111618Srie 
8121618Srie 			/*
8131618Srie 			 * Relocation against the GLOBAL_OFFSET_TABLE.
8141618Srie 			 */
8151618Srie 			if (arsp->rel_flags & FLG_REL_GOT)
8161618Srie 				arsp->rel_osdesc = ofl->ofl_osgot;
8171618Srie 
8181618Srie 			/*
8191618Srie 			 * If loadable and not producing a relocatable object
8201618Srie 			 * add the sections virtual address to the reference
8211618Srie 			 * address.
8221618Srie 			 */
8231618Srie 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
8241618Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8251618Srie 				refaddr += arsp->rel_isdesc->is_osdesc->
8261618Srie 				    os_shdr->sh_addr;
8271618Srie 
8281618Srie 			/*
8291618Srie 			 * If this entry has a PLT assigned to it, it's
8301618Srie 			 * value is actually the address of the PLT (and
8311618Srie 			 * not the address of the function).
8321618Srie 			 */
8331618Srie 			if (IS_PLT(arsp->rel_rtype)) {
8341618Srie 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8351618Srie 					value = ld_calc_plt_addr(sdp, ofl);
8361618Srie 			}
8371618Srie 
8381618Srie 			/*
8391618Srie 			 * Determine whether the value needs further adjustment.
8401618Srie 			 * Filter through the attributes of the relocation to
8411618Srie 			 * determine what adjustment is required.  Note, many
8421618Srie 			 * of the following cases are only applicable when a
8431618Srie 			 * .got is present.  As a .got is not generated when a
8441618Srie 			 * relocatable object is being built, any adjustments
8451618Srie 			 * that require a .got need to be skipped.
8461618Srie 			 */
8471618Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
8481618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8491618Srie 				Xword		R1addr;
8501618Srie 				uintptr_t	R2addr;
8511618Srie 				Word		gotndx;
8521618Srie 				Gotndx		*gnp;
8531618Srie 
8541618Srie 				/*
8551618Srie 				 * Perform relocation against GOT table.  Since
8561618Srie 				 * this doesn't fit exactly into a relocation
8571618Srie 				 * we place the appropriate byte in the GOT
8581618Srie 				 * directly
8591618Srie 				 *
8601618Srie 				 * Calculate offset into GOT at which to apply
8611618Srie 				 * the relocation.
8621618Srie 				 */
8631618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8641618Srie 				    ofl, 0);
8651618Srie 				assert(gnp);
8661618Srie 
8671618Srie 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
8681618Srie 					gotndx = gnp->gn_gotndx + 1;
8691618Srie 				else
8701618Srie 					gotndx = gnp->gn_gotndx;
8711618Srie 
8721618Srie 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8731618Srie 
8741618Srie 				/*
8751618Srie 				 * Add the GOTs data's offset.
8761618Srie 				 */
8771618Srie 				R2addr = R1addr + (uintptr_t)
8781618Srie 				    arsp->rel_osdesc->os_outdata->d_buf;
8791618Srie 
8801618Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8811618Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
8821618Srie 				    arsp->rel_rtype, R1addr, value,
8831618Srie 				    arsp->rel_sname, arsp->rel_osdesc));
8841618Srie 
8851618Srie 				/*
8861618Srie 				 * And do it.
8871618Srie 				 */
8885189Sab196087 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
8895189Sab196087 					*(Xword *)R2addr =
8906206Sab196087 					    ld_bswap_Xword(value);
8915189Sab196087 				else
8925189Sab196087 					*(Xword *)R2addr = value;
8931618Srie 				continue;
8941618Srie 
8951618Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
8961618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8971618Srie 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
8981618Srie 
8991618Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
9001618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9011618Srie 				value =
9021618Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
9031618Srie 				    refaddr;
9041618Srie 
9051618Srie 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9061618Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9071618Srie 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9081618Srie 				value -= refaddr;
9091618Srie 
9101618Srie 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
9111618Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
9121618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9131618Srie 				Gotndx	*gnp;
9141618Srie 
9151618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
9161618Srie 				    ofl, 0);
9171618Srie 				assert(gnp);
9181618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9191618Srie 				if (arsp->rel_rtype == R_386_TLS_IE) {
9201618Srie 					value +=
9211618Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9221618Srie 				}
9231618Srie 
9241618Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
9251618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9261618Srie 				Gotndx *gnp;
9271618Srie 
9281618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9291618Srie 				    GOT_REF_GENERIC, ofl, 0);
9301618Srie 				assert(gnp);
9311618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9321618Srie 
9331618Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
9341618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9351618Srie 				Xword	tlsstatsize;
9361618Srie 
9371618Srie 				/*
9381618Srie 				 * This is the LE TLS reference model.  Static
9391618Srie 				 * offset is hard-coded.
9401618Srie 				 */
9411618Srie 				tlsstatsize =
9421618Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9431618Srie 				    M_TLSSTATALIGN);
9441618Srie 				value = tlsstatsize - value;
9451618Srie 
9461618Srie 				/*
9471618Srie 				 * Since this code is fixed up, it assumes a
9481618Srie 				 * negative offset that can be added to the
9491618Srie 				 * thread pointer.
9501618Srie 				 */
9511618Srie 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9521618Srie 				    (arsp->rel_rtype == R_386_TLS_LE))
9531618Srie 					value = -value;
9541618Srie 			}
9551618Srie 
9561618Srie 			if (arsp->rel_isdesc->is_file)
9571618Srie 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9581618Srie 			else
9591618Srie 				ifl_name = MSG_INTL(MSG_STR_NULL);
9601618Srie 
9611618Srie 			/*
9621618Srie 			 * Make sure we have data to relocate.  Compiler and
9631618Srie 			 * assembler developers have been known to generate
9641618Srie 			 * relocations against invalid sections (normally .bss),
9651618Srie 			 * so for their benefit give them sufficient information
9661618Srie 			 * to help analyze the problem.  End users should never
9671618Srie 			 * see this.
9681618Srie 			 */
9691618Srie 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
9704734Sab196087 				Conv_inv_buf_t	inv_buf;
9714734Sab196087 
9721618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9731618Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
9744734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
9754734Sab196087 				    0, &inv_buf),
9761618Srie 				    ifl_name, demangle(arsp->rel_sname),
9771618Srie 				    arsp->rel_isdesc->is_name);
9781618Srie 				return (S_ERROR);
9791618Srie 			}
9801618Srie 
9811618Srie 			/*
9821618Srie 			 * Get the address of the data item we need to modify.
9831618Srie 			 */
9841618Srie 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
9851618Srie 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
9861618Srie 			    is_indata));
9871618Srie 
9881618Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9891618Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
9901618Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9911618Srie 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9921618Srie 
9931618Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9941618Srie 			    ofl->ofl_size) || (arsp->rel_roffset >
9951618Srie 			    arsp->rel_osdesc->os_shdr->sh_size)) {
9964734Sab196087 				Conv_inv_buf_t	inv_buf;
9974734Sab196087 				int		class;
9981618Srie 
9991618Srie 				if (((uintptr_t)addr -
10001618Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10011618Srie 					class = ERR_FATAL;
10021618Srie 				else
10031618Srie 					class = ERR_WARNING;
10041618Srie 
10051618Srie 				eprintf(ofl->ofl_lml, class,
10061618Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
10074734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10084734Sab196087 				    0, &inv_buf),
10091618Srie 				    ifl_name, arsp->rel_isdesc->is_name,
10101618Srie 				    demangle(arsp->rel_sname),
10111618Srie 				    EC_ADDR((uintptr_t)addr -
10121618Srie 				    (uintptr_t)ofl->ofl_nehdr));
10131618Srie 
10141618Srie 				if (class == ERR_FATAL) {
10151618Srie 					return_code = S_ERROR;
10161618Srie 					continue;
10171618Srie 				}
10181618Srie 			}
10191618Srie 
10201618Srie 			/*
10211618Srie 			 * The relocation is additive.  Ignore the previous
10221618Srie 			 * symbol value if this local partial symbol is
10231618Srie 			 * expanded.
10241618Srie 			 */
10251618Srie 			if (moved)
10261618Srie 				value -= *addr;
10271618Srie 
10281618Srie 			/*
10295892Sab196087 			 * If we have a replacement value for the relocation
10305892Sab196087 			 * target, put it in place now.
10315892Sab196087 			 */
10325892Sab196087 			if (arsp->rel_flags & FLG_REL_NADDEND) {
10335892Sab196087 				Xword addend = arsp->rel_raddend;
10345892Sab196087 
10355892Sab196087 				if (ld_reloc_targval_set(ofl, arsp,
10365892Sab196087 				    addr, addend) == 0)
10375892Sab196087 					return (S_ERROR);
10385892Sab196087 			}
10395892Sab196087 
10405892Sab196087 			/*
10415189Sab196087 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10421618Srie 			 * stage.
10431618Srie 			 */
10445189Sab196087 			if (OFL_DO_RELOC(ofl)) {
10455189Sab196087 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10461618Srie 				    &value, arsp->rel_sname, ifl_name,
10475189Sab196087 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10481618Srie 				    ofl->ofl_lml) == 0)
10491618Srie 					return_code = S_ERROR;
10501618Srie 			}
10511618Srie 		}
10521618Srie 	}
10531618Srie 	return (return_code);
10541618Srie }
10551618Srie 
10561618Srie /*
10571618Srie  * Add an output relocation record.
10581618Srie  */
10596206Sab196087 static uintptr_t
10601618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10611618Srie {
10621618Srie 	Rel_desc	*orsp;
10631618Srie 	Rel_cache	*rcp;
10641618Srie 	Sym_desc	*sdp = rsp->rel_sym;
10651618Srie 
10661618Srie 	/*
10671618Srie 	 * Static executables *do not* want any relocations against them.
10681618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10691618Srie 	 * symbol in a static executable, it's best to disable them here
10701618Srie 	 * instead of through out the relocation code.
10711618Srie 	 */
10721618Srie 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
10731618Srie 	    (FLG_OF_STATIC | FLG_OF_EXEC))
10741618Srie 		return (1);
10751618Srie 
10761618Srie 	/*
10771618Srie 	 * If no relocation cache structures are available allocate
10781618Srie 	 * a new one and link it into the cache list.
10791618Srie 	 */
10801618Srie 	if ((ofl->ofl_outrels.tail == 0) ||
10811618Srie 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10821618Srie 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10831618Srie 		static size_t	nextsize = 0;
10841618Srie 		size_t		size;
10851618Srie 
10861618Srie 		/*
10871618Srie 		 * Output relocation numbers can vary considerably between
10881618Srie 		 * building executables or shared objects (pic vs. non-pic),
10891618Srie 		 * etc.  But, they typically aren't very large, so for these
10901618Srie 		 * objects use a standard bucket size.  For building relocatable
10911618Srie 		 * objects, typically there will be an output relocation for
10921618Srie 		 * every input relocation.
10931618Srie 		 */
10941618Srie 		if (nextsize == 0) {
10951618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10961618Srie 				if ((size = ofl->ofl_relocincnt) == 0)
10971618Srie 					size = REL_LOIDESCNO;
10981618Srie 				if (size > REL_HOIDESCNO)
10991618Srie 					nextsize = REL_HOIDESCNO;
11001618Srie 				else
11011618Srie 					nextsize = REL_LOIDESCNO;
11021618Srie 			} else
11031618Srie 				nextsize = size = REL_HOIDESCNO;
11041618Srie 		} else
11051618Srie 			size = nextsize;
11061618Srie 
11071618Srie 		size = size * sizeof (Rel_desc);
11081618Srie 
11091618Srie 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
11101618Srie 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
11111618Srie 			return (S_ERROR);
11121618Srie 
11131618Srie 		/* LINTED */
11141618Srie 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
11151618Srie 		/* LINTED */
11161618Srie 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
11171618Srie 	}
11181618Srie 
11191618Srie 	/*
11201618Srie 	 * If we are adding a output relocation against a section
11211618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11221618Srie 	 * will be added to the .dynsym symbol table.
11231618Srie 	 */
11241618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11251618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11261618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11271618Srie 
11281618Srie 		/*
11291618Srie 		 * If this is a COMMON symbol - no output section
11301618Srie 		 * exists yet - (it's created as part of sym_validate()).
11311618Srie 		 * So - we mark here that when it's created it should
11321618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11331618Srie 		 */
11341618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11351682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11361618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11371618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11381618Srie 			else
11391618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11401618Srie 		} else {
11411618Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11421618Srie 
11432347Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11441618Srie 				ofl->ofl_dynshdrcnt++;
11451618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11461618Srie 			}
11471618Srie 		}
11481618Srie 	}
11491618Srie 
11501618Srie 	*orsp = *rsp;
11511618Srie 	orsp->rel_flags |= flags;
11521618Srie 
11531618Srie 	rcp->rc_free++;
11541618Srie 	ofl->ofl_outrelscnt++;
11551618Srie 
11561618Srie 	if (flags & FLG_REL_GOT)
11571618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11581618Srie 	else if (flags & FLG_REL_PLT)
11591618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11601618Srie 	else if (flags & FLG_REL_BSS)
11611618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11621618Srie 	else if (flags & FLG_REL_NOINFO)
11631618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11641618Srie 	else
11651618Srie 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
11661618Srie 
11671618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11681618Srie 		ofl->ofl_relocrelcnt++;
11691618Srie 
11701618Srie 	/*
11711618Srie 	 * We don't perform sorting on PLT relocations because
11721618Srie 	 * they have already been assigned a PLT index and if we
11731618Srie 	 * were to sort them we would have to re-assign the plt indexes.
11741618Srie 	 */
11751618Srie 	if (!(flags & FLG_REL_PLT))
11761618Srie 		ofl->ofl_reloccnt++;
11771618Srie 
11781618Srie 	/*
11791618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11801618Srie 	 */
11811618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11821618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11831618Srie 
11841618Srie 	/*
11851618Srie 	 * Identify and possibly warn of a displacement relocation.
11861618Srie 	 */
11871618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11881618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11891618Srie 
11901618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11911618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11921618Srie 	}
11931618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11941618Srie 	    M_MACH, orsp));
11951618Srie 	return (1);
11961618Srie }
11971618Srie 
11981618Srie /*
11991618Srie  * process relocation for a LOCAL symbol
12001618Srie  */
12016206Sab196087 static uintptr_t
12021618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12031618Srie {
12046299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12051618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12061682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12071618Srie 
12081618Srie 	/*
12091618Srie 	 * if ((shared object) and (not pc relative relocation) and
12101618Srie 	 *    (not against ABS symbol))
12111618Srie 	 * then
12121618Srie 	 *	build R_386_RELATIVE
12131618Srie 	 * fi
12141618Srie 	 */
12151618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12162850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12171618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12181618Srie 	    !(rsp->rel_isdesc != NULL &&
12191618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12201618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12211618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12221618Srie 		Word	ortype = rsp->rel_rtype;
12231618Srie 
12241618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12251618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12261618Srie 			return (S_ERROR);
12271618Srie 		rsp->rel_rtype = ortype;
12281618Srie 	}
12291618Srie 
12301618Srie 	/*
12311618Srie 	 * If the relocation is against a 'non-allocatable' section
12321618Srie 	 * and we can not resolve it now - then give a warning
12331618Srie 	 * message.
12341618Srie 	 *
12351618Srie 	 * We can not resolve the symbol if either:
12361618Srie 	 *	a) it's undefined
12371618Srie 	 *	b) it's defined in a shared library and a
12381618Srie 	 *	   COPY relocation hasn't moved it to the executable
12391618Srie 	 *
12401618Srie 	 * Note: because we process all of the relocations against the
12411618Srie 	 *	text segment before any others - we know whether
12421618Srie 	 *	or not a copy relocation will be generated before
12431618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12441618Srie 	 */
12451618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12461618Srie 	    ((shndx == SHN_UNDEF) ||
12471618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12481618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
12494734Sab196087 		Conv_inv_buf_t inv_buf;
12504734Sab196087 
12511618Srie 		/*
12521618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12531618Srie 		 * section - then silently ignore that the relocation
12541618Srie 		 * can not be resolved.
12551618Srie 		 */
12561618Srie 		if (rsp->rel_osdesc &&
12571618Srie 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12581618Srie 			return (0);
12591618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12604734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12611618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
12621618Srie 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12631618Srie 		return (1);
12641618Srie 	}
12651618Srie 
12661618Srie 	/*
12671618Srie 	 * Perform relocation.
12681618Srie 	 */
12691618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12701618Srie }
12711618Srie 
12726206Sab196087 static uintptr_t
12731618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12741618Srie {
12751618Srie 	Word		rtype = rsp->rel_rtype;
12761618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12776299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12781618Srie 	Gotndx		*gnp;
12791618Srie 
12801618Srie 	/*
12812145Srie 	 * If we're building an executable - use either the IE or LE access
12822145Srie 	 * model.  If we're building a shared object process any IE model.
12831618Srie 	 */
12842145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12851618Srie 		/*
12862145Srie 		 * Set the DF_STATIC_TLS flag.
12871618Srie 		 */
12881618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12891618Srie 
12902145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12911618Srie 			/*
12922145Srie 			 * Assign a GOT entry for static TLS references.
12931618Srie 			 */
12942145Srie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12952145Srie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
12962145Srie 
12972145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
12982145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
12992145Srie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
13002145Srie 					return (S_ERROR);
13011618Srie 			}
13021618Srie 
13032145Srie 			/*
13042145Srie 			 * IE access model.
13052145Srie 			 */
13062145Srie 			if (IS_TLS_IE(rtype)) {
13072145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13082145Srie 				    rsp, ofl) == S_ERROR)
13092145Srie 					return (S_ERROR);
13102145Srie 
13112145Srie 				/*
13122145Srie 				 * A non-pic shared object needs to adjust the
13132145Srie 				 * active relocation (indntpoff).
13142145Srie 				 */
13152145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13162145Srie 				    (rtype == R_386_TLS_IE)) {
13172145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13182145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13192145Srie 				}
13202145Srie 				return (1);
13212145Srie 			}
13221618Srie 
13231618Srie 			/*
13242145Srie 			 * Fixups are required for other executable models.
13251618Srie 			 */
13261618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13271618Srie 			    rsp, ofl));
13281618Srie 		}
13292145Srie 
13301618Srie 		/*
13312145Srie 		 * LE access model.
13321618Srie 		 */
13331618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13341618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13351618Srie 
13361618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13371618Srie 		    rsp, ofl));
13381618Srie 	}
13391618Srie 
13401618Srie 	/*
13412145Srie 	 * Building a shared object.
13422145Srie 	 *
13432145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13441618Srie 	 */
13452145Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13462145Srie 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
13472145Srie 
13482145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
13492145Srie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13502145Srie 			return (S_ERROR);
13512145Srie 
13522145Srie 	} else if (IS_TLS_GD(rtype) &&
13532145Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13542145Srie 	    ofl, 0)) == 0)) {
13552145Srie 
13562145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
13572145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
13582145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13592145Srie 			return (S_ERROR);
13601618Srie 	}
13611618Srie 
13621618Srie 	/*
13631618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
13642145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
13652145Srie 	 * symbol now, and prepare for the PLT magic.
13661618Srie 	 */
13671618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
13683862Srie 		Sym_desc	*tlsgetsym;
13691618Srie 
13701618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
13713862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13721618Srie 			return (S_ERROR);
13732145Srie 
13741618Srie 		rsp->rel_sym = tlsgetsym;
13751618Srie 		rsp->rel_sname = tlsgetsym->sd_name;
13761618Srie 		rsp->rel_rtype = R_386_PLT32;
13772145Srie 
13781618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13791618Srie 			return (S_ERROR);
13802145Srie 
13811618Srie 		rsp->rel_sym = sdp;
13821618Srie 		rsp->rel_sname = sdp->sd_name;
13831618Srie 		rsp->rel_rtype = rtype;
13841618Srie 		return (1);
13851618Srie 	}
13861618Srie 
13871618Srie 	if (IS_TLS_LD(rtype))
13881618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13891618Srie 
13901618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13911618Srie }
13921618Srie 
13931618Srie /* ARGSUSED3 */
13946206Sab196087 static Gotndx *
13951618Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13961618Srie {
13971618Srie 	Listnode *	lnp;
13981618Srie 	Gotndx *	gnp;
13991618Srie 
14001618Srie 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14011618Srie 		return (ofl->ofl_tlsldgotndx);
14021618Srie 
14031618Srie 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14041618Srie 		if (gnp->gn_gotref == gref)
14051618Srie 			return (gnp);
14061618Srie 	}
14071618Srie 	return ((Gotndx *)0);
14081618Srie }
14091618Srie 
14106206Sab196087 static Xword
14111618Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14121618Srie {
14131618Srie 	Os_desc		*osp = ofl->ofl_osgot;
14141618Srie 	Sym_desc	*sdp = rdesc->rel_sym;
14151618Srie 	Xword		gotndx;
14161618Srie 	Gotref		gref;
14171618Srie 	Gotndx		*gnp;
14181618Srie 
14191618Srie 	if (rdesc->rel_flags & FLG_REL_DTLS)
14201618Srie 		gref = GOT_REF_TLSGD;
14211618Srie 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14221618Srie 		gref = GOT_REF_TLSLD;
14231618Srie 	else if (rdesc->rel_flags & FLG_REL_STLS)
14241618Srie 		gref = GOT_REF_TLSIE;
14251618Srie 	else
14261618Srie 		gref = GOT_REF_GENERIC;
14271618Srie 
14281618Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14291618Srie 	assert(gnp);
14301618Srie 
14311618Srie 	gotndx = (Xword)gnp->gn_gotndx;
14321618Srie 
14331618Srie 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14341618Srie 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14351618Srie 		gotndx++;
14361618Srie 
14371618Srie 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14381618Srie }
14391618Srie 
14401618Srie 
14411618Srie /* ARGSUSED4 */
14426206Sab196087 static uintptr_t
14432145Srie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14441618Srie     Rel_desc * rsp, Sym_desc * sdp)
14451618Srie {
14461618Srie 	Gotndx	*gnp;
14471618Srie 	uint_t	gotents;
14481618Srie 
14491618Srie 	if (pgnp)
14501618Srie 		return (1);
14511618Srie 
14521618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14531618Srie 		gotents = 2;
14541618Srie 	else
14551618Srie 		gotents = 1;
14561618Srie 
14571618Srie 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14581618Srie 		return (S_ERROR);
14591618Srie 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14601618Srie 	gnp->gn_gotref = gref;
14611618Srie 
14621618Srie 	ofl->ofl_gotcnt += gotents;
14631618Srie 
14641618Srie 	if (gref == GOT_REF_TLSLD) {
14651618Srie 		ofl->ofl_tlsldgotndx = gnp;
14661618Srie 		return (1);
14671618Srie 	}
14681618Srie 
14691618Srie 	if (list_appendc(lst, (void *)gnp) == 0)
14701618Srie 		return (S_ERROR);
14711618Srie 
14721618Srie 	return (1);
14731618Srie }
14741618Srie 
14756206Sab196087 static void
14761618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14771618Srie {
14781618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14791618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14801618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14811618Srie }
14821618Srie 
14831618Srie /*
14841618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14851618Srie  */
14866206Sab196087 static uintptr_t
14872145Srie ld_fillin_gotplt(Ofl_desc *ofl)
14881618Srie {
14896299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
14906299Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
14912145Srie 
14921618Srie 	if (ofl->ofl_osgot) {
14932145Srie 		Sym_desc	*sdp;
14941618Srie 
14951618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14961618Srie 		    SYM_NOHASH, 0, ofl)) != NULL) {
14972145Srie 			uchar_t	*genptr;
14982145Srie 
14992145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15001618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15011618Srie 			/* LINTED */
15021618Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
15036206Sab196087 			if (bswap)
15046206Sab196087 				/* LINTED */
15056206Sab196087 				*(Word *)genptr =
15066206Sab196087 				    /* LINTED */
15076206Sab196087 				    ld_bswap_Word(*(Word *)genptr);
15081618Srie 		}
15091618Srie 	}
15101618Srie 
15111618Srie 	/*
15121618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15131618Srie 	 * entry is:
15141618Srie 	 *  if (building a.out) {
15151618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
15161618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
15171618Srie 	 *  } else {
15181618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15191618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15201618Srie 	 *  }
15211618Srie 	 */
15222145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15231618Srie 		uchar_t *pltent;
15241618Srie 
15251618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15262145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
15271618Srie 			pltent[0] = M_SPECIAL_INST;
15281618Srie 			pltent[1] = M_PUSHL_DISP;
15291618Srie 			pltent += 2;
15301618Srie 			/* LINTED */
15311618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15324734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
15336206Sab196087 			if (bswap)
15346206Sab196087 				/* LINTED */
15356206Sab196087 				*(Word *)pltent =
15366206Sab196087 				    /* LINTED */
15376206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15381618Srie 			pltent += 4;
15391618Srie 			pltent[0] = M_SPECIAL_INST;
15401618Srie 			pltent[1] = M_JMP_DISP_IND;
15411618Srie 			pltent += 2;
15421618Srie 			/* LINTED */
15431618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15444734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15456206Sab196087 			if (bswap)
15466206Sab196087 				/* LINTED */
15476206Sab196087 				*(Word *)pltent =
15486206Sab196087 				    /* LINTED */
15496206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15501618Srie 		} else {
15511618Srie 			pltent[0] = M_SPECIAL_INST;
15521618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15531618Srie 			pltent += 2;
15541618Srie 			/* LINTED */
15551618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15564734Sab196087 			    M_GOT_ENTSIZE);
15576206Sab196087 			if (bswap)
15586206Sab196087 				/* LINTED */
15596206Sab196087 				*(Word *)pltent =
15606206Sab196087 				    /* LINTED */
15616206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15621618Srie 			pltent += 4;
15631618Srie 			pltent[0] = M_SPECIAL_INST;
15641618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15651618Srie 			pltent += 2;
15661618Srie 			/* LINTED */
15671618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15684734Sab196087 			    M_GOT_ENTSIZE);
15696206Sab196087 			if (bswap)
15706206Sab196087 				/* LINTED */
15716206Sab196087 				*(Word *)pltent =
15726206Sab196087 				    /* LINTED */
15736206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15741618Srie 		}
15751618Srie 	}
15761618Srie 	return (1);
15771618Srie }
15786206Sab196087 
15796206Sab196087 
15806206Sab196087 
15816206Sab196087 /*
15826206Sab196087  * Template for generating "void (*)(void)" function
15836206Sab196087  */
15846206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
15856206Sab196087 /* 0x00 */	0xc3				/* ret */
15866206Sab196087 };
15876206Sab196087 
15886206Sab196087 
15896206Sab196087 
15906206Sab196087 /*
15916206Sab196087  * Return the ld_targ definition for this target.
15926206Sab196087  */
15936206Sab196087 const Target *
15946206Sab196087 ld_targ_init_x86(void)
15956206Sab196087 {
15966206Sab196087 	static const Target _ld_targ = {
15976206Sab196087 		{			/* Target_mach */
15986206Sab196087 			M_MACH,			/* m_mach */
15996206Sab196087 			M_MACHPLUS,		/* m_machplus */
16006206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
16016206Sab196087 			M_CLASS,		/* m_class */
16026206Sab196087 			M_DATA,			/* m_data */
16036206Sab196087 
16046206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
16056206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
16066206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
16076206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
16086206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
16096206Sab196087 
16106206Sab196087 			/* Relocation type codes */
16116206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
16126206Sab196087 			M_R_COPY,		/* m_r_copy */
16136206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
16146206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
16156206Sab196087 			M_R_NUM,		/* m_r_num */
16166206Sab196087 			M_R_NONE,		/* m_r_none */
16176206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
16186206Sab196087 			M_R_REGISTER,		/* m_r_register */
16196206Sab196087 
16206206Sab196087 			/* Relocation related constants */
16216206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
16226206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
16236206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
16246206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
16256206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
16266206Sab196087 
16276206Sab196087 			/* GOT related constants */
16286206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
16296206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
16306206Sab196087 
16316206Sab196087 			/* PLT related constants */
16326206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
16336206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
16346206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
16356206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
16366206Sab196087 
16376206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
16386206Sab196087 		},
16396206Sab196087 		{			/* Target_machid */
16406206Sab196087 			M_ID_ARRAY,		/* id_array */
16416206Sab196087 			M_ID_BSS,		/* id_bss */
16426206Sab196087 			M_ID_CAP,		/* id_cap */
16436206Sab196087 			M_ID_DATA,		/* id_data */
16446206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
16456206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
16466206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
16476206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
16486206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
16496206Sab196087 			M_ID_GOT,		/* id_got */
16506206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
16516206Sab196087 			M_ID_HASH,		/* id_hash */
16526206Sab196087 			M_ID_INTERP,		/* id_interp */
16536206Sab196087 			M_ID_LBSS,		/* id_lbss */
16546206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
16556206Sab196087 			M_ID_NOTE,		/* id_note */
16566206Sab196087 			M_ID_NULL,		/* id_null */
16576206Sab196087 			M_ID_PLT,		/* id_plt */
16586206Sab196087 			M_ID_REL,		/* id_rel */
16596206Sab196087 			M_ID_STRTAB,		/* id_strtab */
16606206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
16616206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
16626206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
16636206Sab196087 			M_ID_TEXT,		/* id_text */
16646206Sab196087 			M_ID_TLS,		/* id_tls */
16656206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
16666206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
16676206Sab196087 			M_ID_UNWIND,		/* id_unwind */
16686206Sab196087 			M_ID_USER,		/* id_user */
16696206Sab196087 			M_ID_VERSION,		/* id_version */
16706206Sab196087 		},
16716206Sab196087 		{			/* Target_nullfunc */
16726206Sab196087 			nullfunc_tmpl,		/* nf_template */
16736206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
16746206Sab196087 		},
16756206Sab196087 		{			/* Target_machrel */
16766206Sab196087 			reloc_table,
16776206Sab196087 
16786206Sab196087 			ld_init_rel,		/* mr_init_rel */
16796206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
16806206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
16816206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
16826206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
16836206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
16846206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
16856206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
16866206Sab196087 			NULL,			/* mr_reloc_register */
16876206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
16886206Sab196087 			NULL,			/* mr_reloc_GOTOP */
16896206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
16906206Sab196087 			NULL,			/* mr_assign_got */
16916206Sab196087 			ld_find_gotndx,		/* mr_find_gotndx */
16926206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
16936206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
16946206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
16956206Sab196087 			NULL,			/* mr_allocate_got */
16966206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
16976206Sab196087 		},
16986206Sab196087 		{			/* Target_machsym */
16996206Sab196087 			NULL,			/* ms_reg_check */
17006206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
17016206Sab196087 			NULL,			/* ms_is_regsym */
17026206Sab196087 			NULL,			/* ms_reg_find */
17036206Sab196087 			NULL			/* ms_reg_enter */
17046206Sab196087 		},
17056206Sab196087 		{			/* Target_unwind */
17066206Sab196087 			NULL,		/* uw_make_unwindhdr */
17076206Sab196087 			NULL,		/* uw_populate_unwindhdr */
17086206Sab196087 			NULL,		/* uw_append_unwind */
17096206Sab196087 		}
17106206Sab196087 	};
17116206Sab196087 
17126206Sab196087 	return (&_ld_targ);
17136206Sab196087 }
1714