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 	/*
359*7010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
360*7010c12aSrie 	 * 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,
4785aefb655Srie 			    rtype, R_AMD64_GOTTPOFF, arsp->rel_roffset,
4795aefb655Srie 			    sdp->sd_name));
4807c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
4817c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
4827c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
4835aefb655Srie 
4847c478bd9Sstevel@tonic-gate 			/*
4857c478bd9Sstevel@tonic-gate 			 * Addjust 'offset' to beginning of instruction
4867c478bd9Sstevel@tonic-gate 			 * sequence.
4877c478bd9Sstevel@tonic-gate 			 */
4887c478bd9Sstevel@tonic-gate 			offset -= 4;
4897c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4905aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4917c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4925aefb655Srie 
4937c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
4947c478bd9Sstevel@tonic-gate 			/*
4957c478bd9Sstevel@tonic-gate 			 * Fixup done via the TLS_GD relocation
4967c478bd9Sstevel@tonic-gate 			 */
4975aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4985aefb655Srie 			    rtype, R_AMD64_NONE, arsp->rel_roffset,
4995aefb655Srie 			    sdp->sd_name));
5007c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * LE reference model
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	switch (rtype) {
5087c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * GD -> LE
5117c478bd9Sstevel@tonic-gate 		 *
5127c478bd9Sstevel@tonic-gate 		 * Transition:
5137c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5147c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5157c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
5167c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
5177c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
5187c478bd9Sstevel@tonic-gate 		 *	0x10
5197c478bd9Sstevel@tonic-gate 		 * To:
5207c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5217c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5227c478bd9Sstevel@tonic-gate 		 *	0x10
5237c478bd9Sstevel@tonic-gate 		 */
5245aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5255aefb655Srie 		    rtype, R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name));
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5287c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
5297c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5305aefb655Srie 
5317c478bd9Sstevel@tonic-gate 		/*
5327c478bd9Sstevel@tonic-gate 		 * Addjust 'offset' to beginning of instruction
5337c478bd9Sstevel@tonic-gate 		 * sequence.
5347c478bd9Sstevel@tonic-gate 		 */
5357c478bd9Sstevel@tonic-gate 		offset -= 4;
5365aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5377c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5385aefb655Srie 
5397c478bd9Sstevel@tonic-gate 	case R_AMD64_GOTTPOFF:
5407c478bd9Sstevel@tonic-gate 		/*
5417c478bd9Sstevel@tonic-gate 		 * IE -> LE
5427c478bd9Sstevel@tonic-gate 		 *
5437c478bd9Sstevel@tonic-gate 		 * Transition:
5447c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5457c478bd9Sstevel@tonic-gate 		 *	0x09 addq x@gottopoff(%rip), %rax
5467c478bd9Sstevel@tonic-gate 		 *	0x10
5477c478bd9Sstevel@tonic-gate 		 * To:
5487c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5497c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5507c478bd9Sstevel@tonic-gate 		 *	0x10
5517c478bd9Sstevel@tonic-gate 		 */
5525aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
5535aefb655Srie 		    R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name));
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5567c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5575aefb655Srie 
5587c478bd9Sstevel@tonic-gate 		/*
5597c478bd9Sstevel@tonic-gate 		 * Addjust 'offset' to beginning of instruction
5607c478bd9Sstevel@tonic-gate 		 * sequence.
5617c478bd9Sstevel@tonic-gate 		 */
5627c478bd9Sstevel@tonic-gate 		offset -= 12;
5635aefb655Srie 
5647c478bd9Sstevel@tonic-gate 		/*
5657c478bd9Sstevel@tonic-gate 		 * Same code sequence used in the GD -> LE
5667c478bd9Sstevel@tonic-gate 		 * transition.
5677c478bd9Sstevel@tonic-gate 		 */
5685aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5697c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5705aefb655Srie 
5717c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
5727c478bd9Sstevel@tonic-gate 		/*
5737c478bd9Sstevel@tonic-gate 		 * LD -> LE
5747c478bd9Sstevel@tonic-gate 		 *
5757c478bd9Sstevel@tonic-gate 		 * Transition
5767c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
5777c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
5787c478bd9Sstevel@tonic-gate 		 *	0x0c
5797c478bd9Sstevel@tonic-gate 		 * To:
5807c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5817c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
5827c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
5837c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
5847c478bd9Sstevel@tonic-gate 		 */
5855aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
5865aefb655Srie 		    R_AMD64_NONE, arsp->rel_roffset, sdp->sd_name));
5877c478bd9Sstevel@tonic-gate 		offset -= 3;
5885aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
5897c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5905aefb655Srie 
5917c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
5927c478bd9Sstevel@tonic-gate 		/*
5937c478bd9Sstevel@tonic-gate 		 * LD->LE
5947c478bd9Sstevel@tonic-gate 		 *
5957c478bd9Sstevel@tonic-gate 		 * Transition:
5967c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
5977c478bd9Sstevel@tonic-gate 		 * To:
5987c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
5997c478bd9Sstevel@tonic-gate 		 */
6005aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
6015aefb655Srie 		    R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name));
6027c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6037c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6047c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate uintptr_t
6115aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6127c478bd9Sstevel@tonic-gate {
613141040e8Srie 	Rel_desc	*arsp;
614141040e8Srie 	Rel_cache	*rcp;
615141040e8Srie 	Listnode	*lnp;
6167c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6177c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
6187c478bd9Sstevel@tonic-gate 	Word		dtflags1 = ofl->ofl_dtflags_1;
6197c478bd9Sstevel@tonic-gate 
620*7010c12aSrie 	if (ofl->ofl_actrels.head)
621*7010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
622*7010c12aSrie 
6237c478bd9Sstevel@tonic-gate 	/*
624141040e8Srie 	 * Process active relocations.
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6277c478bd9Sstevel@tonic-gate 		/* LINTED */
6287c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6297c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
630b3fbe5e6Sseizo 			uchar_t		*addr;
6317c478bd9Sstevel@tonic-gate 			Xword 		value;
6327c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
6337c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
6347c478bd9Sstevel@tonic-gate 			Xword		refaddr;
6357c478bd9Sstevel@tonic-gate 			int		moved = 0;
6367c478bd9Sstevel@tonic-gate 			Gotref		gref;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			/*
6397c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
6407c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
6417c478bd9Sstevel@tonic-gate 			 * relocation itself.
6427c478bd9Sstevel@tonic-gate 			 */
6437c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6447c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
6457c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
6467c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6475aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6485aefb655Srie 				    M_MACH, arsp));
6497c478bd9Sstevel@tonic-gate 				continue;
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			/*
6537c478bd9Sstevel@tonic-gate 			 * We deteremine what the 'got reference'
6547c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
6557c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
6567c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
6577c478bd9Sstevel@tonic-gate 			 *
6587c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
6597c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
6607c478bd9Sstevel@tonic-gate 			 */
6617c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
6627c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
6637c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
6647c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
6657c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
6667c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
6677c478bd9Sstevel@tonic-gate 			else
6687c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
6727c478bd9Sstevel@tonic-gate 			 */
6737c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
6747c478bd9Sstevel@tonic-gate 				Fixupret	ret;
675141040e8Srie 
6765aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
6777c478bd9Sstevel@tonic-gate 					return (S_ERROR);
6787c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
6797c478bd9Sstevel@tonic-gate 					continue;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			/*
6837c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
6847c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
6857c478bd9Sstevel@tonic-gate 			 */
6867c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
6875aefb655Srie 				ld_adj_movereloc(ofl, arsp);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
6907c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
6917c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
6947c478bd9Sstevel@tonic-gate 			    (arsp->rel_flags & FLG_REL_GOTCL))
6957c478bd9Sstevel@tonic-gate 				value = 0;
6967c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
6977c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
698141040e8Srie 				Sym_desc	*sym;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 				/*
7017c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
7027c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
7037c478bd9Sstevel@tonic-gate 				 *
7045aefb655Srie 				 * The second argument of the ld_am_I_partial()
7055aefb655Srie 				 * is the value stored at the target address
7067c478bd9Sstevel@tonic-gate 				 * relocation is going to be applied.
7077c478bd9Sstevel@tonic-gate 				 */
7087c478bd9Sstevel@tonic-gate 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7097c478bd9Sstevel@tonic-gate 				    /* LINTED */
7105aefb655Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
711b3fbe5e6Sseizo 				    ((uchar_t *)
712b3fbe5e6Sseizo 				    arsp->rel_isdesc->is_indata->d_buf +
7137c478bd9Sstevel@tonic-gate 				    arsp->rel_roffset)))) {
7147c478bd9Sstevel@tonic-gate 					/*
7157c478bd9Sstevel@tonic-gate 					 * If the symbol is moved,
7167c478bd9Sstevel@tonic-gate 					 * adjust the value
7177c478bd9Sstevel@tonic-gate 					 */
7187c478bd9Sstevel@tonic-gate 					value = sym->sd_sym->st_value;
7197c478bd9Sstevel@tonic-gate 					moved = 1;
7207c478bd9Sstevel@tonic-gate 				} else {
721141040e8Srie 					value = _elf_getxoff(
722141040e8Srie 					    sdp->sd_isc->is_indata);
7237c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
7247c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
7257c478bd9Sstevel@tonic-gate 					    value += sdp->sd_isc->is_osdesc->
7267c478bd9Sstevel@tonic-gate 					    os_shdr->sh_addr;
7277c478bd9Sstevel@tonic-gate 				}
7287c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7297c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
730141040e8Srie 			} else {
7317c478bd9Sstevel@tonic-gate 				/*
732*7010c12aSrie 				 * Else the value is the symbols value.
7337c478bd9Sstevel@tonic-gate 				 */
7347c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
735141040e8Srie 			}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 			/*
7387c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7397c478bd9Sstevel@tonic-gate 			 */
7407c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
7417c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			/*
7447c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
7457c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
7467c478bd9Sstevel@tonic-gate 			 * address.
7477c478bd9Sstevel@tonic-gate 			 */
7487c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
749141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
7507c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
7517c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			/*
7547c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
7557c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
7567c478bd9Sstevel@tonic-gate 			 * not the address of the function).
7577c478bd9Sstevel@tonic-gate 			 */
7587c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
7597c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
7605aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 			/*
7647c478bd9Sstevel@tonic-gate 			 * Add relocations addend to value.  Add extra
7657c478bd9Sstevel@tonic-gate 			 * relocation addend if needed.
7667c478bd9Sstevel@tonic-gate 			 *
7677c478bd9Sstevel@tonic-gate 			 * Note: for GOT relative relocations on amd64
7687c478bd9Sstevel@tonic-gate 			 *	 we discard the addend.  It was relevant
7697c478bd9Sstevel@tonic-gate 			 *	 to the reference - not to the data item
7707c478bd9Sstevel@tonic-gate 			 *	 being referenced (ie: that -4 thing).
7717c478bd9Sstevel@tonic-gate 			 */
7727c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_GOT) == 0)
7737c478bd9Sstevel@tonic-gate 				value += arsp->rel_raddend;
7747c478bd9Sstevel@tonic-gate 
775141040e8Srie 			/*
776141040e8Srie 			 * Determine whether the value needs further adjustment.
777141040e8Srie 			 * Filter through the attributes of the relocation to
778141040e8Srie 			 * determine what adjustment is required.  Note, many
779141040e8Srie 			 * of the following cases are only applicable when a
780141040e8Srie 			 * .got is present.  As a .got is not generated when a
781141040e8Srie 			 * relocatable object is being built, any adjustments
782141040e8Srie 			 * that require a .got need to be skipped.
783141040e8Srie 			 */
784141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
785141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
7867c478bd9Sstevel@tonic-gate 				Xword		R1addr;
7877c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
7887c478bd9Sstevel@tonic-gate 				Word		gotndx;
7897c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 				/*
7927c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
7937c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
7947c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
7957c478bd9Sstevel@tonic-gate 				 * directly
7967c478bd9Sstevel@tonic-gate 				 *
7977c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
7987c478bd9Sstevel@tonic-gate 				 * the relocation.
7997c478bd9Sstevel@tonic-gate 				 */
8005aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8017c478bd9Sstevel@tonic-gate 				    ofl, arsp);
8027c478bd9Sstevel@tonic-gate 				assert(gnp);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_DTPOFF64)
8057c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
8067c478bd9Sstevel@tonic-gate 				else
8077c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 				/*
8127c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
8137c478bd9Sstevel@tonic-gate 				 */
8147c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
8157c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
8167c478bd9Sstevel@tonic-gate 
8175aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8185aefb655Srie 				    ELF_DBG_LD, M_MACH, SHT_RELA,
8197c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
8207c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 				/*
8237c478bd9Sstevel@tonic-gate 				 * And do it.
8247c478bd9Sstevel@tonic-gate 				 */
825141040e8Srie 				*(Xword *)R2addr = value;
8267c478bd9Sstevel@tonic-gate 				continue;
8277c478bd9Sstevel@tonic-gate 
828141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
829141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8307c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
831141040e8Srie 
832141040e8Srie 			} else if (IS_GOTPCREL(arsp->rel_rtype) &&
833141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
834141040e8Srie 				Gotndx *gnp;
835141040e8Srie 
8367c478bd9Sstevel@tonic-gate 				/*
8377c478bd9Sstevel@tonic-gate 				 * Calculation:
8387c478bd9Sstevel@tonic-gate 				 *	G + GOT + A - P
8397c478bd9Sstevel@tonic-gate 				 */
8405aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8417c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
842141040e8Srie 				assert(gnp);
8437c478bd9Sstevel@tonic-gate 				value = (Xword)(ofl->ofl_osgot->os_shdr->
8447c478bd9Sstevel@tonic-gate 				    sh_addr) + ((Xword)gnp->gn_gotndx *
8457c478bd9Sstevel@tonic-gate 				    M_GOT_ENTSIZE) + arsp->rel_raddend -
8467c478bd9Sstevel@tonic-gate 				    refaddr;
847141040e8Srie 
848141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
849141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
850141040e8Srie 				value = (Xword)(ofl->ofl_osgot->os_shdr->
85154d82594Sseizo 				    sh_addr) - refaddr + arsp->rel_raddend;
852141040e8Srie 
8537c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
854141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
8557c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
8567c478bd9Sstevel@tonic-gate 				value -= refaddr;
857141040e8Srie 
8587c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
859141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
860141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8617c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
8627c478bd9Sstevel@tonic-gate 
8635aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8647c478bd9Sstevel@tonic-gate 				    ofl, arsp);
865141040e8Srie 				assert(gnp);
8667c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
867141040e8Srie 
868141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
869141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
870141040e8Srie 				Gotndx *gnp;
8717c478bd9Sstevel@tonic-gate 
8725aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8737c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
8747c478bd9Sstevel@tonic-gate 				assert(gnp);
8757c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
876141040e8Srie 
877141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
878141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8797c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
880141040e8Srie 
8817c478bd9Sstevel@tonic-gate 				/*
8827c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
8837c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
8847c478bd9Sstevel@tonic-gate 				 */
885141040e8Srie 				tlsstatsize =
886141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
8877c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
8887c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
889141040e8Srie 
8907c478bd9Sstevel@tonic-gate 				/*
891141040e8Srie 				 * Since this code is fixed up, it assumes a
892141040e8Srie 				 * negative offset that can be added to the
893141040e8Srie 				 * thread pointer.
8947c478bd9Sstevel@tonic-gate 				 */
8957c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_TPOFF32)
8967c478bd9Sstevel@tonic-gate 					value = -value;
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
9007c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9017c478bd9Sstevel@tonic-gate 			else
9027c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 			/*
9057c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9067c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9077c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
9087c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
9097c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
9107c478bd9Sstevel@tonic-gate 			 * see this.
9117c478bd9Sstevel@tonic-gate 			 */
9127c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
9135aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9145aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
915c13de8f6Sab 				    conv_reloc_amd64_type(arsp->rel_rtype, 0),
9167c478bd9Sstevel@tonic-gate 				    ifl_name, demangle(arsp->rel_sname),
9177c478bd9Sstevel@tonic-gate 				    arsp->rel_isdesc->is_name);
9187c478bd9Sstevel@tonic-gate 				return (S_ERROR);
9197c478bd9Sstevel@tonic-gate 			}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 			/*
9227c478bd9Sstevel@tonic-gate 			 * Get the address of the data item we need to modify.
9237c478bd9Sstevel@tonic-gate 			 */
924b3fbe5e6Sseizo 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
925b3fbe5e6Sseizo 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
926b3fbe5e6Sseizo 			    is_indata));
9277c478bd9Sstevel@tonic-gate 
9285aefb655Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
9295aefb655Srie 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
9305aefb655Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9317c478bd9Sstevel@tonic-gate 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9327c478bd9Sstevel@tonic-gate 
9335aefb655Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9347c478bd9Sstevel@tonic-gate 			    ofl->ofl_size) || (arsp->rel_roffset >
9357c478bd9Sstevel@tonic-gate 			    arsp->rel_osdesc->os_shdr->sh_size)) {
9367c478bd9Sstevel@tonic-gate 				int	class;
9377c478bd9Sstevel@tonic-gate 
938b3fbe5e6Sseizo 				if (((uintptr_t)addr -
9395aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
9407c478bd9Sstevel@tonic-gate 					class = ERR_FATAL;
9417c478bd9Sstevel@tonic-gate 				else
9427c478bd9Sstevel@tonic-gate 					class = ERR_WARNING;
9437c478bd9Sstevel@tonic-gate 
9445aefb655Srie 				eprintf(ofl->ofl_lml, class,
9455aefb655Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
946c13de8f6Sab 				    conv_reloc_amd64_type(arsp->rel_rtype, 0),
9477c478bd9Sstevel@tonic-gate 				    ifl_name, arsp->rel_isdesc->is_name,
9487c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
9497c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
9505aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
9537c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9547c478bd9Sstevel@tonic-gate 					continue;
9557c478bd9Sstevel@tonic-gate 				}
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			/*
9597c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
9607c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
9617c478bd9Sstevel@tonic-gate 			 * expanded.
9627c478bd9Sstevel@tonic-gate 			 */
9637c478bd9Sstevel@tonic-gate 			if (moved)
9647c478bd9Sstevel@tonic-gate 				value -= *addr;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 			/*
9677c478bd9Sstevel@tonic-gate 			 * If '-z noreloc' is specified - skip the do_reloc
9687c478bd9Sstevel@tonic-gate 			 * stage.
9697c478bd9Sstevel@tonic-gate 			 */
9707c478bd9Sstevel@tonic-gate 			if ((flags & FLG_OF_RELOBJ) ||
9717c478bd9Sstevel@tonic-gate 			    !(dtflags1 & DF_1_NORELOC)) {
972b3fbe5e6Sseizo 				if (do_reloc((uchar_t)arsp->rel_rtype,
9735aefb655Srie 				    addr, &value, arsp->rel_sname, ifl_name,
9745aefb655Srie 				    ofl->ofl_lml) == 0)
9757c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9767c478bd9Sstevel@tonic-gate 			}
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	return (return_code);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate uintptr_t
9835aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
9847c478bd9Sstevel@tonic-gate {
985141040e8Srie 	Rel_desc	*orsp;
986141040e8Srie 	Rel_cache	*rcp;
987141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/*
9907c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
9917c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
9927c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
9937c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
9967c478bd9Sstevel@tonic-gate 	    (FLG_OF_STATIC | FLG_OF_EXEC))
9977c478bd9Sstevel@tonic-gate 		return (1);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * If no relocation cache structures are available allocate
10017c478bd9Sstevel@tonic-gate 	 * a new one and link it into the cache list.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_outrels.tail == 0) ||
10047c478bd9Sstevel@tonic-gate 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10057c478bd9Sstevel@tonic-gate 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10067c478bd9Sstevel@tonic-gate 		static size_t	nextsize = 0;
10077c478bd9Sstevel@tonic-gate 		size_t		size;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		/*
10107c478bd9Sstevel@tonic-gate 		 * Output relocation numbers can vary considerably between
10117c478bd9Sstevel@tonic-gate 		 * building executables or shared objects (pic vs. non-pic),
10127c478bd9Sstevel@tonic-gate 		 * etc.  But, they typically aren't very large, so for these
10137c478bd9Sstevel@tonic-gate 		 * objects use a standard bucket size.  For building relocatable
10147c478bd9Sstevel@tonic-gate 		 * objects, typically there will be an output relocation for
10157c478bd9Sstevel@tonic-gate 		 * every input relocation.
10167c478bd9Sstevel@tonic-gate 		 */
10177c478bd9Sstevel@tonic-gate 		if (nextsize == 0) {
10187c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10197c478bd9Sstevel@tonic-gate 				if ((size = ofl->ofl_relocincnt) == 0)
10207c478bd9Sstevel@tonic-gate 					size = REL_LOIDESCNO;
10217c478bd9Sstevel@tonic-gate 				if (size > REL_HOIDESCNO)
10227c478bd9Sstevel@tonic-gate 					nextsize = REL_HOIDESCNO;
10237c478bd9Sstevel@tonic-gate 				else
10247c478bd9Sstevel@tonic-gate 					nextsize = REL_LOIDESCNO;
10257c478bd9Sstevel@tonic-gate 			} else
10267c478bd9Sstevel@tonic-gate 				nextsize = size = REL_HOIDESCNO;
10277c478bd9Sstevel@tonic-gate 		} else
10287c478bd9Sstevel@tonic-gate 			size = nextsize;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		size = size * sizeof (Rel_desc);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
10337c478bd9Sstevel@tonic-gate 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
10347c478bd9Sstevel@tonic-gate 			return (S_ERROR);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 		/* LINTED */
10377c478bd9Sstevel@tonic-gate 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
10387c478bd9Sstevel@tonic-gate 		/* LINTED */
10397c478bd9Sstevel@tonic-gate 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
10447c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
10457c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10487c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
10497c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 		/*
10527c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
10537c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
10547c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
10557c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
10567c478bd9Sstevel@tonic-gate 		 */
10577c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10580bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10597c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10607c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10617c478bd9Sstevel@tonic-gate 			else
10627c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1063141040e8Srie 		} else {
1064141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
10657c478bd9Sstevel@tonic-gate 
1066c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
10677c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
10687c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
10697c478bd9Sstevel@tonic-gate 			}
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
10747c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
10777c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
10807c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
10817c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
10827c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
10837c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
10847c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
10857c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
10867c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
10877c478bd9Sstevel@tonic-gate 	else
10887c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
10917c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/*
10947c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
10957c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
10967c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
10977c478bd9Sstevel@tonic-gate 	 */
10987c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
10997c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11007c478bd9Sstevel@tonic-gate 
1101141040e8Srie 	/*
1102141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1103141040e8Srie 	 */
1104141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1105141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1106141040e8Srie 
11077c478bd9Sstevel@tonic-gate 	/*
11087c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11117c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11145aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11157c478bd9Sstevel@tonic-gate 	}
11165aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11175aefb655Srie 	    M_MACH, orsp));
11187c478bd9Sstevel@tonic-gate 	return (1);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * Stub routine since register symbols are not supported on amd64.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate /* ARGSUSED */
11257c478bd9Sstevel@tonic-gate uintptr_t
11265aefb655Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
11277c478bd9Sstevel@tonic-gate {
11285aefb655Srie 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
11297c478bd9Sstevel@tonic-gate 	return (S_ERROR);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate /*
11337c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11347c478bd9Sstevel@tonic-gate  */
11357c478bd9Sstevel@tonic-gate uintptr_t
11365aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
11397c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11400bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11417c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
11457c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
11467c478bd9Sstevel@tonic-gate 	 * then
11477c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
11487c478bd9Sstevel@tonic-gate 	 * fi
11497c478bd9Sstevel@tonic-gate 	 */
11507c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11517c478bd9Sstevel@tonic-gate 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) &&
11527c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11537c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
11547c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11557c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11567c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		/*
11597c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11607c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
11617c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
11627c478bd9Sstevel@tonic-gate 		 */
11637c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11645aefb655Srie 			return (ld_add_outrel(NULL, rsp, ofl));
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
11685aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11697c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11707c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
11717c478bd9Sstevel@tonic-gate 		return (1);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
1174b3fbe5e6Sseizo 	/*
1175b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1176b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1177b3fbe5e6Sseizo 	 * message.
1178b3fbe5e6Sseizo 	 *
1179b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1180b3fbe5e6Sseizo 	 *	a) it's undefined
1181b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1182b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1183b3fbe5e6Sseizo 	 *
1184b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1185b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1186b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1187b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1188b3fbe5e6Sseizo 	 */
11897c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1190b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1191b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1192b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1193b3fbe5e6Sseizo 		/*
1194b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1195b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1196b3fbe5e6Sseizo 		 * can not be resolved.
1197b3fbe5e6Sseizo 		 */
1198b3fbe5e6Sseizo 		if (rsp->rel_osdesc &&
1199b3fbe5e6Sseizo 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1200b3fbe5e6Sseizo 			return (0);
12015aefb655Srie 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
12025aefb655Srie 		    MSG_INTL(MSG_REL_EXTERNSYM),
1203c13de8f6Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0),
12047c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12057c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12067c478bd9Sstevel@tonic-gate 		return (1);
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	/*
12107c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12117c478bd9Sstevel@tonic-gate 	 */
12125aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate uintptr_t
12177c478bd9Sstevel@tonic-gate /* ARGSUSED */
12185aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	/*
12217c478bd9Sstevel@tonic-gate 	 * Stub routine for common code compatibility, we shouldn't
12227c478bd9Sstevel@tonic-gate 	 * actually get here on amd64.
12237c478bd9Sstevel@tonic-gate 	 */
1224d326b23bSrie 	assert(0);
12257c478bd9Sstevel@tonic-gate 	return (S_ERROR);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate uintptr_t
12295aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12327c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12337c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
12347c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
1237d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1238d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12397c478bd9Sstevel@tonic-gate 	 */
1240d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12417c478bd9Sstevel@tonic-gate 		/*
1242d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12437c478bd9Sstevel@tonic-gate 		 */
12447c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12457c478bd9Sstevel@tonic-gate 
1246d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12477c478bd9Sstevel@tonic-gate 			/*
1248d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12497c478bd9Sstevel@tonic-gate 			 */
1250d326b23bSrie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1251d326b23bSrie 			    GOT_REF_TLSIE, ofl, rsp)) == 0) {
12527c478bd9Sstevel@tonic-gate 
1253d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1254d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1255d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1256d326b23bSrie 					return (S_ERROR);
1257d326b23bSrie 			}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 			/*
1260d326b23bSrie 			 * IE access model.
12617c478bd9Sstevel@tonic-gate 			 */
12627c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
12635aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 			/*
1266d326b23bSrie 			 * Fixups are required for other executable models.
12677c478bd9Sstevel@tonic-gate 			 */
12685aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12697c478bd9Sstevel@tonic-gate 			    rsp, ofl));
12707c478bd9Sstevel@tonic-gate 		}
1271d326b23bSrie 
12727c478bd9Sstevel@tonic-gate 		/*
1273d326b23bSrie 		 * LE access model.
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
12765aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1277d326b23bSrie 
12785aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12795aefb655Srie 		    rsp, ofl));
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
1283d326b23bSrie 	 * Building a shared object.
1284d326b23bSrie 	 *
1285d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
12867c478bd9Sstevel@tonic-gate 	 */
12875aefb655Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12887c478bd9Sstevel@tonic-gate 	    GOT_REF_TLSLD, ofl, rsp)) == 0)) {
1289d326b23bSrie 
1290d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1291d326b23bSrie 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR)
12927c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1293d326b23bSrie 
12945aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
1295d326b23bSrie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1296d326b23bSrie 	    ofl, rsp)) == 0)) {
1297d326b23bSrie 
1298d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1299d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1300d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13017c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13055aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13067c478bd9Sstevel@tonic-gate 
13075aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
13117c478bd9Sstevel@tonic-gate Gotndx *
13125aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
13157c478bd9Sstevel@tonic-gate 	Gotndx *	gnp;
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	assert(rdesc != 0);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
13207c478bd9Sstevel@tonic-gate 		return (ofl->ofl_tlsldgotndx);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
13237c478bd9Sstevel@tonic-gate 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
13247c478bd9Sstevel@tonic-gate 		    (gnp->gn_gotref == gref)) {
13257c478bd9Sstevel@tonic-gate 			return (gnp);
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 	return ((Gotndx *)0);
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate Xword
13325aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	Os_desc		*osp = ofl->ofl_osgot;
13357c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rdesc->rel_sym;
13367c478bd9Sstevel@tonic-gate 	Xword		gotndx;
13377c478bd9Sstevel@tonic-gate 	Gotref		gref;
13387c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	if (rdesc->rel_flags & FLG_REL_DTLS)
13417c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSGD;
13427c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_MTLS)
13437c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSLD;
13447c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_STLS)
13457c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSIE;
13467c478bd9Sstevel@tonic-gate 	else
13477c478bd9Sstevel@tonic-gate 		gref = GOT_REF_GENERIC;
13487c478bd9Sstevel@tonic-gate 
13495aefb655Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
13507c478bd9Sstevel@tonic-gate 	assert(gnp);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	gotndx = (Xword)gnp->gn_gotndx;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
13557c478bd9Sstevel@tonic-gate 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
13567c478bd9Sstevel@tonic-gate 		gotndx++;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
13637c478bd9Sstevel@tonic-gate uintptr_t
1364d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
13657c478bd9Sstevel@tonic-gate     Rel_desc * rsp, Sym_desc * sdp)
13667c478bd9Sstevel@tonic-gate {
13677c478bd9Sstevel@tonic-gate 	Xword		raddend;
13687c478bd9Sstevel@tonic-gate 	Gotndx		*gnp, *_gnp;
13697c478bd9Sstevel@tonic-gate 	Listnode	*lnp, *plnp;
13707c478bd9Sstevel@tonic-gate 	uint_t		gotents;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
13737c478bd9Sstevel@tonic-gate 	if (pgnp && (pgnp->gn_addend == raddend) &&
13747c478bd9Sstevel@tonic-gate 	    (pgnp->gn_gotref == gref))
13757c478bd9Sstevel@tonic-gate 		return (1);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13787c478bd9Sstevel@tonic-gate 		gotents = 2;
13797c478bd9Sstevel@tonic-gate 	else
13807c478bd9Sstevel@tonic-gate 		gotents = 1;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	plnp = 0;
13837c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
13847c478bd9Sstevel@tonic-gate 		if (_gnp->gn_addend > raddend)
13857c478bd9Sstevel@tonic-gate 			break;
13867c478bd9Sstevel@tonic-gate 		plnp = lnp;
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Allocate a new entry.
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
13937c478bd9Sstevel@tonic-gate 		return (S_ERROR);
13947c478bd9Sstevel@tonic-gate 	gnp->gn_addend = raddend;
13957c478bd9Sstevel@tonic-gate 	gnp->gn_gotndx = ofl->ofl_gotcnt;
13967c478bd9Sstevel@tonic-gate 	gnp->gn_gotref = gref;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
14017c478bd9Sstevel@tonic-gate 		ofl->ofl_tlsldgotndx = gnp;
14027c478bd9Sstevel@tonic-gate 		return (1);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (plnp == 0) {
14067c478bd9Sstevel@tonic-gate 		/*
14077c478bd9Sstevel@tonic-gate 		 * Insert at head of list
14087c478bd9Sstevel@tonic-gate 		 */
14097c478bd9Sstevel@tonic-gate 		if (list_prependc(lst, (void *)gnp) == 0)
14107c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14117c478bd9Sstevel@tonic-gate 	} else if (_gnp->gn_addend > raddend) {
14127c478bd9Sstevel@tonic-gate 		/*
14137c478bd9Sstevel@tonic-gate 		 * Insert in middle of lest
14147c478bd9Sstevel@tonic-gate 		 */
14157c478bd9Sstevel@tonic-gate 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
14167c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14177c478bd9Sstevel@tonic-gate 	} else {
14187c478bd9Sstevel@tonic-gate 		/*
14197c478bd9Sstevel@tonic-gate 		 * Append to tail of list
14207c478bd9Sstevel@tonic-gate 		 */
14217c478bd9Sstevel@tonic-gate 		if (list_appendc(lst, (void *)gnp) == 0)
14227c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	return (1);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate void
14285aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14317c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1432141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
1435b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14367c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14377c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14387c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
14397c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
14407c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
14417c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
14427c478bd9Sstevel@tonic-gate };
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate /*
14457c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14467c478bd9Sstevel@tonic-gate  */
14477c478bd9Sstevel@tonic-gate uintptr_t
1448d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	Word	flags = ofl->ofl_flags;
14517c478bd9Sstevel@tonic-gate 	Word	dtflags1 = ofl->ofl_dtflags_1;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1454d326b23bSrie 		Sym_desc	*sdp;
14557c478bd9Sstevel@tonic-gate 
14565aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14577c478bd9Sstevel@tonic-gate 		    SYM_NOHASH, 0, ofl)) != NULL) {
1458d326b23bSrie 			uchar_t	*genptr;
1459d326b23bSrie 
1460d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14617c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14627c478bd9Sstevel@tonic-gate 			/* LINTED */
14637c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
14647c478bd9Sstevel@tonic-gate 		}
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14697c478bd9Sstevel@tonic-gate 	 * entry is:
14707c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14717c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14727c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
14737c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
14747c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
14757c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1478d326b23bSrie 		uchar_t	*pltent;
1479d326b23bSrie 		Xword	val1;
14807c478bd9Sstevel@tonic-gate 
1481b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14827c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * filin:
14867c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
14877c478bd9Sstevel@tonic-gate 		 *
14887c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
14897c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
14907c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
14917c478bd9Sstevel@tonic-gate 		 */
14927c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
14937c478bd9Sstevel@tonic-gate 			(M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
14947c478bd9Sstevel@tonic-gate 			ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 		/*
14977c478bd9Sstevel@tonic-gate 		 * If '-z noreloc' is specified - skip the do_reloc
14987c478bd9Sstevel@tonic-gate 		 * stage.
14997c478bd9Sstevel@tonic-gate 		 */
15007c478bd9Sstevel@tonic-gate 		if ((flags & FLG_OF_RELOBJ) ||
15017c478bd9Sstevel@tonic-gate 		    !(dtflags1 & DF_1_NORELOC)) {
15027c478bd9Sstevel@tonic-gate 			if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x02],
15037c478bd9Sstevel@tonic-gate 			    &val1, MSG_ORIG(MSG_SYM_PLTENT),
15045aefb655Srie 			    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
15055aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
15065aefb655Srie 				    MSG_INTL(MSG_PLT_PLT0FAIL));
15077c478bd9Sstevel@tonic-gate 				return (S_ERROR);
15087c478bd9Sstevel@tonic-gate 			}
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		/*
15127c478bd9Sstevel@tonic-gate 		 * filin:
15137c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
15147c478bd9Sstevel@tonic-gate 		 */
15157c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15167c478bd9Sstevel@tonic-gate 			(M_GOT_XRTLD * M_GOT_ENTSIZE) -
15177c478bd9Sstevel@tonic-gate 			ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
15187c478bd9Sstevel@tonic-gate 		/*
15197c478bd9Sstevel@tonic-gate 		 * If '-z noreloc' is specified - skip the do_reloc
15207c478bd9Sstevel@tonic-gate 		 * stage.
15217c478bd9Sstevel@tonic-gate 		 */
15227c478bd9Sstevel@tonic-gate 		if ((flags & FLG_OF_RELOBJ) ||
15237c478bd9Sstevel@tonic-gate 		    !(dtflags1 & DF_1_NORELOC)) {
15247c478bd9Sstevel@tonic-gate 			if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x08],
15257c478bd9Sstevel@tonic-gate 			    &val1, MSG_ORIG(MSG_SYM_PLTENT),
15265aefb655Srie 			    MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) {
15275aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
15285aefb655Srie 				    MSG_INTL(MSG_PLT_PLT0FAIL));
15297c478bd9Sstevel@tonic-gate 				return (S_ERROR);
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 	return (1);
15347c478bd9Sstevel@tonic-gate }
1535