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 /*
23141040e8Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24b3fbe5e6Sseizo  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include	<string.h>
297c478bd9Sstevel@tonic-gate #include	<stdio.h>
307c478bd9Sstevel@tonic-gate #include	<strings.h>
317c478bd9Sstevel@tonic-gate #include	<sys/elf_amd64.h>
327c478bd9Sstevel@tonic-gate #include	<debug.h>
337c478bd9Sstevel@tonic-gate #include	<reloc.h>
345aefb655Srie #include	"msg.h"
355aefb655Srie #include	"_libld.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate Word
385aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	Rela *	rel = (Rela *)reloc;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	/* LINTED */
437c478bd9Sstevel@tonic-gate 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info);
447c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
457c478bd9Sstevel@tonic-gate 	reld->rel_raddend = rel->r_addend;
467c478bd9Sstevel@tonic-gate 	reld->rel_typedata = 0;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	reld->rel_flags |= FLG_REL_RELA;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate void
545aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
557c478bd9Sstevel@tonic-gate {
565aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate void
605aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
637c478bd9Sstevel@tonic-gate 		/*
647c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
657c478bd9Sstevel@tonic-gate 		 */
667c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
677c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
725aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
737c478bd9Sstevel@tonic-gate {
745aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
755aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
765aefb655Srie 		if (ofl->ofl_osgot)
775aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
785aefb655Srie 		else
795aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
805aefb655Srie 		(*dyn)++;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate Xword
855aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	Xword	value;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
907c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
917c478bd9Sstevel@tonic-gate 	return (value);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
967c478bd9Sstevel@tonic-gate  *	JMP	*name1@GOTPCREL(%rip)
977c478bd9Sstevel@tonic-gate  *	PUSHL	$index
987c478bd9Sstevel@tonic-gate  *	JMP	.PLT0
997c478bd9Sstevel@tonic-gate  */
100b3fbe5e6Sseizo static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
1017c478bd9Sstevel@tonic-gate /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1027c478bd9Sstevel@tonic-gate /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
1037c478bd9Sstevel@tonic-gate /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
1047c478bd9Sstevel@tonic-gate /* 0x10 */
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static uintptr_t
1087c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
1097c478bd9Sstevel@tonic-gate {
110b3fbe5e6Sseizo 	uchar_t		*plt0, *pltent, *gotent;
1117c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1127c478bd9Sstevel@tonic-gate 	Word		got_off;
1137c478bd9Sstevel@tonic-gate 	Xword		val1;
1147c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
1157c478bd9Sstevel@tonic-gate 	Word		dtflags1 = ofl->ofl_dtflags_1;
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 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
1217c478bd9Sstevel@tonic-gate 	pltent = plt0 + plt_off;
122b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1277c478bd9Sstevel@tonic-gate 	 */
1287c478bd9Sstevel@tonic-gate 	/* LINTED */
129b3fbe5e6Sseizo 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
130b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * patchup:
1347c478bd9Sstevel@tonic-gate 	 *	jmpq	*name1@gotpcrel(%rip)
1357c478bd9Sstevel@tonic-gate 	 *
1367c478bd9Sstevel@tonic-gate 	 * NOTE: 0x06 represents next instruction.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
1397c478bd9Sstevel@tonic-gate 		(ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * If '-z noreloc' is specified - skip the do_reloc
1437c478bd9Sstevel@tonic-gate 	 * stage.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_RELOBJ) ||
1467c478bd9Sstevel@tonic-gate 	    !(dtflags1 & DF_1_NORELOC)) {
1477c478bd9Sstevel@tonic-gate 		if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x02],
1487c478bd9Sstevel@tonic-gate 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1495aefb655Srie 		    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
1505aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1515aefb655Srie 			    MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx,
1525aefb655Srie 			    demangle(sdp->sd_name));
1537c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * patchup:
1597c478bd9Sstevel@tonic-gate 	 *	pushq	$pltndx
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * If '-z noreloc' is specified - skip the do_reloc
1647c478bd9Sstevel@tonic-gate 	 * stage.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_RELOBJ) ||
1677c478bd9Sstevel@tonic-gate 	    !(dtflags1 & DF_1_NORELOC)) {
1687c478bd9Sstevel@tonic-gate 		if (do_reloc(R_AMD64_32, &pltent[0x07],
1697c478bd9Sstevel@tonic-gate 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1705aefb655Srie 		    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
1715aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1725aefb655Srie 			    MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx,
1735aefb655Srie 			    demangle(sdp->sd_name));
1747c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * patchup:
1807c478bd9Sstevel@tonic-gate 	 *	jmpq	.plt0(%rip)
181b3fbe5e6Sseizo 	 * NOTE: 0x10 represents next instruction.  The rather complex series
182b3fbe5e6Sseizo 	 * of casts is necessary to sign extend an offset into a 64-bit value
183b3fbe5e6Sseizo 	 * while satisfying various compiler error checks.  Handle with care.
1847c478bd9Sstevel@tonic-gate 	 */
185b3fbe5e6Sseizo 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
186b3fbe5e6Sseizo 	    (uintptr_t)(&pltent[0x10])));
187b3fbe5e6Sseizo 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * If '-z noreloc' is specified - skip the do_reloc
1907c478bd9Sstevel@tonic-gate 	 * stage.
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_RELOBJ) ||
1937c478bd9Sstevel@tonic-gate 	    !(dtflags1 & DF_1_NORELOC)) {
1947c478bd9Sstevel@tonic-gate 		if (do_reloc(R_AMD64_PC32, &pltent[0x0c],
1957c478bd9Sstevel@tonic-gate 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1965aefb655Srie 		    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
1975aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1985aefb655Srie 			    MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx,
1995aefb655Srie 			    demangle(sdp->sd_name));
2007c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate uintptr_t
2075aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
2107c478bd9Sstevel@tonic-gate 	Word		ndx;
2117c478bd9Sstevel@tonic-gate 	Xword		roffset, value;
2127c478bd9Sstevel@tonic-gate 	Sxword		raddend;
2137c478bd9Sstevel@tonic-gate 	Rela		rea;
2147c478bd9Sstevel@tonic-gate 	char		*relbits;
2157c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2167c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	raddend = orsp->rel_raddend;
2197c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
2237c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2267c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2277c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2285aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2297c478bd9Sstevel@tonic-gate 		return (1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
2347c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	if (orsp->rel_move)
2375aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section then we need to adjust the
2417c478bd9Sstevel@tonic-gate 	 * raddend field to compensate for the new position of the input section
2427c478bd9Sstevel@tonic-gate 	 * within the new output section.
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2457c478bd9Sstevel@tonic-gate 		if (ofl->ofl_parsym.head &&
2467c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2477c478bd9Sstevel@tonic-gate 		    /* LINTED */
2485aefb655Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
2495aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2507c478bd9Sstevel@tonic-gate 			sectmoved = 1;
2517c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
2527c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value;
2537c478bd9Sstevel@tonic-gate 			else
2547c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value -
2557c478bd9Sstevel@tonic-gate 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
2567c478bd9Sstevel@tonic-gate 			/* LINTED */
2577c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
2587c478bd9Sstevel@tonic-gate 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
2597c478bd9Sstevel@tonic-gate 				raddend +=
2607c478bd9Sstevel@tonic-gate 				psym->sd_isc->is_osdesc->os_shdr->sh_addr;
2617c478bd9Sstevel@tonic-gate 		} else {
2627c478bd9Sstevel@tonic-gate 			/* LINTED */
2637c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
2647c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
2657c478bd9Sstevel@tonic-gate 				raddend +=
2667c478bd9Sstevel@tonic-gate 				sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
2737c478bd9Sstevel@tonic-gate 		/*
2747c478bd9Sstevel@tonic-gate 		 * Note: for GOT relative relocations on amd64
2757c478bd9Sstevel@tonic-gate 		 *	 we discard the addend.  It was relevant
2767c478bd9Sstevel@tonic-gate 		 *	 to the reference - not to the data item
2777c478bd9Sstevel@tonic-gate 		 *	 being referenced (ie: that -4 thing).
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		raddend = 0;
2807c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
2815aefb655Srie 		roffset = ld_calc_got_offset(orsp, ofl);
2825aefb655Srie 
2837c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2847c478bd9Sstevel@tonic-gate 		/*
2857c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
2867c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
2897c478bd9Sstevel@tonic-gate 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
2907c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2917c478bd9Sstevel@tonic-gate 		raddend = 0;
2927c478bd9Sstevel@tonic-gate 		if (plt_entry(ofl, sdp) == S_ERROR)
2937c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * This must be a R_AMD64_COPY.  For these set the roffset to
2987c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
3017c478bd9Sstevel@tonic-gate 		roffset = value;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		/*
3047c478bd9Sstevel@tonic-gate 		 * The raddend doesn't mean anything in a R_SPARC_COPY
3057c478bd9Sstevel@tonic-gate 		 * relocation.  Null it out because it can confuse people.
3067c478bd9Sstevel@tonic-gate 		 */
3077c478bd9Sstevel@tonic-gate 		raddend = 0;
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		osp = orsp->rel_osdesc;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		/*
3127c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
3137c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
3147c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
3157c478bd9Sstevel@tonic-gate 		 */
3167c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
3177c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3187c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3197c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
3207c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3247c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
3287c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
3297c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
3307c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
3337c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
3347c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3357c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3367c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
3377c478bd9Sstevel@tonic-gate 			/*
3387c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
3397c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
3407c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
3417c478bd9Sstevel@tonic-gate 			 */
3427c478bd9Sstevel@tonic-gate 			if ((sdp->sd_isc != 0) &&
3437c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_osdesc != 0))
3447c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
3457c478bd9Sstevel@tonic-gate 			else
3467c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
3477c478bd9Sstevel@tonic-gate 		} else
3487c478bd9Sstevel@tonic-gate 			ndx = ofl->ofl_sunwdata1ndx;
3497c478bd9Sstevel@tonic-gate 	} else
3507c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Add the symbols 'value' to the addend field.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_ADVAL)
3567c478bd9Sstevel@tonic-gate 		raddend += value;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
3607010c12aSrie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
3637c478bd9Sstevel@tonic-gate 		raddend = 0;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3687c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
3697c478bd9Sstevel@tonic-gate 	rea.r_addend = raddend;
3705aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
3715aefb655Srie 	    orsp->rel_sname));
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3797c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rela));
3807c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += (Xword)sizeof (Rela);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3847c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3857c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
3867c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
3897c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3907c478bd9Sstevel@tonic-gate 
3915aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3927c478bd9Sstevel@tonic-gate 	return (1);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * amd64 Instructions for TLS processing
3977c478bd9Sstevel@tonic-gate  */
398b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4037c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 *	0x09 addq x@gottpoff(%rip), %rax
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	0x48, 0x03, 0x05, 0x00, 0x00,
4087c478bd9Sstevel@tonic-gate 	0x00, 0x00
4097c478bd9Sstevel@tonic-gate };
4107c478bd9Sstevel@tonic-gate 
411b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4147c478bd9Sstevel@tonic-gate 	 */
4157c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4167c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 *	0x09 leaq x@gottpoff(%rip), %rax
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	0x48, 0x8d, 0x80, 0x00, 0x00,
4217c478bd9Sstevel@tonic-gate 	0x00, 0x00
4227c478bd9Sstevel@tonic-gate };
4237c478bd9Sstevel@tonic-gate 
424b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = {
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	0x66,
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	0x66,
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	0x66,
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * movq %fs:0, %rax
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4417c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00
4427c478bd9Sstevel@tonic-gate };
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
4455aefb655Srie static Fixupret
4465aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4497c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
450b3fbe5e6Sseizo 	uchar_t		*offset;
4517c478bd9Sstevel@tonic-gate 
452b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
453b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
454b3fbe5e6Sseizo 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
4577c478bd9Sstevel@tonic-gate 		/*
4587c478bd9Sstevel@tonic-gate 		 * IE reference model
4597c478bd9Sstevel@tonic-gate 		 */
4607c478bd9Sstevel@tonic-gate 		switch (rtype) {
4617c478bd9Sstevel@tonic-gate 		case R_AMD64_TLSGD:
4627c478bd9Sstevel@tonic-gate 			/*
4637c478bd9Sstevel@tonic-gate 			 *  GD -> IE
4647c478bd9Sstevel@tonic-gate 			 *
4657c478bd9Sstevel@tonic-gate 			 * Transition:
4667c478bd9Sstevel@tonic-gate 			 *	0x00 .byte 0x66
4677c478bd9Sstevel@tonic-gate 			 *	0x01 leaq x@tlsgd(%rip), %rdi
4687c478bd9Sstevel@tonic-gate 			 *	0x08 .word 0x6666
4697c478bd9Sstevel@tonic-gate 			 *	0x0a rex64
4707c478bd9Sstevel@tonic-gate 			 *	0x0b call __tls_get_addr@plt
4717c478bd9Sstevel@tonic-gate 			 *	0x10
4727c478bd9Sstevel@tonic-gate 			 * To:
4737c478bd9Sstevel@tonic-gate 			 *	0x00 movq %fs:0, %rax
4747c478bd9Sstevel@tonic-gate 			 *	0x09 addq x@gottpoff(%rip), %rax
4757c478bd9Sstevel@tonic-gate 			 *	0x10
4767c478bd9Sstevel@tonic-gate 			 */
4775aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
478*051d39bbSrie 			    R_AMD64_GOTTPOFF, arsp));
4797c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
4807c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
4817c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
4825aefb655Srie 
4837c478bd9Sstevel@tonic-gate 			/*
484*051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4857c478bd9Sstevel@tonic-gate 			 * sequence.
4867c478bd9Sstevel@tonic-gate 			 */
4877c478bd9Sstevel@tonic-gate 			offset -= 4;
4887c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4895aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4907c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4915aefb655Srie 
4927c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
4937c478bd9Sstevel@tonic-gate 			/*
494*051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
4957c478bd9Sstevel@tonic-gate 			 */
4965aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
497*051d39bbSrie 			    R_AMD64_NONE, arsp));
4987c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * LE reference model
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	switch (rtype) {
5067c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
5077c478bd9Sstevel@tonic-gate 		/*
5087c478bd9Sstevel@tonic-gate 		 * GD -> LE
5097c478bd9Sstevel@tonic-gate 		 *
5107c478bd9Sstevel@tonic-gate 		 * Transition:
5117c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5127c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5137c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
5147c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
5157c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
5167c478bd9Sstevel@tonic-gate 		 *	0x10
5177c478bd9Sstevel@tonic-gate 		 * To:
5187c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5197c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5207c478bd9Sstevel@tonic-gate 		 *	0x10
5217c478bd9Sstevel@tonic-gate 		 */
5225aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
523*051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5247c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5257c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
5267c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5275aefb655Srie 
5287c478bd9Sstevel@tonic-gate 		/*
529*051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5307c478bd9Sstevel@tonic-gate 		 */
5317c478bd9Sstevel@tonic-gate 		offset -= 4;
5325aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5337c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5345aefb655Srie 
5357c478bd9Sstevel@tonic-gate 	case R_AMD64_GOTTPOFF:
5367c478bd9Sstevel@tonic-gate 		/*
5377c478bd9Sstevel@tonic-gate 		 * IE -> LE
5387c478bd9Sstevel@tonic-gate 		 *
5397c478bd9Sstevel@tonic-gate 		 * Transition:
5407c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5417c478bd9Sstevel@tonic-gate 		 *	0x09 addq x@gottopoff(%rip), %rax
5427c478bd9Sstevel@tonic-gate 		 *	0x10
5437c478bd9Sstevel@tonic-gate 		 * To:
5447c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5457c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5467c478bd9Sstevel@tonic-gate 		 *	0x10
5477c478bd9Sstevel@tonic-gate 		 */
548*051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
549*051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5507c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5517c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5525aefb655Srie 
5537c478bd9Sstevel@tonic-gate 		/*
554*051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 		offset -= 12;
5575aefb655Srie 
5587c478bd9Sstevel@tonic-gate 		/*
559*051d39bbSrie 		 * Same code sequence used in the GD -> LE transition.
5607c478bd9Sstevel@tonic-gate 		 */
5615aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5627c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5635aefb655Srie 
5647c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * LD -> LE
5677c478bd9Sstevel@tonic-gate 		 *
5687c478bd9Sstevel@tonic-gate 		 * Transition
5697c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
5707c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
5717c478bd9Sstevel@tonic-gate 		 *	0x0c
5727c478bd9Sstevel@tonic-gate 		 * To:
5737c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5747c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
5757c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
5767c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
5777c478bd9Sstevel@tonic-gate 		 */
578*051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
579*051d39bbSrie 		    R_AMD64_NONE, arsp));
5807c478bd9Sstevel@tonic-gate 		offset -= 3;
5815aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
5827c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5835aefb655Srie 
5847c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
5857c478bd9Sstevel@tonic-gate 		/*
5867c478bd9Sstevel@tonic-gate 		 * LD->LE
5877c478bd9Sstevel@tonic-gate 		 *
5887c478bd9Sstevel@tonic-gate 		 * Transition:
5897c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
5907c478bd9Sstevel@tonic-gate 		 * To:
5917c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
5927c478bd9Sstevel@tonic-gate 		 */
593*051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
594*051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5957c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5967c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5977c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate uintptr_t
6045aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6057c478bd9Sstevel@tonic-gate {
606141040e8Srie 	Rel_desc	*arsp;
607141040e8Srie 	Rel_cache	*rcp;
608141040e8Srie 	Listnode	*lnp;
6097c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6107c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
6117c478bd9Sstevel@tonic-gate 	Word		dtflags1 = ofl->ofl_dtflags_1;
6127c478bd9Sstevel@tonic-gate 
6137010c12aSrie 	if (ofl->ofl_actrels.head)
6147010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6157010c12aSrie 
6167c478bd9Sstevel@tonic-gate 	/*
617141040e8Srie 	 * Process active relocations.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6207c478bd9Sstevel@tonic-gate 		/* LINTED */
6217c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6227c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
623b3fbe5e6Sseizo 			uchar_t		*addr;
6247c478bd9Sstevel@tonic-gate 			Xword 		value;
6257c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
6267c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
6277c478bd9Sstevel@tonic-gate 			Xword		refaddr;
6287c478bd9Sstevel@tonic-gate 			int		moved = 0;
6297c478bd9Sstevel@tonic-gate 			Gotref		gref;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 			/*
6327c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
6337c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
6347c478bd9Sstevel@tonic-gate 			 * relocation itself.
6357c478bd9Sstevel@tonic-gate 			 */
6367c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6377c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
6387c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
6397c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6405aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6415aefb655Srie 				    M_MACH, arsp));
6427c478bd9Sstevel@tonic-gate 				continue;
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 			/*
6467c478bd9Sstevel@tonic-gate 			 * We deteremine what the 'got reference'
6477c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
6487c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
6497c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
6507c478bd9Sstevel@tonic-gate 			 *
6517c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
6527c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
6537c478bd9Sstevel@tonic-gate 			 */
6547c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
6557c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
6567c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
6577c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
6587c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
6597c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
6607c478bd9Sstevel@tonic-gate 			else
6617c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 			/*
6647c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
6657c478bd9Sstevel@tonic-gate 			 */
6667c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
6677c478bd9Sstevel@tonic-gate 				Fixupret	ret;
668141040e8Srie 
6695aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
6707c478bd9Sstevel@tonic-gate 					return (S_ERROR);
6717c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
6727c478bd9Sstevel@tonic-gate 					continue;
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 			/*
6767c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
6777c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
6787c478bd9Sstevel@tonic-gate 			 */
6797c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
6805aefb655Srie 				ld_adj_movereloc(ofl, arsp);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
6837c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
6847c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
6877c478bd9Sstevel@tonic-gate 			    (arsp->rel_flags & FLG_REL_GOTCL))
6887c478bd9Sstevel@tonic-gate 				value = 0;
6897c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
6907c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
691141040e8Srie 				Sym_desc	*sym;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 				/*
6947c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
6957c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
6967c478bd9Sstevel@tonic-gate 				 *
6975aefb655Srie 				 * The second argument of the ld_am_I_partial()
6985aefb655Srie 				 * is the value stored at the target address
6997c478bd9Sstevel@tonic-gate 				 * relocation is going to be applied.
7007c478bd9Sstevel@tonic-gate 				 */
7017c478bd9Sstevel@tonic-gate 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7027c478bd9Sstevel@tonic-gate 				    /* LINTED */
7035aefb655Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
704b3fbe5e6Sseizo 				    ((uchar_t *)
705b3fbe5e6Sseizo 				    arsp->rel_isdesc->is_indata->d_buf +
7067c478bd9Sstevel@tonic-gate 				    arsp->rel_roffset)))) {
7077c478bd9Sstevel@tonic-gate 					/*
7087c478bd9Sstevel@tonic-gate 					 * If the symbol is moved,
7097c478bd9Sstevel@tonic-gate 					 * adjust the value
7107c478bd9Sstevel@tonic-gate 					 */
7117c478bd9Sstevel@tonic-gate 					value = sym->sd_sym->st_value;
7127c478bd9Sstevel@tonic-gate 					moved = 1;
7137c478bd9Sstevel@tonic-gate 				} else {
714141040e8Srie 					value = _elf_getxoff(
715141040e8Srie 					    sdp->sd_isc->is_indata);
7167c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
7177c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
7187c478bd9Sstevel@tonic-gate 					    value += sdp->sd_isc->is_osdesc->
7197c478bd9Sstevel@tonic-gate 					    os_shdr->sh_addr;
7207c478bd9Sstevel@tonic-gate 				}
7217c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7227c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
7232926dd2eSrie 
7242926dd2eSrie 			} else if (IS_SIZE(arsp->rel_rtype)) {
7252926dd2eSrie 				/*
7262926dd2eSrie 				 * Size relocations require the symbols size.
7272926dd2eSrie 				 */
7282926dd2eSrie 				value = sdp->sd_sym->st_size;
729141040e8Srie 			} else {
7307c478bd9Sstevel@tonic-gate 				/*
7317010c12aSrie 				 * Else the value is the symbols value.
7327c478bd9Sstevel@tonic-gate 				 */
7337c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
734141040e8Srie 			}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 			/*
7377c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7387c478bd9Sstevel@tonic-gate 			 */
7397c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
7407c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 			/*
7437c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
7447c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
7457c478bd9Sstevel@tonic-gate 			 * address.
7467c478bd9Sstevel@tonic-gate 			 */
7477c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
748141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
7497c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
7507c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 			/*
7537c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
7547c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
7557c478bd9Sstevel@tonic-gate 			 * not the address of the function).
7567c478bd9Sstevel@tonic-gate 			 */
7577c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
7587c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
7595aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
7607c478bd9Sstevel@tonic-gate 			}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 			/*
7637c478bd9Sstevel@tonic-gate 			 * Add relocations addend to value.  Add extra
7647c478bd9Sstevel@tonic-gate 			 * relocation addend if needed.
7657c478bd9Sstevel@tonic-gate 			 *
7667c478bd9Sstevel@tonic-gate 			 * Note: for GOT relative relocations on amd64
7677c478bd9Sstevel@tonic-gate 			 *	 we discard the addend.  It was relevant
7687c478bd9Sstevel@tonic-gate 			 *	 to the reference - not to the data item
7697c478bd9Sstevel@tonic-gate 			 *	 being referenced (ie: that -4 thing).
7707c478bd9Sstevel@tonic-gate 			 */
7717c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_GOT) == 0)
7727c478bd9Sstevel@tonic-gate 				value += arsp->rel_raddend;
7737c478bd9Sstevel@tonic-gate 
774141040e8Srie 			/*
775141040e8Srie 			 * Determine whether the value needs further adjustment.
776141040e8Srie 			 * Filter through the attributes of the relocation to
777141040e8Srie 			 * determine what adjustment is required.  Note, many
778141040e8Srie 			 * of the following cases are only applicable when a
779141040e8Srie 			 * .got is present.  As a .got is not generated when a
780141040e8Srie 			 * relocatable object is being built, any adjustments
781141040e8Srie 			 * that require a .got need to be skipped.
782141040e8Srie 			 */
783141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
784141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
7857c478bd9Sstevel@tonic-gate 				Xword		R1addr;
7867c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
7877c478bd9Sstevel@tonic-gate 				Word		gotndx;
7887c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 				/*
7917c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
7927c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
7937c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
7947c478bd9Sstevel@tonic-gate 				 * directly
7957c478bd9Sstevel@tonic-gate 				 *
7967c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
7977c478bd9Sstevel@tonic-gate 				 * the relocation.
7987c478bd9Sstevel@tonic-gate 				 */
7995aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8007c478bd9Sstevel@tonic-gate 				    ofl, arsp);
8017c478bd9Sstevel@tonic-gate 				assert(gnp);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_DTPOFF64)
8047c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
8057c478bd9Sstevel@tonic-gate 				else
8067c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 				/*
8117c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
8127c478bd9Sstevel@tonic-gate 				 */
8137c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
8147c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
8157c478bd9Sstevel@tonic-gate 
8165aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8175aefb655Srie 				    ELF_DBG_LD, M_MACH, SHT_RELA,
8187c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
8197c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 				/*
8227c478bd9Sstevel@tonic-gate 				 * And do it.
8237c478bd9Sstevel@tonic-gate 				 */
824141040e8Srie 				*(Xword *)R2addr = value;
8257c478bd9Sstevel@tonic-gate 				continue;
8267c478bd9Sstevel@tonic-gate 
827141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
828141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8297c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
830141040e8Srie 
831141040e8Srie 			} else if (IS_GOTPCREL(arsp->rel_rtype) &&
832141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
833141040e8Srie 				Gotndx *gnp;
834141040e8Srie 
8357c478bd9Sstevel@tonic-gate 				/*
8367c478bd9Sstevel@tonic-gate 				 * Calculation:
8377c478bd9Sstevel@tonic-gate 				 *	G + GOT + A - P
8387c478bd9Sstevel@tonic-gate 				 */
8395aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8407c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
841141040e8Srie 				assert(gnp);
8427c478bd9Sstevel@tonic-gate 				value = (Xword)(ofl->ofl_osgot->os_shdr->
8437c478bd9Sstevel@tonic-gate 				    sh_addr) + ((Xword)gnp->gn_gotndx *
8447c478bd9Sstevel@tonic-gate 				    M_GOT_ENTSIZE) + arsp->rel_raddend -
8457c478bd9Sstevel@tonic-gate 				    refaddr;
846141040e8Srie 
847141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
848141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
849141040e8Srie 				value = (Xword)(ofl->ofl_osgot->os_shdr->
85054d82594Sseizo 				    sh_addr) - refaddr + arsp->rel_raddend;
851141040e8Srie 
8527c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
853141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
8547c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
8557c478bd9Sstevel@tonic-gate 				value -= refaddr;
856141040e8Srie 
8577c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
858141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
859141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8607c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
8617c478bd9Sstevel@tonic-gate 
8625aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8637c478bd9Sstevel@tonic-gate 				    ofl, arsp);
864141040e8Srie 				assert(gnp);
8657c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
866141040e8Srie 
867141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
868141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
869141040e8Srie 				Gotndx *gnp;
8707c478bd9Sstevel@tonic-gate 
8715aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8727c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
8737c478bd9Sstevel@tonic-gate 				assert(gnp);
8747c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
875141040e8Srie 
876141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
877141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8787c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
879141040e8Srie 
8807c478bd9Sstevel@tonic-gate 				/*
8817c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
8827c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
8837c478bd9Sstevel@tonic-gate 				 */
884141040e8Srie 				tlsstatsize =
885141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
8867c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
8877c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
888141040e8Srie 
8897c478bd9Sstevel@tonic-gate 				/*
890141040e8Srie 				 * Since this code is fixed up, it assumes a
891141040e8Srie 				 * negative offset that can be added to the
892141040e8Srie 				 * thread pointer.
8937c478bd9Sstevel@tonic-gate 				 */
8947c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_TPOFF32)
8957c478bd9Sstevel@tonic-gate 					value = -value;
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
8997c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9007c478bd9Sstevel@tonic-gate 			else
9017c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 			/*
9047c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9057c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9067c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
9077c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
9087c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
9097c478bd9Sstevel@tonic-gate 			 * see this.
9107c478bd9Sstevel@tonic-gate 			 */
9117c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
9125aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9135aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
914c13de8f6Sab 				    conv_reloc_amd64_type(arsp->rel_rtype, 0),
9157c478bd9Sstevel@tonic-gate 				    ifl_name, demangle(arsp->rel_sname),
9167c478bd9Sstevel@tonic-gate 				    arsp->rel_isdesc->is_name);
9177c478bd9Sstevel@tonic-gate 				return (S_ERROR);
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 			/*
9217c478bd9Sstevel@tonic-gate 			 * Get the address of the data item we need to modify.
9227c478bd9Sstevel@tonic-gate 			 */
923b3fbe5e6Sseizo 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
924b3fbe5e6Sseizo 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
925b3fbe5e6Sseizo 			    is_indata));
9267c478bd9Sstevel@tonic-gate 
9275aefb655Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9285aefb655Srie 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
9295aefb655Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9307c478bd9Sstevel@tonic-gate 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9317c478bd9Sstevel@tonic-gate 
9325aefb655Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9337c478bd9Sstevel@tonic-gate 			    ofl->ofl_size) || (arsp->rel_roffset >
9347c478bd9Sstevel@tonic-gate 			    arsp->rel_osdesc->os_shdr->sh_size)) {
9357c478bd9Sstevel@tonic-gate 				int	class;
9367c478bd9Sstevel@tonic-gate 
937b3fbe5e6Sseizo 				if (((uintptr_t)addr -
9385aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
9397c478bd9Sstevel@tonic-gate 					class = ERR_FATAL;
9407c478bd9Sstevel@tonic-gate 				else
9417c478bd9Sstevel@tonic-gate 					class = ERR_WARNING;
9427c478bd9Sstevel@tonic-gate 
9435aefb655Srie 				eprintf(ofl->ofl_lml, class,
9445aefb655Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
945c13de8f6Sab 				    conv_reloc_amd64_type(arsp->rel_rtype, 0),
9467c478bd9Sstevel@tonic-gate 				    ifl_name, arsp->rel_isdesc->is_name,
9477c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
9487c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
9495aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
9527c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9537c478bd9Sstevel@tonic-gate 					continue;
9547c478bd9Sstevel@tonic-gate 				}
9557c478bd9Sstevel@tonic-gate 			}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 			/*
9587c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
9597c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
9607c478bd9Sstevel@tonic-gate 			 * expanded.
9617c478bd9Sstevel@tonic-gate 			 */
9627c478bd9Sstevel@tonic-gate 			if (moved)
9637c478bd9Sstevel@tonic-gate 				value -= *addr;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 			/*
9667c478bd9Sstevel@tonic-gate 			 * If '-z noreloc' is specified - skip the do_reloc
9677c478bd9Sstevel@tonic-gate 			 * stage.
9687c478bd9Sstevel@tonic-gate 			 */
9697c478bd9Sstevel@tonic-gate 			if ((flags & FLG_OF_RELOBJ) ||
9707c478bd9Sstevel@tonic-gate 			    !(dtflags1 & DF_1_NORELOC)) {
971b3fbe5e6Sseizo 				if (do_reloc((uchar_t)arsp->rel_rtype,
9725aefb655Srie 				    addr, &value, arsp->rel_sname, ifl_name,
9735aefb655Srie 				    ofl->ofl_lml) == 0)
9747c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9757c478bd9Sstevel@tonic-gate 			}
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 	return (return_code);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate uintptr_t
9825aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
9837c478bd9Sstevel@tonic-gate {
984141040e8Srie 	Rel_desc	*orsp;
985141040e8Srie 	Rel_cache	*rcp;
986141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/*
9897c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
9907c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
9917c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
9927c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
9937c478bd9Sstevel@tonic-gate 	 */
9947c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
9957c478bd9Sstevel@tonic-gate 	    (FLG_OF_STATIC | FLG_OF_EXEC))
9967c478bd9Sstevel@tonic-gate 		return (1);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * If no relocation cache structures are available allocate
10007c478bd9Sstevel@tonic-gate 	 * a new one and link it into the cache list.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_outrels.tail == 0) ||
10037c478bd9Sstevel@tonic-gate 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10047c478bd9Sstevel@tonic-gate 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10057c478bd9Sstevel@tonic-gate 		static size_t	nextsize = 0;
10067c478bd9Sstevel@tonic-gate 		size_t		size;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		/*
10097c478bd9Sstevel@tonic-gate 		 * Output relocation numbers can vary considerably between
10107c478bd9Sstevel@tonic-gate 		 * building executables or shared objects (pic vs. non-pic),
10117c478bd9Sstevel@tonic-gate 		 * etc.  But, they typically aren't very large, so for these
10127c478bd9Sstevel@tonic-gate 		 * objects use a standard bucket size.  For building relocatable
10137c478bd9Sstevel@tonic-gate 		 * objects, typically there will be an output relocation for
10147c478bd9Sstevel@tonic-gate 		 * every input relocation.
10157c478bd9Sstevel@tonic-gate 		 */
10167c478bd9Sstevel@tonic-gate 		if (nextsize == 0) {
10177c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10187c478bd9Sstevel@tonic-gate 				if ((size = ofl->ofl_relocincnt) == 0)
10197c478bd9Sstevel@tonic-gate 					size = REL_LOIDESCNO;
10207c478bd9Sstevel@tonic-gate 				if (size > REL_HOIDESCNO)
10217c478bd9Sstevel@tonic-gate 					nextsize = REL_HOIDESCNO;
10227c478bd9Sstevel@tonic-gate 				else
10237c478bd9Sstevel@tonic-gate 					nextsize = REL_LOIDESCNO;
10247c478bd9Sstevel@tonic-gate 			} else
10257c478bd9Sstevel@tonic-gate 				nextsize = size = REL_HOIDESCNO;
10267c478bd9Sstevel@tonic-gate 		} else
10277c478bd9Sstevel@tonic-gate 			size = nextsize;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		size = size * sizeof (Rel_desc);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
10327c478bd9Sstevel@tonic-gate 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
10337c478bd9Sstevel@tonic-gate 			return (S_ERROR);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		/* LINTED */
10367c478bd9Sstevel@tonic-gate 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
10377c478bd9Sstevel@tonic-gate 		/* LINTED */
10387c478bd9Sstevel@tonic-gate 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/*
10427c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
10437c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
10447c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10477c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
10487c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		/*
10517c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
10527c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
10537c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
10547c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
10557c478bd9Sstevel@tonic-gate 		 */
10567c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10570bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10587c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10597c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10607c478bd9Sstevel@tonic-gate 			else
10617c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1062141040e8Srie 		} else {
1063141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
10647c478bd9Sstevel@tonic-gate 
1065c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
10667c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
10677c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
10687c478bd9Sstevel@tonic-gate 			}
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
10737c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
10767c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
10797c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
10807c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
10817c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
10827c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
10837c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
10847c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
10857c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
10867c478bd9Sstevel@tonic-gate 	else
10877c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
10907c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
10947c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
10957c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
10967c478bd9Sstevel@tonic-gate 	 */
10977c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
10987c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
10997c478bd9Sstevel@tonic-gate 
1100141040e8Srie 	/*
1101141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1102141040e8Srie 	 */
1103141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1104141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1105141040e8Srie 
11067c478bd9Sstevel@tonic-gate 	/*
11077c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11087c478bd9Sstevel@tonic-gate 	 */
11097c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11107c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11135aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11147c478bd9Sstevel@tonic-gate 	}
11155aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11165aefb655Srie 	    M_MACH, orsp));
11177c478bd9Sstevel@tonic-gate 	return (1);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /*
11217c478bd9Sstevel@tonic-gate  * Stub routine since register symbols are not supported on amd64.
11227c478bd9Sstevel@tonic-gate  */
11237c478bd9Sstevel@tonic-gate /* ARGSUSED */
11247c478bd9Sstevel@tonic-gate uintptr_t
11255aefb655Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
11267c478bd9Sstevel@tonic-gate {
11275aefb655Srie 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
11287c478bd9Sstevel@tonic-gate 	return (S_ERROR);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11337c478bd9Sstevel@tonic-gate  */
11347c478bd9Sstevel@tonic-gate uintptr_t
11355aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11367c478bd9Sstevel@tonic-gate {
11377c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
11387c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11390bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11407c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/*
11437c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
11447c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
11457c478bd9Sstevel@tonic-gate 	 * then
11467c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
11477c478bd9Sstevel@tonic-gate 	 * fi
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11502926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
11517c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11527c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
11537c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11547c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11557c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 		/*
11587c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11597c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
11607c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
11617c478bd9Sstevel@tonic-gate 		 */
11627c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11635aefb655Srie 			return (ld_add_outrel(NULL, rsp, ofl));
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
11675aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11687c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11697c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
11707c478bd9Sstevel@tonic-gate 		return (1);
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
1173b3fbe5e6Sseizo 	/*
1174b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1175b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1176b3fbe5e6Sseizo 	 * message.
1177b3fbe5e6Sseizo 	 *
1178b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1179b3fbe5e6Sseizo 	 *	a) it's undefined
1180b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1181b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1182b3fbe5e6Sseizo 	 *
1183b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1184b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1185b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1186b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1187b3fbe5e6Sseizo 	 */
11887c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1189b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1190b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1191b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1192b3fbe5e6Sseizo 		/*
1193b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1194b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1195b3fbe5e6Sseizo 		 * can not be resolved.
1196b3fbe5e6Sseizo 		 */
1197b3fbe5e6Sseizo 		if (rsp->rel_osdesc &&
1198b3fbe5e6Sseizo 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1199b3fbe5e6Sseizo 			return (0);
12005aefb655Srie 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
12015aefb655Srie 		    MSG_INTL(MSG_REL_EXTERNSYM),
1202c13de8f6Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0),
12037c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12047c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12057c478bd9Sstevel@tonic-gate 		return (1);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12107c478bd9Sstevel@tonic-gate 	 */
12115aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate uintptr_t
12167c478bd9Sstevel@tonic-gate /* ARGSUSED */
12175aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	/*
12207c478bd9Sstevel@tonic-gate 	 * Stub routine for common code compatibility, we shouldn't
12217c478bd9Sstevel@tonic-gate 	 * actually get here on amd64.
12227c478bd9Sstevel@tonic-gate 	 */
1223d326b23bSrie 	assert(0);
12247c478bd9Sstevel@tonic-gate 	return (S_ERROR);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate uintptr_t
12285aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12317c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12327c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
12337c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
1236d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1237d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12387c478bd9Sstevel@tonic-gate 	 */
1239d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12407c478bd9Sstevel@tonic-gate 		/*
1241d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12427c478bd9Sstevel@tonic-gate 		 */
12437c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12447c478bd9Sstevel@tonic-gate 
1245d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12467c478bd9Sstevel@tonic-gate 			/*
1247d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12487c478bd9Sstevel@tonic-gate 			 */
1249d326b23bSrie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1250d326b23bSrie 			    GOT_REF_TLSIE, ofl, rsp)) == 0) {
12517c478bd9Sstevel@tonic-gate 
1252d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1253d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1254d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1255d326b23bSrie 					return (S_ERROR);
1256d326b23bSrie 			}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			/*
1259d326b23bSrie 			 * IE access model.
12607c478bd9Sstevel@tonic-gate 			 */
12617c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
12625aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 			/*
1265d326b23bSrie 			 * Fixups are required for other executable models.
12667c478bd9Sstevel@tonic-gate 			 */
12675aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12687c478bd9Sstevel@tonic-gate 			    rsp, ofl));
12697c478bd9Sstevel@tonic-gate 		}
1270d326b23bSrie 
12717c478bd9Sstevel@tonic-gate 		/*
1272d326b23bSrie 		 * LE access model.
12737c478bd9Sstevel@tonic-gate 		 */
12747c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
12755aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1276d326b23bSrie 
12775aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12785aefb655Srie 		    rsp, ofl));
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
1282d326b23bSrie 	 * Building a shared object.
1283d326b23bSrie 	 *
1284d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
12857c478bd9Sstevel@tonic-gate 	 */
12865aefb655Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12877c478bd9Sstevel@tonic-gate 	    GOT_REF_TLSLD, ofl, rsp)) == 0)) {
1288d326b23bSrie 
1289d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1290d326b23bSrie 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR)
12917c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1292d326b23bSrie 
12935aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
1294d326b23bSrie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1295d326b23bSrie 	    ofl, rsp)) == 0)) {
1296d326b23bSrie 
1297d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1298d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1299d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13007c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13045aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13057c478bd9Sstevel@tonic-gate 
13065aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
13107c478bd9Sstevel@tonic-gate Gotndx *
13115aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
13147c478bd9Sstevel@tonic-gate 	Gotndx *	gnp;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	assert(rdesc != 0);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
13197c478bd9Sstevel@tonic-gate 		return (ofl->ofl_tlsldgotndx);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
13227c478bd9Sstevel@tonic-gate 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
13237c478bd9Sstevel@tonic-gate 		    (gnp->gn_gotref == gref)) {
13247c478bd9Sstevel@tonic-gate 			return (gnp);
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 	return ((Gotndx *)0);
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate Xword
13315aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	Os_desc		*osp = ofl->ofl_osgot;
13347c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rdesc->rel_sym;
13357c478bd9Sstevel@tonic-gate 	Xword		gotndx;
13367c478bd9Sstevel@tonic-gate 	Gotref		gref;
13377c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (rdesc->rel_flags & FLG_REL_DTLS)
13407c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSGD;
13417c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_MTLS)
13427c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSLD;
13437c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_STLS)
13447c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSIE;
13457c478bd9Sstevel@tonic-gate 	else
13467c478bd9Sstevel@tonic-gate 		gref = GOT_REF_GENERIC;
13477c478bd9Sstevel@tonic-gate 
13485aefb655Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
13497c478bd9Sstevel@tonic-gate 	assert(gnp);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	gotndx = (Xword)gnp->gn_gotndx;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
13547c478bd9Sstevel@tonic-gate 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
13557c478bd9Sstevel@tonic-gate 		gotndx++;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
13627c478bd9Sstevel@tonic-gate uintptr_t
1363d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
13647c478bd9Sstevel@tonic-gate     Rel_desc * rsp, Sym_desc * sdp)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate 	Xword		raddend;
13677c478bd9Sstevel@tonic-gate 	Gotndx		*gnp, *_gnp;
13687c478bd9Sstevel@tonic-gate 	Listnode	*lnp, *plnp;
13697c478bd9Sstevel@tonic-gate 	uint_t		gotents;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
13727c478bd9Sstevel@tonic-gate 	if (pgnp && (pgnp->gn_addend == raddend) &&
13737c478bd9Sstevel@tonic-gate 	    (pgnp->gn_gotref == gref))
13747c478bd9Sstevel@tonic-gate 		return (1);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13777c478bd9Sstevel@tonic-gate 		gotents = 2;
13787c478bd9Sstevel@tonic-gate 	else
13797c478bd9Sstevel@tonic-gate 		gotents = 1;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	plnp = 0;
13827c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
13837c478bd9Sstevel@tonic-gate 		if (_gnp->gn_addend > raddend)
13847c478bd9Sstevel@tonic-gate 			break;
13857c478bd9Sstevel@tonic-gate 		plnp = lnp;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/*
13897c478bd9Sstevel@tonic-gate 	 * Allocate a new entry.
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
13927c478bd9Sstevel@tonic-gate 		return (S_ERROR);
13937c478bd9Sstevel@tonic-gate 	gnp->gn_addend = raddend;
13947c478bd9Sstevel@tonic-gate 	gnp->gn_gotndx = ofl->ofl_gotcnt;
13957c478bd9Sstevel@tonic-gate 	gnp->gn_gotref = gref;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
14007c478bd9Sstevel@tonic-gate 		ofl->ofl_tlsldgotndx = gnp;
14017c478bd9Sstevel@tonic-gate 		return (1);
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (plnp == 0) {
14057c478bd9Sstevel@tonic-gate 		/*
14067c478bd9Sstevel@tonic-gate 		 * Insert at head of list
14077c478bd9Sstevel@tonic-gate 		 */
14087c478bd9Sstevel@tonic-gate 		if (list_prependc(lst, (void *)gnp) == 0)
14097c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14107c478bd9Sstevel@tonic-gate 	} else if (_gnp->gn_addend > raddend) {
14117c478bd9Sstevel@tonic-gate 		/*
14127c478bd9Sstevel@tonic-gate 		 * Insert in middle of lest
14137c478bd9Sstevel@tonic-gate 		 */
14147c478bd9Sstevel@tonic-gate 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
14157c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14167c478bd9Sstevel@tonic-gate 	} else {
14177c478bd9Sstevel@tonic-gate 		/*
14187c478bd9Sstevel@tonic-gate 		 * Append to tail of list
14197c478bd9Sstevel@tonic-gate 		 */
14207c478bd9Sstevel@tonic-gate 		if (list_appendc(lst, (void *)gnp) == 0)
14217c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 	return (1);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate void
14275aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14307c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1431141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14327c478bd9Sstevel@tonic-gate }
14337c478bd9Sstevel@tonic-gate 
1434b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14357c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14367c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14377c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
14387c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
14397c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
14407c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
14417c478bd9Sstevel@tonic-gate };
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate uintptr_t
1447d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	Word	flags = ofl->ofl_flags;
14507c478bd9Sstevel@tonic-gate 	Word	dtflags1 = ofl->ofl_dtflags_1;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1453d326b23bSrie 		Sym_desc	*sdp;
14547c478bd9Sstevel@tonic-gate 
14555aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14567c478bd9Sstevel@tonic-gate 		    SYM_NOHASH, 0, ofl)) != NULL) {
1457d326b23bSrie 			uchar_t	*genptr;
1458d326b23bSrie 
1459d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14607c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14617c478bd9Sstevel@tonic-gate 			/* LINTED */
14627c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
14637c478bd9Sstevel@tonic-gate 		}
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14687c478bd9Sstevel@tonic-gate 	 * entry is:
14697c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14707c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14717c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
14727c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
14737c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
14747c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
14757c478bd9Sstevel@tonic-gate 	 */
14767c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1477d326b23bSrie 		uchar_t	*pltent;
1478d326b23bSrie 		Xword	val1;
14797c478bd9Sstevel@tonic-gate 
1480b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14817c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * filin:
14857c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
14867c478bd9Sstevel@tonic-gate 		 *
14877c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
14887c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
14897c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
14907c478bd9Sstevel@tonic-gate 		 */
14917c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
14927c478bd9Sstevel@tonic-gate 			(M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
14937c478bd9Sstevel@tonic-gate 			ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 		/*
14967c478bd9Sstevel@tonic-gate 		 * If '-z noreloc' is specified - skip the do_reloc
14977c478bd9Sstevel@tonic-gate 		 * stage.
14987c478bd9Sstevel@tonic-gate 		 */
14997c478bd9Sstevel@tonic-gate 		if ((flags & FLG_OF_RELOBJ) ||
15007c478bd9Sstevel@tonic-gate 		    !(dtflags1 & DF_1_NORELOC)) {
15017c478bd9Sstevel@tonic-gate 			if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x02],
15027c478bd9Sstevel@tonic-gate 			    &val1, MSG_ORIG(MSG_SYM_PLTENT),
15035aefb655Srie 			    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
15045aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
15055aefb655Srie 				    MSG_INTL(MSG_PLT_PLT0FAIL));
15067c478bd9Sstevel@tonic-gate 				return (S_ERROR);
15077c478bd9Sstevel@tonic-gate 			}
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 		/*
15117c478bd9Sstevel@tonic-gate 		 * filin:
15127c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
15137c478bd9Sstevel@tonic-gate 		 */
15147c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15157c478bd9Sstevel@tonic-gate 			(M_GOT_XRTLD * M_GOT_ENTSIZE) -
15167c478bd9Sstevel@tonic-gate 			ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
15177c478bd9Sstevel@tonic-gate 		/*
15187c478bd9Sstevel@tonic-gate 		 * If '-z noreloc' is specified - skip the do_reloc
15197c478bd9Sstevel@tonic-gate 		 * stage.
15207c478bd9Sstevel@tonic-gate 		 */
15217c478bd9Sstevel@tonic-gate 		if ((flags & FLG_OF_RELOBJ) ||
15227c478bd9Sstevel@tonic-gate 		    !(dtflags1 & DF_1_NORELOC)) {
15237c478bd9Sstevel@tonic-gate 			if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x08],
15247c478bd9Sstevel@tonic-gate 			    &val1, MSG_ORIG(MSG_SYM_PLTENT),
15255aefb655Srie 			    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
15265aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
15275aefb655Srie 				    MSG_INTL(MSG_PLT_PLT0FAIL));
15287c478bd9Sstevel@tonic-gate 				return (S_ERROR);
15297c478bd9Sstevel@tonic-gate 			}
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	return (1);
15337c478bd9Sstevel@tonic-gate }
1534