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
2938159SAli.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 			/*
7238324SAli.Bahrami@Sun.COM 			 * We determine 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 				/*
7681618Srie 				 * The value for a symbol pointing to a SECTION
7691618Srie 				 * is based off of that sections position.
7701618Srie 				 */
771*8369SAli.Bahrami@Sun.COM 				if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
772*8369SAli.Bahrami@Sun.COM 					Sym_desc	*sym;
773*8369SAli.Bahrami@Sun.COM 					Xword		radd;
774*8369SAli.Bahrami@Sun.COM 					uchar_t		*raddr = (uchar_t *)
775*8369SAli.Bahrami@Sun.COM 					    arsp->rel_isdesc->is_indata->d_buf +
776*8369SAli.Bahrami@Sun.COM 					    arsp->rel_roffset;
777*8369SAli.Bahrami@Sun.COM 
7781618Srie 					/*
779*8369SAli.Bahrami@Sun.COM 					 * This is a REL platform. Hence, the
780*8369SAli.Bahrami@Sun.COM 					 * second argument of ld_am_I_partial()
781*8369SAli.Bahrami@Sun.COM 					 * is the value stored at the target
782*8369SAli.Bahrami@Sun.COM 					 * address where the relocation is
783*8369SAli.Bahrami@Sun.COM 					 * going to be applied.
7841618Srie 					 */
785*8369SAli.Bahrami@Sun.COM 					if (ld_reloc_targval_get(ofl, arsp,
786*8369SAli.Bahrami@Sun.COM 					    raddr, &radd) == 0)
787*8369SAli.Bahrami@Sun.COM 						return (S_ERROR);
788*8369SAli.Bahrami@Sun.COM 					sym = ld_am_I_partial(arsp, radd);
789*8369SAli.Bahrami@Sun.COM 					if (sym) {
790*8369SAli.Bahrami@Sun.COM 						Sym	*osym = sym->sd_osym;
791*8369SAli.Bahrami@Sun.COM 
792*8369SAli.Bahrami@Sun.COM 						/*
793*8369SAli.Bahrami@Sun.COM 						 * The symbol was moved, so
794*8369SAli.Bahrami@Sun.COM 						 * adjust the value relative
795*8369SAli.Bahrami@Sun.COM 						 * to the new section.
796*8369SAli.Bahrami@Sun.COM 						 */
797*8369SAli.Bahrami@Sun.COM 						value = sym->sd_sym->st_value;
798*8369SAli.Bahrami@Sun.COM 						moved = 1;
799*8369SAli.Bahrami@Sun.COM 
800*8369SAli.Bahrami@Sun.COM 						/*
801*8369SAli.Bahrami@Sun.COM 						 * The original raddend covers
802*8369SAli.Bahrami@Sun.COM 						 * the displacement from the
803*8369SAli.Bahrami@Sun.COM 						 * section start to the desired
804*8369SAli.Bahrami@Sun.COM 						 * address. The value computed
805*8369SAli.Bahrami@Sun.COM 						 * above gets us from the
806*8369SAli.Bahrami@Sun.COM 						 * section start to the start
807*8369SAli.Bahrami@Sun.COM 						 * of the symbol range. Adjust
808*8369SAli.Bahrami@Sun.COM 						 * the old raddend to remove the
809*8369SAli.Bahrami@Sun.COM 						 * offset from section start to
810*8369SAli.Bahrami@Sun.COM 						 * symbol start, leaving the
811*8369SAli.Bahrami@Sun.COM 						 * displacement within the
812*8369SAli.Bahrami@Sun.COM 						 * range of the symbol.
813*8369SAli.Bahrami@Sun.COM 						 */
814*8369SAli.Bahrami@Sun.COM 						if (osym->st_value != 0) {
815*8369SAli.Bahrami@Sun.COM 							radd -= osym->st_value;
816*8369SAli.Bahrami@Sun.COM 							if (ld_reloc_targval_set
817*8369SAli.Bahrami@Sun.COM 							    (ofl, arsp, raddr,
818*8369SAli.Bahrami@Sun.COM 							    radd) == 0)
819*8369SAli.Bahrami@Sun.COM 								return (
820*8369SAli.Bahrami@Sun.COM 								    S_ERROR);
821*8369SAli.Bahrami@Sun.COM 						}
822*8369SAli.Bahrami@Sun.COM 					}
823*8369SAli.Bahrami@Sun.COM 				}
824*8369SAli.Bahrami@Sun.COM 				if (!moved) {
8251618Srie 					value = _elf_getxoff(
8261618Srie 					    sdp->sd_isc->is_indata);
8271618Srie 					if (sdp->sd_isc->is_shdr->sh_flags &
8281618Srie 					    SHF_ALLOC)
8291618Srie 						value += sdp->sd_isc->
8301618Srie 						    is_osdesc->os_shdr->sh_addr;
8311618Srie 				}
8321618Srie 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8331618Srie 					value -= ofl->ofl_tlsphdr->p_vaddr;
8342850Srie 
8352850Srie 			} else if (IS_SIZE(arsp->rel_rtype)) {
8362850Srie 				/*
8372850Srie 				 * Size relocations require the symbols size.
8382850Srie 				 */
8392850Srie 				value = sdp->sd_sym->st_size;
8401618Srie 			} else {
8411618Srie 				/*
8422647Srie 				 * Else the value is the symbols value.
8431618Srie 				 */
8441618Srie 				value = sdp->sd_sym->st_value;
8451618Srie 			}
8461618Srie 
8471618Srie 			/*
8481618Srie 			 * Relocation against the GLOBAL_OFFSET_TABLE.
8491618Srie 			 */
8501618Srie 			if (arsp->rel_flags & FLG_REL_GOT)
8511618Srie 				arsp->rel_osdesc = ofl->ofl_osgot;
8521618Srie 
8531618Srie 			/*
8541618Srie 			 * If loadable and not producing a relocatable object
8551618Srie 			 * add the sections virtual address to the reference
8561618Srie 			 * address.
8571618Srie 			 */
8581618Srie 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
8591618Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8601618Srie 				refaddr += arsp->rel_isdesc->is_osdesc->
8611618Srie 				    os_shdr->sh_addr;
8621618Srie 
8631618Srie 			/*
8641618Srie 			 * If this entry has a PLT assigned to it, it's
8651618Srie 			 * value is actually the address of the PLT (and
8661618Srie 			 * not the address of the function).
8671618Srie 			 */
8681618Srie 			if (IS_PLT(arsp->rel_rtype)) {
8691618Srie 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8701618Srie 					value = ld_calc_plt_addr(sdp, ofl);
8711618Srie 			}
8721618Srie 
8731618Srie 			/*
8741618Srie 			 * Determine whether the value needs further adjustment.
8751618Srie 			 * Filter through the attributes of the relocation to
8761618Srie 			 * determine what adjustment is required.  Note, many
8771618Srie 			 * of the following cases are only applicable when a
8781618Srie 			 * .got is present.  As a .got is not generated when a
8791618Srie 			 * relocatable object is being built, any adjustments
8801618Srie 			 * that require a .got need to be skipped.
8811618Srie 			 */
8821618Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
8831618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8841618Srie 				Xword		R1addr;
8851618Srie 				uintptr_t	R2addr;
8861618Srie 				Word		gotndx;
8871618Srie 				Gotndx		*gnp;
8881618Srie 
8891618Srie 				/*
8901618Srie 				 * Perform relocation against GOT table.  Since
8911618Srie 				 * this doesn't fit exactly into a relocation
8921618Srie 				 * we place the appropriate byte in the GOT
8931618Srie 				 * directly
8941618Srie 				 *
8951618Srie 				 * Calculate offset into GOT at which to apply
8961618Srie 				 * the relocation.
8971618Srie 				 */
8981618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8991618Srie 				    ofl, 0);
9001618Srie 				assert(gnp);
9011618Srie 
9021618Srie 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
9031618Srie 					gotndx = gnp->gn_gotndx + 1;
9041618Srie 				else
9051618Srie 					gotndx = gnp->gn_gotndx;
9061618Srie 
9071618Srie 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9081618Srie 
9091618Srie 				/*
9101618Srie 				 * Add the GOTs data's offset.
9111618Srie 				 */
9121618Srie 				R2addr = R1addr + (uintptr_t)
9131618Srie 				    arsp->rel_osdesc->os_outdata->d_buf;
9141618Srie 
9151618Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
9161618Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
9171618Srie 				    arsp->rel_rtype, R1addr, value,
9181618Srie 				    arsp->rel_sname, arsp->rel_osdesc));
9191618Srie 
9201618Srie 				/*
9211618Srie 				 * And do it.
9221618Srie 				 */
9235189Sab196087 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
9245189Sab196087 					*(Xword *)R2addr =
9256206Sab196087 					    ld_bswap_Xword(value);
9265189Sab196087 				else
9275189Sab196087 					*(Xword *)R2addr = value;
9281618Srie 				continue;
9291618Srie 
9301618Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
9311618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9321618Srie 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
9331618Srie 
9341618Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
9351618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9361618Srie 				value =
9371618Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
9381618Srie 				    refaddr;
9391618Srie 
9401618Srie 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9411618Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9421618Srie 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9431618Srie 				value -= refaddr;
9441618Srie 
9451618Srie 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
9461618Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
9471618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9481618Srie 				Gotndx	*gnp;
9491618Srie 
9501618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
9511618Srie 				    ofl, 0);
9521618Srie 				assert(gnp);
9531618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9541618Srie 				if (arsp->rel_rtype == R_386_TLS_IE) {
9551618Srie 					value +=
9561618Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9571618Srie 				}
9581618Srie 
9591618Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
9601618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9611618Srie 				Gotndx *gnp;
9621618Srie 
9631618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9641618Srie 				    GOT_REF_GENERIC, ofl, 0);
9651618Srie 				assert(gnp);
9661618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9671618Srie 
9681618Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
9691618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9701618Srie 				Xword	tlsstatsize;
9711618Srie 
9721618Srie 				/*
9731618Srie 				 * This is the LE TLS reference model.  Static
9741618Srie 				 * offset is hard-coded.
9751618Srie 				 */
9761618Srie 				tlsstatsize =
9771618Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9781618Srie 				    M_TLSSTATALIGN);
9791618Srie 				value = tlsstatsize - value;
9801618Srie 
9811618Srie 				/*
9821618Srie 				 * Since this code is fixed up, it assumes a
9831618Srie 				 * negative offset that can be added to the
9841618Srie 				 * thread pointer.
9851618Srie 				 */
9861618Srie 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9871618Srie 				    (arsp->rel_rtype == R_386_TLS_LE))
9881618Srie 					value = -value;
9891618Srie 			}
9901618Srie 
9911618Srie 			if (arsp->rel_isdesc->is_file)
9921618Srie 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9931618Srie 			else
9941618Srie 				ifl_name = MSG_INTL(MSG_STR_NULL);
9951618Srie 
9961618Srie 			/*
9971618Srie 			 * Make sure we have data to relocate.  Compiler and
9981618Srie 			 * assembler developers have been known to generate
9991618Srie 			 * relocations against invalid sections (normally .bss),
10001618Srie 			 * so for their benefit give them sufficient information
10011618Srie 			 * to help analyze the problem.  End users should never
10021618Srie 			 * see this.
10031618Srie 			 */
10041618Srie 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
10054734Sab196087 				Conv_inv_buf_t	inv_buf;
10064734Sab196087 
10071618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
10081618Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
10094734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10104734Sab196087 				    0, &inv_buf),
10111618Srie 				    ifl_name, demangle(arsp->rel_sname),
10121618Srie 				    arsp->rel_isdesc->is_name);
10131618Srie 				return (S_ERROR);
10141618Srie 			}
10151618Srie 
10161618Srie 			/*
10171618Srie 			 * Get the address of the data item we need to modify.
10181618Srie 			 */
10191618Srie 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
10201618Srie 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
10211618Srie 			    is_indata));
10221618Srie 
10231618Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
10241618Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
10251618Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
10261618Srie 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
10271618Srie 
10281618Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
10291618Srie 			    ofl->ofl_size) || (arsp->rel_roffset >
10301618Srie 			    arsp->rel_osdesc->os_shdr->sh_size)) {
10314734Sab196087 				Conv_inv_buf_t	inv_buf;
10324734Sab196087 				int		class;
10331618Srie 
10341618Srie 				if (((uintptr_t)addr -
10351618Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10361618Srie 					class = ERR_FATAL;
10371618Srie 				else
10381618Srie 					class = ERR_WARNING;
10391618Srie 
10401618Srie 				eprintf(ofl->ofl_lml, class,
10411618Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
10424734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10434734Sab196087 				    0, &inv_buf),
10441618Srie 				    ifl_name, arsp->rel_isdesc->is_name,
10451618Srie 				    demangle(arsp->rel_sname),
10461618Srie 				    EC_ADDR((uintptr_t)addr -
10471618Srie 				    (uintptr_t)ofl->ofl_nehdr));
10481618Srie 
10491618Srie 				if (class == ERR_FATAL) {
10501618Srie 					return_code = S_ERROR;
10511618Srie 					continue;
10521618Srie 				}
10531618Srie 			}
10541618Srie 
10551618Srie 			/*
10561618Srie 			 * The relocation is additive.  Ignore the previous
10571618Srie 			 * symbol value if this local partial symbol is
10581618Srie 			 * expanded.
10591618Srie 			 */
10601618Srie 			if (moved)
10611618Srie 				value -= *addr;
10621618Srie 
10631618Srie 			/*
10645892Sab196087 			 * If we have a replacement value for the relocation
10655892Sab196087 			 * target, put it in place now.
10665892Sab196087 			 */
10675892Sab196087 			if (arsp->rel_flags & FLG_REL_NADDEND) {
10685892Sab196087 				Xword addend = arsp->rel_raddend;
10695892Sab196087 
10705892Sab196087 				if (ld_reloc_targval_set(ofl, arsp,
10715892Sab196087 				    addr, addend) == 0)
10725892Sab196087 					return (S_ERROR);
10735892Sab196087 			}
10745892Sab196087 
10755892Sab196087 			/*
10765189Sab196087 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10771618Srie 			 * stage.
10781618Srie 			 */
10795189Sab196087 			if (OFL_DO_RELOC(ofl)) {
10805189Sab196087 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10811618Srie 				    &value, arsp->rel_sname, ifl_name,
10825189Sab196087 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10831618Srie 				    ofl->ofl_lml) == 0)
10841618Srie 					return_code = S_ERROR;
10851618Srie 			}
10861618Srie 		}
10871618Srie 	}
10881618Srie 	return (return_code);
10891618Srie }
10901618Srie 
10911618Srie /*
10921618Srie  * Add an output relocation record.
10931618Srie  */
10946206Sab196087 static uintptr_t
10951618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10961618Srie {
10971618Srie 	Rel_desc	*orsp;
10981618Srie 	Rel_cache	*rcp;
10991618Srie 	Sym_desc	*sdp = rsp->rel_sym;
11001618Srie 
11011618Srie 	/*
11021618Srie 	 * Static executables *do not* want any relocations against them.
11031618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11041618Srie 	 * symbol in a static executable, it's best to disable them here
11051618Srie 	 * instead of through out the relocation code.
11061618Srie 	 */
11071618Srie 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
11081618Srie 	    (FLG_OF_STATIC | FLG_OF_EXEC))
11091618Srie 		return (1);
11101618Srie 
11111618Srie 	/*
11121618Srie 	 * If no relocation cache structures are available allocate
11131618Srie 	 * a new one and link it into the cache list.
11141618Srie 	 */
11151618Srie 	if ((ofl->ofl_outrels.tail == 0) ||
11161618Srie 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
11171618Srie 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
11181618Srie 		static size_t	nextsize = 0;
11191618Srie 		size_t		size;
11201618Srie 
11211618Srie 		/*
11221618Srie 		 * Output relocation numbers can vary considerably between
11231618Srie 		 * building executables or shared objects (pic vs. non-pic),
11241618Srie 		 * etc.  But, they typically aren't very large, so for these
11251618Srie 		 * objects use a standard bucket size.  For building relocatable
11261618Srie 		 * objects, typically there will be an output relocation for
11271618Srie 		 * every input relocation.
11281618Srie 		 */
11291618Srie 		if (nextsize == 0) {
11301618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
11311618Srie 				if ((size = ofl->ofl_relocincnt) == 0)
11321618Srie 					size = REL_LOIDESCNO;
11331618Srie 				if (size > REL_HOIDESCNO)
11341618Srie 					nextsize = REL_HOIDESCNO;
11351618Srie 				else
11361618Srie 					nextsize = REL_LOIDESCNO;
11371618Srie 			} else
11381618Srie 				nextsize = size = REL_HOIDESCNO;
11391618Srie 		} else
11401618Srie 			size = nextsize;
11411618Srie 
11421618Srie 		size = size * sizeof (Rel_desc);
11431618Srie 
11441618Srie 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
11451618Srie 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
11461618Srie 			return (S_ERROR);
11471618Srie 
11481618Srie 		/* LINTED */
11491618Srie 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
11501618Srie 		/* LINTED */
11511618Srie 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
11521618Srie 	}
11531618Srie 
11541618Srie 	/*
11551618Srie 	 * If we are adding a output relocation against a section
11561618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11571618Srie 	 * will be added to the .dynsym symbol table.
11581618Srie 	 */
11591618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11601618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11611618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11621618Srie 
11631618Srie 		/*
11641618Srie 		 * If this is a COMMON symbol - no output section
11651618Srie 		 * exists yet - (it's created as part of sym_validate()).
11661618Srie 		 * So - we mark here that when it's created it should
11671618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11681618Srie 		 */
11691618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11701682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11711618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11721618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11731618Srie 			else
11741618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11751618Srie 		} else {
11761618Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11771618Srie 
11782347Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11791618Srie 				ofl->ofl_dynshdrcnt++;
11801618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11811618Srie 			}
11821618Srie 		}
11831618Srie 	}
11841618Srie 
11851618Srie 	*orsp = *rsp;
11861618Srie 	orsp->rel_flags |= flags;
11871618Srie 
11881618Srie 	rcp->rc_free++;
11891618Srie 	ofl->ofl_outrelscnt++;
11901618Srie 
11911618Srie 	if (flags & FLG_REL_GOT)
11921618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11931618Srie 	else if (flags & FLG_REL_PLT)
11941618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11951618Srie 	else if (flags & FLG_REL_BSS)
11961618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11971618Srie 	else if (flags & FLG_REL_NOINFO)
11981618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11991618Srie 	else
12001618Srie 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
12011618Srie 
12021618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
12031618Srie 		ofl->ofl_relocrelcnt++;
12041618Srie 
12051618Srie 	/*
12061618Srie 	 * We don't perform sorting on PLT relocations because
12071618Srie 	 * they have already been assigned a PLT index and if we
12081618Srie 	 * were to sort them we would have to re-assign the plt indexes.
12091618Srie 	 */
12101618Srie 	if (!(flags & FLG_REL_PLT))
12111618Srie 		ofl->ofl_reloccnt++;
12121618Srie 
12131618Srie 	/*
12141618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
12151618Srie 	 */
12161618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12171618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12181618Srie 
12191618Srie 	/*
12201618Srie 	 * Identify and possibly warn of a displacement relocation.
12211618Srie 	 */
12221618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
12231618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12241618Srie 
12251618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12261618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12271618Srie 	}
12281618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
12291618Srie 	    M_MACH, orsp));
12301618Srie 	return (1);
12311618Srie }
12321618Srie 
12331618Srie /*
12341618Srie  * process relocation for a LOCAL symbol
12351618Srie  */
12366206Sab196087 static uintptr_t
12371618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12381618Srie {
12396299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12401618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12411682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12421618Srie 
12431618Srie 	/*
12441618Srie 	 * if ((shared object) and (not pc relative relocation) and
12451618Srie 	 *    (not against ABS symbol))
12461618Srie 	 * then
12471618Srie 	 *	build R_386_RELATIVE
12481618Srie 	 * fi
12491618Srie 	 */
12501618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12512850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12521618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12531618Srie 	    !(rsp->rel_isdesc != NULL &&
12541618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12551618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12561618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12571618Srie 		Word	ortype = rsp->rel_rtype;
12581618Srie 
12591618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12601618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12611618Srie 			return (S_ERROR);
12621618Srie 		rsp->rel_rtype = ortype;
12631618Srie 	}
12641618Srie 
12651618Srie 	/*
12661618Srie 	 * If the relocation is against a 'non-allocatable' section
12671618Srie 	 * and we can not resolve it now - then give a warning
12681618Srie 	 * message.
12691618Srie 	 *
12701618Srie 	 * We can not resolve the symbol if either:
12711618Srie 	 *	a) it's undefined
12721618Srie 	 *	b) it's defined in a shared library and a
12731618Srie 	 *	   COPY relocation hasn't moved it to the executable
12741618Srie 	 *
12751618Srie 	 * Note: because we process all of the relocations against the
12761618Srie 	 *	text segment before any others - we know whether
12771618Srie 	 *	or not a copy relocation will be generated before
12781618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12791618Srie 	 */
12801618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12811618Srie 	    ((shndx == SHN_UNDEF) ||
12821618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12831618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
12844734Sab196087 		Conv_inv_buf_t inv_buf;
12854734Sab196087 
12861618Srie 		/*
12871618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12881618Srie 		 * section - then silently ignore that the relocation
12891618Srie 		 * can not be resolved.
12901618Srie 		 */
12911618Srie 		if (rsp->rel_osdesc &&
12921618Srie 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12931618Srie 			return (0);
12941618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12954734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12961618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
12971618Srie 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12981618Srie 		return (1);
12991618Srie 	}
13001618Srie 
13011618Srie 	/*
13021618Srie 	 * Perform relocation.
13031618Srie 	 */
13041618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
13051618Srie }
13061618Srie 
13076206Sab196087 static uintptr_t
13081618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13091618Srie {
13101618Srie 	Word		rtype = rsp->rel_rtype;
13111618Srie 	Sym_desc	*sdp = rsp->rel_sym;
13126299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
13131618Srie 	Gotndx		*gnp;
13141618Srie 
13151618Srie 	/*
13162145Srie 	 * If we're building an executable - use either the IE or LE access
13172145Srie 	 * model.  If we're building a shared object process any IE model.
13181618Srie 	 */
13192145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13201618Srie 		/*
13212145Srie 		 * Set the DF_STATIC_TLS flag.
13221618Srie 		 */
13231618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13241618Srie 
13252145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13261618Srie 			/*
13272145Srie 			 * Assign a GOT entry for static TLS references.
13281618Srie 			 */
13292145Srie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13302145Srie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
13312145Srie 
13322145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
13332145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
13342145Srie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
13352145Srie 					return (S_ERROR);
13361618Srie 			}
13371618Srie 
13382145Srie 			/*
13392145Srie 			 * IE access model.
13402145Srie 			 */
13412145Srie 			if (IS_TLS_IE(rtype)) {
13422145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13432145Srie 				    rsp, ofl) == S_ERROR)
13442145Srie 					return (S_ERROR);
13452145Srie 
13462145Srie 				/*
13472145Srie 				 * A non-pic shared object needs to adjust the
13482145Srie 				 * active relocation (indntpoff).
13492145Srie 				 */
13502145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13512145Srie 				    (rtype == R_386_TLS_IE)) {
13522145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13532145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13542145Srie 				}
13552145Srie 				return (1);
13562145Srie 			}
13571618Srie 
13581618Srie 			/*
13592145Srie 			 * Fixups are required for other executable models.
13601618Srie 			 */
13611618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13621618Srie 			    rsp, ofl));
13631618Srie 		}
13642145Srie 
13651618Srie 		/*
13662145Srie 		 * LE access model.
13671618Srie 		 */
13681618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13691618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13701618Srie 
13711618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13721618Srie 		    rsp, ofl));
13731618Srie 	}
13741618Srie 
13751618Srie 	/*
13762145Srie 	 * Building a shared object.
13772145Srie 	 *
13782145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13791618Srie 	 */
13802145Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13812145Srie 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
13822145Srie 
13832145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
13842145Srie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13852145Srie 			return (S_ERROR);
13862145Srie 
13872145Srie 	} else if (IS_TLS_GD(rtype) &&
13882145Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13892145Srie 	    ofl, 0)) == 0)) {
13902145Srie 
13912145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
13922145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
13932145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13942145Srie 			return (S_ERROR);
13951618Srie 	}
13961618Srie 
13971618Srie 	/*
13981618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
13992145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
14002145Srie 	 * symbol now, and prepare for the PLT magic.
14011618Srie 	 */
14021618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
14033862Srie 		Sym_desc	*tlsgetsym;
14041618Srie 
14051618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
14063862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
14071618Srie 			return (S_ERROR);
14082145Srie 
14091618Srie 		rsp->rel_sym = tlsgetsym;
14101618Srie 		rsp->rel_sname = tlsgetsym->sd_name;
14111618Srie 		rsp->rel_rtype = R_386_PLT32;
14122145Srie 
14131618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
14141618Srie 			return (S_ERROR);
14152145Srie 
14161618Srie 		rsp->rel_sym = sdp;
14171618Srie 		rsp->rel_sname = sdp->sd_name;
14181618Srie 		rsp->rel_rtype = rtype;
14191618Srie 		return (1);
14201618Srie 	}
14211618Srie 
14221618Srie 	if (IS_TLS_LD(rtype))
14231618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14241618Srie 
14251618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14261618Srie }
14271618Srie 
14281618Srie /* ARGSUSED3 */
14296206Sab196087 static Gotndx *
14301618Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
14311618Srie {
14321618Srie 	Listnode *	lnp;
14331618Srie 	Gotndx *	gnp;
14341618Srie 
14351618Srie 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14361618Srie 		return (ofl->ofl_tlsldgotndx);
14371618Srie 
14381618Srie 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14391618Srie 		if (gnp->gn_gotref == gref)
14401618Srie 			return (gnp);
14411618Srie 	}
14421618Srie 	return ((Gotndx *)0);
14431618Srie }
14441618Srie 
14456206Sab196087 static Xword
14461618Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14471618Srie {
14481618Srie 	Os_desc		*osp = ofl->ofl_osgot;
14491618Srie 	Sym_desc	*sdp = rdesc->rel_sym;
14501618Srie 	Xword		gotndx;
14511618Srie 	Gotref		gref;
14521618Srie 	Gotndx		*gnp;
14531618Srie 
14541618Srie 	if (rdesc->rel_flags & FLG_REL_DTLS)
14551618Srie 		gref = GOT_REF_TLSGD;
14561618Srie 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14571618Srie 		gref = GOT_REF_TLSLD;
14581618Srie 	else if (rdesc->rel_flags & FLG_REL_STLS)
14591618Srie 		gref = GOT_REF_TLSIE;
14601618Srie 	else
14611618Srie 		gref = GOT_REF_GENERIC;
14621618Srie 
14631618Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14641618Srie 	assert(gnp);
14651618Srie 
14661618Srie 	gotndx = (Xword)gnp->gn_gotndx;
14671618Srie 
14681618Srie 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14691618Srie 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14701618Srie 		gotndx++;
14711618Srie 
14721618Srie 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14731618Srie }
14741618Srie 
14751618Srie 
14761618Srie /* ARGSUSED4 */
14776206Sab196087 static uintptr_t
14782145Srie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14791618Srie     Rel_desc * rsp, Sym_desc * sdp)
14801618Srie {
14811618Srie 	Gotndx	*gnp;
14821618Srie 	uint_t	gotents;
14831618Srie 
14841618Srie 	if (pgnp)
14851618Srie 		return (1);
14861618Srie 
14871618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14881618Srie 		gotents = 2;
14891618Srie 	else
14901618Srie 		gotents = 1;
14911618Srie 
14921618Srie 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14931618Srie 		return (S_ERROR);
14941618Srie 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14951618Srie 	gnp->gn_gotref = gref;
14961618Srie 
14971618Srie 	ofl->ofl_gotcnt += gotents;
14981618Srie 
14991618Srie 	if (gref == GOT_REF_TLSLD) {
15001618Srie 		ofl->ofl_tlsldgotndx = gnp;
15011618Srie 		return (1);
15021618Srie 	}
15031618Srie 
15041618Srie 	if (list_appendc(lst, (void *)gnp) == 0)
15051618Srie 		return (S_ERROR);
15061618Srie 
15071618Srie 	return (1);
15081618Srie }
15091618Srie 
15106206Sab196087 static void
15111618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
15121618Srie {
15131618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
15141618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
15151618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
15161618Srie }
15171618Srie 
15181618Srie /*
15191618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
15201618Srie  */
15216206Sab196087 static uintptr_t
15222145Srie ld_fillin_gotplt(Ofl_desc *ofl)
15231618Srie {
15246299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
15256299Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
15262145Srie 
15271618Srie 	if (ofl->ofl_osgot) {
15282145Srie 		Sym_desc	*sdp;
15291618Srie 
15301618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
15311618Srie 		    SYM_NOHASH, 0, ofl)) != NULL) {
15322145Srie 			uchar_t	*genptr;
15332145Srie 
15342145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15351618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15361618Srie 			/* LINTED */
15371618Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
15386206Sab196087 			if (bswap)
15396206Sab196087 				/* LINTED */
15406206Sab196087 				*(Word *)genptr =
15416206Sab196087 				    /* LINTED */
15426206Sab196087 				    ld_bswap_Word(*(Word *)genptr);
15431618Srie 		}
15441618Srie 	}
15451618Srie 
15461618Srie 	/*
15471618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15481618Srie 	 * entry is:
15491618Srie 	 *  if (building a.out) {
15501618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
15511618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
15521618Srie 	 *  } else {
15531618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15541618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15551618Srie 	 *  }
15561618Srie 	 */
15572145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15581618Srie 		uchar_t *pltent;
15591618Srie 
15601618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15612145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
15621618Srie 			pltent[0] = M_SPECIAL_INST;
15631618Srie 			pltent[1] = M_PUSHL_DISP;
15641618Srie 			pltent += 2;
15651618Srie 			/* LINTED */
15661618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15674734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
15686206Sab196087 			if (bswap)
15696206Sab196087 				/* LINTED */
15706206Sab196087 				*(Word *)pltent =
15716206Sab196087 				    /* LINTED */
15726206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15731618Srie 			pltent += 4;
15741618Srie 			pltent[0] = M_SPECIAL_INST;
15751618Srie 			pltent[1] = M_JMP_DISP_IND;
15761618Srie 			pltent += 2;
15771618Srie 			/* LINTED */
15781618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15794734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15806206Sab196087 			if (bswap)
15816206Sab196087 				/* LINTED */
15826206Sab196087 				*(Word *)pltent =
15836206Sab196087 				    /* LINTED */
15846206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15851618Srie 		} else {
15861618Srie 			pltent[0] = M_SPECIAL_INST;
15871618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15881618Srie 			pltent += 2;
15891618Srie 			/* LINTED */
15901618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15914734Sab196087 			    M_GOT_ENTSIZE);
15926206Sab196087 			if (bswap)
15936206Sab196087 				/* LINTED */
15946206Sab196087 				*(Word *)pltent =
15956206Sab196087 				    /* LINTED */
15966206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15971618Srie 			pltent += 4;
15981618Srie 			pltent[0] = M_SPECIAL_INST;
15991618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
16001618Srie 			pltent += 2;
16011618Srie 			/* LINTED */
16021618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
16034734Sab196087 			    M_GOT_ENTSIZE);
16046206Sab196087 			if (bswap)
16056206Sab196087 				/* LINTED */
16066206Sab196087 				*(Word *)pltent =
16076206Sab196087 				    /* LINTED */
16086206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
16091618Srie 		}
16101618Srie 	}
16111618Srie 	return (1);
16121618Srie }
16136206Sab196087 
16146206Sab196087 
16156206Sab196087 
16166206Sab196087 /*
16176206Sab196087  * Template for generating "void (*)(void)" function
16186206Sab196087  */
16196206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
16206206Sab196087 /* 0x00 */	0xc3				/* ret */
16216206Sab196087 };
16226206Sab196087 
16236206Sab196087 
16246206Sab196087 
16256206Sab196087 /*
16266206Sab196087  * Return the ld_targ definition for this target.
16276206Sab196087  */
16286206Sab196087 const Target *
16296206Sab196087 ld_targ_init_x86(void)
16306206Sab196087 {
16316206Sab196087 	static const Target _ld_targ = {
16326206Sab196087 		{			/* Target_mach */
16336206Sab196087 			M_MACH,			/* m_mach */
16346206Sab196087 			M_MACHPLUS,		/* m_machplus */
16356206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
16366206Sab196087 			M_CLASS,		/* m_class */
16376206Sab196087 			M_DATA,			/* m_data */
16386206Sab196087 
16396206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
16406206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
16416206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
16426206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
16436206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
16446206Sab196087 
16456206Sab196087 			/* Relocation type codes */
16466206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
16476206Sab196087 			M_R_COPY,		/* m_r_copy */
16486206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
16496206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
16506206Sab196087 			M_R_NUM,		/* m_r_num */
16516206Sab196087 			M_R_NONE,		/* m_r_none */
16526206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
16536206Sab196087 			M_R_REGISTER,		/* m_r_register */
16546206Sab196087 
16556206Sab196087 			/* Relocation related constants */
16566206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
16576206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
16586206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
16596206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
16606206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
16616206Sab196087 
16626206Sab196087 			/* GOT related constants */
16636206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
16646206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
16656206Sab196087 
16666206Sab196087 			/* PLT related constants */
16676206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
16686206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
16696206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
16706206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
16716206Sab196087 
16726206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
16736206Sab196087 		},
16746206Sab196087 		{			/* Target_machid */
16756206Sab196087 			M_ID_ARRAY,		/* id_array */
16766206Sab196087 			M_ID_BSS,		/* id_bss */
16776206Sab196087 			M_ID_CAP,		/* id_cap */
16786206Sab196087 			M_ID_DATA,		/* id_data */
16796206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
16806206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
16816206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
16826206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
16836206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
16846206Sab196087 			M_ID_GOT,		/* id_got */
16856206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
16866206Sab196087 			M_ID_HASH,		/* id_hash */
16876206Sab196087 			M_ID_INTERP,		/* id_interp */
16886206Sab196087 			M_ID_LBSS,		/* id_lbss */
16896206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
16906206Sab196087 			M_ID_NOTE,		/* id_note */
16916206Sab196087 			M_ID_NULL,		/* id_null */
16926206Sab196087 			M_ID_PLT,		/* id_plt */
16936206Sab196087 			M_ID_REL,		/* id_rel */
16946206Sab196087 			M_ID_STRTAB,		/* id_strtab */
16956206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
16966206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
16976206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
16986206Sab196087 			M_ID_TEXT,		/* id_text */
16996206Sab196087 			M_ID_TLS,		/* id_tls */
17006206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
17016206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
17026206Sab196087 			M_ID_UNWIND,		/* id_unwind */
17036206Sab196087 			M_ID_USER,		/* id_user */
17046206Sab196087 			M_ID_VERSION,		/* id_version */
17056206Sab196087 		},
17066206Sab196087 		{			/* Target_nullfunc */
17076206Sab196087 			nullfunc_tmpl,		/* nf_template */
17086206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
17096206Sab196087 		},
17106206Sab196087 		{			/* Target_machrel */
17116206Sab196087 			reloc_table,
17126206Sab196087 
17136206Sab196087 			ld_init_rel,		/* mr_init_rel */
17146206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
17156206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
17166206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
17176206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
17186206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
17196206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
17206206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
17216206Sab196087 			NULL,			/* mr_reloc_register */
17226206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
17236206Sab196087 			NULL,			/* mr_reloc_GOTOP */
17246206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
17256206Sab196087 			NULL,			/* mr_assign_got */
17266206Sab196087 			ld_find_gotndx,		/* mr_find_gotndx */
17276206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
17286206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
17296206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
17306206Sab196087 			NULL,			/* mr_allocate_got */
17316206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
17326206Sab196087 		},
17336206Sab196087 		{			/* Target_machsym */
17346206Sab196087 			NULL,			/* ms_reg_check */
17356206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
17366206Sab196087 			NULL,			/* ms_is_regsym */
17376206Sab196087 			NULL,			/* ms_reg_find */
17386206Sab196087 			NULL			/* ms_reg_enter */
17396206Sab196087 		},
17406206Sab196087 		{			/* Target_unwind */
17416206Sab196087 			NULL,		/* uw_make_unwindhdr */
17426206Sab196087 			NULL,		/* uw_populate_unwindhdr */
17436206Sab196087 			NULL,		/* uw_append_unwind */
17446206Sab196087 		}
17456206Sab196087 	};
17466206Sab196087 
17476206Sab196087 	return (&_ld_targ);
17486206Sab196087 }
1749