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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
311618Srie 
32*6206Sab196087 /* Get the x86 version of the relocation engine */
33*6206Sab196087 #define	DO_RELOC_LIBLD_X86
34*6206Sab196087 
351618Srie #include	<string.h>
361618Srie #include	<stdio.h>
371618Srie #include	<sys/elf_386.h>
381618Srie #include	<debug.h>
391618Srie #include	<reloc.h>
40*6206Sab196087 #include	<i386/machdep_x86.h>
411618Srie #include	"msg.h"
421618Srie #include	"_libld.h"
431618Srie 
44*6206Sab196087 /* Forward declarations */
45*6206Sab196087 static Gotndx	*ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *);
46*6206Sab196087 static Xword	ld_calc_got_offset(Rel_desc *, Ofl_desc *);
47*6206Sab196087 
48*6206Sab196087 
49*6206Sab196087 static Word
501618Srie ld_init_rel(Rel_desc *reld, void *reloc)
511618Srie {
521618Srie 	Rel *	rel = (Rel *)reloc;
531618Srie 
541618Srie 	/* LINTED */
55*6206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
561618Srie 	reld->rel_roffset = rel->r_offset;
571618Srie 	reld->rel_raddend = 0;
581618Srie 	reld->rel_typedata = 0;
591618Srie 
601618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
611618Srie }
621618Srie 
63*6206Sab196087 static void
641618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
651618Srie {
661618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
671618Srie }
681618Srie 
69*6206Sab196087 static void
701618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
711618Srie {
721618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
731618Srie 		/*
741618Srie 		 * Create this entry if we are going to create a PLT table.
751618Srie 		 */
761618Srie 		if (ofl->ofl_pltcnt)
771618Srie 			(*cnt)++;		/* DT_PLTGOT */
781618Srie 	}
791618Srie }
801618Srie 
81*6206Sab196087 static void
822145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
831618Srie {
841618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
851618Srie 		(*dyn)->d_tag = DT_PLTGOT;
861618Srie 		if (ofl->ofl_osgot)
871618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
881618Srie 		else
891618Srie 			(*dyn)->d_un.d_ptr = 0;
901618Srie 		(*dyn)++;
911618Srie 	}
921618Srie }
931618Srie 
94*6206Sab196087 static Xword
951618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
961618Srie {
971618Srie 	Xword	value;
981618Srie 
991618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1001618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1011618Srie 	return (value);
1021618Srie }
1031618Srie 
1041618Srie /*
1051618Srie  *  Build a single plt entry - code is:
1061618Srie  *	if (building a.out)
1071618Srie  *		JMP	*got_off
1081618Srie  *	else
1091618Srie  *		JMP	*got_off@GOT(%ebx)
1101618Srie  *	PUSHL	&rel_off
1111618Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1121618Srie  *
1131618Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1141618Srie  *	so the first pass through the plt jumps back here, jumping
1151618Srie  *	in turn to the first plt entry, which jumps to the dynamic
1161618Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1171618Srie  *	future plt calls to the proper destination.
1181618Srie  */
1191618Srie static void
1201618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1211618Srie {
1221618Srie 	uchar_t		*pltent, *gotent;
1231618Srie 	Sword		plt_off;
1241618Srie 	Word		got_off;
125*6206Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1261618Srie 
1271618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1281618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1291618Srie 	    M_PLT_ENTSIZE);
1301618Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1311618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1321618Srie 
1331618Srie 	/*
1341618Srie 	 * Fill in the got entry with the address of the next instruction.
1351618Srie 	 */
1361618Srie 	/* LINTED */
1371618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1381618Srie 	    M_PLT_INSSIZE;
139*6206Sab196087 	if (bswap)
140*6206Sab196087 		/* LINTED */
141*6206Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1421618Srie 
1431618Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1441618Srie 		pltent[0] = M_SPECIAL_INST;
1451618Srie 		pltent[1] = M_JMP_DISP_IND;
1461618Srie 		pltent += 2;
1471618Srie 		/* LINTED */
1481618Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1494734Sab196087 		    got_off);
1501618Srie 	} else {
1511618Srie 		pltent[0] = M_SPECIAL_INST;
1521618Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1531618Srie 		pltent += 2;
1541618Srie 		/* LINTED */
1551618Srie 		*(Word *)pltent = (Word)got_off;
1561618Srie 	}
157*6206Sab196087 	if (bswap)
158*6206Sab196087 		/* LINTED */
159*6206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1601618Srie 	pltent += 4;
1611618Srie 
1621618Srie 	pltent[0] = M_INST_PUSHL;
1631618Srie 	pltent++;
1641618Srie 	/* LINTED */
1651618Srie 	*(Word *)pltent = (Word)rel_off;
166*6206Sab196087 	if (bswap)
167*6206Sab196087 		/* LINTED */
168*6206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1691618Srie 	pltent += 4;
1701618Srie 
1711618Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
1721618Srie 	pltent[0] = M_INST_JMP;
1731618Srie 	pltent++;
1741618Srie 	/* LINTED */
1751618Srie 	*(Word *)pltent = (Word)plt_off;
176*6206Sab196087 	if (bswap)
177*6206Sab196087 		/* LINTED */
178*6206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
1791618Srie }
1801618Srie 
181*6206Sab196087 static uintptr_t
1821618Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
1831618Srie {
1841618Srie 	Os_desc *	relosp, * osp = 0;
1851618Srie 	Word		ndx, roffset, value;
1861618Srie 	Rel		rea;
1871618Srie 	char		*relbits;
1881618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
1891618Srie 	int		sectmoved = 0;
1901618Srie 
1911618Srie 	sdp = orsp->rel_sym;
1921618Srie 
1931618Srie 	/*
1941618Srie 	 * If the section this relocation is against has been discarded
1951618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
1961618Srie 	 */
1971618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
1981618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
1991618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2001618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2011618Srie 		return (1);
2021618Srie 	}
2031618Srie 
2041618Srie 	/*
2051618Srie 	 * If this is a relocation against a move table, or expanded move
2061618Srie 	 * table, adjust the relocation entries.
2071618Srie 	 */
2081618Srie 	if (orsp->rel_move)
2091618Srie 		ld_adj_movereloc(ofl, orsp);
2101618Srie 
2111618Srie 	/*
2121618Srie 	 * If this is a relocation against a section using a partial initialized
2131618Srie 	 * symbol, adjust the embedded symbol info.
2141618Srie 	 *
2151618Srie 	 * The second argument of the am_I_partial() is the value stored at the
2161618Srie 	 * target address relocation is going to be applied.
2171618Srie 	 */
2181618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2191618Srie 		if (ofl->ofl_parsym.head &&
2201618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2211618Srie 		    /* LINTED */
2221618Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2231618Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2241618Srie 		    orsp->rel_roffset)))) {
2251618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2261618Srie 			sectmoved = 1;
2274734Sab196087 		}
2281618Srie 	}
2291618Srie 
2301618Srie 	value = sdp->sd_sym->st_value;
2311618Srie 
2321618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2331618Srie 		osp = ofl->ofl_osgot;
2341618Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2351618Srie 
2361618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2371618Srie 		/*
2381618Srie 		 * Note that relocations for PLT's actually
2391618Srie 		 * cause a relocation againt the GOT.
2401618Srie 		 */
2411618Srie 		osp = ofl->ofl_osplt;
2421618Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2431618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2441618Srie 
2451618Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2461618Srie 
2471618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2481618Srie 		/*
2491618Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2501618Srie 		 * point to the new symbols location.
2511618Srie 		 */
2521618Srie 		osp = ofl->ofl_isbss->is_osdesc;
2531618Srie 		roffset = (Word)value;
2541618Srie 	} else {
2551618Srie 		osp = orsp->rel_osdesc;
2561618Srie 
2571618Srie 		/*
2581618Srie 		 * Calculate virtual offset of reference point; equals offset
2591618Srie 		 * into section + vaddr of section for loadable sections, or
2601618Srie 		 * offset plus section displacement for nonloadable sections.
2611618Srie 		 */
2621618Srie 		roffset = orsp->rel_roffset +
2631618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
2641618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2651618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
2661618Srie 			    os_shdr->sh_addr;
2671618Srie 	}
2681618Srie 
2691618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
2701618Srie 		relosp = ofl->ofl_osrel;
2711618Srie 
2721618Srie 	/*
2731618Srie 	 * Assign the symbols index for the output relocation.  If the
2741618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
2751618Srie 	 * the output sections symbols index.  Otherwise the index can be
2761618Srie 	 * derived from the symbols index itself.
2771618Srie 	 */
2781618Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
2791618Srie 		ndx = STN_UNDEF;
2801618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
2811618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2821618Srie 		if (sectmoved == 0) {
2831618Srie 			/*
2841618Srie 			 * Check for a null input section. This can
2851618Srie 			 * occur if this relocation references a symbol
2861618Srie 			 * generated by sym_add_sym().
2871618Srie 			 */
2881618Srie 			if ((sdp->sd_isc != 0) &&
2891618Srie 			    (sdp->sd_isc->is_osdesc != 0))
2901618Srie 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
2911618Srie 			else
2921618Srie 				ndx = sdp->sd_shndx;
2931618Srie 		} else
2941618Srie 			ndx = ofl->ofl_sunwdata1ndx;
2951618Srie 	} else
2961618Srie 		ndx = sdp->sd_symndx;
2971618Srie 
2985892Sab196087 	/*
2995892Sab196087 	 * If we have a replacement value for the relocation
3005892Sab196087 	 * target, put it in place now.
3015892Sab196087 	 */
3025892Sab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
3035892Sab196087 		Xword	addend = orsp->rel_raddend;
3045892Sab196087 		uchar_t	*addr;
3055892Sab196087 
3065892Sab196087 		/*
3075892Sab196087 		 * Get the address of the data item we need to modify.
3085892Sab196087 		 */
3095892Sab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
3105892Sab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
3115892Sab196087 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
3125892Sab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
3135892Sab196087 			return (S_ERROR);
3145892Sab196087 	}
3155892Sab196087 
3161618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
3171618Srie 
3181618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3191618Srie 	rea.r_offset = roffset;
3201618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
3211618Srie 	    orsp->rel_sname));
3221618Srie 
3231618Srie 	/*
3241618Srie 	 * Assert we haven't walked off the end of our relocation table.
3251618Srie 	 */
3261618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3271618Srie 
3281618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3291618Srie 	    (char *)&rea, sizeof (Rel));
3301618Srie 	relosp->os_szoutrels += sizeof (Rel);
3311618Srie 
3321618Srie 	/*
3331618Srie 	 * Determine if this relocation is against a non-writable, allocatable
3341618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3351618Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3361618Srie 	 * result in modifications to the .got.
3371618Srie 	 */
3381618Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3391618Srie 		osp = ofl->ofl_osgot;
3401618Srie 
3411618Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3421618Srie 	return (1);
3431618Srie }
3441618Srie 
3451618Srie /*
3461618Srie  * i386 Instructions for TLS processing
3471618Srie  */
3481618Srie static uchar_t tlsinstr_gd_ie[] = {
3491618Srie 	/*
3501618Srie 	 * 0x00	movl %gs:0x0, %eax
3511618Srie 	 */
3521618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3531618Srie 	/*
3541618Srie 	 * 0x06	addl x(%eax), %eax
3551618Srie 	 * 0x0c ...
3561618Srie 	 */
3571618Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
3581618Srie };
3591618Srie 
3601618Srie static uchar_t tlsinstr_gd_le[] = {
3611618Srie 	/*
3621618Srie 	 * 0x00 movl %gs:0x0, %eax
3631618Srie 	 */
3641618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3651618Srie 	/*
3661618Srie 	 * 0x06 addl $0x0, %eax
3671618Srie 	 */
3681618Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
3691618Srie 	/*
3701618Srie 	 * 0x0b nop
3711618Srie 	 * 0x0c
3721618Srie 	 */
3731618Srie 	0x90
3741618Srie };
3751618Srie 
3761618Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
3771618Srie 	/*
3781618Srie 	 *	movl %gs:0x0,%eax
3791618Srie 	 */
3801618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
3811618Srie };
3821618Srie 
3831618Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
3841618Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
3851618Srie 
3861618Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
3871618Srie 
3881618Srie #define	TLS_NOP		0x90	/* NOP instruction */
3891618Srie 
3901618Srie #define	MODRM_MSK_MOD	0xc0
3911618Srie #define	MODRM_MSK_RO	0x38
3921618Srie #define	MODRM_MSK_RM	0x07
3931618Srie 
3941618Srie #define	SIB_MSK_SS	0xc0
3951618Srie #define	SIB_MSK_IND	0x38
3961618Srie #define	SIB_MSK_BS	0x07
3971618Srie 
3981618Srie static Fixupret
3991618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4001618Srie {
4011618Srie 	Sym_desc	*sdp = arsp->rel_sym;
4021618Srie 	Word		rtype = arsp->rel_rtype;
4031618Srie 	uchar_t		*offset, r1, r2;
4041618Srie 
4051618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
4061618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
4071618Srie 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4081618Srie 
4091618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
4101618Srie 		/*
4111618Srie 		 * IE reference model
4121618Srie 		 */
4131618Srie 		switch (rtype) {
4141618Srie 		case R_386_TLS_GD:
4151618Srie 			/*
4161618Srie 			 * Transition:
4171618Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4181618Srie 			 *	0x7 call ___tls_get_addr
4191618Srie 			 *	0xc
4201618Srie 			 * To:
4211618Srie 			 *	0x0 movl %gs:0, %eax
4221618Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4231618Srie 			 */
4241618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4253304Srie 			    R_386_TLS_GOTIE, arsp));
4261618Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4271618Srie 			arsp->rel_roffset += 5;
4281618Srie 
4291618Srie 			/*
4303304Srie 			 * Adjust 'offset' to beginning of instruction
4311618Srie 			 * sequence.
4321618Srie 			 */
4331618Srie 			offset -= 3;
4341618Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4351618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4361618Srie 			    sizeof (tlsinstr_gd_ie));
4371618Srie 
4381618Srie 			/*
4391618Srie 			 * set register %r1 into the addl
4401618Srie 			 * instruction.
4411618Srie 			 */
4421618Srie 			offset[0x7] |= r1;
4431618Srie 			return (FIX_RELOC);
4441618Srie 
4451618Srie 		case R_386_TLS_GD_PLT:
4461618Srie 			/*
4471618Srie 			 * Fixup done via the TLS_GD relocation
4481618Srie 			 */
4491618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4503304Srie 			    R_386_NONE, arsp));
4511618Srie 			return (FIX_DONE);
4521618Srie 		}
4531618Srie 	}
4541618Srie 
4551618Srie 	/*
4561618Srie 	 * LE reference model
4571618Srie 	 */
4581618Srie 	switch (rtype) {
4591618Srie 	case R_386_TLS_GD:
4601618Srie 		/*
4611618Srie 		 * Transition:
4621618Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
4631618Srie 		 *	0x7 call ___tls_get_addr
4641618Srie 		 *	0xc
4651618Srie 		 * To:
4661618Srie 		 *	0x0 movl %gs:0, %eax
4671618Srie 		 *	0x6 addl $x@ntpoff, %eax
4681618Srie 		 *	0xb nop
4691618Srie 		 *	0xc
4701618Srie 		 */
4711618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4723304Srie 		    R_386_TLS_LE, arsp));
4731618Srie 
4741618Srie 		arsp->rel_rtype = R_386_TLS_LE;
4751618Srie 		arsp->rel_roffset += 4;
4761618Srie 
4771618Srie 		/*
4783304Srie 		 * Adjust 'offset' to beginning of instruction
4791618Srie 		 * sequence.
4801618Srie 		 */
4811618Srie 		offset -= 3;
4821618Srie 		(void) memcpy(offset, tlsinstr_gd_le,
4831618Srie 		    sizeof (tlsinstr_gd_le));
4841618Srie 		return (FIX_RELOC);
4851618Srie 
4861618Srie 	case R_386_TLS_GD_PLT:
4871618Srie 	case R_386_PLT32:
4881618Srie 		/*
4891618Srie 		 * Fixup done via the TLS_GD relocation
4901618Srie 		 */
4911618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4923304Srie 		    R_386_NONE, arsp));
4931618Srie 		return (FIX_DONE);
4941618Srie 
4951618Srie 	case R_386_TLS_LDM_PLT:
4961618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4973304Srie 		    R_386_NONE, arsp));
4981618Srie 
4991618Srie 		/*
5001618Srie 		 * Transition:
5011618Srie 		 *	call __tls_get_addr()
5021618Srie 		 * to:
5031618Srie 		 *	nop
5041618Srie 		 *	nop
5051618Srie 		 *	nop
5061618Srie 		 *	nop
5071618Srie 		 *	nop
5081618Srie 		 */
5091618Srie 		*(offset - 1) = TLS_NOP;
5101618Srie 		*(offset) = TLS_NOP;
5111618Srie 		*(offset + 1) = TLS_NOP;
5121618Srie 		*(offset + 2) = TLS_NOP;
5131618Srie 		*(offset + 3) = TLS_NOP;
5141618Srie 		return (FIX_DONE);
5151618Srie 
5161618Srie 	case R_386_TLS_LDM:
5171618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5183304Srie 		    R_386_NONE, arsp));
5191618Srie 
5201618Srie 		/*
5211618Srie 		 * Transition:
5221618Srie 		 *
5231618Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5241618Srie 		 *  0x06 call ___tls_get_addr
5251618Srie 		 *
5261618Srie 		 * to:
5271618Srie 		 *
5281618Srie 		 *  0x00 movl %gs:0, %eax
5291618Srie 		 */
5301618Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5311618Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5321618Srie 		return (FIX_DONE);
5331618Srie 
5341618Srie 	case R_386_TLS_LDO_32:
5351618Srie 		/*
5361618Srie 		 *  Instructions:
5371618Srie 		 *
5381618Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5391618Srie 		 *		to
5401618Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5411618Srie 		 *
5421618Srie 		 */
5431618Srie 		offset -= 2;
5441618Srie 
5451618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5463304Srie 		    R_386_TLS_LE, arsp));
5471618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5481618Srie 		return (FIX_RELOC);
5491618Srie 
5501618Srie 	case R_386_TLS_GOTIE:
5511618Srie 		/*
5521618Srie 		 * These transitions are a little different than the
5531618Srie 		 * others, in that we could have multiple instructions
5541618Srie 		 * pointed to by a single relocation.  Depending upon the
5551618Srie 		 * instruction, we perform a different code transition.
5561618Srie 		 *
5571618Srie 		 * Here's the known transitions:
5581618Srie 		 *
5591618Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
5601618Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5611618Srie 		 *
5621618Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
5631618Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5641618Srie 		 *
5651618Srie 		 *  Transitions IE -> LE
5661618Srie 		 *
5671618Srie 		 *  1) movl $foo@ntpoff, %reg2
5681618Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
5691618Srie 		 *
5701618Srie 		 *  2) addl $foo@ntpoff, %reg2
5711618Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
5721618Srie 		 *
5731618Srie 		 * Note: reg1 != 4 (%esp)
5741618Srie 		 */
5751618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5763304Srie 		    R_386_TLS_LE, arsp));
5771618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5781618Srie 
5791618Srie 		offset -= 2;
5801618Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
5811618Srie 		if (offset[0] == 0x8b) {
5821618Srie 			/* case 1 above */
5831618Srie 			offset[0] = 0xc7;	/* movl */
5841618Srie 			offset[1] = 0xc0 | r2;
5851618Srie 			return (FIX_RELOC);
5861618Srie 		}
5871618Srie 
5881618Srie 		if (offset[0] == 0x03) {
5891618Srie 			/* case 2 above */
5901618Srie 			assert(offset[0] == 0x03);
5911618Srie 			offset[0] = 0x81;	/* addl */
5921618Srie 			offset[1] = 0xc0 | r2;
5931618Srie 			return (FIX_RELOC);
5941618Srie 		}
5951618Srie 
5961618Srie 		/*
5971618Srie 		 * Unexpected instruction sequence - fatal error.
5981618Srie 		 */
5994734Sab196087 		{
6004734Sab196087 			Conv_inv_buf_t	inv_buf;
6014734Sab196087 
6024734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6034734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6044734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6054734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6064734Sab196087 			    demangle(arsp->rel_sname),
6074734Sab196087 			    arsp->rel_isdesc->is_name,
6084734Sab196087 			    EC_OFF(arsp->rel_roffset));
6094734Sab196087 		}
6101618Srie 		return (FIX_ERROR);
6111618Srie 
6121618Srie 	case R_386_TLS_IE:
6131618Srie 		/*
6141618Srie 		 * These transitions are a little different than the
6151618Srie 		 * others, in that we could have multiple instructions
6161618Srie 		 * pointed to by a single relocation.  Depending upon the
6171618Srie 		 * instruction, we perform a different code transition.
6181618Srie 		 *
6191618Srie 		 * Here's the known transitions:
6201618Srie 		 *  1) movl foo@indntpoff, %eax
6211618Srie 		 *	0xa1, foo@indntpoff
6221618Srie 		 *
6231618Srie 		 *  2) movl foo@indntpoff, %eax
6241618Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6251618Srie 		 *
6261618Srie 		 *  3) addl foo@indntpoff, %eax
6271618Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6281618Srie 		 *
6291618Srie 		 *  Transitions IE -> LE
6301618Srie 		 *
6311618Srie 		 *  1) movl $foo@ntpoff, %eax
6321618Srie 		 *	0xb8, foo@ntpoff
6331618Srie 		 *
6341618Srie 		 *  2) movl $foo@ntpoff, %reg
6351618Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6361618Srie 		 *
6371618Srie 		 *  3) addl $foo@ntpoff, %reg
6381618Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6391618Srie 		 */
6401618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6411618Srie 		offset--;
6421618Srie 		if (offset[0] == 0xa1) {
6431618Srie 			/* case 1 above */
6441618Srie 			offset[0] = 0xb8;	/*  movl */
6451618Srie 			return (FIX_RELOC);
6461618Srie 		}
6471618Srie 
6481618Srie 		offset--;
6491618Srie 		if (offset[0] == 0x8b) {
6501618Srie 			/* case 2 above */
6511618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6521618Srie 			offset[0] = 0xc7;	/* movl */
6531618Srie 			offset[1] = 0xc0 | r2;
6541618Srie 			return (FIX_RELOC);
6551618Srie 		}
6561618Srie 		if (offset[0] == 0x03) {
6571618Srie 			/* case 3 above */
6581618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6591618Srie 			offset[0] = 0x81;	/* addl */
6601618Srie 			offset[1] = 0xc0 | r2;
6611618Srie 			return (FIX_RELOC);
6621618Srie 		}
6631618Srie 		/*
6641618Srie 		 * Unexpected instruction sequence - fatal error.
6651618Srie 		 */
6664734Sab196087 		{
6674734Sab196087 			Conv_inv_buf_t	inv_buf;
6684734Sab196087 
6694734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6704734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6714734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6724734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6734734Sab196087 			    demangle(arsp->rel_sname),
6744734Sab196087 			    arsp->rel_isdesc->is_name,
6754734Sab196087 			    EC_OFF(arsp->rel_roffset));
6764734Sab196087 		}
6771618Srie 		return (FIX_ERROR);
6781618Srie 	}
6791618Srie 	return (FIX_RELOC);
6801618Srie }
6811618Srie 
682*6206Sab196087 static uintptr_t
6831618Srie ld_do_activerelocs(Ofl_desc *ofl)
6841618Srie {
6851618Srie 	Rel_desc	*arsp;
6861618Srie 	Rel_cache	*rcp;
6871618Srie 	Listnode	*lnp;
6881618Srie 	uintptr_t	return_code = 1;
6891618Srie 	Word		flags = ofl->ofl_flags;
6901618Srie 
6912647Srie 	if (ofl->ofl_actrels.head)
6922647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6932647Srie 
6941618Srie 	/*
6951618Srie 	 * Process active relocations.
6961618Srie 	 */
6971618Srie 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6981618Srie 		/* LINTED */
6991618Srie 		for (arsp = (Rel_desc *)(rcp + 1);
7001618Srie 		    arsp < rcp->rc_free; arsp++) {
7011618Srie 			uchar_t		*addr;
7021618Srie 			Xword 		value;
7031618Srie 			Sym_desc	*sdp;
7041618Srie 			const char	*ifl_name;
7051618Srie 			Xword		refaddr;
7061618Srie 			int		moved = 0;
7071618Srie 			Gotref		gref;
7081618Srie 
7091618Srie 			/*
7101618Srie 			 * If the section this relocation is against has been
7111618Srie 			 * discarded (-zignore), then discard (skip) the
7121618Srie 			 * relocation itself.
7131618Srie 			 */
7141618Srie 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
7151618Srie 			    ((arsp->rel_flags &
7161618Srie 			    (FLG_REL_GOT | FLG_REL_BSS |
7171618Srie 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
7181618Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
7191618Srie 				    M_MACH, arsp));
7201618Srie 				continue;
7211618Srie 			}
7221618Srie 
7231618Srie 			/*
7241618Srie 			 * We deteremine what the 'got reference'
7251618Srie 			 * model (if required) is at this point.  This
7261618Srie 			 * needs to be done before tls_fixup() since
7271618Srie 			 * it may 'transition' our instructions.
7281618Srie 			 *
7291618Srie 			 * The got table entries have already been assigned,
7301618Srie 			 * and we bind to those initial entries.
7311618Srie 			 */
7321618Srie 			if (arsp->rel_flags & FLG_REL_DTLS)
7331618Srie 				gref = GOT_REF_TLSGD;
7341618Srie 			else if (arsp->rel_flags & FLG_REL_MTLS)
7351618Srie 				gref = GOT_REF_TLSLD;
7361618Srie 			else if (arsp->rel_flags & FLG_REL_STLS)
7371618Srie 				gref = GOT_REF_TLSIE;
7381618Srie 			else
7391618Srie 				gref = GOT_REF_GENERIC;
7401618Srie 
7411618Srie 			/*
7421618Srie 			 * Perform any required TLS fixups.
7431618Srie 			 */
7441618Srie 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7451618Srie 				Fixupret	ret;
7461618Srie 
7471618Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7481618Srie 					return (S_ERROR);
7491618Srie 				if (ret == FIX_DONE)
7501618Srie 					continue;
7511618Srie 			}
7521618Srie 
7531618Srie 			/*
7541618Srie 			 * If this is a relocation against a move table, or
7551618Srie 			 * expanded move table, adjust the relocation entries.
7561618Srie 			 */
7571618Srie 			if (arsp->rel_move)
7581618Srie 				ld_adj_movereloc(ofl, arsp);
7591618Srie 
7601618Srie 			sdp = arsp->rel_sym;
7611618Srie 			refaddr = arsp->rel_roffset +
7621618Srie 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7631618Srie 
7641618Srie 			if (arsp->rel_flags & FLG_REL_CLVAL)
7651618Srie 				value = 0;
7661618Srie 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7671618Srie 			    STT_SECTION) {
7681618Srie 				Sym_desc	*sym;
7691618Srie 
7701618Srie 				/*
7711618Srie 				 * The value for a symbol pointing to a SECTION
7721618Srie 				 * is based off of that sections position.
7731618Srie 				 *
7741618Srie 				 * The second argument of the ld_am_I_partial()
7751618Srie 				 * is the value stored at the target address
7761618Srie 				 * relocation is going to be applied.
7771618Srie 				 */
7781618Srie 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7791618Srie 				    /* LINTED */
7801618Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
7811618Srie 				    ((uchar_t *)
7821618Srie 				    arsp->rel_isdesc->is_indata->d_buf +
7831618Srie 				    arsp->rel_roffset)))) {
7841618Srie 					/*
7851618Srie 					 * If the symbol is moved,
7861618Srie 					 * adjust the value
7871618Srie 					 */
7881618Srie 					value = sym->sd_sym->st_value;
7891618Srie 					moved = 1;
7901618Srie 				} else {
7911618Srie 					value = _elf_getxoff(
7921618Srie 					    sdp->sd_isc->is_indata);
7931618Srie 					if (sdp->sd_isc->is_shdr->sh_flags &
7941618Srie 					    SHF_ALLOC)
7951618Srie 						value += sdp->sd_isc->
7961618Srie 						    is_osdesc->os_shdr->sh_addr;
7971618Srie 				}
7981618Srie 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7991618Srie 					value -= ofl->ofl_tlsphdr->p_vaddr;
8002850Srie 
8012850Srie 			} else if (IS_SIZE(arsp->rel_rtype)) {
8022850Srie 				/*
8032850Srie 				 * Size relocations require the symbols size.
8042850Srie 				 */
8052850Srie 				value = sdp->sd_sym->st_size;
8061618Srie 			} else {
8071618Srie 				/*
8082647Srie 				 * Else the value is the symbols value.
8091618Srie 				 */
8101618Srie 				value = sdp->sd_sym->st_value;
8111618Srie 			}
8121618Srie 
8131618Srie 			/*
8141618Srie 			 * Relocation against the GLOBAL_OFFSET_TABLE.
8151618Srie 			 */
8161618Srie 			if (arsp->rel_flags & FLG_REL_GOT)
8171618Srie 				arsp->rel_osdesc = ofl->ofl_osgot;
8181618Srie 
8191618Srie 			/*
8201618Srie 			 * If loadable and not producing a relocatable object
8211618Srie 			 * add the sections virtual address to the reference
8221618Srie 			 * address.
8231618Srie 			 */
8241618Srie 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
8251618Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8261618Srie 				refaddr += arsp->rel_isdesc->is_osdesc->
8271618Srie 				    os_shdr->sh_addr;
8281618Srie 
8291618Srie 			/*
8301618Srie 			 * If this entry has a PLT assigned to it, it's
8311618Srie 			 * value is actually the address of the PLT (and
8321618Srie 			 * not the address of the function).
8331618Srie 			 */
8341618Srie 			if (IS_PLT(arsp->rel_rtype)) {
8351618Srie 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8361618Srie 					value = ld_calc_plt_addr(sdp, ofl);
8371618Srie 			}
8381618Srie 
8391618Srie 			/*
8401618Srie 			 * Determine whether the value needs further adjustment.
8411618Srie 			 * Filter through the attributes of the relocation to
8421618Srie 			 * determine what adjustment is required.  Note, many
8431618Srie 			 * of the following cases are only applicable when a
8441618Srie 			 * .got is present.  As a .got is not generated when a
8451618Srie 			 * relocatable object is being built, any adjustments
8461618Srie 			 * that require a .got need to be skipped.
8471618Srie 			 */
8481618Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
8491618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8501618Srie 				Xword		R1addr;
8511618Srie 				uintptr_t	R2addr;
8521618Srie 				Word		gotndx;
8531618Srie 				Gotndx		*gnp;
8541618Srie 
8551618Srie 				/*
8561618Srie 				 * Perform relocation against GOT table.  Since
8571618Srie 				 * this doesn't fit exactly into a relocation
8581618Srie 				 * we place the appropriate byte in the GOT
8591618Srie 				 * directly
8601618Srie 				 *
8611618Srie 				 * Calculate offset into GOT at which to apply
8621618Srie 				 * the relocation.
8631618Srie 				 */
8641618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8651618Srie 				    ofl, 0);
8661618Srie 				assert(gnp);
8671618Srie 
8681618Srie 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
8691618Srie 					gotndx = gnp->gn_gotndx + 1;
8701618Srie 				else
8711618Srie 					gotndx = gnp->gn_gotndx;
8721618Srie 
8731618Srie 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8741618Srie 
8751618Srie 				/*
8761618Srie 				 * Add the GOTs data's offset.
8771618Srie 				 */
8781618Srie 				R2addr = R1addr + (uintptr_t)
8791618Srie 				    arsp->rel_osdesc->os_outdata->d_buf;
8801618Srie 
8811618Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8821618Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
8831618Srie 				    arsp->rel_rtype, R1addr, value,
8841618Srie 				    arsp->rel_sname, arsp->rel_osdesc));
8851618Srie 
8861618Srie 				/*
8871618Srie 				 * And do it.
8881618Srie 				 */
8895189Sab196087 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
8905189Sab196087 					*(Xword *)R2addr =
891*6206Sab196087 					    ld_bswap_Xword(value);
8925189Sab196087 				else
8935189Sab196087 					*(Xword *)R2addr = value;
8941618Srie 				continue;
8951618Srie 
8961618Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
8971618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8981618Srie 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
8991618Srie 
9001618Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
9011618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9021618Srie 				value =
9031618Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
9041618Srie 				    refaddr;
9051618Srie 
9061618Srie 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9071618Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9081618Srie 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9091618Srie 				value -= refaddr;
9101618Srie 
9111618Srie 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
9121618Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
9131618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9141618Srie 				Gotndx	*gnp;
9151618Srie 
9161618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
9171618Srie 				    ofl, 0);
9181618Srie 				assert(gnp);
9191618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9201618Srie 				if (arsp->rel_rtype == R_386_TLS_IE) {
9211618Srie 					value +=
9221618Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9231618Srie 				}
9241618Srie 
9251618Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
9261618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9271618Srie 				Gotndx *gnp;
9281618Srie 
9291618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9301618Srie 				    GOT_REF_GENERIC, ofl, 0);
9311618Srie 				assert(gnp);
9321618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9331618Srie 
9341618Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
9351618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9361618Srie 				Xword	tlsstatsize;
9371618Srie 
9381618Srie 				/*
9391618Srie 				 * This is the LE TLS reference model.  Static
9401618Srie 				 * offset is hard-coded.
9411618Srie 				 */
9421618Srie 				tlsstatsize =
9431618Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9441618Srie 				    M_TLSSTATALIGN);
9451618Srie 				value = tlsstatsize - value;
9461618Srie 
9471618Srie 				/*
9481618Srie 				 * Since this code is fixed up, it assumes a
9491618Srie 				 * negative offset that can be added to the
9501618Srie 				 * thread pointer.
9511618Srie 				 */
9521618Srie 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9531618Srie 				    (arsp->rel_rtype == R_386_TLS_LE))
9541618Srie 					value = -value;
9551618Srie 			}
9561618Srie 
9571618Srie 			if (arsp->rel_isdesc->is_file)
9581618Srie 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9591618Srie 			else
9601618Srie 				ifl_name = MSG_INTL(MSG_STR_NULL);
9611618Srie 
9621618Srie 			/*
9631618Srie 			 * Make sure we have data to relocate.  Compiler and
9641618Srie 			 * assembler developers have been known to generate
9651618Srie 			 * relocations against invalid sections (normally .bss),
9661618Srie 			 * so for their benefit give them sufficient information
9671618Srie 			 * to help analyze the problem.  End users should never
9681618Srie 			 * see this.
9691618Srie 			 */
9701618Srie 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
9714734Sab196087 				Conv_inv_buf_t	inv_buf;
9724734Sab196087 
9731618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9741618Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
9754734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
9764734Sab196087 				    0, &inv_buf),
9771618Srie 				    ifl_name, demangle(arsp->rel_sname),
9781618Srie 				    arsp->rel_isdesc->is_name);
9791618Srie 				return (S_ERROR);
9801618Srie 			}
9811618Srie 
9821618Srie 			/*
9831618Srie 			 * Get the address of the data item we need to modify.
9841618Srie 			 */
9851618Srie 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
9861618Srie 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
9871618Srie 			    is_indata));
9881618Srie 
9891618Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9901618Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
9911618Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9921618Srie 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9931618Srie 
9941618Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9951618Srie 			    ofl->ofl_size) || (arsp->rel_roffset >
9961618Srie 			    arsp->rel_osdesc->os_shdr->sh_size)) {
9974734Sab196087 				Conv_inv_buf_t	inv_buf;
9984734Sab196087 				int		class;
9991618Srie 
10001618Srie 				if (((uintptr_t)addr -
10011618Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10021618Srie 					class = ERR_FATAL;
10031618Srie 				else
10041618Srie 					class = ERR_WARNING;
10051618Srie 
10061618Srie 				eprintf(ofl->ofl_lml, class,
10071618Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
10084734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10094734Sab196087 				    0, &inv_buf),
10101618Srie 				    ifl_name, arsp->rel_isdesc->is_name,
10111618Srie 				    demangle(arsp->rel_sname),
10121618Srie 				    EC_ADDR((uintptr_t)addr -
10131618Srie 				    (uintptr_t)ofl->ofl_nehdr));
10141618Srie 
10151618Srie 				if (class == ERR_FATAL) {
10161618Srie 					return_code = S_ERROR;
10171618Srie 					continue;
10181618Srie 				}
10191618Srie 			}
10201618Srie 
10211618Srie 			/*
10221618Srie 			 * The relocation is additive.  Ignore the previous
10231618Srie 			 * symbol value if this local partial symbol is
10241618Srie 			 * expanded.
10251618Srie 			 */
10261618Srie 			if (moved)
10271618Srie 				value -= *addr;
10281618Srie 
10291618Srie 			/*
10305892Sab196087 			 * If we have a replacement value for the relocation
10315892Sab196087 			 * target, put it in place now.
10325892Sab196087 			 */
10335892Sab196087 			if (arsp->rel_flags & FLG_REL_NADDEND) {
10345892Sab196087 				Xword addend = arsp->rel_raddend;
10355892Sab196087 
10365892Sab196087 				if (ld_reloc_targval_set(ofl, arsp,
10375892Sab196087 				    addr, addend) == 0)
10385892Sab196087 					return (S_ERROR);
10395892Sab196087 			}
10405892Sab196087 
10415892Sab196087 			/*
10425189Sab196087 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10431618Srie 			 * stage.
10441618Srie 			 */
10455189Sab196087 			if (OFL_DO_RELOC(ofl)) {
10465189Sab196087 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10471618Srie 				    &value, arsp->rel_sname, ifl_name,
10485189Sab196087 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10491618Srie 				    ofl->ofl_lml) == 0)
10501618Srie 					return_code = S_ERROR;
10511618Srie 			}
10521618Srie 		}
10531618Srie 	}
10541618Srie 	return (return_code);
10551618Srie }
10561618Srie 
10571618Srie /*
10581618Srie  * Add an output relocation record.
10591618Srie  */
1060*6206Sab196087 static uintptr_t
10611618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10621618Srie {
10631618Srie 	Rel_desc	*orsp;
10641618Srie 	Rel_cache	*rcp;
10651618Srie 	Sym_desc	*sdp = rsp->rel_sym;
10661618Srie 
10671618Srie 	/*
10681618Srie 	 * Static executables *do not* want any relocations against them.
10691618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10701618Srie 	 * symbol in a static executable, it's best to disable them here
10711618Srie 	 * instead of through out the relocation code.
10721618Srie 	 */
10731618Srie 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
10741618Srie 	    (FLG_OF_STATIC | FLG_OF_EXEC))
10751618Srie 		return (1);
10761618Srie 
10771618Srie 	/*
10781618Srie 	 * If no relocation cache structures are available allocate
10791618Srie 	 * a new one and link it into the cache list.
10801618Srie 	 */
10811618Srie 	if ((ofl->ofl_outrels.tail == 0) ||
10821618Srie 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10831618Srie 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10841618Srie 		static size_t	nextsize = 0;
10851618Srie 		size_t		size;
10861618Srie 
10871618Srie 		/*
10881618Srie 		 * Output relocation numbers can vary considerably between
10891618Srie 		 * building executables or shared objects (pic vs. non-pic),
10901618Srie 		 * etc.  But, they typically aren't very large, so for these
10911618Srie 		 * objects use a standard bucket size.  For building relocatable
10921618Srie 		 * objects, typically there will be an output relocation for
10931618Srie 		 * every input relocation.
10941618Srie 		 */
10951618Srie 		if (nextsize == 0) {
10961618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10971618Srie 				if ((size = ofl->ofl_relocincnt) == 0)
10981618Srie 					size = REL_LOIDESCNO;
10991618Srie 				if (size > REL_HOIDESCNO)
11001618Srie 					nextsize = REL_HOIDESCNO;
11011618Srie 				else
11021618Srie 					nextsize = REL_LOIDESCNO;
11031618Srie 			} else
11041618Srie 				nextsize = size = REL_HOIDESCNO;
11051618Srie 		} else
11061618Srie 			size = nextsize;
11071618Srie 
11081618Srie 		size = size * sizeof (Rel_desc);
11091618Srie 
11101618Srie 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
11111618Srie 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
11121618Srie 			return (S_ERROR);
11131618Srie 
11141618Srie 		/* LINTED */
11151618Srie 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
11161618Srie 		/* LINTED */
11171618Srie 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
11181618Srie 	}
11191618Srie 
11201618Srie 	/*
11211618Srie 	 * If we are adding a output relocation against a section
11221618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11231618Srie 	 * will be added to the .dynsym symbol table.
11241618Srie 	 */
11251618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11261618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11271618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11281618Srie 
11291618Srie 		/*
11301618Srie 		 * If this is a COMMON symbol - no output section
11311618Srie 		 * exists yet - (it's created as part of sym_validate()).
11321618Srie 		 * So - we mark here that when it's created it should
11331618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11341618Srie 		 */
11351618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11361682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11371618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11381618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11391618Srie 			else
11401618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11411618Srie 		} else {
11421618Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11431618Srie 
11442347Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11451618Srie 				ofl->ofl_dynshdrcnt++;
11461618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11471618Srie 			}
11481618Srie 		}
11491618Srie 	}
11501618Srie 
11511618Srie 	*orsp = *rsp;
11521618Srie 	orsp->rel_flags |= flags;
11531618Srie 
11541618Srie 	rcp->rc_free++;
11551618Srie 	ofl->ofl_outrelscnt++;
11561618Srie 
11571618Srie 	if (flags & FLG_REL_GOT)
11581618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11591618Srie 	else if (flags & FLG_REL_PLT)
11601618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11611618Srie 	else if (flags & FLG_REL_BSS)
11621618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11631618Srie 	else if (flags & FLG_REL_NOINFO)
11641618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11651618Srie 	else
11661618Srie 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
11671618Srie 
11681618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11691618Srie 		ofl->ofl_relocrelcnt++;
11701618Srie 
11711618Srie 	/*
11721618Srie 	 * We don't perform sorting on PLT relocations because
11731618Srie 	 * they have already been assigned a PLT index and if we
11741618Srie 	 * were to sort them we would have to re-assign the plt indexes.
11751618Srie 	 */
11761618Srie 	if (!(flags & FLG_REL_PLT))
11771618Srie 		ofl->ofl_reloccnt++;
11781618Srie 
11791618Srie 	/*
11801618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11811618Srie 	 */
11821618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11831618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11841618Srie 
11851618Srie 	/*
11861618Srie 	 * Identify and possibly warn of a displacement relocation.
11871618Srie 	 */
11881618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11891618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11901618Srie 
11911618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11921618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11931618Srie 	}
11941618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11951618Srie 	    M_MACH, orsp));
11961618Srie 	return (1);
11971618Srie }
11981618Srie 
11991618Srie /*
12001618Srie  * process relocation for a LOCAL symbol
12011618Srie  */
1202*6206Sab196087 static uintptr_t
12031618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12041618Srie {
12051618Srie 	Word		flags = ofl->ofl_flags;
12061618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12071682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12081618Srie 
12091618Srie 	/*
12101618Srie 	 * if ((shared object) and (not pc relative relocation) and
12111618Srie 	 *    (not against ABS symbol))
12121618Srie 	 * then
12131618Srie 	 *	build R_386_RELATIVE
12141618Srie 	 * fi
12151618Srie 	 */
12161618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12172850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12181618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12191618Srie 	    !(rsp->rel_isdesc != NULL &&
12201618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12211618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12221618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12231618Srie 		Word	ortype = rsp->rel_rtype;
12241618Srie 
12251618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12261618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12271618Srie 			return (S_ERROR);
12281618Srie 		rsp->rel_rtype = ortype;
12291618Srie 	}
12301618Srie 
12311618Srie 	/*
12321618Srie 	 * If the relocation is against a 'non-allocatable' section
12331618Srie 	 * and we can not resolve it now - then give a warning
12341618Srie 	 * message.
12351618Srie 	 *
12361618Srie 	 * We can not resolve the symbol if either:
12371618Srie 	 *	a) it's undefined
12381618Srie 	 *	b) it's defined in a shared library and a
12391618Srie 	 *	   COPY relocation hasn't moved it to the executable
12401618Srie 	 *
12411618Srie 	 * Note: because we process all of the relocations against the
12421618Srie 	 *	text segment before any others - we know whether
12431618Srie 	 *	or not a copy relocation will be generated before
12441618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12451618Srie 	 */
12461618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12471618Srie 	    ((shndx == SHN_UNDEF) ||
12481618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12491618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
12504734Sab196087 		Conv_inv_buf_t inv_buf;
12514734Sab196087 
12521618Srie 		/*
12531618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12541618Srie 		 * section - then silently ignore that the relocation
12551618Srie 		 * can not be resolved.
12561618Srie 		 */
12571618Srie 		if (rsp->rel_osdesc &&
12581618Srie 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12591618Srie 			return (0);
12601618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12614734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12621618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
12631618Srie 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12641618Srie 		return (1);
12651618Srie 	}
12661618Srie 
12671618Srie 	/*
12681618Srie 	 * Perform relocation.
12691618Srie 	 */
12701618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12711618Srie }
12721618Srie 
1273*6206Sab196087 static uintptr_t
12741618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12751618Srie {
12761618Srie 	Word		rtype = rsp->rel_rtype;
12771618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12781618Srie 	Word		flags = ofl->ofl_flags;
12791618Srie 	Gotndx		*gnp;
12801618Srie 
12811618Srie 	/*
12822145Srie 	 * If we're building an executable - use either the IE or LE access
12832145Srie 	 * model.  If we're building a shared object process any IE model.
12841618Srie 	 */
12852145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12861618Srie 		/*
12872145Srie 		 * Set the DF_STATIC_TLS flag.
12881618Srie 		 */
12891618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12901618Srie 
12912145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12921618Srie 			/*
12932145Srie 			 * Assign a GOT entry for static TLS references.
12941618Srie 			 */
12952145Srie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12962145Srie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
12972145Srie 
12982145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
12992145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
13002145Srie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
13012145Srie 					return (S_ERROR);
13021618Srie 			}
13031618Srie 
13042145Srie 			/*
13052145Srie 			 * IE access model.
13062145Srie 			 */
13072145Srie 			if (IS_TLS_IE(rtype)) {
13082145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13092145Srie 				    rsp, ofl) == S_ERROR)
13102145Srie 					return (S_ERROR);
13112145Srie 
13122145Srie 				/*
13132145Srie 				 * A non-pic shared object needs to adjust the
13142145Srie 				 * active relocation (indntpoff).
13152145Srie 				 */
13162145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13172145Srie 				    (rtype == R_386_TLS_IE)) {
13182145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13192145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13202145Srie 				}
13212145Srie 				return (1);
13222145Srie 			}
13231618Srie 
13241618Srie 			/*
13252145Srie 			 * Fixups are required for other executable models.
13261618Srie 			 */
13271618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13281618Srie 			    rsp, ofl));
13291618Srie 		}
13302145Srie 
13311618Srie 		/*
13322145Srie 		 * LE access model.
13331618Srie 		 */
13341618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13351618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13361618Srie 
13371618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13381618Srie 		    rsp, ofl));
13391618Srie 	}
13401618Srie 
13411618Srie 	/*
13422145Srie 	 * Building a shared object.
13432145Srie 	 *
13442145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13451618Srie 	 */
13462145Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13472145Srie 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
13482145Srie 
13492145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
13502145Srie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13512145Srie 			return (S_ERROR);
13522145Srie 
13532145Srie 	} else if (IS_TLS_GD(rtype) &&
13542145Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13552145Srie 	    ofl, 0)) == 0)) {
13562145Srie 
13572145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
13582145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
13592145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13602145Srie 			return (S_ERROR);
13611618Srie 	}
13621618Srie 
13631618Srie 	/*
13641618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
13652145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
13662145Srie 	 * symbol now, and prepare for the PLT magic.
13671618Srie 	 */
13681618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
13693862Srie 		Sym_desc	*tlsgetsym;
13701618Srie 
13711618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
13723862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13731618Srie 			return (S_ERROR);
13742145Srie 
13751618Srie 		rsp->rel_sym = tlsgetsym;
13761618Srie 		rsp->rel_sname = tlsgetsym->sd_name;
13771618Srie 		rsp->rel_rtype = R_386_PLT32;
13782145Srie 
13791618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13801618Srie 			return (S_ERROR);
13812145Srie 
13821618Srie 		rsp->rel_sym = sdp;
13831618Srie 		rsp->rel_sname = sdp->sd_name;
13841618Srie 		rsp->rel_rtype = rtype;
13851618Srie 		return (1);
13861618Srie 	}
13871618Srie 
13881618Srie 	if (IS_TLS_LD(rtype))
13891618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13901618Srie 
13911618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13921618Srie }
13931618Srie 
13941618Srie /* ARGSUSED3 */
1395*6206Sab196087 static Gotndx *
13961618Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13971618Srie {
13981618Srie 	Listnode *	lnp;
13991618Srie 	Gotndx *	gnp;
14001618Srie 
14011618Srie 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14021618Srie 		return (ofl->ofl_tlsldgotndx);
14031618Srie 
14041618Srie 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14051618Srie 		if (gnp->gn_gotref == gref)
14061618Srie 			return (gnp);
14071618Srie 	}
14081618Srie 	return ((Gotndx *)0);
14091618Srie }
14101618Srie 
1411*6206Sab196087 static Xword
14121618Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14131618Srie {
14141618Srie 	Os_desc		*osp = ofl->ofl_osgot;
14151618Srie 	Sym_desc	*sdp = rdesc->rel_sym;
14161618Srie 	Xword		gotndx;
14171618Srie 	Gotref		gref;
14181618Srie 	Gotndx		*gnp;
14191618Srie 
14201618Srie 	if (rdesc->rel_flags & FLG_REL_DTLS)
14211618Srie 		gref = GOT_REF_TLSGD;
14221618Srie 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14231618Srie 		gref = GOT_REF_TLSLD;
14241618Srie 	else if (rdesc->rel_flags & FLG_REL_STLS)
14251618Srie 		gref = GOT_REF_TLSIE;
14261618Srie 	else
14271618Srie 		gref = GOT_REF_GENERIC;
14281618Srie 
14291618Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14301618Srie 	assert(gnp);
14311618Srie 
14321618Srie 	gotndx = (Xword)gnp->gn_gotndx;
14331618Srie 
14341618Srie 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14351618Srie 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14361618Srie 		gotndx++;
14371618Srie 
14381618Srie 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14391618Srie }
14401618Srie 
14411618Srie 
14421618Srie /* ARGSUSED4 */
1443*6206Sab196087 static uintptr_t
14442145Srie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14451618Srie     Rel_desc * rsp, Sym_desc * sdp)
14461618Srie {
14471618Srie 	Gotndx	*gnp;
14481618Srie 	uint_t	gotents;
14491618Srie 
14501618Srie 	if (pgnp)
14511618Srie 		return (1);
14521618Srie 
14531618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14541618Srie 		gotents = 2;
14551618Srie 	else
14561618Srie 		gotents = 1;
14571618Srie 
14581618Srie 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14591618Srie 		return (S_ERROR);
14601618Srie 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14611618Srie 	gnp->gn_gotref = gref;
14621618Srie 
14631618Srie 	ofl->ofl_gotcnt += gotents;
14641618Srie 
14651618Srie 	if (gref == GOT_REF_TLSLD) {
14661618Srie 		ofl->ofl_tlsldgotndx = gnp;
14671618Srie 		return (1);
14681618Srie 	}
14691618Srie 
14701618Srie 	if (list_appendc(lst, (void *)gnp) == 0)
14711618Srie 		return (S_ERROR);
14721618Srie 
14731618Srie 	return (1);
14741618Srie }
14751618Srie 
1476*6206Sab196087 static void
14771618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14781618Srie {
14791618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14801618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14811618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14821618Srie }
14831618Srie 
14841618Srie /*
14851618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14861618Srie  */
1487*6206Sab196087 static uintptr_t
14882145Srie ld_fillin_gotplt(Ofl_desc *ofl)
14891618Srie {
14902145Srie 	Word	flags = ofl->ofl_flags;
1491*6206Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
14922145Srie 
14931618Srie 	if (ofl->ofl_osgot) {
14942145Srie 		Sym_desc	*sdp;
14951618Srie 
14961618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14971618Srie 		    SYM_NOHASH, 0, ofl)) != NULL) {
14982145Srie 			uchar_t	*genptr;
14992145Srie 
15002145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15011618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15021618Srie 			/* LINTED */
15031618Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1504*6206Sab196087 			if (bswap)
1505*6206Sab196087 				/* LINTED */
1506*6206Sab196087 				*(Word *)genptr =
1507*6206Sab196087 				    /* LINTED */
1508*6206Sab196087 				    ld_bswap_Word(*(Word *)genptr);
15091618Srie 		}
15101618Srie 	}
15111618Srie 
15121618Srie 	/*
15131618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15141618Srie 	 * entry is:
15151618Srie 	 *  if (building a.out) {
15161618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
15171618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
15181618Srie 	 *  } else {
15191618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15201618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15211618Srie 	 *  }
15221618Srie 	 */
15232145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15241618Srie 		uchar_t *pltent;
15251618Srie 
15261618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15272145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
15281618Srie 			pltent[0] = M_SPECIAL_INST;
15291618Srie 			pltent[1] = M_PUSHL_DISP;
15301618Srie 			pltent += 2;
15311618Srie 			/* LINTED */
15321618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15334734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1534*6206Sab196087 			if (bswap)
1535*6206Sab196087 				/* LINTED */
1536*6206Sab196087 				*(Word *)pltent =
1537*6206Sab196087 				    /* LINTED */
1538*6206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15391618Srie 			pltent += 4;
15401618Srie 			pltent[0] = M_SPECIAL_INST;
15411618Srie 			pltent[1] = M_JMP_DISP_IND;
15421618Srie 			pltent += 2;
15431618Srie 			/* LINTED */
15441618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15454734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1546*6206Sab196087 			if (bswap)
1547*6206Sab196087 				/* LINTED */
1548*6206Sab196087 				*(Word *)pltent =
1549*6206Sab196087 				    /* LINTED */
1550*6206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15511618Srie 		} else {
15521618Srie 			pltent[0] = M_SPECIAL_INST;
15531618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15541618Srie 			pltent += 2;
15551618Srie 			/* LINTED */
15561618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15574734Sab196087 			    M_GOT_ENTSIZE);
1558*6206Sab196087 			if (bswap)
1559*6206Sab196087 				/* LINTED */
1560*6206Sab196087 				*(Word *)pltent =
1561*6206Sab196087 				    /* LINTED */
1562*6206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15631618Srie 			pltent += 4;
15641618Srie 			pltent[0] = M_SPECIAL_INST;
15651618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15661618Srie 			pltent += 2;
15671618Srie 			/* LINTED */
15681618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15694734Sab196087 			    M_GOT_ENTSIZE);
1570*6206Sab196087 			if (bswap)
1571*6206Sab196087 				/* LINTED */
1572*6206Sab196087 				*(Word *)pltent =
1573*6206Sab196087 				    /* LINTED */
1574*6206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15751618Srie 		}
15761618Srie 	}
15771618Srie 	return (1);
15781618Srie }
1579*6206Sab196087 
1580*6206Sab196087 
1581*6206Sab196087 
1582*6206Sab196087 /*
1583*6206Sab196087  * Template for generating "void (*)(void)" function
1584*6206Sab196087  */
1585*6206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1586*6206Sab196087 /* 0x00 */	0xc3				/* ret */
1587*6206Sab196087 };
1588*6206Sab196087 
1589*6206Sab196087 
1590*6206Sab196087 
1591*6206Sab196087 /*
1592*6206Sab196087  * Return the ld_targ definition for this target.
1593*6206Sab196087  */
1594*6206Sab196087 const Target *
1595*6206Sab196087 ld_targ_init_x86(void)
1596*6206Sab196087 {
1597*6206Sab196087 	static const Target _ld_targ = {
1598*6206Sab196087 		{			/* Target_mach */
1599*6206Sab196087 			M_MACH,			/* m_mach */
1600*6206Sab196087 			M_MACHPLUS,		/* m_machplus */
1601*6206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
1602*6206Sab196087 			M_CLASS,		/* m_class */
1603*6206Sab196087 			M_DATA,			/* m_data */
1604*6206Sab196087 
1605*6206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
1606*6206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
1607*6206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
1608*6206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
1609*6206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1610*6206Sab196087 
1611*6206Sab196087 			/* Relocation type codes */
1612*6206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1613*6206Sab196087 			M_R_COPY,		/* m_r_copy */
1614*6206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1615*6206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1616*6206Sab196087 			M_R_NUM,		/* m_r_num */
1617*6206Sab196087 			M_R_NONE,		/* m_r_none */
1618*6206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
1619*6206Sab196087 			M_R_REGISTER,		/* m_r_register */
1620*6206Sab196087 
1621*6206Sab196087 			/* Relocation related constants */
1622*6206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1623*6206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1624*6206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1625*6206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1626*6206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1627*6206Sab196087 
1628*6206Sab196087 			/* GOT related constants */
1629*6206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
1630*6206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
1631*6206Sab196087 
1632*6206Sab196087 			/* PLT related constants */
1633*6206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
1634*6206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1635*6206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1636*6206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1637*6206Sab196087 
1638*6206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
1639*6206Sab196087 		},
1640*6206Sab196087 		{			/* Target_machid */
1641*6206Sab196087 			M_ID_ARRAY,		/* id_array */
1642*6206Sab196087 			M_ID_BSS,		/* id_bss */
1643*6206Sab196087 			M_ID_CAP,		/* id_cap */
1644*6206Sab196087 			M_ID_DATA,		/* id_data */
1645*6206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
1646*6206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
1647*6206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
1648*6206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
1649*6206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1650*6206Sab196087 			M_ID_GOT,		/* id_got */
1651*6206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1652*6206Sab196087 			M_ID_HASH,		/* id_hash */
1653*6206Sab196087 			M_ID_INTERP,		/* id_interp */
1654*6206Sab196087 			M_ID_LBSS,		/* id_lbss */
1655*6206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
1656*6206Sab196087 			M_ID_NOTE,		/* id_note */
1657*6206Sab196087 			M_ID_NULL,		/* id_null */
1658*6206Sab196087 			M_ID_PLT,		/* id_plt */
1659*6206Sab196087 			M_ID_REL,		/* id_rel */
1660*6206Sab196087 			M_ID_STRTAB,		/* id_strtab */
1661*6206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
1662*6206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
1663*6206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1664*6206Sab196087 			M_ID_TEXT,		/* id_text */
1665*6206Sab196087 			M_ID_TLS,		/* id_tls */
1666*6206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
1667*6206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
1668*6206Sab196087 			M_ID_UNWIND,		/* id_unwind */
1669*6206Sab196087 			M_ID_USER,		/* id_user */
1670*6206Sab196087 			M_ID_VERSION,		/* id_version */
1671*6206Sab196087 		},
1672*6206Sab196087 		{			/* Target_nullfunc */
1673*6206Sab196087 			nullfunc_tmpl,		/* nf_template */
1674*6206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
1675*6206Sab196087 		},
1676*6206Sab196087 		{			/* Target_machrel */
1677*6206Sab196087 			reloc_table,
1678*6206Sab196087 
1679*6206Sab196087 			ld_init_rel,		/* mr_init_rel */
1680*6206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
1681*6206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1682*6206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1683*6206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1684*6206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
1685*6206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
1686*6206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
1687*6206Sab196087 			NULL,			/* mr_reloc_register */
1688*6206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
1689*6206Sab196087 			NULL,			/* mr_reloc_GOTOP */
1690*6206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
1691*6206Sab196087 			NULL,			/* mr_assign_got */
1692*6206Sab196087 			ld_find_gotndx,		/* mr_find_gotndx */
1693*6206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
1694*6206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1695*6206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1696*6206Sab196087 			NULL,			/* mr_allocate_got */
1697*6206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1698*6206Sab196087 		},
1699*6206Sab196087 		{			/* Target_machsym */
1700*6206Sab196087 			NULL,			/* ms_reg_check */
1701*6206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
1702*6206Sab196087 			NULL,			/* ms_is_regsym */
1703*6206Sab196087 			NULL,			/* ms_reg_find */
1704*6206Sab196087 			NULL			/* ms_reg_enter */
1705*6206Sab196087 		},
1706*6206Sab196087 		{			/* Target_unwind */
1707*6206Sab196087 			NULL,		/* uw_make_unwindhdr */
1708*6206Sab196087 			NULL,		/* uw_populate_unwindhdr */
1709*6206Sab196087 			NULL,		/* uw_append_unwind */
1710*6206Sab196087 		}
1711*6206Sab196087 	};
1712*6206Sab196087 
1713*6206Sab196087 	return (&_ld_targ);
1714*6206Sab196087 }
1715