17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21141040e8Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
247c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
257c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
267c478bd9Sstevel@tonic-gate  *
27*cce0e03bSab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	<string.h>
337c478bd9Sstevel@tonic-gate #include	<stdio.h>
347c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
355aefb655Srie #include	<debug.h>
365aefb655Srie #include	<reloc.h>
377c478bd9Sstevel@tonic-gate #include	"msg.h"
387c478bd9Sstevel@tonic-gate #include	"_libld.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate Word
415aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	Rel *	rel = (Rel *)reloc;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	/* LINTED */
467c478bd9Sstevel@tonic-gate 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info);
477c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
487c478bd9Sstevel@tonic-gate 	reld->rel_raddend = 0;
497c478bd9Sstevel@tonic-gate 	reld->rel_typedata = 0;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void
555aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
567c478bd9Sstevel@tonic-gate {
575aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
615aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
647c478bd9Sstevel@tonic-gate 		/*
657c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
667c478bd9Sstevel@tonic-gate 		 */
677c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
687c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate void
73d326b23bSrie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
747c478bd9Sstevel@tonic-gate {
755aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
765aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
775aefb655Srie 		if (ofl->ofl_osgot)
785aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
795aefb655Srie 		else
805aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
815aefb655Srie 		(*dyn)++;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate Xword
865aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	Xword	value;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
917c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
927c478bd9Sstevel@tonic-gate 	return (value);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
977c478bd9Sstevel@tonic-gate  *	if (building a.out)
987c478bd9Sstevel@tonic-gate  *		JMP	*got_off
997c478bd9Sstevel@tonic-gate  *	else
1007c478bd9Sstevel@tonic-gate  *		JMP	*got_off@GOT(%ebx)
1017c478bd9Sstevel@tonic-gate  *	PUSHL	&rel_off
1027c478bd9Sstevel@tonic-gate  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1057c478bd9Sstevel@tonic-gate  *	so the first pass through the plt jumps back here, jumping
1067c478bd9Sstevel@tonic-gate  *	in turn to the first plt entry, which jumps to the dynamic
1077c478bd9Sstevel@tonic-gate  *	linker.	 The dynamic linker then patches the GOT, rerouting
1087c478bd9Sstevel@tonic-gate  *	future plt calls to the proper destination.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate static void
1117c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1127c478bd9Sstevel@tonic-gate {
113b3fbe5e6Sseizo 	uchar_t		*pltent, *gotent;
1147c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1157c478bd9Sstevel@tonic-gate 	Word		got_off;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1187c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1197c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
120b3fbe5e6Sseizo 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
121b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	/* LINTED */
1277c478bd9Sstevel@tonic-gate 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
128b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1317c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1327c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_DISP_IND;
1337c478bd9Sstevel@tonic-gate 		pltent += 2;
1347c478bd9Sstevel@tonic-gate 		/* LINTED */
1357c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
136de777a60Sab 		    got_off);
1377c478bd9Sstevel@tonic-gate 	} else {
1387c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1397c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_REG_DISP_IND;
1407c478bd9Sstevel@tonic-gate 		pltent += 2;
1417c478bd9Sstevel@tonic-gate 		/* LINTED */
1427c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)got_off;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	pltent += 4;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_PUSHL;
1477c478bd9Sstevel@tonic-gate 	pltent++;
1487c478bd9Sstevel@tonic-gate 	/* LINTED */
1497c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)rel_off;
1507c478bd9Sstevel@tonic-gate 	pltent += 4;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
1537c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_JMP;
1547c478bd9Sstevel@tonic-gate 	pltent++;
1557c478bd9Sstevel@tonic-gate 	/* LINTED */
1567c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)plt_off;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate uintptr_t
1605aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
1637c478bd9Sstevel@tonic-gate 	Word		ndx, roffset, value;
1647c478bd9Sstevel@tonic-gate 	Rel		rea;
1657c478bd9Sstevel@tonic-gate 	char		*relbits;
1667c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
1677c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
1737c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
1767c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
1777c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
1785aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
1797c478bd9Sstevel@tonic-gate 		return (1);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
1847c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	if (orsp->rel_move)
1875aefb655Srie 		ld_adj_movereloc(ofl, orsp);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section using a partial initialized
1917c478bd9Sstevel@tonic-gate 	 * symbol, adjust the embedded symbol info.
1927c478bd9Sstevel@tonic-gate 	 *
1937c478bd9Sstevel@tonic-gate 	 * The second argument of the am_I_partial() is the value stored at the
1947c478bd9Sstevel@tonic-gate 	 * target address relocation is going to be applied.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
1977c478bd9Sstevel@tonic-gate 		if (ofl->ofl_parsym.head &&
1987c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1997c478bd9Sstevel@tonic-gate 		    /* LINTED */
2005aefb655Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
201b3fbe5e6Sseizo 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2027c478bd9Sstevel@tonic-gate 		    orsp->rel_roffset)))) {
2035aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2047c478bd9Sstevel@tonic-gate 			sectmoved = 1;
205de777a60Sab 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
2117c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
2125aefb655Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2135aefb655Srie 
2147c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2157c478bd9Sstevel@tonic-gate 		/*
2167c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
2177c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
2187c478bd9Sstevel@tonic-gate 		 */
2197c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
2207c478bd9Sstevel@tonic-gate 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2217c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * This must be a R_386_COPY.  For these set the roffset to
2287c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
2317c478bd9Sstevel@tonic-gate 		roffset = (Word)value;
2327c478bd9Sstevel@tonic-gate 	} else {
2337c478bd9Sstevel@tonic-gate 		osp = orsp->rel_osdesc;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
2377c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
2387c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
2397c478bd9Sstevel@tonic-gate 		 */
2407c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
2417c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
2427c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2437c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
2447c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
2487c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
2527c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
2537c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
2547c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_RELATIVE)
2577c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
2587c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
2597c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2607c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
2617c478bd9Sstevel@tonic-gate 			/*
2627c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
2637c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
2647c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
2657c478bd9Sstevel@tonic-gate 			 */
2667c478bd9Sstevel@tonic-gate 			if ((sdp->sd_isc != 0) &&
2677c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_osdesc != 0))
2687c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
2697c478bd9Sstevel@tonic-gate 			else
2707c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
2717c478bd9Sstevel@tonic-gate 		} else
2727c478bd9Sstevel@tonic-gate 			ndx = ofl->ofl_sunwdata1ndx;
2737c478bd9Sstevel@tonic-gate 	} else
2747c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
2757c478bd9Sstevel@tonic-gate 
276*cce0e03bSab 	/*
277*cce0e03bSab 	 * If we have a replacement value for the relocation
278*cce0e03bSab 	 * target, put it in place now.
279*cce0e03bSab 	 */
280*cce0e03bSab 	if (orsp->rel_flags & FLG_REL_NADDEND) {
281*cce0e03bSab 		Xword	addend = orsp->rel_raddend;
282*cce0e03bSab 		uchar_t	*addr;
283*cce0e03bSab 
284*cce0e03bSab 		/*
285*cce0e03bSab 		 * Get the address of the data item we need to modify.
286*cce0e03bSab 		 */
287*cce0e03bSab 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
288*cce0e03bSab 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
289*cce0e03bSab 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
290*cce0e03bSab 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
291*cce0e03bSab 			return (S_ERROR);
292*cce0e03bSab 	}
293*cce0e03bSab 
2947c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
2977c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
2985aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
2995aefb655Srie 	    orsp->rel_sname));
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/*
3027c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3077c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rel));
3087c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += sizeof (Rel);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3127c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3137c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3147c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3177c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3187c478bd9Sstevel@tonic-gate 
3195aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3207c478bd9Sstevel@tonic-gate 	return (1);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * i386 Instructions for TLS processing
3257c478bd9Sstevel@tonic-gate  */
326b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * 0x00	movl %gs:0x0, %eax
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * 0x06	addl x(%eax), %eax
3337c478bd9Sstevel@tonic-gate 	 * 0x0c ...
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
3367c478bd9Sstevel@tonic-gate };
3377c478bd9Sstevel@tonic-gate 
338b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * 0x00 movl %gs:0x0, %eax
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * 0x06 addl $0x0, %eax
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	0x05, 0x00, 0x00, 0x00, 0x00,
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * 0x0b nop
3497c478bd9Sstevel@tonic-gate 	 * 0x0c
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	0x90
3527c478bd9Sstevel@tonic-gate };
3537c478bd9Sstevel@tonic-gate 
354b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie_movgs[] = {
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 *	movl %gs:0x0,%eax
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
3597c478bd9Sstevel@tonic-gate };
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
3627c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_POP	0x58	/* popl + reg */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate #define	TLS_NOP		0x90	/* NOP instruction */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define	MODRM_MSK_MOD	0xc0
3697c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RO	0x38
3707c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RM	0x07
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #define	SIB_MSK_SS	0xc0
3737c478bd9Sstevel@tonic-gate #define	SIB_MSK_IND	0x38
3747c478bd9Sstevel@tonic-gate #define	SIB_MSK_BS	0x07
3757c478bd9Sstevel@tonic-gate 
3765aefb655Srie static Fixupret
3775aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
3807c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
381b3fbe5e6Sseizo 	uchar_t		*offset, r1, r2;
3827c478bd9Sstevel@tonic-gate 
383b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
384b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
385b3fbe5e6Sseizo 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
3887c478bd9Sstevel@tonic-gate 		/*
3897c478bd9Sstevel@tonic-gate 		 * IE reference model
3907c478bd9Sstevel@tonic-gate 		 */
3917c478bd9Sstevel@tonic-gate 		switch (rtype) {
3927c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD:
3937c478bd9Sstevel@tonic-gate 			/*
3947c478bd9Sstevel@tonic-gate 			 * Transition:
3957c478bd9Sstevel@tonic-gate 			 *	0x0 leal x@tlsgd(,r1,1), %eax
3967c478bd9Sstevel@tonic-gate 			 *	0x7 call ___tls_get_addr
3977c478bd9Sstevel@tonic-gate 			 *	0xc
3987c478bd9Sstevel@tonic-gate 			 * To:
3997c478bd9Sstevel@tonic-gate 			 *	0x0 movl %gs:0, %eax
4007c478bd9Sstevel@tonic-gate 			 *	0x6 addl x@gotntpoff(r1), %eax
4017c478bd9Sstevel@tonic-gate 			 */
4025aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
403051d39bbSrie 			    R_386_TLS_GOTIE, arsp));
4047c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_386_TLS_GOTIE;
4057c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 5;
4065aefb655Srie 
4077c478bd9Sstevel@tonic-gate 			/*
408051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4097c478bd9Sstevel@tonic-gate 			 * sequence.
4107c478bd9Sstevel@tonic-gate 			 */
4117c478bd9Sstevel@tonic-gate 			offset -= 3;
4127c478bd9Sstevel@tonic-gate 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4137c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4145aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4155aefb655Srie 
4167c478bd9Sstevel@tonic-gate 			/*
4177c478bd9Sstevel@tonic-gate 			 * set register %r1 into the addl
4187c478bd9Sstevel@tonic-gate 			 * instruction.
4197c478bd9Sstevel@tonic-gate 			 */
4207c478bd9Sstevel@tonic-gate 			offset[0x7] |= r1;
4217c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4225aefb655Srie 
4237c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD_PLT:
4247c478bd9Sstevel@tonic-gate 			/*
4257c478bd9Sstevel@tonic-gate 			 * Fixup done via the TLS_GD relocation
4267c478bd9Sstevel@tonic-gate 			 */
4275aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
428051d39bbSrie 			    R_386_NONE, arsp));
4297c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * LE reference model
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	switch (rtype) {
4377c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD:
4387c478bd9Sstevel@tonic-gate 		/*
4397c478bd9Sstevel@tonic-gate 		 * Transition:
4407c478bd9Sstevel@tonic-gate 		 *	0x0 leal x@tlsgd(,r1,1), %eax
4417c478bd9Sstevel@tonic-gate 		 *	0x7 call ___tls_get_addr
4427c478bd9Sstevel@tonic-gate 		 *	0xc
4437c478bd9Sstevel@tonic-gate 		 * To:
4447c478bd9Sstevel@tonic-gate 		 *	0x0 movl %gs:0, %eax
4457c478bd9Sstevel@tonic-gate 		 *	0x6 addl $x@ntpoff, %eax
4467c478bd9Sstevel@tonic-gate 		 *	0xb nop
4477c478bd9Sstevel@tonic-gate 		 *	0xc
4487c478bd9Sstevel@tonic-gate 		 */
4495aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
450051d39bbSrie 		    R_386_TLS_LE, arsp));
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
4537c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 4;
4545aefb655Srie 
4557c478bd9Sstevel@tonic-gate 		/*
456051d39bbSrie 		 * Adjust 'offset' to beginning of instruction
4577c478bd9Sstevel@tonic-gate 		 * sequence.
4587c478bd9Sstevel@tonic-gate 		 */
4597c478bd9Sstevel@tonic-gate 		offset -= 3;
4607c478bd9Sstevel@tonic-gate 		(void) memcpy(offset, tlsinstr_gd_le,
4615aefb655Srie 		    sizeof (tlsinstr_gd_le));
4627c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
4635aefb655Srie 
4647c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD_PLT:
4657c478bd9Sstevel@tonic-gate 	case R_386_PLT32:
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * Fixup done via the TLS_GD relocation
4687c478bd9Sstevel@tonic-gate 		 */
4695aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
470051d39bbSrie 		    R_386_NONE, arsp));
4717c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
4725aefb655Srie 
4737c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM_PLT:
4745aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
475051d39bbSrie 		    R_386_NONE, arsp));
4765aefb655Srie 
4777c478bd9Sstevel@tonic-gate 		/*
4787c478bd9Sstevel@tonic-gate 		 * Transition:
4797c478bd9Sstevel@tonic-gate 		 *	call __tls_get_addr()
4807c478bd9Sstevel@tonic-gate 		 * to:
4817c478bd9Sstevel@tonic-gate 		 *	nop
4827c478bd9Sstevel@tonic-gate 		 *	nop
4837c478bd9Sstevel@tonic-gate 		 *	nop
4847c478bd9Sstevel@tonic-gate 		 *	nop
4857c478bd9Sstevel@tonic-gate 		 *	nop
4867c478bd9Sstevel@tonic-gate 		 */
4877c478bd9Sstevel@tonic-gate 		*(offset - 1) = TLS_NOP;
4887c478bd9Sstevel@tonic-gate 		*(offset) = TLS_NOP;
4897c478bd9Sstevel@tonic-gate 		*(offset + 1) = TLS_NOP;
4907c478bd9Sstevel@tonic-gate 		*(offset + 2) = TLS_NOP;
4917c478bd9Sstevel@tonic-gate 		*(offset + 3) = TLS_NOP;
4927c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
4935aefb655Srie 
4947c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM:
4955aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
496051d39bbSrie 		    R_386_NONE, arsp));
4975aefb655Srie 
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * Transition:
5007c478bd9Sstevel@tonic-gate 		 *
5017c478bd9Sstevel@tonic-gate 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5027c478bd9Sstevel@tonic-gate 		 *  0x06 call ___tls_get_addr
5037c478bd9Sstevel@tonic-gate 		 *
5047c478bd9Sstevel@tonic-gate 		 * to:
5057c478bd9Sstevel@tonic-gate 		 *
5067c478bd9Sstevel@tonic-gate 		 *  0x00 movl %gs:0, %eax
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5095aefb655Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5107c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5115aefb655Srie 
5127c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDO_32:
5137c478bd9Sstevel@tonic-gate 		/*
5147c478bd9Sstevel@tonic-gate 		 *  Instructions:
5157c478bd9Sstevel@tonic-gate 		 *
5167c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5177c478bd9Sstevel@tonic-gate 		 *		to
5187c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5197c478bd9Sstevel@tonic-gate 		 *
5207c478bd9Sstevel@tonic-gate 		 */
5217c478bd9Sstevel@tonic-gate 		offset -= 2;
5227c478bd9Sstevel@tonic-gate 
5235aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
524051d39bbSrie 		    R_386_TLS_LE, arsp));
5257c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5267c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5275aefb655Srie 
5287c478bd9Sstevel@tonic-gate 	case R_386_TLS_GOTIE:
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
5317c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
5327c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
5337c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
5347c478bd9Sstevel@tonic-gate 		 *
5357c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
5367c478bd9Sstevel@tonic-gate 		 *
5377c478bd9Sstevel@tonic-gate 		 *  1) movl foo@gotntpoff(%reg1), %reg2
5387c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5397c478bd9Sstevel@tonic-gate 		 *
5407c478bd9Sstevel@tonic-gate 		 *  2) addl foo@gotntpoff(%reg1), %reg2
5417c478bd9Sstevel@tonic-gate 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
5427c478bd9Sstevel@tonic-gate 		 *
5437c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
5447c478bd9Sstevel@tonic-gate 		 *
5457c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %reg2
5467c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
5477c478bd9Sstevel@tonic-gate 		 *
5487c478bd9Sstevel@tonic-gate 		 *  2) addl $foo@ntpoff, %reg2
5497c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg2, foo@ntpoff
5507c478bd9Sstevel@tonic-gate 		 *
5517c478bd9Sstevel@tonic-gate 		 * Note: reg1 != 4 (%esp)
5527c478bd9Sstevel@tonic-gate 		 */
5535aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
554051d39bbSrie 		    R_386_TLS_LE, arsp));
5557c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5565aefb655Srie 
5577c478bd9Sstevel@tonic-gate 		offset -= 2;
5587c478bd9Sstevel@tonic-gate 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
5597c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
5607c478bd9Sstevel@tonic-gate 			/* case 1 above */
5617c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
5627c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
5637c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
5677c478bd9Sstevel@tonic-gate 			/* case 2 above */
5687c478bd9Sstevel@tonic-gate 			assert(offset[0] == 0x03);
5697c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
5707c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
5717c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		/*
5757c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
5767c478bd9Sstevel@tonic-gate 		 */
577de777a60Sab 		{
578de777a60Sab 			Conv_inv_buf_t	inv_buf;
579de777a60Sab 
580de777a60Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
581de777a60Sab 			    MSG_INTL(MSG_REL_BADTLSINS),
582de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
583de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
584de777a60Sab 			    demangle(arsp->rel_sname),
585de777a60Sab 			    arsp->rel_isdesc->is_name,
586de777a60Sab 			    EC_OFF(arsp->rel_roffset));
587de777a60Sab 		}
5887c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
5895aefb655Srie 
5907c478bd9Sstevel@tonic-gate 	case R_386_TLS_IE:
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
5937c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
5947c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
5957c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
5967c478bd9Sstevel@tonic-gate 		 *
5977c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
5987c478bd9Sstevel@tonic-gate 		 *  1) movl foo@indntpoff, %eax
5997c478bd9Sstevel@tonic-gate 		 *	0xa1, foo@indntpoff
6007c478bd9Sstevel@tonic-gate 		 *
6017c478bd9Sstevel@tonic-gate 		 *  2) movl foo@indntpoff, %eax
6027c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6037c478bd9Sstevel@tonic-gate 		 *
6047c478bd9Sstevel@tonic-gate 		 *  3) addl foo@indntpoff, %eax
6057c478bd9Sstevel@tonic-gate 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6067c478bd9Sstevel@tonic-gate 		 *
6077c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
6087c478bd9Sstevel@tonic-gate 		 *
6097c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %eax
6107c478bd9Sstevel@tonic-gate 		 *	0xb8, foo@ntpoff
6117c478bd9Sstevel@tonic-gate 		 *
6127c478bd9Sstevel@tonic-gate 		 *  2) movl $foo@ntpoff, %reg
6137c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6147c478bd9Sstevel@tonic-gate 		 *
6157c478bd9Sstevel@tonic-gate 		 *  3) addl $foo@ntpoff, %reg
6167c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg, foo@ntpoff
6177c478bd9Sstevel@tonic-gate 		 */
6187c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6197c478bd9Sstevel@tonic-gate 		offset--;
6207c478bd9Sstevel@tonic-gate 		if (offset[0] == 0xa1) {
6217c478bd9Sstevel@tonic-gate 			/* case 1 above */
6227c478bd9Sstevel@tonic-gate 			offset[0] = 0xb8;	/*  movl */
6237c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		offset--;
6277c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
6287c478bd9Sstevel@tonic-gate 			/* case 2 above */
6297c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6307c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
6317c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6327c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
6357c478bd9Sstevel@tonic-gate 			/* case 3 above */
6367c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6377c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
6387c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6397c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 		/*
6427c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
6437c478bd9Sstevel@tonic-gate 		 */
644de777a60Sab 		{
645de777a60Sab 			Conv_inv_buf_t	inv_buf;
646de777a60Sab 
647de777a60Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
648de777a60Sab 			    MSG_INTL(MSG_REL_BADTLSINS),
649de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
650de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
651de777a60Sab 			    demangle(arsp->rel_sname),
652de777a60Sab 			    arsp->rel_isdesc->is_name,
653de777a60Sab 			    EC_OFF(arsp->rel_roffset));
654de777a60Sab 		}
6557c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate uintptr_t
6615aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6627c478bd9Sstevel@tonic-gate {
663141040e8Srie 	Rel_desc	*arsp;
664141040e8Srie 	Rel_cache	*rcp;
665141040e8Srie 	Listnode	*lnp;
6667c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6677c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
6687c478bd9Sstevel@tonic-gate 
6697010c12aSrie 	if (ofl->ofl_actrels.head)
6707010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6717010c12aSrie 
6727c478bd9Sstevel@tonic-gate 	/*
673141040e8Srie 	 * Process active relocations.
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6767c478bd9Sstevel@tonic-gate 		/* LINTED */
6777c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6787c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
679b3fbe5e6Sseizo 			uchar_t		*addr;
680141040e8Srie 			Xword 		value;
6817c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
6827c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
683141040e8Srie 			Xword		refaddr;
6847c478bd9Sstevel@tonic-gate 			int		moved = 0;
6857c478bd9Sstevel@tonic-gate 			Gotref		gref;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 			/*
6887c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
6897c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
6907c478bd9Sstevel@tonic-gate 			 * relocation itself.
6917c478bd9Sstevel@tonic-gate 			 */
6927c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6937c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
6947c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
6957c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6965aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6975aefb655Srie 				    M_MACH, arsp));
6987c478bd9Sstevel@tonic-gate 				continue;
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			/*
7027c478bd9Sstevel@tonic-gate 			 * We deteremine what the 'got reference'
7037c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
7047c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
7057c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
7067c478bd9Sstevel@tonic-gate 			 *
7077c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
7087c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
7097c478bd9Sstevel@tonic-gate 			 */
7107c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
7117c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
7127c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
7137c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
7147c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
7157c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
7167c478bd9Sstevel@tonic-gate 			else
7177c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 			/*
7207c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
7217c478bd9Sstevel@tonic-gate 			 */
7227c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7237c478bd9Sstevel@tonic-gate 				Fixupret	ret;
7247c478bd9Sstevel@tonic-gate 
7255aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7267c478bd9Sstevel@tonic-gate 					return (S_ERROR);
7277c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
7287c478bd9Sstevel@tonic-gate 					continue;
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 			/*
7327c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
7337c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
7347c478bd9Sstevel@tonic-gate 			 */
7357c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
7365aefb655Srie 				ld_adj_movereloc(ofl, arsp);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
7397c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
7407c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_CLVAL)
7437c478bd9Sstevel@tonic-gate 				value = 0;
7447c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7457c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
746141040e8Srie 				Sym_desc	*sym;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 				/*
7497c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
7507c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
7517c478bd9Sstevel@tonic-gate 				 *
7525aefb655Srie 				 * The second argument of the ld_am_I_partial()
7535aefb655Srie 				 * is the value stored at the target address
7547c478bd9Sstevel@tonic-gate 				 * relocation is going to be applied.
7557c478bd9Sstevel@tonic-gate 				 */
7567c478bd9Sstevel@tonic-gate 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7577c478bd9Sstevel@tonic-gate 				    /* LINTED */
7585aefb655Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
759b3fbe5e6Sseizo 				    ((uchar_t *)
760b3fbe5e6Sseizo 				    arsp->rel_isdesc->is_indata->d_buf +
7617c478bd9Sstevel@tonic-gate 				    arsp->rel_roffset)))) {
7627c478bd9Sstevel@tonic-gate 					/*
7637c478bd9Sstevel@tonic-gate 					 * If the symbol is moved,
7647c478bd9Sstevel@tonic-gate 					 * adjust the value
7657c478bd9Sstevel@tonic-gate 					 */
7667c478bd9Sstevel@tonic-gate 					value = sym->sd_sym->st_value;
7677c478bd9Sstevel@tonic-gate 					moved = 1;
7687c478bd9Sstevel@tonic-gate 				} else {
769141040e8Srie 					value = _elf_getxoff(
770141040e8Srie 					    sdp->sd_isc->is_indata);
7717c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
7727c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
773141040e8Srie 						value += sdp->sd_isc->
774141040e8Srie 						    is_osdesc->os_shdr->sh_addr;
7757c478bd9Sstevel@tonic-gate 				}
7767c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7777c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
7782926dd2eSrie 
7792926dd2eSrie 			} else if (IS_SIZE(arsp->rel_rtype)) {
7802926dd2eSrie 				/*
7812926dd2eSrie 				 * Size relocations require the symbols size.
7822926dd2eSrie 				 */
7832926dd2eSrie 				value = sdp->sd_sym->st_size;
784141040e8Srie 			} else {
7857c478bd9Sstevel@tonic-gate 				/*
7867010c12aSrie 				 * Else the value is the symbols value.
7877c478bd9Sstevel@tonic-gate 				 */
7887c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
789141040e8Srie 			}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			/*
7927c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7937c478bd9Sstevel@tonic-gate 			 */
7947c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
7957c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 			/*
7987c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
7997c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
8007c478bd9Sstevel@tonic-gate 			 * address.
8017c478bd9Sstevel@tonic-gate 			 */
8027c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
803141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8047c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
8057c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			/*
8087c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
8097c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
8107c478bd9Sstevel@tonic-gate 			 * not the address of the function).
8117c478bd9Sstevel@tonic-gate 			 */
8127c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
8137c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8145aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 
817141040e8Srie 			/*
818141040e8Srie 			 * Determine whether the value needs further adjustment.
819141040e8Srie 			 * Filter through the attributes of the relocation to
820141040e8Srie 			 * determine what adjustment is required.  Note, many
821141040e8Srie 			 * of the following cases are only applicable when a
822141040e8Srie 			 * .got is present.  As a .got is not generated when a
823141040e8Srie 			 * relocatable object is being built, any adjustments
824141040e8Srie 			 * that require a .got need to be skipped.
825141040e8Srie 			 */
826141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
827141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8287c478bd9Sstevel@tonic-gate 				Xword		R1addr;
8297c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
8307c478bd9Sstevel@tonic-gate 				Word		gotndx;
8317c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 				/*
8347c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
8357c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
8367c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
8377c478bd9Sstevel@tonic-gate 				 * directly
8387c478bd9Sstevel@tonic-gate 				 *
8397c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
8407c478bd9Sstevel@tonic-gate 				 * the relocation.
8417c478bd9Sstevel@tonic-gate 				 */
8425aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8437c478bd9Sstevel@tonic-gate 				    ofl, 0);
8447c478bd9Sstevel@tonic-gate 				assert(gnp);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
8477c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
8487c478bd9Sstevel@tonic-gate 				else
8497c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 				/*
8547c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
8557c478bd9Sstevel@tonic-gate 				 */
8567c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
8577c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
8587c478bd9Sstevel@tonic-gate 
8595aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8605aefb655Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
8617c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
8627c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 				/*
8657c478bd9Sstevel@tonic-gate 				 * And do it.
8667c478bd9Sstevel@tonic-gate 				 */
867f3324781Sab 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
868f3324781Sab 					*(Xword *)R2addr =
869f3324781Sab 					    ld_byteswap_Xword(value);
870f3324781Sab 				else
871f3324781Sab 					*(Xword *)R2addr = value;
8727c478bd9Sstevel@tonic-gate 				continue;
8737c478bd9Sstevel@tonic-gate 
874141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
875141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8767c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
877141040e8Srie 
878141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
879141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
880141040e8Srie 				value =
881141040e8Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
882141040e8Srie 				    refaddr;
883141040e8Srie 
8847c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
885141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
8867c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
8877c478bd9Sstevel@tonic-gate 				value -= refaddr;
888141040e8Srie 
8897c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
890141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
891141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8927c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
8937c478bd9Sstevel@tonic-gate 
8945aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8957c478bd9Sstevel@tonic-gate 				    ofl, 0);
8967c478bd9Sstevel@tonic-gate 				assert(gnp);
897141040e8Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
8987c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_386_TLS_IE) {
899141040e8Srie 					value +=
900141040e8Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
9017c478bd9Sstevel@tonic-gate 				}
902141040e8Srie 
903141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
904141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
905141040e8Srie 				Gotndx *gnp;
9067c478bd9Sstevel@tonic-gate 
9075aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
9087c478bd9Sstevel@tonic-gate 				    GOT_REF_GENERIC, ofl, 0);
9097c478bd9Sstevel@tonic-gate 				assert(gnp);
910141040e8Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
911141040e8Srie 
912141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
913141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9147c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
915141040e8Srie 
9167c478bd9Sstevel@tonic-gate 				/*
9177c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
9187c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
9197c478bd9Sstevel@tonic-gate 				 */
920141040e8Srie 				tlsstatsize =
921141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9227c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
9237c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
924141040e8Srie 
9257c478bd9Sstevel@tonic-gate 				/*
926141040e8Srie 				 * Since this code is fixed up, it assumes a
927141040e8Srie 				 * negative offset that can be added to the
928141040e8Srie 				 * thread pointer.
9297c478bd9Sstevel@tonic-gate 				 */
9307c478bd9Sstevel@tonic-gate 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
9317c478bd9Sstevel@tonic-gate 				    (arsp->rel_rtype == R_386_TLS_LE))
9327c478bd9Sstevel@tonic-gate 					value = -value;
9337c478bd9Sstevel@tonic-gate 			}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
9367c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9377c478bd9Sstevel@tonic-gate 			else
9387c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 			/*
9417c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9427c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9437c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
9447c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
9457c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
9467c478bd9Sstevel@tonic-gate 			 * see this.
9477c478bd9Sstevel@tonic-gate 			 */
9487c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
949de777a60Sab 				Conv_inv_buf_t	inv_buf;
950de777a60Sab 
9515aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9525aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
953de777a60Sab 				    conv_reloc_386_type(arsp->rel_rtype,
954de777a60Sab 				    0, &inv_buf),
9557c478bd9Sstevel@tonic-gate 				    ifl_name, demangle(arsp->rel_sname),
9567c478bd9Sstevel@tonic-gate 				    arsp->rel_isdesc->is_name);
9577c478bd9Sstevel@tonic-gate 				return (S_ERROR);
9587c478bd9Sstevel@tonic-gate 			}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 			/*
9617c478bd9Sstevel@tonic-gate 			 * Get the address of the data item we need to modify.
9627c478bd9Sstevel@tonic-gate 			 */
963b3fbe5e6Sseizo 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
964b3fbe5e6Sseizo 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
965b3fbe5e6Sseizo 			    is_indata));
9667c478bd9Sstevel@tonic-gate 
9675aefb655Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9685aefb655Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
9695aefb655Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9707c478bd9Sstevel@tonic-gate 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9717c478bd9Sstevel@tonic-gate 
9725aefb655Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
973b3fbe5e6Sseizo 			    ofl->ofl_size) || (arsp->rel_roffset >
9747c478bd9Sstevel@tonic-gate 			    arsp->rel_osdesc->os_shdr->sh_size)) {
975de777a60Sab 				Conv_inv_buf_t	inv_buf;
976de777a60Sab 				int		class;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 				if (((uintptr_t)addr -
9795aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
9807c478bd9Sstevel@tonic-gate 					class = ERR_FATAL;
9817c478bd9Sstevel@tonic-gate 				else
9827c478bd9Sstevel@tonic-gate 					class = ERR_WARNING;
9837c478bd9Sstevel@tonic-gate 
9845aefb655Srie 				eprintf(ofl->ofl_lml, class,
9855aefb655Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
986de777a60Sab 				    conv_reloc_386_type(arsp->rel_rtype,
987de777a60Sab 				    0, &inv_buf),
9887c478bd9Sstevel@tonic-gate 				    ifl_name, arsp->rel_isdesc->is_name,
9897c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
9907c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
9915aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
9947c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9957c478bd9Sstevel@tonic-gate 					continue;
9967c478bd9Sstevel@tonic-gate 				}
9977c478bd9Sstevel@tonic-gate 			}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 			/*
10007c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
10017c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
10027c478bd9Sstevel@tonic-gate 			 * expanded.
10037c478bd9Sstevel@tonic-gate 			 */
10047c478bd9Sstevel@tonic-gate 			if (moved)
10057c478bd9Sstevel@tonic-gate 				value -= *addr;
10067c478bd9Sstevel@tonic-gate 
1007*cce0e03bSab 			/*
1008*cce0e03bSab 			 * If we have a replacement value for the relocation
1009*cce0e03bSab 			 * target, put it in place now.
1010*cce0e03bSab 			 */
1011*cce0e03bSab 			if (arsp->rel_flags & FLG_REL_NADDEND) {
1012*cce0e03bSab 				Xword addend = arsp->rel_raddend;
1013*cce0e03bSab 
1014*cce0e03bSab 				if (ld_reloc_targval_set(ofl, arsp,
1015*cce0e03bSab 				    addr, addend) == 0)
1016*cce0e03bSab 					return (S_ERROR);
1017*cce0e03bSab 			}
1018*cce0e03bSab 
10197c478bd9Sstevel@tonic-gate 			/*
1020f3324781Sab 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10217c478bd9Sstevel@tonic-gate 			 * stage.
10227c478bd9Sstevel@tonic-gate 			 */
1023f3324781Sab 			if (OFL_DO_RELOC(ofl)) {
1024f3324781Sab 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
10255aefb655Srie 				    &value, arsp->rel_sname, ifl_name,
1026f3324781Sab 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10275aefb655Srie 				    ofl->ofl_lml) == 0)
10287c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
10297c478bd9Sstevel@tonic-gate 			}
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	return (return_code);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
1035141040e8Srie /*
1036141040e8Srie  * Add an output relocation record.
1037141040e8Srie  */
10387c478bd9Sstevel@tonic-gate uintptr_t
10395aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10407c478bd9Sstevel@tonic-gate {
1041141040e8Srie 	Rel_desc	*orsp;
1042141040e8Srie 	Rel_cache	*rcp;
1043141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/*
10467c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
10477c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10487c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
10497c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
10527c478bd9Sstevel@tonic-gate 	    (FLG_OF_STATIC | FLG_OF_EXEC))
10537c478bd9Sstevel@tonic-gate 		return (1);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * If no relocation cache structures are available allocate
10577c478bd9Sstevel@tonic-gate 	 * a new one and link it into the cache list.
10587c478bd9Sstevel@tonic-gate 	 */
10597c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_outrels.tail == 0) ||
10607c478bd9Sstevel@tonic-gate 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10617c478bd9Sstevel@tonic-gate 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10627c478bd9Sstevel@tonic-gate 		static size_t	nextsize = 0;
10637c478bd9Sstevel@tonic-gate 		size_t		size;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		/*
10667c478bd9Sstevel@tonic-gate 		 * Output relocation numbers can vary considerably between
10677c478bd9Sstevel@tonic-gate 		 * building executables or shared objects (pic vs. non-pic),
10687c478bd9Sstevel@tonic-gate 		 * etc.  But, they typically aren't very large, so for these
10697c478bd9Sstevel@tonic-gate 		 * objects use a standard bucket size.  For building relocatable
10707c478bd9Sstevel@tonic-gate 		 * objects, typically there will be an output relocation for
10717c478bd9Sstevel@tonic-gate 		 * every input relocation.
10727c478bd9Sstevel@tonic-gate 		 */
10737c478bd9Sstevel@tonic-gate 		if (nextsize == 0) {
10747c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10757c478bd9Sstevel@tonic-gate 				if ((size = ofl->ofl_relocincnt) == 0)
10767c478bd9Sstevel@tonic-gate 					size = REL_LOIDESCNO;
10777c478bd9Sstevel@tonic-gate 				if (size > REL_HOIDESCNO)
10787c478bd9Sstevel@tonic-gate 					nextsize = REL_HOIDESCNO;
10797c478bd9Sstevel@tonic-gate 				else
10807c478bd9Sstevel@tonic-gate 					nextsize = REL_LOIDESCNO;
10817c478bd9Sstevel@tonic-gate 			} else
10827c478bd9Sstevel@tonic-gate 				nextsize = size = REL_HOIDESCNO;
10837c478bd9Sstevel@tonic-gate 		} else
10847c478bd9Sstevel@tonic-gate 			size = nextsize;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		size = size * sizeof (Rel_desc);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
10897c478bd9Sstevel@tonic-gate 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
10907c478bd9Sstevel@tonic-gate 			return (S_ERROR);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		/* LINTED */
10937c478bd9Sstevel@tonic-gate 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
10947c478bd9Sstevel@tonic-gate 		/* LINTED */
10957c478bd9Sstevel@tonic-gate 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	/*
10997c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
11007c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
11017c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11047c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
11057c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		/*
11087c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
11097c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
11107c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
11117c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
11127c478bd9Sstevel@tonic-gate 		 */
11137c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11140bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11157c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11167c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11177c478bd9Sstevel@tonic-gate 			else
11187c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1119141040e8Srie 		} else {
1120141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11217c478bd9Sstevel@tonic-gate 
1122c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11237c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11247c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11257c478bd9Sstevel@tonic-gate 			}
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
11307c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
11337c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
11367c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11377c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
11387c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11397c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
11407c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11417c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
11427c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11437c478bd9Sstevel@tonic-gate 	else
11447c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
11477c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
11517c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
11527c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
11557c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
1158141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11597c478bd9Sstevel@tonic-gate 	 */
1160141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11617c478bd9Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11677c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11705aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11717c478bd9Sstevel@tonic-gate 	}
11725aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11735aefb655Srie 	    M_MACH, orsp));
11747c478bd9Sstevel@tonic-gate 	return (1);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * Stub routine since register symbols are not supported on i386.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate /* ARGSUSED */
11817c478bd9Sstevel@tonic-gate uintptr_t
11825aefb655Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
11837c478bd9Sstevel@tonic-gate {
11845aefb655Srie 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
11857c478bd9Sstevel@tonic-gate 	return (S_ERROR);
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate /*
11897c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate uintptr_t
11925aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
11957c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11960bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
12007c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
12017c478bd9Sstevel@tonic-gate 	 * then
12027c478bd9Sstevel@tonic-gate 	 *	build R_386_RELATIVE
12037c478bd9Sstevel@tonic-gate 	 * fi
12047c478bd9Sstevel@tonic-gate 	 */
12057c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12062926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12077c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12087c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
12097c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12107c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12117c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12127c478bd9Sstevel@tonic-gate 		Word	ortype = rsp->rel_rtype;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_RELATIVE;
12155aefb655Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12167c478bd9Sstevel@tonic-gate 			return (S_ERROR);
12177c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
12187c478bd9Sstevel@tonic-gate 	}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	/*
12217c478bd9Sstevel@tonic-gate 	 * If the relocation is against a 'non-allocatable' section
12227c478bd9Sstevel@tonic-gate 	 * and we can not resolve it now - then give a warning
12237c478bd9Sstevel@tonic-gate 	 * message.
12247c478bd9Sstevel@tonic-gate 	 *
12257c478bd9Sstevel@tonic-gate 	 * We can not resolve the symbol if either:
12267c478bd9Sstevel@tonic-gate 	 *	a) it's undefined
12277c478bd9Sstevel@tonic-gate 	 *	b) it's defined in a shared library and a
12287c478bd9Sstevel@tonic-gate 	 *	   COPY relocation hasn't moved it to the executable
12297c478bd9Sstevel@tonic-gate 	 *
12307c478bd9Sstevel@tonic-gate 	 * Note: because we process all of the relocations against the
12317c478bd9Sstevel@tonic-gate 	 *	text segment before any others - we know whether
12327c478bd9Sstevel@tonic-gate 	 *	or not a copy relocation will be generated before
12337c478bd9Sstevel@tonic-gate 	 *	we get here (see reloc_init()->reloc_segments()).
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12367c478bd9Sstevel@tonic-gate 	    ((shndx == SHN_UNDEF) ||
12377c478bd9Sstevel@tonic-gate 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12387c478bd9Sstevel@tonic-gate 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1239de777a60Sab 		Conv_inv_buf_t inv_buf;
1240de777a60Sab 
12417c478bd9Sstevel@tonic-gate 		/*
12427c478bd9Sstevel@tonic-gate 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12437c478bd9Sstevel@tonic-gate 		 * section - then silently ignore that the relocation
12447c478bd9Sstevel@tonic-gate 		 * can not be resolved.
12457c478bd9Sstevel@tonic-gate 		 */
12467c478bd9Sstevel@tonic-gate 		if (rsp->rel_osdesc &&
12477c478bd9Sstevel@tonic-gate 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12487c478bd9Sstevel@tonic-gate 			return (0);
12495aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1250de777a60Sab 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12517c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12527c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12537c478bd9Sstevel@tonic-gate 		return (1);
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/*
12577c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12587c478bd9Sstevel@tonic-gate 	 */
12595aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate uintptr_t
12637c478bd9Sstevel@tonic-gate /* ARGSUSED */
12645aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12657c478bd9Sstevel@tonic-gate {
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * Stub routine for common code compatibility, we shouldn't
12687c478bd9Sstevel@tonic-gate 	 * actually get here on x86.
12697c478bd9Sstevel@tonic-gate 	 */
12707c478bd9Sstevel@tonic-gate 	assert(0);
12717c478bd9Sstevel@tonic-gate 	return (S_ERROR);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate uintptr_t
12755aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12787c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12797c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
12807c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
1283d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1284d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12857c478bd9Sstevel@tonic-gate 	 */
1286d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12877c478bd9Sstevel@tonic-gate 		/*
1288d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12897c478bd9Sstevel@tonic-gate 		 */
12907c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12917c478bd9Sstevel@tonic-gate 
1292d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12937c478bd9Sstevel@tonic-gate 			/*
1294d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12957c478bd9Sstevel@tonic-gate 			 */
1296d326b23bSrie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1297d326b23bSrie 			    GOT_REF_TLSIE, ofl, 0)) == 0) {
12987c478bd9Sstevel@tonic-gate 
1299d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1300d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1301d326b23bSrie 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
1302d326b23bSrie 					return (S_ERROR);
1303d326b23bSrie 			}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 			/*
1306d326b23bSrie 			 * IE access model.
13077c478bd9Sstevel@tonic-gate 			 */
1308d326b23bSrie 			if (IS_TLS_IE(rtype)) {
1309d326b23bSrie 				if (ld_add_actrel(FLG_REL_STLS,
13107c478bd9Sstevel@tonic-gate 				    rsp, ofl) == S_ERROR)
13117c478bd9Sstevel@tonic-gate 					return (S_ERROR);
1312d326b23bSrie 
1313d326b23bSrie 				/*
1314d326b23bSrie 				 * A non-pic shared object needs to adjust the
1315d326b23bSrie 				 * active relocation (indntpoff).
1316d326b23bSrie 				 */
1317d326b23bSrie 				if (((flags & FLG_OF_EXEC) == 0) &&
1318d326b23bSrie 				    (rtype == R_386_TLS_IE)) {
1319d326b23bSrie 					rsp->rel_rtype = R_386_RELATIVE;
1320d326b23bSrie 					return (ld_add_outrel(NULL, rsp, ofl));
1321d326b23bSrie 				}
1322d326b23bSrie 				return (1);
13237c478bd9Sstevel@tonic-gate 			}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 			/*
1326d326b23bSrie 			 * Fixups are required for other executable models.
13277c478bd9Sstevel@tonic-gate 			 */
13285aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13297c478bd9Sstevel@tonic-gate 			    rsp, ofl));
13307c478bd9Sstevel@tonic-gate 		}
1331d326b23bSrie 
13327c478bd9Sstevel@tonic-gate 		/*
1333d326b23bSrie 		 * LE access model.
13347c478bd9Sstevel@tonic-gate 		 */
13357c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13365aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13377c478bd9Sstevel@tonic-gate 
13385aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13395aefb655Srie 		    rsp, ofl));
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/*
1343d326b23bSrie 	 * Building a shared object.
1344d326b23bSrie 	 *
1345d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13467c478bd9Sstevel@tonic-gate 	 */
13475aefb655Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
13487c478bd9Sstevel@tonic-gate 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
1349d326b23bSrie 
1350d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1351d326b23bSrie 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13527c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1353d326b23bSrie 
13545aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
13555aefb655Srie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
13565aefb655Srie 	    ofl, 0)) == 0)) {
1357d326b23bSrie 
1358d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1359d326b23bSrie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1360d326b23bSrie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13617c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13627c478bd9Sstevel@tonic-gate 	}
1363d326b23bSrie 
13647c478bd9Sstevel@tonic-gate 	/*
13657c478bd9Sstevel@tonic-gate 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1366d326b23bSrie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1367d326b23bSrie 	 * symbol now, and prepare for the PLT magic.
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1370f5a18a30Srie 		Sym_desc	*tlsgetsym;
13717c478bd9Sstevel@tonic-gate 
13725aefb655Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1373f5a18a30Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13747c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1375d326b23bSrie 
13767c478bd9Sstevel@tonic-gate 		rsp->rel_sym = tlsgetsym;
13777c478bd9Sstevel@tonic-gate 		rsp->rel_sname = tlsgetsym->sd_name;
13787c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_PLT32;
1379d326b23bSrie 
13805aefb655Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13817c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1382d326b23bSrie 
13837c478bd9Sstevel@tonic-gate 		rsp->rel_sym = sdp;
13847c478bd9Sstevel@tonic-gate 		rsp->rel_sname = sdp->sd_name;
13857c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = rtype;
13867c478bd9Sstevel@tonic-gate 		return (1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13905aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13917c478bd9Sstevel@tonic-gate 
13925aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
13967c478bd9Sstevel@tonic-gate Gotndx *
13975aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13987c478bd9Sstevel@tonic-gate {
13997c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
14007c478bd9Sstevel@tonic-gate 	Gotndx *	gnp;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
14037c478bd9Sstevel@tonic-gate 		return (ofl->ofl_tlsldgotndx);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
14067c478bd9Sstevel@tonic-gate 		if (gnp->gn_gotref == gref)
14077c478bd9Sstevel@tonic-gate 			return (gnp);
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 	return ((Gotndx *)0);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate Xword
14135aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	Os_desc		*osp = ofl->ofl_osgot;
14167c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rdesc->rel_sym;
14177c478bd9Sstevel@tonic-gate 	Xword		gotndx;
14187c478bd9Sstevel@tonic-gate 	Gotref		gref;
14197c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (rdesc->rel_flags & FLG_REL_DTLS)
14227c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSGD;
14237c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_MTLS)
14247c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSLD;
14257c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_STLS)
14267c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSIE;
14277c478bd9Sstevel@tonic-gate 	else
14287c478bd9Sstevel@tonic-gate 		gref = GOT_REF_GENERIC;
14297c478bd9Sstevel@tonic-gate 
14305aefb655Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
14317c478bd9Sstevel@tonic-gate 	assert(gnp);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	gotndx = (Xword)gnp->gn_gotndx;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
14367c478bd9Sstevel@tonic-gate 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
14377c478bd9Sstevel@tonic-gate 		gotndx++;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
14447c478bd9Sstevel@tonic-gate uintptr_t
1445d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
14467c478bd9Sstevel@tonic-gate     Rel_desc * rsp, Sym_desc * sdp)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	Gotndx	*gnp;
14497c478bd9Sstevel@tonic-gate 	uint_t	gotents;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (pgnp)
14527c478bd9Sstevel@tonic-gate 		return (1);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14557c478bd9Sstevel@tonic-gate 		gotents = 2;
14567c478bd9Sstevel@tonic-gate 	else
14577c478bd9Sstevel@tonic-gate 		gotents = 1;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14607c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14617c478bd9Sstevel@tonic-gate 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14627c478bd9Sstevel@tonic-gate 	gnp->gn_gotref = gref;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
14677c478bd9Sstevel@tonic-gate 		ofl->ofl_tlsldgotndx = gnp;
14687c478bd9Sstevel@tonic-gate 		return (1);
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if (list_appendc(lst, (void *)gnp) == 0)
14727c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	return (1);
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate void
14785aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14817c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1482141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /*
14867c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14877c478bd9Sstevel@tonic-gate  */
14887c478bd9Sstevel@tonic-gate uintptr_t
1489d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14907c478bd9Sstevel@tonic-gate {
1491d326b23bSrie 	Word	flags = ofl->ofl_flags;
1492d326b23bSrie 
14937c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1494d326b23bSrie 		Sym_desc	*sdp;
14957c478bd9Sstevel@tonic-gate 
14965aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14977c478bd9Sstevel@tonic-gate 		    SYM_NOHASH, 0, ofl)) != NULL) {
1498d326b23bSrie 			uchar_t	*genptr;
1499d326b23bSrie 
1500d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15017c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15027c478bd9Sstevel@tonic-gate 			/* LINTED */
15037c478bd9Sstevel@tonic-gate 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	/*
15087c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
15097c478bd9Sstevel@tonic-gate 	 * entry is:
15107c478bd9Sstevel@tonic-gate 	 *  if (building a.out) {
15117c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]		    # the address of the link map entry
15127c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]		    # the address of rtbinder
15137c478bd9Sstevel@tonic-gate 	 *  } else {
15147c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15157c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15167c478bd9Sstevel@tonic-gate 	 *  }
15177c478bd9Sstevel@tonic-gate 	 */
1518d326b23bSrie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1519b3fbe5e6Sseizo 		uchar_t *pltent;
15207c478bd9Sstevel@tonic-gate 
1521b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1522d326b23bSrie 		if (!(flags & FLG_OF_SHAROBJ)) {
15237c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15247c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_DISP;
15257c478bd9Sstevel@tonic-gate 			pltent += 2;
15267c478bd9Sstevel@tonic-gate 			/* LINTED */
15277c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1528de777a60Sab 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
15297c478bd9Sstevel@tonic-gate 			pltent += 4;
15307c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15317c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_DISP_IND;
15327c478bd9Sstevel@tonic-gate 			pltent += 2;
15337c478bd9Sstevel@tonic-gate 			/* LINTED */
15347c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1535de777a60Sab 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15367c478bd9Sstevel@tonic-gate 		} else {
15377c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15387c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_REG_DISP;
15397c478bd9Sstevel@tonic-gate 			pltent += 2;
15407c478bd9Sstevel@tonic-gate 			/* LINTED */
15417c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1542de777a60Sab 			    M_GOT_ENTSIZE);
15437c478bd9Sstevel@tonic-gate 			pltent += 4;
15447c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15457c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_REG_DISP_IND;
15467c478bd9Sstevel@tonic-gate 			pltent += 2;
15477c478bd9Sstevel@tonic-gate 			/* LINTED */
15487c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1549de777a60Sab 			    M_GOT_ENTSIZE);
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 	return (1);
15537c478bd9Sstevel@tonic-gate }
1554