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 /*
23de777a60Sab  * Copyright 2007 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;
114*f3324781Sab 	int		bswap;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1177c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1187c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
119b3fbe5e6Sseizo 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
1207c478bd9Sstevel@tonic-gate 	pltent = plt0 + plt_off;
121b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	/* LINTED */
128b3fbe5e6Sseizo 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
129b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
1307c478bd9Sstevel@tonic-gate 
131*f3324781Sab 	/*
132*f3324781Sab 	 * If '-z noreloc' is specified - skip the do_reloc_ld
133*f3324781Sab 	 * stage.
134*f3324781Sab 	 */
135*f3324781Sab 	if (!OFL_DO_RELOC(ofl))
136*f3324781Sab 		return (1);
137*f3324781Sab 
138*f3324781Sab 	/*
139*f3324781Sab 	 * If the running linker has a different byte order than
140*f3324781Sab 	 * the target host, tell do_reloc_ld() to swap bytes.
141*f3324781Sab 	 *
142*f3324781Sab 	 * We know the PLT is PROGBITS --- we don't have to check
143*f3324781Sab 	 */
144*f3324781Sab 	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
145*f3324781Sab 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * patchup:
1487c478bd9Sstevel@tonic-gate 	 *	jmpq	*name1@gotpcrel(%rip)
1497c478bd9Sstevel@tonic-gate 	 *
1507c478bd9Sstevel@tonic-gate 	 * NOTE: 0x06 represents next instruction.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
153de777a60Sab 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
1547c478bd9Sstevel@tonic-gate 
155*f3324781Sab 	if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
156*f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
157*f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
158*f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
159*f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
160*f3324781Sab 		return (S_ERROR);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * patchup:
1657c478bd9Sstevel@tonic-gate 	 *	pushq	$pltndx
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
168*f3324781Sab 
169*f3324781Sab 	if (do_reloc_ld(R_AMD64_32, &pltent[0x07],
170*f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
171*f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
172*f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
173*f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
174*f3324781Sab 		return (S_ERROR);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/*
1787c478bd9Sstevel@tonic-gate 	 * patchup:
1797c478bd9Sstevel@tonic-gate 	 *	jmpq	.plt0(%rip)
180*f3324781Sab 	 * NOTE: 0x10 represents next instruction. The rather complex
181*f3324781Sab 	 * series of casts is necessary to sign extend an offset into
182*f3324781Sab 	 * a 64-bit value while satisfying various compiler error
183*f3324781Sab 	 * checks.  Handle with care.
1847c478bd9Sstevel@tonic-gate 	 */
185b3fbe5e6Sseizo 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
186b3fbe5e6Sseizo 	    (uintptr_t)(&pltent[0x10])));
187b3fbe5e6Sseizo 
188*f3324781Sab 	if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c],
189*f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
190*f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
191*f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
192*f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
193*f3324781Sab 		return (S_ERROR);
1947c478bd9Sstevel@tonic-gate 	}
195*f3324781Sab 
1967c478bd9Sstevel@tonic-gate 	return (1);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate uintptr_t
2005aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
2037c478bd9Sstevel@tonic-gate 	Word		ndx;
2047c478bd9Sstevel@tonic-gate 	Xword		roffset, value;
2057c478bd9Sstevel@tonic-gate 	Sxword		raddend;
2067c478bd9Sstevel@tonic-gate 	Rela		rea;
2077c478bd9Sstevel@tonic-gate 	char		*relbits;
2087c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2097c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	raddend = orsp->rel_raddend;
2127c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
2167c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2197c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2207c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2215aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2227c478bd9Sstevel@tonic-gate 		return (1);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
2277c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	if (orsp->rel_move)
2305aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section then we need to adjust the
2347c478bd9Sstevel@tonic-gate 	 * raddend field to compensate for the new position of the input section
2357c478bd9Sstevel@tonic-gate 	 * within the new output section.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2387c478bd9Sstevel@tonic-gate 		if (ofl->ofl_parsym.head &&
2397c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2407c478bd9Sstevel@tonic-gate 		    /* LINTED */
2415aefb655Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
2425aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2437c478bd9Sstevel@tonic-gate 			sectmoved = 1;
2447c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
2457c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value;
2467c478bd9Sstevel@tonic-gate 			else
2477c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value -
2487c478bd9Sstevel@tonic-gate 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
2497c478bd9Sstevel@tonic-gate 			/* LINTED */
2507c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
2517c478bd9Sstevel@tonic-gate 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
2527c478bd9Sstevel@tonic-gate 				raddend +=
253de777a60Sab 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
2547c478bd9Sstevel@tonic-gate 		} else {
2557c478bd9Sstevel@tonic-gate 			/* LINTED */
2567c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
2577c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
2587c478bd9Sstevel@tonic-gate 				raddend +=
259de777a60Sab 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * Note: for GOT relative relocations on amd64
2687c478bd9Sstevel@tonic-gate 		 *	 we discard the addend.  It was relevant
2697c478bd9Sstevel@tonic-gate 		 *	 to the reference - not to the data item
2707c478bd9Sstevel@tonic-gate 		 *	 being referenced (ie: that -4 thing).
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		raddend = 0;
2737c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
2745aefb655Srie 		roffset = ld_calc_got_offset(orsp, ofl);
2755aefb655Srie 
2767c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
2797c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
2807c478bd9Sstevel@tonic-gate 		 */
2817c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
2827c478bd9Sstevel@tonic-gate 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
2837c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2847c478bd9Sstevel@tonic-gate 		raddend = 0;
2857c478bd9Sstevel@tonic-gate 		if (plt_entry(ofl, sdp) == S_ERROR)
2867c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2897c478bd9Sstevel@tonic-gate 		/*
2907c478bd9Sstevel@tonic-gate 		 * This must be a R_AMD64_COPY.  For these set the roffset to
2917c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
2927c478bd9Sstevel@tonic-gate 		 */
2937c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
2947c478bd9Sstevel@tonic-gate 		roffset = value;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * The raddend doesn't mean anything in a R_SPARC_COPY
2987c478bd9Sstevel@tonic-gate 		 * relocation.  Null it out because it can confuse people.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		raddend = 0;
3017c478bd9Sstevel@tonic-gate 	} else {
3027c478bd9Sstevel@tonic-gate 		osp = orsp->rel_osdesc;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		/*
3057c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
3067c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
3077c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
3087c478bd9Sstevel@tonic-gate 		 */
3097c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
3107c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3117c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3127c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
3137c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3177c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
3217c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
3227c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
3237c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
3267c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
3277c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3287c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3297c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
3307c478bd9Sstevel@tonic-gate 			/*
3317c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
3327c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
3337c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
3347c478bd9Sstevel@tonic-gate 			 */
3357c478bd9Sstevel@tonic-gate 			if ((sdp->sd_isc != 0) &&
3367c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_osdesc != 0))
3377c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
3387c478bd9Sstevel@tonic-gate 			else
3397c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
3407c478bd9Sstevel@tonic-gate 		} else
3417c478bd9Sstevel@tonic-gate 			ndx = ofl->ofl_sunwdata1ndx;
3427c478bd9Sstevel@tonic-gate 	} else
3437c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * Add the symbols 'value' to the addend field.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_ADVAL)
3497c478bd9Sstevel@tonic-gate 		raddend += value;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
3537010c12aSrie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
3567c478bd9Sstevel@tonic-gate 		raddend = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3617c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
3627c478bd9Sstevel@tonic-gate 	rea.r_addend = raddend;
3635aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
3645aefb655Srie 	    orsp->rel_sname));
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3727c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rela));
3737c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += (Xword)sizeof (Rela);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3777c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3787c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
3797c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
3827c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3837c478bd9Sstevel@tonic-gate 
3845aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3857c478bd9Sstevel@tonic-gate 	return (1);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * amd64 Instructions for TLS processing
3907c478bd9Sstevel@tonic-gate  */
391b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
3927c478bd9Sstevel@tonic-gate 	/*
3937c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
3967c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 *	0x09 addq x@gottpoff(%rip), %rax
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	0x48, 0x03, 0x05, 0x00, 0x00,
4017c478bd9Sstevel@tonic-gate 	0x00, 0x00
4027c478bd9Sstevel@tonic-gate };
4037c478bd9Sstevel@tonic-gate 
404b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4097c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 *	0x09 leaq x@gottpoff(%rip), %rax
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	0x48, 0x8d, 0x80, 0x00, 0x00,
4147c478bd9Sstevel@tonic-gate 	0x00, 0x00
4157c478bd9Sstevel@tonic-gate };
4167c478bd9Sstevel@tonic-gate 
417b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = {
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	0x66,
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	0x66,
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	0x66,
4307c478bd9Sstevel@tonic-gate 	/*
4317c478bd9Sstevel@tonic-gate 	 * movq %fs:0, %rax
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4347c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00
4357c478bd9Sstevel@tonic-gate };
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
4385aefb655Srie static Fixupret
4395aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4427c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
443b3fbe5e6Sseizo 	uchar_t		*offset;
4447c478bd9Sstevel@tonic-gate 
445b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
446b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
447b3fbe5e6Sseizo 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * IE reference model
4527c478bd9Sstevel@tonic-gate 		 */
4537c478bd9Sstevel@tonic-gate 		switch (rtype) {
4547c478bd9Sstevel@tonic-gate 		case R_AMD64_TLSGD:
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 *  GD -> IE
4577c478bd9Sstevel@tonic-gate 			 *
4587c478bd9Sstevel@tonic-gate 			 * Transition:
4597c478bd9Sstevel@tonic-gate 			 *	0x00 .byte 0x66
4607c478bd9Sstevel@tonic-gate 			 *	0x01 leaq x@tlsgd(%rip), %rdi
4617c478bd9Sstevel@tonic-gate 			 *	0x08 .word 0x6666
4627c478bd9Sstevel@tonic-gate 			 *	0x0a rex64
4637c478bd9Sstevel@tonic-gate 			 *	0x0b call __tls_get_addr@plt
4647c478bd9Sstevel@tonic-gate 			 *	0x10
4657c478bd9Sstevel@tonic-gate 			 * To:
4667c478bd9Sstevel@tonic-gate 			 *	0x00 movq %fs:0, %rax
4677c478bd9Sstevel@tonic-gate 			 *	0x09 addq x@gottpoff(%rip), %rax
4687c478bd9Sstevel@tonic-gate 			 *	0x10
4697c478bd9Sstevel@tonic-gate 			 */
4705aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
471051d39bbSrie 			    R_AMD64_GOTTPOFF, arsp));
4727c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
4737c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
4747c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
4755aefb655Srie 
4767c478bd9Sstevel@tonic-gate 			/*
477051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4787c478bd9Sstevel@tonic-gate 			 * sequence.
4797c478bd9Sstevel@tonic-gate 			 */
4807c478bd9Sstevel@tonic-gate 			offset -= 4;
4817c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
4825aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4837c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
4845aefb655Srie 
4857c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
4867c478bd9Sstevel@tonic-gate 			/*
487051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
4887c478bd9Sstevel@tonic-gate 			 */
4895aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
490051d39bbSrie 			    R_AMD64_NONE, arsp));
4917c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * LE reference model
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 	switch (rtype) {
4997c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
5007c478bd9Sstevel@tonic-gate 		/*
5017c478bd9Sstevel@tonic-gate 		 * GD -> LE
5027c478bd9Sstevel@tonic-gate 		 *
5037c478bd9Sstevel@tonic-gate 		 * Transition:
5047c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5057c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5067c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
5077c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
5087c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
5097c478bd9Sstevel@tonic-gate 		 *	0x10
5107c478bd9Sstevel@tonic-gate 		 * To:
5117c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5127c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5137c478bd9Sstevel@tonic-gate 		 *	0x10
5147c478bd9Sstevel@tonic-gate 		 */
5155aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
516051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5177c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5187c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
5197c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5205aefb655Srie 
5217c478bd9Sstevel@tonic-gate 		/*
522051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		offset -= 4;
5255aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5267c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5275aefb655Srie 
5287c478bd9Sstevel@tonic-gate 	case R_AMD64_GOTTPOFF:
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * IE -> LE
5317c478bd9Sstevel@tonic-gate 		 *
5327c478bd9Sstevel@tonic-gate 		 * Transition:
5337c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5347c478bd9Sstevel@tonic-gate 		 *	0x09 addq x@gottopoff(%rip), %rax
5357c478bd9Sstevel@tonic-gate 		 *	0x10
5367c478bd9Sstevel@tonic-gate 		 * To:
5377c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5387c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5397c478bd9Sstevel@tonic-gate 		 *	0x10
5407c478bd9Sstevel@tonic-gate 		 */
541051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
542051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5437c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5447c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5455aefb655Srie 
5467c478bd9Sstevel@tonic-gate 		/*
547051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5487c478bd9Sstevel@tonic-gate 		 */
5497c478bd9Sstevel@tonic-gate 		offset -= 12;
5505aefb655Srie 
5517c478bd9Sstevel@tonic-gate 		/*
552051d39bbSrie 		 * Same code sequence used in the GD -> LE transition.
5537c478bd9Sstevel@tonic-gate 		 */
5545aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5557c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5565aefb655Srie 
5577c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
5587c478bd9Sstevel@tonic-gate 		/*
5597c478bd9Sstevel@tonic-gate 		 * LD -> LE
5607c478bd9Sstevel@tonic-gate 		 *
5617c478bd9Sstevel@tonic-gate 		 * Transition
5627c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
5637c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
5647c478bd9Sstevel@tonic-gate 		 *	0x0c
5657c478bd9Sstevel@tonic-gate 		 * To:
5667c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5677c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
5687c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
5697c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
5707c478bd9Sstevel@tonic-gate 		 */
571051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
572051d39bbSrie 		    R_AMD64_NONE, arsp));
5737c478bd9Sstevel@tonic-gate 		offset -= 3;
5745aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
5757c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5765aefb655Srie 
5777c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
5787c478bd9Sstevel@tonic-gate 		/*
5797c478bd9Sstevel@tonic-gate 		 * LD->LE
5807c478bd9Sstevel@tonic-gate 		 *
5817c478bd9Sstevel@tonic-gate 		 * Transition:
5827c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
5837c478bd9Sstevel@tonic-gate 		 * To:
5847c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
5857c478bd9Sstevel@tonic-gate 		 */
586051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
587051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5887c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5897c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5907c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate uintptr_t
5975aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
5987c478bd9Sstevel@tonic-gate {
599141040e8Srie 	Rel_desc	*arsp;
600141040e8Srie 	Rel_cache	*rcp;
601141040e8Srie 	Listnode	*lnp;
6027c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6037c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
6047c478bd9Sstevel@tonic-gate 
6057010c12aSrie 	if (ofl->ofl_actrels.head)
6067010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6077010c12aSrie 
6087c478bd9Sstevel@tonic-gate 	/*
609141040e8Srie 	 * Process active relocations.
6107c478bd9Sstevel@tonic-gate 	 */
6117c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
6127c478bd9Sstevel@tonic-gate 		/* LINTED */
6137c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6147c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
615b3fbe5e6Sseizo 			uchar_t		*addr;
6167c478bd9Sstevel@tonic-gate 			Xword 		value;
6177c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
6187c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
6197c478bd9Sstevel@tonic-gate 			Xword		refaddr;
6207c478bd9Sstevel@tonic-gate 			int		moved = 0;
6217c478bd9Sstevel@tonic-gate 			Gotref		gref;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			/*
6247c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
6257c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
6267c478bd9Sstevel@tonic-gate 			 * relocation itself.
6277c478bd9Sstevel@tonic-gate 			 */
6287c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6297c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
6307c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
6317c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6325aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6335aefb655Srie 				    M_MACH, arsp));
6347c478bd9Sstevel@tonic-gate 				continue;
6357c478bd9Sstevel@tonic-gate 			}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 			/*
6387c478bd9Sstevel@tonic-gate 			 * We deteremine what the 'got reference'
6397c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
6407c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
6417c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
6427c478bd9Sstevel@tonic-gate 			 *
6437c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
6447c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
6457c478bd9Sstevel@tonic-gate 			 */
6467c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
6477c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
6487c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
6497c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
6507c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
6517c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
6527c478bd9Sstevel@tonic-gate 			else
6537c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			/*
6567c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
6577c478bd9Sstevel@tonic-gate 			 */
6587c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
6597c478bd9Sstevel@tonic-gate 				Fixupret	ret;
660141040e8Srie 
6615aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
6627c478bd9Sstevel@tonic-gate 					return (S_ERROR);
6637c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
6647c478bd9Sstevel@tonic-gate 					continue;
6657c478bd9Sstevel@tonic-gate 			}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 			/*
6687c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
6697c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
6707c478bd9Sstevel@tonic-gate 			 */
6717c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
6725aefb655Srie 				ld_adj_movereloc(ofl, arsp);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
6757c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
6767c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
6797c478bd9Sstevel@tonic-gate 			    (arsp->rel_flags & FLG_REL_GOTCL))
6807c478bd9Sstevel@tonic-gate 				value = 0;
6817c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
6827c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
683141040e8Srie 				Sym_desc	*sym;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 				/*
6867c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
6877c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
6887c478bd9Sstevel@tonic-gate 				 *
6895aefb655Srie 				 * The second argument of the ld_am_I_partial()
6905aefb655Srie 				 * is the value stored at the target address
6917c478bd9Sstevel@tonic-gate 				 * relocation is going to be applied.
6927c478bd9Sstevel@tonic-gate 				 */
6937c478bd9Sstevel@tonic-gate 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
6947c478bd9Sstevel@tonic-gate 				    /* LINTED */
6955aefb655Srie 				    (sym = ld_am_I_partial(arsp, *(Xword *)
696b3fbe5e6Sseizo 				    ((uchar_t *)
697b3fbe5e6Sseizo 				    arsp->rel_isdesc->is_indata->d_buf +
6987c478bd9Sstevel@tonic-gate 				    arsp->rel_roffset)))) {
6997c478bd9Sstevel@tonic-gate 					/*
7007c478bd9Sstevel@tonic-gate 					 * If the symbol is moved,
7017c478bd9Sstevel@tonic-gate 					 * adjust the value
7027c478bd9Sstevel@tonic-gate 					 */
7037c478bd9Sstevel@tonic-gate 					value = sym->sd_sym->st_value;
7047c478bd9Sstevel@tonic-gate 					moved = 1;
7057c478bd9Sstevel@tonic-gate 				} else {
706141040e8Srie 					value = _elf_getxoff(
707141040e8Srie 					    sdp->sd_isc->is_indata);
7087c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
7097c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
710de777a60Sab 						value +=
711de777a60Sab 						    sdp->sd_isc->is_osdesc->
712de777a60Sab 						    os_shdr->sh_addr;
7137c478bd9Sstevel@tonic-gate 				}
7147c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7157c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
7162926dd2eSrie 
7172926dd2eSrie 			} else if (IS_SIZE(arsp->rel_rtype)) {
7182926dd2eSrie 				/*
7192926dd2eSrie 				 * Size relocations require the symbols size.
7202926dd2eSrie 				 */
7212926dd2eSrie 				value = sdp->sd_sym->st_size;
722141040e8Srie 			} else {
7237c478bd9Sstevel@tonic-gate 				/*
7247010c12aSrie 				 * Else the value is the symbols value.
7257c478bd9Sstevel@tonic-gate 				 */
7267c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
727141040e8Srie 			}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 			/*
7307c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7317c478bd9Sstevel@tonic-gate 			 */
7327c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
7337c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 			/*
7367c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
7377c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
7387c478bd9Sstevel@tonic-gate 			 * address.
7397c478bd9Sstevel@tonic-gate 			 */
7407c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
741141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
7427c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
7437c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 			/*
7467c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
7477c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
7487c478bd9Sstevel@tonic-gate 			 * not the address of the function).
7497c478bd9Sstevel@tonic-gate 			 */
7507c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
7517c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
7525aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 			/*
7567c478bd9Sstevel@tonic-gate 			 * Add relocations addend to value.  Add extra
7577c478bd9Sstevel@tonic-gate 			 * relocation addend if needed.
7587c478bd9Sstevel@tonic-gate 			 *
7597c478bd9Sstevel@tonic-gate 			 * Note: for GOT relative relocations on amd64
7607c478bd9Sstevel@tonic-gate 			 *	 we discard the addend.  It was relevant
7617c478bd9Sstevel@tonic-gate 			 *	 to the reference - not to the data item
7627c478bd9Sstevel@tonic-gate 			 *	 being referenced (ie: that -4 thing).
7637c478bd9Sstevel@tonic-gate 			 */
7647c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_GOT) == 0)
7657c478bd9Sstevel@tonic-gate 				value += arsp->rel_raddend;
7667c478bd9Sstevel@tonic-gate 
767141040e8Srie 			/*
768141040e8Srie 			 * Determine whether the value needs further adjustment.
769141040e8Srie 			 * Filter through the attributes of the relocation to
770141040e8Srie 			 * determine what adjustment is required.  Note, many
771141040e8Srie 			 * of the following cases are only applicable when a
772141040e8Srie 			 * .got is present.  As a .got is not generated when a
773141040e8Srie 			 * relocatable object is being built, any adjustments
774141040e8Srie 			 * that require a .got need to be skipped.
775141040e8Srie 			 */
776141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
777141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
7787c478bd9Sstevel@tonic-gate 				Xword		R1addr;
7797c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
7807c478bd9Sstevel@tonic-gate 				Word		gotndx;
7817c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 				/*
7847c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
7857c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
7867c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
7877c478bd9Sstevel@tonic-gate 				 * directly
7887c478bd9Sstevel@tonic-gate 				 *
7897c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
7907c478bd9Sstevel@tonic-gate 				 * the relocation.
7917c478bd9Sstevel@tonic-gate 				 */
7925aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
7937c478bd9Sstevel@tonic-gate 				    ofl, arsp);
7947c478bd9Sstevel@tonic-gate 				assert(gnp);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_DTPOFF64)
7977c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
7987c478bd9Sstevel@tonic-gate 				else
7997c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 				/*
8047c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
8057c478bd9Sstevel@tonic-gate 				 */
8067c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
8077c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
8087c478bd9Sstevel@tonic-gate 
8095aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
8105aefb655Srie 				    ELF_DBG_LD, M_MACH, SHT_RELA,
8117c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
8127c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 				/*
8157c478bd9Sstevel@tonic-gate 				 * And do it.
8167c478bd9Sstevel@tonic-gate 				 */
817*f3324781Sab 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
818*f3324781Sab 					*(Xword *)R2addr =
819*f3324781Sab 					    ld_byteswap_Xword(value);
820*f3324781Sab 				else
821*f3324781Sab 					*(Xword *)R2addr = value;
8227c478bd9Sstevel@tonic-gate 				continue;
8237c478bd9Sstevel@tonic-gate 
824141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
825141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8267c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
827141040e8Srie 
828141040e8Srie 			} else if (IS_GOTPCREL(arsp->rel_rtype) &&
829141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
830141040e8Srie 				Gotndx *gnp;
831141040e8Srie 
8327c478bd9Sstevel@tonic-gate 				/*
8337c478bd9Sstevel@tonic-gate 				 * Calculation:
8347c478bd9Sstevel@tonic-gate 				 *	G + GOT + A - P
8357c478bd9Sstevel@tonic-gate 				 */
8365aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8377c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
838141040e8Srie 				assert(gnp);
8397c478bd9Sstevel@tonic-gate 				value = (Xword)(ofl->ofl_osgot->os_shdr->
8407c478bd9Sstevel@tonic-gate 				    sh_addr) + ((Xword)gnp->gn_gotndx *
8417c478bd9Sstevel@tonic-gate 				    M_GOT_ENTSIZE) + arsp->rel_raddend -
8427c478bd9Sstevel@tonic-gate 				    refaddr;
843141040e8Srie 
844141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
845141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
846141040e8Srie 				value = (Xword)(ofl->ofl_osgot->os_shdr->
84754d82594Sseizo 				    sh_addr) - refaddr + arsp->rel_raddend;
848141040e8Srie 
8497c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
850141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
8517c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
8527c478bd9Sstevel@tonic-gate 				value -= refaddr;
853141040e8Srie 
8547c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
855141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
856141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8577c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
8587c478bd9Sstevel@tonic-gate 
8595aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
8607c478bd9Sstevel@tonic-gate 				    ofl, arsp);
861141040e8Srie 				assert(gnp);
8627c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
863141040e8Srie 
864141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
865141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
866141040e8Srie 				Gotndx *gnp;
8677c478bd9Sstevel@tonic-gate 
8685aefb655Srie 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
8697c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
8707c478bd9Sstevel@tonic-gate 				assert(gnp);
8717c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
872141040e8Srie 
873141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
874141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8757c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
876141040e8Srie 
8777c478bd9Sstevel@tonic-gate 				/*
8787c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
8797c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
8807c478bd9Sstevel@tonic-gate 				 */
881141040e8Srie 				tlsstatsize =
882141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
8837c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
8847c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
885141040e8Srie 
8867c478bd9Sstevel@tonic-gate 				/*
887141040e8Srie 				 * Since this code is fixed up, it assumes a
888141040e8Srie 				 * negative offset that can be added to the
889141040e8Srie 				 * thread pointer.
8907c478bd9Sstevel@tonic-gate 				 */
8917c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_TPOFF32)
8927c478bd9Sstevel@tonic-gate 					value = -value;
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
8967c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
8977c478bd9Sstevel@tonic-gate 			else
8987c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/*
9017c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9027c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9037c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
9047c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
9057c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
9067c478bd9Sstevel@tonic-gate 			 * see this.
9077c478bd9Sstevel@tonic-gate 			 */
9087c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
909de777a60Sab 				Conv_inv_buf_t inv_buf;
910de777a60Sab 
9115aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9125aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
913de777a60Sab 				    conv_reloc_amd64_type(arsp->rel_rtype,
914de777a60Sab 				    0, &inv_buf), ifl_name,
915de777a60Sab 				    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)) {
935de777a60Sab 				int		class;
936de777a60Sab 				Conv_inv_buf_t inv_buf;
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),
946de777a60Sab 				    conv_reloc_amd64_type(arsp->rel_rtype,
947de777a60Sab 				    0, &inv_buf), ifl_name,
948de777a60Sab 				    arsp->rel_isdesc->is_name,
9497c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
9507c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
9515aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
9547c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9557c478bd9Sstevel@tonic-gate 					continue;
9567c478bd9Sstevel@tonic-gate 				}
9577c478bd9Sstevel@tonic-gate 			}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 			/*
9607c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
9617c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
9627c478bd9Sstevel@tonic-gate 			 * expanded.
9637c478bd9Sstevel@tonic-gate 			 */
9647c478bd9Sstevel@tonic-gate 			if (moved)
9657c478bd9Sstevel@tonic-gate 				value -= *addr;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 			/*
968*f3324781Sab 			 * If '-z noreloc' is specified - skip the do_reloc_ld
9697c478bd9Sstevel@tonic-gate 			 * stage.
9707c478bd9Sstevel@tonic-gate 			 */
971*f3324781Sab 			if (OFL_DO_RELOC(ofl)) {
972*f3324781Sab 				/*
973*f3324781Sab 				 * If this is a PROGBITS section and the
974*f3324781Sab 				 * running linker has a different byte order
975*f3324781Sab 				 * than the target host, tell do_reloc_ld()
976*f3324781Sab 				 * to swap bytes.
977*f3324781Sab 				 */
978*f3324781Sab 				if (do_reloc_ld((uchar_t)arsp->rel_rtype,
9795aefb655Srie 				    addr, &value, arsp->rel_sname, ifl_name,
980*f3324781Sab 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
9815aefb655Srie 				    ofl->ofl_lml) == 0)
9827c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
9837c478bd9Sstevel@tonic-gate 			}
9847c478bd9Sstevel@tonic-gate 		}
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	return (return_code);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate uintptr_t
9905aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
9917c478bd9Sstevel@tonic-gate {
992141040e8Srie 	Rel_desc	*orsp;
993141040e8Srie 	Rel_cache	*rcp;
994141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
9987c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
9997c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
10007c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
10037c478bd9Sstevel@tonic-gate 	    (FLG_OF_STATIC | FLG_OF_EXEC))
10047c478bd9Sstevel@tonic-gate 		return (1);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * If no relocation cache structures are available allocate
10087c478bd9Sstevel@tonic-gate 	 * a new one and link it into the cache list.
10097c478bd9Sstevel@tonic-gate 	 */
10107c478bd9Sstevel@tonic-gate 	if ((ofl->ofl_outrels.tail == 0) ||
10117c478bd9Sstevel@tonic-gate 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
10127c478bd9Sstevel@tonic-gate 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
10137c478bd9Sstevel@tonic-gate 		static size_t	nextsize = 0;
10147c478bd9Sstevel@tonic-gate 		size_t		size;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 		/*
10177c478bd9Sstevel@tonic-gate 		 * Output relocation numbers can vary considerably between
10187c478bd9Sstevel@tonic-gate 		 * building executables or shared objects (pic vs. non-pic),
10197c478bd9Sstevel@tonic-gate 		 * etc.  But, they typically aren't very large, so for these
10207c478bd9Sstevel@tonic-gate 		 * objects use a standard bucket size.  For building relocatable
10217c478bd9Sstevel@tonic-gate 		 * objects, typically there will be an output relocation for
10227c478bd9Sstevel@tonic-gate 		 * every input relocation.
10237c478bd9Sstevel@tonic-gate 		 */
10247c478bd9Sstevel@tonic-gate 		if (nextsize == 0) {
10257c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
10267c478bd9Sstevel@tonic-gate 				if ((size = ofl->ofl_relocincnt) == 0)
10277c478bd9Sstevel@tonic-gate 					size = REL_LOIDESCNO;
10287c478bd9Sstevel@tonic-gate 				if (size > REL_HOIDESCNO)
10297c478bd9Sstevel@tonic-gate 					nextsize = REL_HOIDESCNO;
10307c478bd9Sstevel@tonic-gate 				else
10317c478bd9Sstevel@tonic-gate 					nextsize = REL_LOIDESCNO;
10327c478bd9Sstevel@tonic-gate 			} else
10337c478bd9Sstevel@tonic-gate 				nextsize = size = REL_HOIDESCNO;
10347c478bd9Sstevel@tonic-gate 		} else
10357c478bd9Sstevel@tonic-gate 			size = nextsize;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		size = size * sizeof (Rel_desc);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
10407c478bd9Sstevel@tonic-gate 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
10417c478bd9Sstevel@tonic-gate 			return (S_ERROR);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		/* LINTED */
10447c478bd9Sstevel@tonic-gate 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
10457c478bd9Sstevel@tonic-gate 		/* LINTED */
10467c478bd9Sstevel@tonic-gate 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/*
10507c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
10517c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
10527c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10557c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
10567c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		/*
10597c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
10607c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
10617c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
10627c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10650bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10667c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10677c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10687c478bd9Sstevel@tonic-gate 			else
10697c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1070141040e8Srie 		} else {
1071141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
10727c478bd9Sstevel@tonic-gate 
1073c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
10747c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
10757c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
10767c478bd9Sstevel@tonic-gate 			}
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
10817c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
10847c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
10877c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
10887c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
10897c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
10907c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
10917c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
10927c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
10937c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
10947c478bd9Sstevel@tonic-gate 	else
10957c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
10987c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/*
11017c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
11027c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
11037c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
11067c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11077c478bd9Sstevel@tonic-gate 
1108141040e8Srie 	/*
1109141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1110141040e8Srie 	 */
1111141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1112141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1113141040e8Srie 
11147c478bd9Sstevel@tonic-gate 	/*
11157c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11187c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11215aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11227c478bd9Sstevel@tonic-gate 	}
11235aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11245aefb655Srie 	    M_MACH, orsp));
11257c478bd9Sstevel@tonic-gate 	return (1);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /*
11297c478bd9Sstevel@tonic-gate  * Stub routine since register symbols are not supported on amd64.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate /* ARGSUSED */
11327c478bd9Sstevel@tonic-gate uintptr_t
11335aefb655Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
11347c478bd9Sstevel@tonic-gate {
11355aefb655Srie 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
11367c478bd9Sstevel@tonic-gate 	return (S_ERROR);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /*
11407c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11417c478bd9Sstevel@tonic-gate  */
11427c478bd9Sstevel@tonic-gate uintptr_t
11435aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
11467c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11470bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11487c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
11527c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
11537c478bd9Sstevel@tonic-gate 	 * then
11547c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
11557c478bd9Sstevel@tonic-gate 	 * fi
11567c478bd9Sstevel@tonic-gate 	 */
11577c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11582926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
11597c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11607c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
11617c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11627c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11637c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 		/*
11667c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11677c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
11687c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
11697c478bd9Sstevel@tonic-gate 		 */
11707c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11715aefb655Srie 			return (ld_add_outrel(NULL, rsp, ofl));
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
11755aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11767c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11777c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
11787c478bd9Sstevel@tonic-gate 		return (1);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
1181b3fbe5e6Sseizo 	/*
1182b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1183b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1184b3fbe5e6Sseizo 	 * message.
1185b3fbe5e6Sseizo 	 *
1186b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1187b3fbe5e6Sseizo 	 *	a) it's undefined
1188b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1189b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1190b3fbe5e6Sseizo 	 *
1191b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1192b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1193b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1194b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1195b3fbe5e6Sseizo 	 */
11967c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1197b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1198b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1199b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1200de777a60Sab 		Conv_inv_buf_t inv_buf;
1201de777a60Sab 
1202b3fbe5e6Sseizo 		/*
1203b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1204b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1205b3fbe5e6Sseizo 		 * can not be resolved.
1206b3fbe5e6Sseizo 		 */
1207b3fbe5e6Sseizo 		if (rsp->rel_osdesc &&
1208b3fbe5e6Sseizo 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1209b3fbe5e6Sseizo 			return (0);
12105aefb655Srie 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
12115aefb655Srie 		    MSG_INTL(MSG_REL_EXTERNSYM),
1212de777a60Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
12137c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12147c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12157c478bd9Sstevel@tonic-gate 		return (1);
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12207c478bd9Sstevel@tonic-gate 	 */
12215aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate uintptr_t
12267c478bd9Sstevel@tonic-gate /* ARGSUSED */
12275aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * Stub routine for common code compatibility, we shouldn't
12317c478bd9Sstevel@tonic-gate 	 * actually get here on amd64.
12327c478bd9Sstevel@tonic-gate 	 */
1233d326b23bSrie 	assert(0);
12347c478bd9Sstevel@tonic-gate 	return (S_ERROR);
12357c478bd9Sstevel@tonic-gate }
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate uintptr_t
12385aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12417c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12427c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
12437c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
1246d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1247d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12487c478bd9Sstevel@tonic-gate 	 */
1249d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12507c478bd9Sstevel@tonic-gate 		/*
1251d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12527c478bd9Sstevel@tonic-gate 		 */
12537c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12547c478bd9Sstevel@tonic-gate 
1255d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12567c478bd9Sstevel@tonic-gate 			/*
1257d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12587c478bd9Sstevel@tonic-gate 			 */
1259d326b23bSrie 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1260d326b23bSrie 			    GOT_REF_TLSIE, ofl, rsp)) == 0) {
12617c478bd9Sstevel@tonic-gate 
1262d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1263d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1264d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1265d326b23bSrie 					return (S_ERROR);
1266d326b23bSrie 			}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 			/*
1269d326b23bSrie 			 * IE access model.
12707c478bd9Sstevel@tonic-gate 			 */
12717c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
12725aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 			/*
1275d326b23bSrie 			 * Fixups are required for other executable models.
12767c478bd9Sstevel@tonic-gate 			 */
12775aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12787c478bd9Sstevel@tonic-gate 			    rsp, ofl));
12797c478bd9Sstevel@tonic-gate 		}
1280d326b23bSrie 
12817c478bd9Sstevel@tonic-gate 		/*
1282d326b23bSrie 		 * LE access model.
12837c478bd9Sstevel@tonic-gate 		 */
12847c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
12855aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1286d326b23bSrie 
12875aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12885aefb655Srie 		    rsp, ofl));
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/*
1292d326b23bSrie 	 * Building a shared object.
1293d326b23bSrie 	 *
1294d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
12957c478bd9Sstevel@tonic-gate 	 */
12965aefb655Srie 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
12977c478bd9Sstevel@tonic-gate 	    GOT_REF_TLSLD, ofl, rsp)) == 0)) {
1298d326b23bSrie 
1299d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1300d326b23bSrie 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR)
13017c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1302d326b23bSrie 
13035aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
1304d326b23bSrie 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1305d326b23bSrie 	    ofl, rsp)) == 0)) {
1306d326b23bSrie 
1307d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1308d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1309d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13107c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13145aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13157c478bd9Sstevel@tonic-gate 
13165aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
13207c478bd9Sstevel@tonic-gate Gotndx *
13215aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
13247c478bd9Sstevel@tonic-gate 	Gotndx *	gnp;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	assert(rdesc != 0);
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
13297c478bd9Sstevel@tonic-gate 		return (ofl->ofl_tlsldgotndx);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
13327c478bd9Sstevel@tonic-gate 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
13337c478bd9Sstevel@tonic-gate 		    (gnp->gn_gotref == gref)) {
13347c478bd9Sstevel@tonic-gate 			return (gnp);
13357c478bd9Sstevel@tonic-gate 		}
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	return ((Gotndx *)0);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate Xword
13415aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate 	Os_desc		*osp = ofl->ofl_osgot;
13447c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rdesc->rel_sym;
13457c478bd9Sstevel@tonic-gate 	Xword		gotndx;
13467c478bd9Sstevel@tonic-gate 	Gotref		gref;
13477c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	if (rdesc->rel_flags & FLG_REL_DTLS)
13507c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSGD;
13517c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_MTLS)
13527c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSLD;
13537c478bd9Sstevel@tonic-gate 	else if (rdesc->rel_flags & FLG_REL_STLS)
13547c478bd9Sstevel@tonic-gate 		gref = GOT_REF_TLSIE;
13557c478bd9Sstevel@tonic-gate 	else
13567c478bd9Sstevel@tonic-gate 		gref = GOT_REF_GENERIC;
13577c478bd9Sstevel@tonic-gate 
13585aefb655Srie 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
13597c478bd9Sstevel@tonic-gate 	assert(gnp);
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	gotndx = (Xword)gnp->gn_gotndx;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
13647c478bd9Sstevel@tonic-gate 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
13657c478bd9Sstevel@tonic-gate 		gotndx++;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
13727c478bd9Sstevel@tonic-gate uintptr_t
1373d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
13747c478bd9Sstevel@tonic-gate     Rel_desc * rsp, Sym_desc * sdp)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	Xword		raddend;
13777c478bd9Sstevel@tonic-gate 	Gotndx		*gnp, *_gnp;
13787c478bd9Sstevel@tonic-gate 	Listnode	*lnp, *plnp;
13797c478bd9Sstevel@tonic-gate 	uint_t		gotents;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
13827c478bd9Sstevel@tonic-gate 	if (pgnp && (pgnp->gn_addend == raddend) &&
13837c478bd9Sstevel@tonic-gate 	    (pgnp->gn_gotref == gref))
13847c478bd9Sstevel@tonic-gate 		return (1);
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13877c478bd9Sstevel@tonic-gate 		gotents = 2;
13887c478bd9Sstevel@tonic-gate 	else
13897c478bd9Sstevel@tonic-gate 		gotents = 1;
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	plnp = 0;
13927c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
13937c478bd9Sstevel@tonic-gate 		if (_gnp->gn_addend > raddend)
13947c478bd9Sstevel@tonic-gate 			break;
13957c478bd9Sstevel@tonic-gate 		plnp = lnp;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/*
13997c478bd9Sstevel@tonic-gate 	 * Allocate a new entry.
14007c478bd9Sstevel@tonic-gate 	 */
14017c478bd9Sstevel@tonic-gate 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
14027c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14037c478bd9Sstevel@tonic-gate 	gnp->gn_addend = raddend;
14047c478bd9Sstevel@tonic-gate 	gnp->gn_gotndx = ofl->ofl_gotcnt;
14057c478bd9Sstevel@tonic-gate 	gnp->gn_gotref = gref;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
14107c478bd9Sstevel@tonic-gate 		ofl->ofl_tlsldgotndx = gnp;
14117c478bd9Sstevel@tonic-gate 		return (1);
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	if (plnp == 0) {
14157c478bd9Sstevel@tonic-gate 		/*
14167c478bd9Sstevel@tonic-gate 		 * Insert at head of list
14177c478bd9Sstevel@tonic-gate 		 */
14187c478bd9Sstevel@tonic-gate 		if (list_prependc(lst, (void *)gnp) == 0)
14197c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14207c478bd9Sstevel@tonic-gate 	} else if (_gnp->gn_addend > raddend) {
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * Insert in middle of lest
14237c478bd9Sstevel@tonic-gate 		 */
14247c478bd9Sstevel@tonic-gate 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
14257c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14267c478bd9Sstevel@tonic-gate 	} else {
14277c478bd9Sstevel@tonic-gate 		/*
14287c478bd9Sstevel@tonic-gate 		 * Append to tail of list
14297c478bd9Sstevel@tonic-gate 		 */
14307c478bd9Sstevel@tonic-gate 		if (list_appendc(lst, (void *)gnp) == 0)
14317c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 	return (1);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate void
14375aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14407c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1441141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14427c478bd9Sstevel@tonic-gate }
14437c478bd9Sstevel@tonic-gate 
1444b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14457c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14467c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14477c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
14487c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
14497c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
14507c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
14517c478bd9Sstevel@tonic-gate };
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate /*
14547c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate uintptr_t
1457d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1460d326b23bSrie 		Sym_desc	*sdp;
14617c478bd9Sstevel@tonic-gate 
14625aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
14637c478bd9Sstevel@tonic-gate 		    SYM_NOHASH, 0, ofl)) != NULL) {
1464d326b23bSrie 			uchar_t	*genptr;
1465d326b23bSrie 
1466d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14677c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14687c478bd9Sstevel@tonic-gate 			/* LINTED */
14697c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
14707c478bd9Sstevel@tonic-gate 		}
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/*
14747c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14757c478bd9Sstevel@tonic-gate 	 * entry is:
14767c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14777c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14787c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
14797c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
14807c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
14817c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
14827c478bd9Sstevel@tonic-gate 	 */
1483*f3324781Sab 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1484d326b23bSrie 		uchar_t	*pltent;
1485d326b23bSrie 		Xword	val1;
1486*f3324781Sab 		int	bswap;
14877c478bd9Sstevel@tonic-gate 
1488b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14897c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14907c478bd9Sstevel@tonic-gate 
1491*f3324781Sab 		/*
1492*f3324781Sab 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1493*f3324781Sab 		 * stage.
1494*f3324781Sab 		 */
1495*f3324781Sab 		if (!OFL_DO_RELOC(ofl))
1496*f3324781Sab 			return (1);
1497*f3324781Sab 
1498*f3324781Sab 		/*
1499*f3324781Sab 		 * If the running linker has a different byte order than
1500*f3324781Sab 		 * the target host, tell do_reloc_ld() to swap bytes.
1501*f3324781Sab 		 *
1502*f3324781Sab 		 * We know the GOT is PROGBITS --- we don't have
1503*f3324781Sab 		 * to check.
1504*f3324781Sab 		 */
1505*f3324781Sab 		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1506*f3324781Sab 
15077c478bd9Sstevel@tonic-gate 		/*
15087c478bd9Sstevel@tonic-gate 		 * filin:
15097c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
15107c478bd9Sstevel@tonic-gate 		 *
15117c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
15127c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
15137c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
15147c478bd9Sstevel@tonic-gate 		 */
15157c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1516de777a60Sab 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1517de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
15187c478bd9Sstevel@tonic-gate 
1519*f3324781Sab 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
1520*f3324781Sab 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1521*f3324781Sab 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1522*f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1523*f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1524*f3324781Sab 			return (S_ERROR);
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		/*
15287c478bd9Sstevel@tonic-gate 		 * filin:
15297c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
15307c478bd9Sstevel@tonic-gate 		 */
15317c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1532de777a60Sab 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1533de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1534*f3324781Sab 
1535*f3324781Sab 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08],
1536*f3324781Sab 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1537*f3324781Sab 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1538*f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1539*f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1540*f3324781Sab 			return (S_ERROR);
15417c478bd9Sstevel@tonic-gate 		}
15427c478bd9Sstevel@tonic-gate 	}
1543*f3324781Sab 
15447c478bd9Sstevel@tonic-gate 	return (1);
15457c478bd9Sstevel@tonic-gate }
1546