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  *
27*5892Sab196087  * 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 
321618Srie #include	<string.h>
331618Srie #include	<stdio.h>
341618Srie #include	<sys/elf_386.h>
351618Srie #include	<debug.h>
361618Srie #include	<reloc.h>
371618Srie #include	"msg.h"
381618Srie #include	"_libld.h"
391618Srie 
401618Srie Word
411618Srie ld_init_rel(Rel_desc *reld, void *reloc)
421618Srie {
431618Srie 	Rel *	rel = (Rel *)reloc;
441618Srie 
451618Srie 	/* LINTED */
461618Srie 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info);
471618Srie 	reld->rel_roffset = rel->r_offset;
481618Srie 	reld->rel_raddend = 0;
491618Srie 	reld->rel_typedata = 0;
501618Srie 
511618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
521618Srie }
531618Srie 
541618Srie void
551618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
561618Srie {
571618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
581618Srie }
591618Srie 
601618Srie void
611618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
621618Srie {
631618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
641618Srie 		/*
651618Srie 		 * Create this entry if we are going to create a PLT table.
661618Srie 		 */
671618Srie 		if (ofl->ofl_pltcnt)
681618Srie 			(*cnt)++;		/* DT_PLTGOT */
691618Srie 	}
701618Srie }
711618Srie 
721618Srie void
732145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
741618Srie {
751618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
761618Srie 		(*dyn)->d_tag = DT_PLTGOT;
771618Srie 		if (ofl->ofl_osgot)
781618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
791618Srie 		else
801618Srie 			(*dyn)->d_un.d_ptr = 0;
811618Srie 		(*dyn)++;
821618Srie 	}
831618Srie }
841618Srie 
851618Srie Xword
861618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
871618Srie {
881618Srie 	Xword	value;
891618Srie 
901618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
911618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
921618Srie 	return (value);
931618Srie }
941618Srie 
951618Srie /*
961618Srie  *  Build a single plt entry - code is:
971618Srie  *	if (building a.out)
981618Srie  *		JMP	*got_off
991618Srie  *	else
1001618Srie  *		JMP	*got_off@GOT(%ebx)
1011618Srie  *	PUSHL	&rel_off
1021618Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1031618Srie  *
1041618Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1051618Srie  *	so the first pass through the plt jumps back here, jumping
1061618Srie  *	in turn to the first plt entry, which jumps to the dynamic
1071618Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1081618Srie  *	future plt calls to the proper destination.
1091618Srie  */
1101618Srie static void
1111618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1121618Srie {
1131618Srie 	uchar_t		*pltent, *gotent;
1141618Srie 	Sword		plt_off;
1151618Srie 	Word		got_off;
1161618Srie 
1171618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1181618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1191618Srie 	    M_PLT_ENTSIZE);
1201618Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1211618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1221618Srie 
1231618Srie 	/*
1241618Srie 	 * Fill in the got entry with the address of the next instruction.
1251618Srie 	 */
1261618Srie 	/* LINTED */
1271618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1281618Srie 	    M_PLT_INSSIZE;
1291618Srie 
1301618Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1311618Srie 		pltent[0] = M_SPECIAL_INST;
1321618Srie 		pltent[1] = M_JMP_DISP_IND;
1331618Srie 		pltent += 2;
1341618Srie 		/* LINTED */
1351618Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1364734Sab196087 		    got_off);
1371618Srie 	} else {
1381618Srie 		pltent[0] = M_SPECIAL_INST;
1391618Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1401618Srie 		pltent += 2;
1411618Srie 		/* LINTED */
1421618Srie 		*(Word *)pltent = (Word)got_off;
1431618Srie 	}
1441618Srie 	pltent += 4;
1451618Srie 
1461618Srie 	pltent[0] = M_INST_PUSHL;
1471618Srie 	pltent++;
1481618Srie 	/* LINTED */
1491618Srie 	*(Word *)pltent = (Word)rel_off;
1501618Srie 	pltent += 4;
1511618Srie 
1521618Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
1531618Srie 	pltent[0] = M_INST_JMP;
1541618Srie 	pltent++;
1551618Srie 	/* LINTED */
1561618Srie 	*(Word *)pltent = (Word)plt_off;
1571618Srie }
1581618Srie 
1591618Srie uintptr_t
1601618Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
1611618Srie {
1621618Srie 	Os_desc *	relosp, * osp = 0;
1631618Srie 	Word		ndx, roffset, value;
1641618Srie 	Rel		rea;
1651618Srie 	char		*relbits;
1661618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
1671618Srie 	int		sectmoved = 0;
1681618Srie 
1691618Srie 	sdp = orsp->rel_sym;
1701618Srie 
1711618Srie 	/*
1721618Srie 	 * If the section this relocation is against has been discarded
1731618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
1741618Srie 	 */
1751618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
1761618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
1771618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
1781618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
1791618Srie 		return (1);
1801618Srie 	}
1811618Srie 
1821618Srie 	/*
1831618Srie 	 * If this is a relocation against a move table, or expanded move
1841618Srie 	 * table, adjust the relocation entries.
1851618Srie 	 */
1861618Srie 	if (orsp->rel_move)
1871618Srie 		ld_adj_movereloc(ofl, orsp);
1881618Srie 
1891618Srie 	/*
1901618Srie 	 * If this is a relocation against a section using a partial initialized
1911618Srie 	 * symbol, adjust the embedded symbol info.
1921618Srie 	 *
1931618Srie 	 * The second argument of the am_I_partial() is the value stored at the
1941618Srie 	 * target address relocation is going to be applied.
1951618Srie 	 */
1961618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
1971618Srie 		if (ofl->ofl_parsym.head &&
1981618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1991618Srie 		    /* LINTED */
2001618Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2011618Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2021618Srie 		    orsp->rel_roffset)))) {
2031618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2041618Srie 			sectmoved = 1;
2054734Sab196087 		}
2061618Srie 	}
2071618Srie 
2081618Srie 	value = sdp->sd_sym->st_value;
2091618Srie 
2101618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2111618Srie 		osp = ofl->ofl_osgot;
2121618Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2131618Srie 
2141618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2151618Srie 		/*
2161618Srie 		 * Note that relocations for PLT's actually
2171618Srie 		 * cause a relocation againt the GOT.
2181618Srie 		 */
2191618Srie 		osp = ofl->ofl_osplt;
2201618Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2211618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2221618Srie 
2231618Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2241618Srie 
2251618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2261618Srie 		/*
2271618Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2281618Srie 		 * point to the new symbols location.
2291618Srie 		 */
2301618Srie 		osp = ofl->ofl_isbss->is_osdesc;
2311618Srie 		roffset = (Word)value;
2321618Srie 	} else {
2331618Srie 		osp = orsp->rel_osdesc;
2341618Srie 
2351618Srie 		/*
2361618Srie 		 * Calculate virtual offset of reference point; equals offset
2371618Srie 		 * into section + vaddr of section for loadable sections, or
2381618Srie 		 * offset plus section displacement for nonloadable sections.
2391618Srie 		 */
2401618Srie 		roffset = orsp->rel_roffset +
2411618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
2421618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2431618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
2441618Srie 			    os_shdr->sh_addr;
2451618Srie 	}
2461618Srie 
2471618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
2481618Srie 		relosp = ofl->ofl_osrel;
2491618Srie 
2501618Srie 	/*
2511618Srie 	 * Assign the symbols index for the output relocation.  If the
2521618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
2531618Srie 	 * the output sections symbols index.  Otherwise the index can be
2541618Srie 	 * derived from the symbols index itself.
2551618Srie 	 */
2561618Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
2571618Srie 		ndx = STN_UNDEF;
2581618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
2591618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2601618Srie 		if (sectmoved == 0) {
2611618Srie 			/*
2621618Srie 			 * Check for a null input section. This can
2631618Srie 			 * occur if this relocation references a symbol
2641618Srie 			 * generated by sym_add_sym().
2651618Srie 			 */
2661618Srie 			if ((sdp->sd_isc != 0) &&
2671618Srie 			    (sdp->sd_isc->is_osdesc != 0))
2681618Srie 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
2691618Srie 			else
2701618Srie 				ndx = sdp->sd_shndx;
2711618Srie 		} else
2721618Srie 			ndx = ofl->ofl_sunwdata1ndx;
2731618Srie 	} else
2741618Srie 		ndx = sdp->sd_symndx;
2751618Srie 
276*5892Sab196087 	/*
277*5892Sab196087 	 * If we have a replacement value for the relocation
278*5892Sab196087 	 * target, put it in place now.
279*5892Sab196087 	 */
280*5892Sab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
281*5892Sab196087 		Xword	addend = orsp->rel_raddend;
282*5892Sab196087 		uchar_t	*addr;
283*5892Sab196087 
284*5892Sab196087 		/*
285*5892Sab196087 		 * Get the address of the data item we need to modify.
286*5892Sab196087 		 */
287*5892Sab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
288*5892Sab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
289*5892Sab196087 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
290*5892Sab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
291*5892Sab196087 			return (S_ERROR);
292*5892Sab196087 	}
293*5892Sab196087 
2941618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
2951618Srie 
2961618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
2971618Srie 	rea.r_offset = roffset;
2981618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
2991618Srie 	    orsp->rel_sname));
3001618Srie 
3011618Srie 	/*
3021618Srie 	 * Assert we haven't walked off the end of our relocation table.
3031618Srie 	 */
3041618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3051618Srie 
3061618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3071618Srie 	    (char *)&rea, sizeof (Rel));
3081618Srie 	relosp->os_szoutrels += sizeof (Rel);
3091618Srie 
3101618Srie 	/*
3111618Srie 	 * Determine if this relocation is against a non-writable, allocatable
3121618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3131618Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3141618Srie 	 * result in modifications to the .got.
3151618Srie 	 */
3161618Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3171618Srie 		osp = ofl->ofl_osgot;
3181618Srie 
3191618Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3201618Srie 	return (1);
3211618Srie }
3221618Srie 
3231618Srie /*
3241618Srie  * i386 Instructions for TLS processing
3251618Srie  */
3261618Srie static uchar_t tlsinstr_gd_ie[] = {
3271618Srie 	/*
3281618Srie 	 * 0x00	movl %gs:0x0, %eax
3291618Srie 	 */
3301618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3311618Srie 	/*
3321618Srie 	 * 0x06	addl x(%eax), %eax
3331618Srie 	 * 0x0c ...
3341618Srie 	 */
3351618Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
3361618Srie };
3371618Srie 
3381618Srie static uchar_t tlsinstr_gd_le[] = {
3391618Srie 	/*
3401618Srie 	 * 0x00 movl %gs:0x0, %eax
3411618Srie 	 */
3421618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3431618Srie 	/*
3441618Srie 	 * 0x06 addl $0x0, %eax
3451618Srie 	 */
3461618Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
3471618Srie 	/*
3481618Srie 	 * 0x0b nop
3491618Srie 	 * 0x0c
3501618Srie 	 */
3511618Srie 	0x90
3521618Srie };
3531618Srie 
3541618Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
3551618Srie 	/*
3561618Srie 	 *	movl %gs:0x0,%eax
3571618Srie 	 */
3581618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
3591618Srie };
3601618Srie 
3611618Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
3621618Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
3631618Srie 
3641618Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
3651618Srie 
3661618Srie #define	TLS_NOP		0x90	/* NOP instruction */
3671618Srie 
3681618Srie #define	MODRM_MSK_MOD	0xc0
3691618Srie #define	MODRM_MSK_RO	0x38
3701618Srie #define	MODRM_MSK_RM	0x07
3711618Srie 
3721618Srie #define	SIB_MSK_SS	0xc0
3731618Srie #define	SIB_MSK_IND	0x38
3741618Srie #define	SIB_MSK_BS	0x07
3751618Srie 
3761618Srie static Fixupret
3771618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
3781618Srie {
3791618Srie 	Sym_desc	*sdp = arsp->rel_sym;
3801618Srie 	Word		rtype = arsp->rel_rtype;
3811618Srie 	uchar_t		*offset, r1, r2;
3821618Srie 
3831618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
3841618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
3851618Srie 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
3861618Srie 
3871618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
3881618Srie 		/*
3891618Srie 		 * IE reference model
3901618Srie 		 */
3911618Srie 		switch (rtype) {
3921618Srie 		case R_386_TLS_GD:
3931618Srie 			/*
3941618Srie 			 * Transition:
3951618Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
3961618Srie 			 *	0x7 call ___tls_get_addr
3971618Srie 			 *	0xc
3981618Srie 			 * To:
3991618Srie 			 *	0x0 movl %gs:0, %eax
4001618Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4011618Srie 			 */
4021618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4033304Srie 			    R_386_TLS_GOTIE, arsp));
4041618Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4051618Srie 			arsp->rel_roffset += 5;
4061618Srie 
4071618Srie 			/*
4083304Srie 			 * Adjust 'offset' to beginning of instruction
4091618Srie 			 * sequence.
4101618Srie 			 */
4111618Srie 			offset -= 3;
4121618Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4131618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4141618Srie 			    sizeof (tlsinstr_gd_ie));
4151618Srie 
4161618Srie 			/*
4171618Srie 			 * set register %r1 into the addl
4181618Srie 			 * instruction.
4191618Srie 			 */
4201618Srie 			offset[0x7] |= r1;
4211618Srie 			return (FIX_RELOC);
4221618Srie 
4231618Srie 		case R_386_TLS_GD_PLT:
4241618Srie 			/*
4251618Srie 			 * Fixup done via the TLS_GD relocation
4261618Srie 			 */
4271618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4283304Srie 			    R_386_NONE, arsp));
4291618Srie 			return (FIX_DONE);
4301618Srie 		}
4311618Srie 	}
4321618Srie 
4331618Srie 	/*
4341618Srie 	 * LE reference model
4351618Srie 	 */
4361618Srie 	switch (rtype) {
4371618Srie 	case R_386_TLS_GD:
4381618Srie 		/*
4391618Srie 		 * Transition:
4401618Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
4411618Srie 		 *	0x7 call ___tls_get_addr
4421618Srie 		 *	0xc
4431618Srie 		 * To:
4441618Srie 		 *	0x0 movl %gs:0, %eax
4451618Srie 		 *	0x6 addl $x@ntpoff, %eax
4461618Srie 		 *	0xb nop
4471618Srie 		 *	0xc
4481618Srie 		 */
4491618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4503304Srie 		    R_386_TLS_LE, arsp));
4511618Srie 
4521618Srie 		arsp->rel_rtype = R_386_TLS_LE;
4531618Srie 		arsp->rel_roffset += 4;
4541618Srie 
4551618Srie 		/*
4563304Srie 		 * Adjust 'offset' to beginning of instruction
4571618Srie 		 * sequence.
4581618Srie 		 */
4591618Srie 		offset -= 3;
4601618Srie 		(void) memcpy(offset, tlsinstr_gd_le,
4611618Srie 		    sizeof (tlsinstr_gd_le));
4621618Srie 		return (FIX_RELOC);
4631618Srie 
4641618Srie 	case R_386_TLS_GD_PLT:
4651618Srie 	case R_386_PLT32:
4661618Srie 		/*
4671618Srie 		 * Fixup done via the TLS_GD relocation
4681618Srie 		 */
4691618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4703304Srie 		    R_386_NONE, arsp));
4711618Srie 		return (FIX_DONE);
4721618Srie 
4731618Srie 	case R_386_TLS_LDM_PLT:
4741618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4753304Srie 		    R_386_NONE, arsp));
4761618Srie 
4771618Srie 		/*
4781618Srie 		 * Transition:
4791618Srie 		 *	call __tls_get_addr()
4801618Srie 		 * to:
4811618Srie 		 *	nop
4821618Srie 		 *	nop
4831618Srie 		 *	nop
4841618Srie 		 *	nop
4851618Srie 		 *	nop
4861618Srie 		 */
4871618Srie 		*(offset - 1) = TLS_NOP;
4881618Srie 		*(offset) = TLS_NOP;
4891618Srie 		*(offset + 1) = TLS_NOP;
4901618Srie 		*(offset + 2) = TLS_NOP;
4911618Srie 		*(offset + 3) = TLS_NOP;
4921618Srie 		return (FIX_DONE);
4931618Srie 
4941618Srie 	case R_386_TLS_LDM:
4951618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4963304Srie 		    R_386_NONE, arsp));
4971618Srie 
4981618Srie 		/*
4991618Srie 		 * Transition:
5001618Srie 		 *
5011618Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5021618Srie 		 *  0x06 call ___tls_get_addr
5031618Srie 		 *
5041618Srie 		 * to:
5051618Srie 		 *
5061618Srie 		 *  0x00 movl %gs:0, %eax
5071618Srie 		 */
5081618Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5091618Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5101618Srie 		return (FIX_DONE);
5111618Srie 
5121618Srie 	case R_386_TLS_LDO_32:
5131618Srie 		/*
5141618Srie 		 *  Instructions:
5151618Srie 		 *
5161618Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5171618Srie 		 *		to
5181618Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5191618Srie 		 *
5201618Srie 		 */
5211618Srie 		offset -= 2;
5221618Srie 
5231618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5243304Srie 		    R_386_TLS_LE, arsp));
5251618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5261618Srie 		return (FIX_RELOC);
5271618Srie 
5281618Srie 	case R_386_TLS_GOTIE:
5291618Srie 		/*
5301618Srie 		 * These transitions are a little different than the
5311618Srie 		 * others, in that we could have multiple instructions
5321618Srie 		 * pointed to by a single relocation.  Depending upon the
5331618Srie 		 * instruction, we perform a different code transition.
5341618Srie 		 *
5351618Srie 		 * Here's the known transitions:
5361618Srie 		 *
5371618Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
5381618Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5391618Srie 		 *
5401618Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
5411618Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5421618Srie 		 *
5431618Srie 		 *  Transitions IE -> LE
5441618Srie 		 *
5451618Srie 		 *  1) movl $foo@ntpoff, %reg2
5461618Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
5471618Srie 		 *
5481618Srie 		 *  2) addl $foo@ntpoff, %reg2
5491618Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
5501618Srie 		 *
5511618Srie 		 * Note: reg1 != 4 (%esp)
5521618Srie 		 */
5531618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5543304Srie 		    R_386_TLS_LE, arsp));
5551618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5561618Srie 
5571618Srie 		offset -= 2;
5581618Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
5591618Srie 		if (offset[0] == 0x8b) {
5601618Srie 			/* case 1 above */
5611618Srie 			offset[0] = 0xc7;	/* movl */
5621618Srie 			offset[1] = 0xc0 | r2;
5631618Srie 			return (FIX_RELOC);
5641618Srie 		}
5651618Srie 
5661618Srie 		if (offset[0] == 0x03) {
5671618Srie 			/* case 2 above */
5681618Srie 			assert(offset[0] == 0x03);
5691618Srie 			offset[0] = 0x81;	/* addl */
5701618Srie 			offset[1] = 0xc0 | r2;
5711618Srie 			return (FIX_RELOC);
5721618Srie 		}
5731618Srie 
5741618Srie 		/*
5751618Srie 		 * Unexpected instruction sequence - fatal error.
5761618Srie 		 */
5774734Sab196087 		{
5784734Sab196087 			Conv_inv_buf_t	inv_buf;
5794734Sab196087 
5804734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
5814734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
5824734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
5834734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
5844734Sab196087 			    demangle(arsp->rel_sname),
5854734Sab196087 			    arsp->rel_isdesc->is_name,
5864734Sab196087 			    EC_OFF(arsp->rel_roffset));
5874734Sab196087 		}
5881618Srie 		return (FIX_ERROR);
5891618Srie 
5901618Srie 	case R_386_TLS_IE:
5911618Srie 		/*
5921618Srie 		 * These transitions are a little different than the
5931618Srie 		 * others, in that we could have multiple instructions
5941618Srie 		 * pointed to by a single relocation.  Depending upon the
5951618Srie 		 * instruction, we perform a different code transition.
5961618Srie 		 *
5971618Srie 		 * Here's the known transitions:
5981618Srie 		 *  1) movl foo@indntpoff, %eax
5991618Srie 		 *	0xa1, foo@indntpoff
6001618Srie 		 *
6011618Srie 		 *  2) movl foo@indntpoff, %eax
6021618Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6031618Srie 		 *
6041618Srie 		 *  3) addl foo@indntpoff, %eax
6051618Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6061618Srie 		 *
6071618Srie 		 *  Transitions IE -> LE
6081618Srie 		 *
6091618Srie 		 *  1) movl $foo@ntpoff, %eax
6101618Srie 		 *	0xb8, foo@ntpoff
6111618Srie 		 *
6121618Srie 		 *  2) movl $foo@ntpoff, %reg
6131618Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6141618Srie 		 *
6151618Srie 		 *  3) addl $foo@ntpoff, %reg
6161618Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6171618Srie 		 */
6181618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6191618Srie 		offset--;
6201618Srie 		if (offset[0] == 0xa1) {
6211618Srie 			/* case 1 above */
6221618Srie 			offset[0] = 0xb8;	/*  movl */
6231618Srie 			return (FIX_RELOC);
6241618Srie 		}
6251618Srie 
6261618Srie 		offset--;
6271618Srie 		if (offset[0] == 0x8b) {
6281618Srie 			/* case 2 above */
6291618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6301618Srie 			offset[0] = 0xc7;	/* movl */
6311618Srie 			offset[1] = 0xc0 | r2;
6321618Srie 			return (FIX_RELOC);
6331618Srie 		}
6341618Srie 		if (offset[0] == 0x03) {
6351618Srie 			/* case 3 above */
6361618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6371618Srie 			offset[0] = 0x81;	/* addl */
6381618Srie 			offset[1] = 0xc0 | r2;
6391618Srie 			return (FIX_RELOC);
6401618Srie 		}
6411618Srie 		/*
6421618Srie 		 * Unexpected instruction sequence - fatal error.
6431618Srie 		 */
6444734Sab196087 		{
6454734Sab196087 			Conv_inv_buf_t	inv_buf;
6464734Sab196087 
6474734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6484734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6494734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6504734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6514734Sab196087 			    demangle(arsp->rel_sname),
6524734Sab196087 			    arsp->rel_isdesc->is_name,
6534734Sab196087 			    EC_OFF(arsp->rel_roffset));
6544734Sab196087 		}
6551618Srie 		return (FIX_ERROR);
6561618Srie 	}
6571618Srie 	return (FIX_RELOC);
6581618Srie }
6591618Srie 
6601618Srie uintptr_t
6611618Srie ld_do_activerelocs(Ofl_desc *ofl)
6621618Srie {
6631618Srie 	Rel_desc	*arsp;
6641618Srie 	Rel_cache	*rcp;
6651618Srie 	Listnode	*lnp;
6661618Srie 	uintptr_t	return_code = 1;
6671618Srie 	Word		flags = ofl->ofl_flags;
6681618Srie 
6692647Srie 	if (ofl->ofl_actrels.head)
6702647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6712647Srie 
6721618Srie 	/*
6731618Srie 	 * Process active relocations.
6741618Srie 	 */
6751618Srie 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6761618Srie 		/* LINTED */
6771618Srie 		for (arsp = (Rel_desc *)(rcp + 1);
6781618Srie 		    arsp < rcp->rc_free; arsp++) {
6791618Srie 			uchar_t		*addr;
6801618Srie 			Xword 		value;
6811618Srie 			Sym_desc	*sdp;
6821618Srie 			const char	*ifl_name;
6831618Srie 			Xword		refaddr;
6841618Srie 			int		moved = 0;
6851618Srie 			Gotref		gref;
6861618Srie 
6871618Srie 			/*
6881618Srie 			 * If the section this relocation is against has been
6891618Srie 			 * discarded (-zignore), then discard (skip) the
6901618Srie 			 * relocation itself.
6911618Srie 			 */
6921618Srie 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6931618Srie 			    ((arsp->rel_flags &
6941618Srie 			    (FLG_REL_GOT | FLG_REL_BSS |
6951618Srie 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6961618Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6971618Srie 				    M_MACH, arsp));
6981618Srie 				continue;
6991618Srie 			}
7001618Srie 
7011618Srie 			/*
7021618Srie 			 * We deteremine what the 'got reference'
7031618Srie 			 * model (if required) is at this point.  This
7041618Srie 			 * needs to be done before tls_fixup() since
7051618Srie 			 * it may 'transition' our instructions.
7061618Srie 			 *
7071618Srie 			 * The got table entries have already been assigned,
7081618Srie 			 * and we bind to those initial entries.
7091618Srie 			 */
7101618Srie 			if (arsp->rel_flags & FLG_REL_DTLS)
7111618Srie 				gref = GOT_REF_TLSGD;
7121618Srie 			else if (arsp->rel_flags & FLG_REL_MTLS)
7131618Srie 				gref = GOT_REF_TLSLD;
7141618Srie 			else if (arsp->rel_flags & FLG_REL_STLS)
7151618Srie 				gref = GOT_REF_TLSIE;
7161618Srie 			else
7171618Srie 				gref = GOT_REF_GENERIC;
7181618Srie 
7191618Srie 			/*
7201618Srie 			 * Perform any required TLS fixups.
7211618Srie 			 */
7221618Srie 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7231618Srie 				Fixupret	ret;
7241618Srie 
7251618Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7261618Srie 					return (S_ERROR);
7271618Srie 				if (ret == FIX_DONE)
7281618Srie 					continue;
7291618Srie 			}
7301618Srie 
7311618Srie 			/*
7321618Srie 			 * If this is a relocation against a move table, or
7331618Srie 			 * expanded move table, adjust the relocation entries.
7341618Srie 			 */
7351618Srie 			if (arsp->rel_move)
7361618Srie 				ld_adj_movereloc(ofl, arsp);
7371618Srie 
7381618Srie 			sdp = arsp->rel_sym;
7391618Srie 			refaddr = arsp->rel_roffset +
7401618Srie 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7411618Srie 
7421618Srie 			if (arsp->rel_flags & FLG_REL_CLVAL)
7431618Srie 				value = 0;
7441618Srie 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7451618Srie 			    STT_SECTION) {
7461618Srie 				Sym_desc	*sym;
7471618Srie 
7481618Srie 				/*
7491618Srie 				 * The value for a symbol pointing to a SECTION
7501618Srie 				 * is based off of that sections position.
7511618Srie 				 *
7521618Srie 				 * The second argument of the ld_am_I_partial()
7531618Srie 				 * is the value stored at the target address
7541618Srie 				 * relocation is going to be applied.
7551618Srie 				 */
7561618Srie 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7571618Srie 				    /* LINTED */
7581618Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
7591618Srie 				    ((uchar_t *)
7601618Srie 				    arsp->rel_isdesc->is_indata->d_buf +
7611618Srie 				    arsp->rel_roffset)))) {
7621618Srie 					/*
7631618Srie 					 * If the symbol is moved,
7641618Srie 					 * adjust the value
7651618Srie 					 */
7661618Srie 					value = sym->sd_sym->st_value;
7671618Srie 					moved = 1;
7681618Srie 				} else {
7691618Srie 					value = _elf_getxoff(
7701618Srie 					    sdp->sd_isc->is_indata);
7711618Srie 					if (sdp->sd_isc->is_shdr->sh_flags &
7721618Srie 					    SHF_ALLOC)
7731618Srie 						value += sdp->sd_isc->
7741618Srie 						    is_osdesc->os_shdr->sh_addr;
7751618Srie 				}
7761618Srie 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7771618Srie 					value -= ofl->ofl_tlsphdr->p_vaddr;
7782850Srie 
7792850Srie 			} else if (IS_SIZE(arsp->rel_rtype)) {
7802850Srie 				/*
7812850Srie 				 * Size relocations require the symbols size.
7822850Srie 				 */
7832850Srie 				value = sdp->sd_sym->st_size;
7841618Srie 			} else {
7851618Srie 				/*
7862647Srie 				 * Else the value is the symbols value.
7871618Srie 				 */
7881618Srie 				value = sdp->sd_sym->st_value;
7891618Srie 			}
7901618Srie 
7911618Srie 			/*
7921618Srie 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7931618Srie 			 */
7941618Srie 			if (arsp->rel_flags & FLG_REL_GOT)
7951618Srie 				arsp->rel_osdesc = ofl->ofl_osgot;
7961618Srie 
7971618Srie 			/*
7981618Srie 			 * If loadable and not producing a relocatable object
7991618Srie 			 * add the sections virtual address to the reference
8001618Srie 			 * address.
8011618Srie 			 */
8021618Srie 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
8031618Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8041618Srie 				refaddr += arsp->rel_isdesc->is_osdesc->
8051618Srie 				    os_shdr->sh_addr;
8061618Srie 
8071618Srie 			/*
8081618Srie 			 * If this entry has a PLT assigned to it, it's
8091618Srie 			 * value is actually the address of the PLT (and
8101618Srie 			 * not the address of the function).
8111618Srie 			 */
8121618Srie 			if (IS_PLT(arsp->rel_rtype)) {
8131618Srie 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8141618Srie 					value = ld_calc_plt_addr(sdp, ofl);
8151618Srie 			}
8161618Srie 
8171618Srie 			/*
8181618Srie 			 * Determine whether the value needs further adjustment.
8191618Srie 			 * Filter through the attributes of the relocation to
8201618Srie 			 * determine what adjustment is required.  Note, many
8211618Srie 			 * of the following cases are only applicable when a
8221618Srie 			 * .got is present.  As a .got is not generated when a
8231618Srie 			 * relocatable object is being built, any adjustments
8241618Srie 			 * that require a .got need to be skipped.
8251618Srie 			 */
8261618Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
8271618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8281618Srie 				Xword		R1addr;
8291618Srie 				uintptr_t	R2addr;
8301618Srie 				Word		gotndx;
8311618Srie 				Gotndx		*gnp;
8321618Srie 
8331618Srie 				/*
8341618Srie 				 * Perform relocation against GOT table.  Since
8351618Srie 				 * this doesn't fit exactly into a relocation
8361618Srie 				 * we place the appropriate byte in the GOT
8371618Srie 				 * directly
8381618Srie 				 *
8391618Srie 				 * Calculate offset into GOT at which to apply
8401618Srie 				 * the relocation.
8411618Srie 				 */
8421618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8431618Srie 				    ofl, 0);
8441618Srie 				assert(gnp);
8451618Srie 
8461618Srie 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
8471618Srie 					gotndx = gnp->gn_gotndx + 1;
8481618Srie 				else
8491618Srie 					gotndx = gnp->gn_gotndx;
8501618Srie 
8511618Srie 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8521618Srie 
8531618Srie 				/*
8541618Srie 				 * Add the GOTs data's offset.
8551618Srie 				 */
8561618Srie 				R2addr = R1addr + (uintptr_t)
8571618Srie 				    arsp->rel_osdesc->os_outdata->d_buf;
8581618Srie 
8591618Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8601618Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
8611618Srie 				    arsp->rel_rtype, R1addr, value,
8621618Srie 				    arsp->rel_sname, arsp->rel_osdesc));
8631618Srie 
8641618Srie 				/*
8651618Srie 				 * And do it.
8661618Srie 				 */
8675189Sab196087 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
8685189Sab196087 					*(Xword *)R2addr =
8695189Sab196087 					    ld_byteswap_Xword(value);
8705189Sab196087 				else
8715189Sab196087 					*(Xword *)R2addr = value;
8721618Srie 				continue;
8731618Srie 
8741618Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
8751618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8761618Srie 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
8771618Srie 
8781618Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
8791618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8801618Srie 				value =
8811618Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
8821618Srie 				    refaddr;
8831618Srie 
8841618Srie 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
8851618Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
8861618Srie 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
8871618Srie 				value -= refaddr;
8881618Srie 
8891618Srie 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
8901618Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
8911618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8921618Srie 				Gotndx	*gnp;
8931618Srie 
8941618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8951618Srie 				    ofl, 0);
8961618Srie 				assert(gnp);
8971618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
8981618Srie 				if (arsp->rel_rtype == R_386_TLS_IE) {
8991618Srie 					value +=
9001618Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9011618Srie 				}
9021618Srie 
9031618Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
9041618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9051618Srie 				Gotndx *gnp;
9061618Srie 
9071618Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9081618Srie 				    GOT_REF_GENERIC, ofl, 0);
9091618Srie 				assert(gnp);
9101618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9111618Srie 
9121618Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
9131618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9141618Srie 				Xword	tlsstatsize;
9151618Srie 
9161618Srie 				/*
9171618Srie 				 * This is the LE TLS reference model.  Static
9181618Srie 				 * offset is hard-coded.
9191618Srie 				 */
9201618Srie 				tlsstatsize =
9211618Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9221618Srie 				    M_TLSSTATALIGN);
9231618Srie 				value = tlsstatsize - value;
9241618Srie 
9251618Srie 				/*
9261618Srie 				 * Since this code is fixed up, it assumes a
9271618Srie 				 * negative offset that can be added to the
9281618Srie 				 * thread pointer.
9291618Srie 				 */
9301618Srie 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9311618Srie 				    (arsp->rel_rtype == R_386_TLS_LE))
9321618Srie 					value = -value;
9331618Srie 			}
9341618Srie 
9351618Srie 			if (arsp->rel_isdesc->is_file)
9361618Srie 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9371618Srie 			else
9381618Srie 				ifl_name = MSG_INTL(MSG_STR_NULL);
9391618Srie 
9401618Srie 			/*
9411618Srie 			 * Make sure we have data to relocate.  Compiler and
9421618Srie 			 * assembler developers have been known to generate
9431618Srie 			 * relocations against invalid sections (normally .bss),
9441618Srie 			 * so for their benefit give them sufficient information
9451618Srie 			 * to help analyze the problem.  End users should never
9461618Srie 			 * see this.
9471618Srie 			 */
9481618Srie 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
9494734Sab196087 				Conv_inv_buf_t	inv_buf;
9504734Sab196087 
9511618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9521618Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
9534734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
9544734Sab196087 				    0, &inv_buf),
9551618Srie 				    ifl_name, demangle(arsp->rel_sname),
9561618Srie 				    arsp->rel_isdesc->is_name);
9571618Srie 				return (S_ERROR);
9581618Srie 			}
9591618Srie 
9601618Srie 			/*
9611618Srie 			 * Get the address of the data item we need to modify.
9621618Srie 			 */
9631618Srie 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
9641618Srie 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
9651618Srie 			    is_indata));
9661618Srie 
9671618Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9681618Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
9691618Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9701618Srie 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9711618Srie 
9721618Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9731618Srie 			    ofl->ofl_size) || (arsp->rel_roffset >
9741618Srie 			    arsp->rel_osdesc->os_shdr->sh_size)) {
9754734Sab196087 				Conv_inv_buf_t	inv_buf;
9764734Sab196087 				int		class;
9771618Srie 
9781618Srie 				if (((uintptr_t)addr -
9791618Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
9801618Srie 					class = ERR_FATAL;
9811618Srie 				else
9821618Srie 					class = ERR_WARNING;
9831618Srie 
9841618Srie 				eprintf(ofl->ofl_lml, class,
9851618Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
9864734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
9874734Sab196087 				    0, &inv_buf),
9881618Srie 				    ifl_name, arsp->rel_isdesc->is_name,
9891618Srie 				    demangle(arsp->rel_sname),
9901618Srie 				    EC_ADDR((uintptr_t)addr -
9911618Srie 				    (uintptr_t)ofl->ofl_nehdr));
9921618Srie 
9931618Srie 				if (class == ERR_FATAL) {
9941618Srie 					return_code = S_ERROR;
9951618Srie 					continue;
9961618Srie 				}
9971618Srie 			}
9981618Srie 
9991618Srie 			/*
10001618Srie 			 * The relocation is additive.  Ignore the previous
10011618Srie 			 * symbol value if this local partial symbol is
10021618Srie 			 * expanded.
10031618Srie 			 */
10041618Srie 			if (moved)
10051618Srie 				value -= *addr;
10061618Srie 
10071618Srie 			/*
1008*5892Sab196087 			 * If we have a replacement value for the relocation
1009*5892Sab196087 			 * target, put it in place now.
1010*5892Sab196087 			 */
1011*5892Sab196087 			if (arsp->rel_flags & FLG_REL_NADDEND) {
1012*5892Sab196087 				Xword addend = arsp->rel_raddend;
1013*5892Sab196087 
1014*5892Sab196087 				if (ld_reloc_targval_set(ofl, arsp,
1015*5892Sab196087 				    addr, addend) == 0)
1016*5892Sab196087 					return (S_ERROR);
1017*5892Sab196087 			}
1018*5892Sab196087 
1019*5892Sab196087 			/*
10205189Sab196087 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10211618Srie 			 * stage.
10221618Srie 			 */
10235189Sab196087 			if (OFL_DO_RELOC(ofl)) {
10245189Sab196087 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10251618Srie 				    &value, arsp->rel_sname, ifl_name,
10265189Sab196087 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10271618Srie 				    ofl->ofl_lml) == 0)
10281618Srie 					return_code = S_ERROR;
10291618Srie 			}
10301618Srie 		}
10311618Srie 	}
10321618Srie 	return (return_code);
10331618Srie }
10341618Srie 
10351618Srie /*
10361618Srie  * Add an output relocation record.
10371618Srie  */
10381618Srie uintptr_t
10391618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10401618Srie {
10411618Srie 	Rel_desc	*orsp;
10421618Srie 	Rel_cache	*rcp;
10431618Srie 	Sym_desc	*sdp = rsp->rel_sym;
10441618Srie 
10451618Srie 	/*
10461618Srie 	 * Static executables *do not* want any relocations against them.
10471618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10481618Srie 	 * symbol in a static executable, it's best to disable them here
10491618Srie 	 * instead of through out the relocation code.
10501618Srie 	 */
10511618Srie 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
10521618Srie 	    (FLG_OF_STATIC | FLG_OF_EXEC))
10531618Srie 		return (1);
10541618Srie 
10551618Srie 	/*
10561618Srie 	 * If no relocation cache structures are available allocate
10571618Srie 	 * a new one and link it into the cache list.
10581618Srie 	 */
10591618Srie 	if ((ofl->ofl_outrels.tail == 0) ||
10601618Srie 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10611618Srie 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10621618Srie 		static size_t	nextsize = 0;
10631618Srie 		size_t		size;
10641618Srie 
10651618Srie 		/*
10661618Srie 		 * Output relocation numbers can vary considerably between
10671618Srie 		 * building executables or shared objects (pic vs. non-pic),
10681618Srie 		 * etc.  But, they typically aren't very large, so for these
10691618Srie 		 * objects use a standard bucket size.  For building relocatable
10701618Srie 		 * objects, typically there will be an output relocation for
10711618Srie 		 * every input relocation.
10721618Srie 		 */
10731618Srie 		if (nextsize == 0) {
10741618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10751618Srie 				if ((size = ofl->ofl_relocincnt) == 0)
10761618Srie 					size = REL_LOIDESCNO;
10771618Srie 				if (size > REL_HOIDESCNO)
10781618Srie 					nextsize = REL_HOIDESCNO;
10791618Srie 				else
10801618Srie 					nextsize = REL_LOIDESCNO;
10811618Srie 			} else
10821618Srie 				nextsize = size = REL_HOIDESCNO;
10831618Srie 		} else
10841618Srie 			size = nextsize;
10851618Srie 
10861618Srie 		size = size * sizeof (Rel_desc);
10871618Srie 
10881618Srie 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
10891618Srie 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
10901618Srie 			return (S_ERROR);
10911618Srie 
10921618Srie 		/* LINTED */
10931618Srie 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
10941618Srie 		/* LINTED */
10951618Srie 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
10961618Srie 	}
10971618Srie 
10981618Srie 	/*
10991618Srie 	 * If we are adding a output relocation against a section
11001618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11011618Srie 	 * will be added to the .dynsym symbol table.
11021618Srie 	 */
11031618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11041618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11051618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11061618Srie 
11071618Srie 		/*
11081618Srie 		 * If this is a COMMON symbol - no output section
11091618Srie 		 * exists yet - (it's created as part of sym_validate()).
11101618Srie 		 * So - we mark here that when it's created it should
11111618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11121618Srie 		 */
11131618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11141682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11151618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11161618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11171618Srie 			else
11181618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11191618Srie 		} else {
11201618Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11211618Srie 
11222347Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11231618Srie 				ofl->ofl_dynshdrcnt++;
11241618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11251618Srie 			}
11261618Srie 		}
11271618Srie 	}
11281618Srie 
11291618Srie 	*orsp = *rsp;
11301618Srie 	orsp->rel_flags |= flags;
11311618Srie 
11321618Srie 	rcp->rc_free++;
11331618Srie 	ofl->ofl_outrelscnt++;
11341618Srie 
11351618Srie 	if (flags & FLG_REL_GOT)
11361618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11371618Srie 	else if (flags & FLG_REL_PLT)
11381618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11391618Srie 	else if (flags & FLG_REL_BSS)
11401618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11411618Srie 	else if (flags & FLG_REL_NOINFO)
11421618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11431618Srie 	else
11441618Srie 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
11451618Srie 
11461618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11471618Srie 		ofl->ofl_relocrelcnt++;
11481618Srie 
11491618Srie 	/*
11501618Srie 	 * We don't perform sorting on PLT relocations because
11511618Srie 	 * they have already been assigned a PLT index and if we
11521618Srie 	 * were to sort them we would have to re-assign the plt indexes.
11531618Srie 	 */
11541618Srie 	if (!(flags & FLG_REL_PLT))
11551618Srie 		ofl->ofl_reloccnt++;
11561618Srie 
11571618Srie 	/*
11581618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11591618Srie 	 */
11601618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11611618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11621618Srie 
11631618Srie 	/*
11641618Srie 	 * Identify and possibly warn of a displacement relocation.
11651618Srie 	 */
11661618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11671618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11681618Srie 
11691618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11701618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11711618Srie 	}
11721618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11731618Srie 	    M_MACH, orsp));
11741618Srie 	return (1);
11751618Srie }
11761618Srie 
11771618Srie /*
11781618Srie  * Stub routine since register symbols are not supported on i386.
11791618Srie  */
11801618Srie /* ARGSUSED */
11811618Srie uintptr_t
11821618Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
11831618Srie {
11841618Srie 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
11851618Srie 	return (S_ERROR);
11861618Srie }
11871618Srie 
11881618Srie /*
11891618Srie  * process relocation for a LOCAL symbol
11901618Srie  */
11911618Srie uintptr_t
11921618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11931618Srie {
11941618Srie 	Word		flags = ofl->ofl_flags;
11951618Srie 	Sym_desc	*sdp = rsp->rel_sym;
11961682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11971618Srie 
11981618Srie 	/*
11991618Srie 	 * if ((shared object) and (not pc relative relocation) and
12001618Srie 	 *    (not against ABS symbol))
12011618Srie 	 * then
12021618Srie 	 *	build R_386_RELATIVE
12031618Srie 	 * fi
12041618Srie 	 */
12051618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12062850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12071618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12081618Srie 	    !(rsp->rel_isdesc != NULL &&
12091618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12101618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12111618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12121618Srie 		Word	ortype = rsp->rel_rtype;
12131618Srie 
12141618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12151618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12161618Srie 			return (S_ERROR);
12171618Srie 		rsp->rel_rtype = ortype;
12181618Srie 	}
12191618Srie 
12201618Srie 	/*
12211618Srie 	 * If the relocation is against a 'non-allocatable' section
12221618Srie 	 * and we can not resolve it now - then give a warning
12231618Srie 	 * message.
12241618Srie 	 *
12251618Srie 	 * We can not resolve the symbol if either:
12261618Srie 	 *	a) it's undefined
12271618Srie 	 *	b) it's defined in a shared library and a
12281618Srie 	 *	   COPY relocation hasn't moved it to the executable
12291618Srie 	 *
12301618Srie 	 * Note: because we process all of the relocations against the
12311618Srie 	 *	text segment before any others - we know whether
12321618Srie 	 *	or not a copy relocation will be generated before
12331618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12341618Srie 	 */
12351618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12361618Srie 	    ((shndx == SHN_UNDEF) ||
12371618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12381618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
12394734Sab196087 		Conv_inv_buf_t inv_buf;
12404734Sab196087 
12411618Srie 		/*
12421618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12431618Srie 		 * section - then silently ignore that the relocation
12441618Srie 		 * can not be resolved.
12451618Srie 		 */
12461618Srie 		if (rsp->rel_osdesc &&
12471618Srie 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12481618Srie 			return (0);
12491618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12504734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12511618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
12521618Srie 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12531618Srie 		return (1);
12541618Srie 	}
12551618Srie 
12561618Srie 	/*
12571618Srie 	 * Perform relocation.
12581618Srie 	 */
12591618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12601618Srie }
12611618Srie 
12621618Srie uintptr_t
12631618Srie /* ARGSUSED */
12641618Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12651618Srie {
12661618Srie 	/*
12671618Srie 	 * Stub routine for common code compatibility, we shouldn't
12681618Srie 	 * actually get here on x86.
12691618Srie 	 */
12701618Srie 	assert(0);
12711618Srie 	return (S_ERROR);
12721618Srie }
12731618Srie 
12741618Srie uintptr_t
12751618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12761618Srie {
12771618Srie 	Word		rtype = rsp->rel_rtype;
12781618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12791618Srie 	Word		flags = ofl->ofl_flags;
12801618Srie 	Gotndx		*gnp;
12811618Srie 
12821618Srie 	/*
12832145Srie 	 * If we're building an executable - use either the IE or LE access
12842145Srie 	 * model.  If we're building a shared object process any IE model.
12851618Srie 	 */
12862145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12871618Srie 		/*
12882145Srie 		 * Set the DF_STATIC_TLS flag.
12891618Srie 		 */
12901618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12911618Srie 
12922145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12931618Srie 			/*
12942145Srie 			 * Assign a GOT entry for static TLS references.
12951618Srie 			 */
12962145Srie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12972145Srie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
12982145Srie 
12992145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
13002145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
13012145Srie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
13022145Srie 					return (S_ERROR);
13031618Srie 			}
13041618Srie 
13052145Srie 			/*
13062145Srie 			 * IE access model.
13072145Srie 			 */
13082145Srie 			if (IS_TLS_IE(rtype)) {
13092145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13102145Srie 				    rsp, ofl) == S_ERROR)
13112145Srie 					return (S_ERROR);
13122145Srie 
13132145Srie 				/*
13142145Srie 				 * A non-pic shared object needs to adjust the
13152145Srie 				 * active relocation (indntpoff).
13162145Srie 				 */
13172145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13182145Srie 				    (rtype == R_386_TLS_IE)) {
13192145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13202145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13212145Srie 				}
13222145Srie 				return (1);
13232145Srie 			}
13241618Srie 
13251618Srie 			/*
13262145Srie 			 * Fixups are required for other executable models.
13271618Srie 			 */
13281618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13291618Srie 			    rsp, ofl));
13301618Srie 		}
13312145Srie 
13321618Srie 		/*
13332145Srie 		 * LE access model.
13341618Srie 		 */
13351618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13361618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13371618Srie 
13381618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13391618Srie 		    rsp, ofl));
13401618Srie 	}
13411618Srie 
13421618Srie 	/*
13432145Srie 	 * Building a shared object.
13442145Srie 	 *
13452145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13461618Srie 	 */
13472145Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13482145Srie 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
13492145Srie 
13502145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
13512145Srie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13522145Srie 			return (S_ERROR);
13532145Srie 
13542145Srie 	} else if (IS_TLS_GD(rtype) &&
13552145Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13562145Srie 	    ofl, 0)) == 0)) {
13572145Srie 
13582145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
13592145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
13602145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13612145Srie 			return (S_ERROR);
13621618Srie 	}
13631618Srie 
13641618Srie 	/*
13651618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
13662145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
13672145Srie 	 * symbol now, and prepare for the PLT magic.
13681618Srie 	 */
13691618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
13703862Srie 		Sym_desc	*tlsgetsym;
13711618Srie 
13721618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
13733862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13741618Srie 			return (S_ERROR);
13752145Srie 
13761618Srie 		rsp->rel_sym = tlsgetsym;
13771618Srie 		rsp->rel_sname = tlsgetsym->sd_name;
13781618Srie 		rsp->rel_rtype = R_386_PLT32;
13792145Srie 
13801618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13811618Srie 			return (S_ERROR);
13822145Srie 
13831618Srie 		rsp->rel_sym = sdp;
13841618Srie 		rsp->rel_sname = sdp->sd_name;
13851618Srie 		rsp->rel_rtype = rtype;
13861618Srie 		return (1);
13871618Srie 	}
13881618Srie 
13891618Srie 	if (IS_TLS_LD(rtype))
13901618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13911618Srie 
13921618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13931618Srie }
13941618Srie 
13951618Srie /* ARGSUSED3 */
13961618Srie Gotndx *
13971618Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13981618Srie {
13991618Srie 	Listnode *	lnp;
14001618Srie 	Gotndx *	gnp;
14011618Srie 
14021618Srie 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14031618Srie 		return (ofl->ofl_tlsldgotndx);
14041618Srie 
14051618Srie 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14061618Srie 		if (gnp->gn_gotref == gref)
14071618Srie 			return (gnp);
14081618Srie 	}
14091618Srie 	return ((Gotndx *)0);
14101618Srie }
14111618Srie 
14121618Srie Xword
14131618Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14141618Srie {
14151618Srie 	Os_desc		*osp = ofl->ofl_osgot;
14161618Srie 	Sym_desc	*sdp = rdesc->rel_sym;
14171618Srie 	Xword		gotndx;
14181618Srie 	Gotref		gref;
14191618Srie 	Gotndx		*gnp;
14201618Srie 
14211618Srie 	if (rdesc->rel_flags & FLG_REL_DTLS)
14221618Srie 		gref = GOT_REF_TLSGD;
14231618Srie 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14241618Srie 		gref = GOT_REF_TLSLD;
14251618Srie 	else if (rdesc->rel_flags & FLG_REL_STLS)
14261618Srie 		gref = GOT_REF_TLSIE;
14271618Srie 	else
14281618Srie 		gref = GOT_REF_GENERIC;
14291618Srie 
14301618Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14311618Srie 	assert(gnp);
14321618Srie 
14331618Srie 	gotndx = (Xword)gnp->gn_gotndx;
14341618Srie 
14351618Srie 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14361618Srie 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14371618Srie 		gotndx++;
14381618Srie 
14391618Srie 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14401618Srie }
14411618Srie 
14421618Srie 
14431618Srie /* ARGSUSED4 */
14441618Srie uintptr_t
14452145Srie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14461618Srie     Rel_desc * rsp, Sym_desc * sdp)
14471618Srie {
14481618Srie 	Gotndx	*gnp;
14491618Srie 	uint_t	gotents;
14501618Srie 
14511618Srie 	if (pgnp)
14521618Srie 		return (1);
14531618Srie 
14541618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14551618Srie 		gotents = 2;
14561618Srie 	else
14571618Srie 		gotents = 1;
14581618Srie 
14591618Srie 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14601618Srie 		return (S_ERROR);
14611618Srie 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14621618Srie 	gnp->gn_gotref = gref;
14631618Srie 
14641618Srie 	ofl->ofl_gotcnt += gotents;
14651618Srie 
14661618Srie 	if (gref == GOT_REF_TLSLD) {
14671618Srie 		ofl->ofl_tlsldgotndx = gnp;
14681618Srie 		return (1);
14691618Srie 	}
14701618Srie 
14711618Srie 	if (list_appendc(lst, (void *)gnp) == 0)
14721618Srie 		return (S_ERROR);
14731618Srie 
14741618Srie 	return (1);
14751618Srie }
14761618Srie 
14771618Srie void
14781618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14791618Srie {
14801618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14811618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14821618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14831618Srie }
14841618Srie 
14851618Srie /*
14861618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14871618Srie  */
14881618Srie uintptr_t
14892145Srie ld_fillin_gotplt(Ofl_desc *ofl)
14901618Srie {
14912145Srie 	Word	flags = ofl->ofl_flags;
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;
15041618Srie 		}
15051618Srie 	}
15061618Srie 
15071618Srie 	/*
15081618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15091618Srie 	 * entry is:
15101618Srie 	 *  if (building a.out) {
15111618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
15121618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
15131618Srie 	 *  } else {
15141618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15151618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15161618Srie 	 *  }
15171618Srie 	 */
15182145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15191618Srie 		uchar_t *pltent;
15201618Srie 
15211618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15222145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
15231618Srie 			pltent[0] = M_SPECIAL_INST;
15241618Srie 			pltent[1] = M_PUSHL_DISP;
15251618Srie 			pltent += 2;
15261618Srie 			/* LINTED */
15271618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15284734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
15291618Srie 			pltent += 4;
15301618Srie 			pltent[0] = M_SPECIAL_INST;
15311618Srie 			pltent[1] = M_JMP_DISP_IND;
15321618Srie 			pltent += 2;
15331618Srie 			/* LINTED */
15341618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15354734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15361618Srie 		} else {
15371618Srie 			pltent[0] = M_SPECIAL_INST;
15381618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15391618Srie 			pltent += 2;
15401618Srie 			/* LINTED */
15411618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15424734Sab196087 			    M_GOT_ENTSIZE);
15431618Srie 			pltent += 4;
15441618Srie 			pltent[0] = M_SPECIAL_INST;
15451618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15461618Srie 			pltent += 2;
15471618Srie 			/* LINTED */
15481618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15494734Sab196087 			    M_GOT_ENTSIZE);
15501618Srie 		}
15511618Srie 	}
15521618Srie 	return (1);
15531618Srie }
1554