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 /*
23*69112eddSAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24b3fbe5e6Sseizo  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27ba2be530Sab /* Get the x86 version of the relocation engine */
28ba2be530Sab #define	DO_RELOC_LIBLD_X86
29ba2be530Sab 
307c478bd9Sstevel@tonic-gate #include	<string.h>
317c478bd9Sstevel@tonic-gate #include	<stdio.h>
327c478bd9Sstevel@tonic-gate #include	<strings.h>
337c478bd9Sstevel@tonic-gate #include	<sys/elf_amd64.h>
347c478bd9Sstevel@tonic-gate #include	<debug.h>
357c478bd9Sstevel@tonic-gate #include	<reloc.h>
36ba2be530Sab #include	<i386/machdep_x86.h>
375aefb655Srie #include	"msg.h"
385aefb655Srie #include	"_libld.h"
39ba2be530Sab 
4057ef7aa9SRod Evans /*
4157ef7aa9SRod Evans  * Search the GOT index list for a GOT entry with a matching reference and the
4257ef7aa9SRod Evans  * proper addend.
4357ef7aa9SRod Evans  */
4457ef7aa9SRod Evans static Gotndx *
4557ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
4657ef7aa9SRod Evans {
4757ef7aa9SRod Evans 	Aliste	idx;
4857ef7aa9SRod Evans 	Gotndx	*gnp;
4957ef7aa9SRod Evans 
5057ef7aa9SRod Evans 	assert(rdesc != 0);
5157ef7aa9SRod Evans 
5257ef7aa9SRod Evans 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
5357ef7aa9SRod Evans 		return (ofl->ofl_tlsldgotndx);
54ba2be530Sab 
5557ef7aa9SRod Evans 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
5657ef7aa9SRod Evans 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
5757ef7aa9SRod Evans 		    (gnp->gn_gotref == gref)) {
5857ef7aa9SRod Evans 			return (gnp);
5957ef7aa9SRod Evans 		}
6057ef7aa9SRod Evans 	}
6157ef7aa9SRod Evans 	return (NULL);
6257ef7aa9SRod Evans }
637c478bd9Sstevel@tonic-gate 
6457ef7aa9SRod Evans static Xword
6557ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
6657ef7aa9SRod Evans {
6757ef7aa9SRod Evans 	Os_desc		*osp = ofl->ofl_osgot;
6857ef7aa9SRod Evans 	Sym_desc	*sdp = rdesc->rel_sym;
6957ef7aa9SRod Evans 	Xword		gotndx;
7057ef7aa9SRod Evans 	Gotref		gref;
7157ef7aa9SRod Evans 	Gotndx		*gnp;
7257ef7aa9SRod Evans 
7357ef7aa9SRod Evans 	if (rdesc->rel_flags & FLG_REL_DTLS)
7457ef7aa9SRod Evans 		gref = GOT_REF_TLSGD;
7557ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_MTLS)
7657ef7aa9SRod Evans 		gref = GOT_REF_TLSLD;
7757ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_STLS)
7857ef7aa9SRod Evans 		gref = GOT_REF_TLSIE;
7957ef7aa9SRod Evans 	else
8057ef7aa9SRod Evans 		gref = GOT_REF_GENERIC;
8157ef7aa9SRod Evans 
8257ef7aa9SRod Evans 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
8357ef7aa9SRod Evans 	assert(gnp);
8457ef7aa9SRod Evans 
8557ef7aa9SRod Evans 	gotndx = (Xword)gnp->gn_gotndx;
8657ef7aa9SRod Evans 
8757ef7aa9SRod Evans 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
8857ef7aa9SRod Evans 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
8957ef7aa9SRod Evans 		gotndx++;
9057ef7aa9SRod Evans 
9157ef7aa9SRod Evans 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
9257ef7aa9SRod Evans }
93ba2be530Sab 
94ba2be530Sab static Word
955aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc)
967c478bd9Sstevel@tonic-gate {
9757ef7aa9SRod Evans 	Rela	*rel = (Rela *)reloc;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* LINTED */
100ba2be530Sab 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
1017c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
1027c478bd9Sstevel@tonic-gate 	reld->rel_raddend = rel->r_addend;
1037c478bd9Sstevel@tonic-gate 	reld->rel_typedata = 0;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	reld->rel_flags |= FLG_REL_RELA;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
110ba2be530Sab static void
1115aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1127c478bd9Sstevel@tonic-gate {
1135aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
116ba2be530Sab static void
1175aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1207c478bd9Sstevel@tonic-gate 		/*
1217c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
1227c478bd9Sstevel@tonic-gate 		 */
1237c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
1247c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
128ba2be530Sab static void
1295aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1307c478bd9Sstevel@tonic-gate {
1315aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1325aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
1335aefb655Srie 		if (ofl->ofl_osgot)
1345aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1355aefb655Srie 		else
1365aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
1375aefb655Srie 		(*dyn)++;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
141ba2be530Sab static Xword
1425aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	Xword	value;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1477c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1487c478bd9Sstevel@tonic-gate 	return (value);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
1537c478bd9Sstevel@tonic-gate  *	JMP	*name1@GOTPCREL(%rip)
1547c478bd9Sstevel@tonic-gate  *	PUSHL	$index
1557c478bd9Sstevel@tonic-gate  *	JMP	.PLT0
1567c478bd9Sstevel@tonic-gate  */
157b3fbe5e6Sseizo static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
1587c478bd9Sstevel@tonic-gate /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1597c478bd9Sstevel@tonic-gate /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
1607c478bd9Sstevel@tonic-gate /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
1617c478bd9Sstevel@tonic-gate /* 0x10 */
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static uintptr_t
1657c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
1667c478bd9Sstevel@tonic-gate {
167b3fbe5e6Sseizo 	uchar_t		*plt0, *pltent, *gotent;
1687c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1697c478bd9Sstevel@tonic-gate 	Word		got_off;
1707c478bd9Sstevel@tonic-gate 	Xword		val1;
171ba2be530Sab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1747c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1757c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
176b3fbe5e6Sseizo 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
1777c478bd9Sstevel@tonic-gate 	pltent = plt0 + plt_off;
178b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	/* LINTED */
185b3fbe5e6Sseizo 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
186b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
187ba2be530Sab 	if (bswap)
188ba2be530Sab 		/* LINTED */
189ba2be530Sab 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1907c478bd9Sstevel@tonic-gate 
191f3324781Sab 	/*
192f3324781Sab 	 * If '-z noreloc' is specified - skip the do_reloc_ld
193f3324781Sab 	 * stage.
194f3324781Sab 	 */
195f3324781Sab 	if (!OFL_DO_RELOC(ofl))
196f3324781Sab 		return (1);
197f3324781Sab 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * patchup:
2007c478bd9Sstevel@tonic-gate 	 *	jmpq	*name1@gotpcrel(%rip)
2017c478bd9Sstevel@tonic-gate 	 *
2027c478bd9Sstevel@tonic-gate 	 * NOTE: 0x06 represents next instruction.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
205de777a60Sab 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
2067c478bd9Sstevel@tonic-gate 
207f3324781Sab 	if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
208f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
209f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
210f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
211f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
212f3324781Sab 		return (S_ERROR);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * patchup:
2177c478bd9Sstevel@tonic-gate 	 *	pushq	$pltndx
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
220f3324781Sab 
221f3324781Sab 	if (do_reloc_ld(R_AMD64_32, &pltent[0x07],
222f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
223f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
224f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
225f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
226f3324781Sab 		return (S_ERROR);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * patchup:
2317c478bd9Sstevel@tonic-gate 	 *	jmpq	.plt0(%rip)
232f3324781Sab 	 * NOTE: 0x10 represents next instruction. The rather complex
233f3324781Sab 	 * series of casts is necessary to sign extend an offset into
234f3324781Sab 	 * a 64-bit value while satisfying various compiler error
235f3324781Sab 	 * checks.  Handle with care.
2367c478bd9Sstevel@tonic-gate 	 */
237b3fbe5e6Sseizo 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
238b3fbe5e6Sseizo 	    (uintptr_t)(&pltent[0x10])));
239b3fbe5e6Sseizo 
240f3324781Sab 	if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c],
241f3324781Sab 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
242f3324781Sab 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
243f3324781Sab 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
244f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
245f3324781Sab 		return (S_ERROR);
2467c478bd9Sstevel@tonic-gate 	}
247f3324781Sab 
2487c478bd9Sstevel@tonic-gate 	return (1);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
251ba2be530Sab static uintptr_t
2525aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
2557c478bd9Sstevel@tonic-gate 	Word		ndx;
2567c478bd9Sstevel@tonic-gate 	Xword		roffset, value;
2577c478bd9Sstevel@tonic-gate 	Sxword		raddend;
2587c478bd9Sstevel@tonic-gate 	Rela		rea;
2597c478bd9Sstevel@tonic-gate 	char		*relbits;
2607c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2617c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	raddend = orsp->rel_raddend;
2647c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
2687c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
2697c478bd9Sstevel@tonic-gate 	 */
2707c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2717c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2727c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2735aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2747c478bd9Sstevel@tonic-gate 		return (1);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
2797c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if (orsp->rel_move)
2825aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section then we need to adjust the
2867c478bd9Sstevel@tonic-gate 	 * raddend field to compensate for the new position of the input section
2877c478bd9Sstevel@tonic-gate 	 * within the new output section.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
29057ef7aa9SRod Evans 		if (ofl->ofl_parsyms &&
2917c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2927c478bd9Sstevel@tonic-gate 		    /* LINTED */
2935aefb655Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
2945aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2957c478bd9Sstevel@tonic-gate 			sectmoved = 1;
2967c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
2977c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value;
2987c478bd9Sstevel@tonic-gate 			else
2997c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value -
3007c478bd9Sstevel@tonic-gate 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3017c478bd9Sstevel@tonic-gate 			/* LINTED */
3027c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
3037c478bd9Sstevel@tonic-gate 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3047c478bd9Sstevel@tonic-gate 				raddend +=
305de777a60Sab 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3067c478bd9Sstevel@tonic-gate 		} else {
3077c478bd9Sstevel@tonic-gate 			/* LINTED */
3087c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
3097c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3107c478bd9Sstevel@tonic-gate 				raddend +=
311de777a60Sab 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
3187c478bd9Sstevel@tonic-gate 		/*
3197c478bd9Sstevel@tonic-gate 		 * Note: for GOT relative relocations on amd64
3207c478bd9Sstevel@tonic-gate 		 *	 we discard the addend.  It was relevant
3217c478bd9Sstevel@tonic-gate 		 *	 to the reference - not to the data item
3227c478bd9Sstevel@tonic-gate 		 *	 being referenced (ie: that -4 thing).
3237c478bd9Sstevel@tonic-gate 		 */
3247c478bd9Sstevel@tonic-gate 		raddend = 0;
3257c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3265aefb655Srie 		roffset = ld_calc_got_offset(orsp, ofl);
3275aefb655Srie 
3287c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
3297c478bd9Sstevel@tonic-gate 		/*
3307c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
3317c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
3347c478bd9Sstevel@tonic-gate 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
3357c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
3367c478bd9Sstevel@tonic-gate 		raddend = 0;
3377c478bd9Sstevel@tonic-gate 		if (plt_entry(ofl, sdp) == S_ERROR)
3387c478bd9Sstevel@tonic-gate 			return (S_ERROR);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * This must be a R_AMD64_COPY.  For these set the roffset to
3437c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
3467c478bd9Sstevel@tonic-gate 		roffset = value;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		/*
3497c478bd9Sstevel@tonic-gate 		 * The raddend doesn't mean anything in a R_SPARC_COPY
3507c478bd9Sstevel@tonic-gate 		 * relocation.  Null it out because it can confuse people.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		raddend = 0;
3537c478bd9Sstevel@tonic-gate 	} else {
3547c478bd9Sstevel@tonic-gate 		osp = orsp->rel_osdesc;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/*
3577c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
3587c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
3597c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
3627c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3637c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3647c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
3657c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3697c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
3737c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
3747c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
3757c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
3787c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
3797c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3807c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3817c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
3827c478bd9Sstevel@tonic-gate 			/*
3837c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
3847c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
3857c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
3867c478bd9Sstevel@tonic-gate 			 */
38757ef7aa9SRod Evans 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
38857ef7aa9SRod Evans 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
3897c478bd9Sstevel@tonic-gate 			else
3907c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
3917c478bd9Sstevel@tonic-gate 		} else
39235450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
3937c478bd9Sstevel@tonic-gate 	} else
3947c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Add the symbols 'value' to the addend field.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_ADVAL)
4007c478bd9Sstevel@tonic-gate 		raddend += value;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
4047010c12aSrie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
4077c478bd9Sstevel@tonic-gate 		raddend = 0;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4127c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
4137c478bd9Sstevel@tonic-gate 	rea.r_addend = raddend;
4145aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
4155aefb655Srie 	    orsp->rel_sname));
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
4237c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rela));
4247c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
4287c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
4297c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4307c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4337c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
4347c478bd9Sstevel@tonic-gate 
4355aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
4367c478bd9Sstevel@tonic-gate 	return (1);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * amd64 Instructions for TLS processing
4417c478bd9Sstevel@tonic-gate  */
442b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4477c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 *	0x09 addq x@gottpoff(%rip), %rax
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	0x48, 0x03, 0x05, 0x00, 0x00,
4527c478bd9Sstevel@tonic-gate 	0x00, 0x00
4537c478bd9Sstevel@tonic-gate };
4547c478bd9Sstevel@tonic-gate 
455b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4607c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 *	0x09 leaq x@gottpoff(%rip), %rax
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	0x48, 0x8d, 0x80, 0x00, 0x00,
4657c478bd9Sstevel@tonic-gate 	0x00, 0x00
4667c478bd9Sstevel@tonic-gate };
4677c478bd9Sstevel@tonic-gate 
468b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = {
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	0x66,
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	0x66,
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * .byte 0x66
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	0x66,
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * movq %fs:0, %rax
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4857c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00
4867c478bd9Sstevel@tonic-gate };
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 
4895aefb655Srie static Fixupret
4905aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4937c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
494b3fbe5e6Sseizo 	uchar_t		*offset;
4957c478bd9Sstevel@tonic-gate 
496b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
497b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
498b3fbe5e6Sseizo 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * IE reference model
5037c478bd9Sstevel@tonic-gate 		 */
5047c478bd9Sstevel@tonic-gate 		switch (rtype) {
5057c478bd9Sstevel@tonic-gate 		case R_AMD64_TLSGD:
5067c478bd9Sstevel@tonic-gate 			/*
5077c478bd9Sstevel@tonic-gate 			 *  GD -> IE
5087c478bd9Sstevel@tonic-gate 			 *
5097c478bd9Sstevel@tonic-gate 			 * Transition:
5107c478bd9Sstevel@tonic-gate 			 *	0x00 .byte 0x66
5117c478bd9Sstevel@tonic-gate 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5127c478bd9Sstevel@tonic-gate 			 *	0x08 .word 0x6666
5137c478bd9Sstevel@tonic-gate 			 *	0x0a rex64
5147c478bd9Sstevel@tonic-gate 			 *	0x0b call __tls_get_addr@plt
5157c478bd9Sstevel@tonic-gate 			 *	0x10
5167c478bd9Sstevel@tonic-gate 			 * To:
5177c478bd9Sstevel@tonic-gate 			 *	0x00 movq %fs:0, %rax
5187c478bd9Sstevel@tonic-gate 			 *	0x09 addq x@gottpoff(%rip), %rax
5197c478bd9Sstevel@tonic-gate 			 *	0x10
5207c478bd9Sstevel@tonic-gate 			 */
5215aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
522051d39bbSrie 			    R_AMD64_GOTTPOFF, arsp));
5237c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5247c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
5257c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
5265aefb655Srie 
5277c478bd9Sstevel@tonic-gate 			/*
528051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
5297c478bd9Sstevel@tonic-gate 			 * sequence.
5307c478bd9Sstevel@tonic-gate 			 */
5317c478bd9Sstevel@tonic-gate 			offset -= 4;
5327c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
5335aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5347c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5355aefb655Srie 
5367c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
5377c478bd9Sstevel@tonic-gate 			/*
538051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
5397c478bd9Sstevel@tonic-gate 			 */
5405aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
541051d39bbSrie 			    R_AMD64_NONE, arsp));
5427c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 * LE reference model
5487c478bd9Sstevel@tonic-gate 	 */
5497c478bd9Sstevel@tonic-gate 	switch (rtype) {
5507c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
5517c478bd9Sstevel@tonic-gate 		/*
5527c478bd9Sstevel@tonic-gate 		 * GD -> LE
5537c478bd9Sstevel@tonic-gate 		 *
5547c478bd9Sstevel@tonic-gate 		 * Transition:
5557c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5567c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5577c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
5587c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
5597c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
5607c478bd9Sstevel@tonic-gate 		 *	0x10
5617c478bd9Sstevel@tonic-gate 		 * To:
5627c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5637c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5647c478bd9Sstevel@tonic-gate 		 *	0x10
5657c478bd9Sstevel@tonic-gate 		 */
5665aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
567051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5687c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5697c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
5707c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5715aefb655Srie 
5727c478bd9Sstevel@tonic-gate 		/*
573051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5747c478bd9Sstevel@tonic-gate 		 */
5757c478bd9Sstevel@tonic-gate 		offset -= 4;
5765aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
5777c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5785aefb655Srie 
5797c478bd9Sstevel@tonic-gate 	case R_AMD64_GOTTPOFF:
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * IE -> LE
5827c478bd9Sstevel@tonic-gate 		 *
5837c478bd9Sstevel@tonic-gate 		 * Transition:
5847c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5857c478bd9Sstevel@tonic-gate 		 *	0x09 addq x@gottopoff(%rip), %rax
5867c478bd9Sstevel@tonic-gate 		 *	0x10
5877c478bd9Sstevel@tonic-gate 		 * To:
5887c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5897c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5907c478bd9Sstevel@tonic-gate 		 *	0x10
5917c478bd9Sstevel@tonic-gate 		 */
592051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
593051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
5947c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5957c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
5965aefb655Srie 
5977c478bd9Sstevel@tonic-gate 		/*
598051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		offset -= 12;
6015aefb655Srie 
6027c478bd9Sstevel@tonic-gate 		/*
603051d39bbSrie 		 * Same code sequence used in the GD -> LE transition.
6047c478bd9Sstevel@tonic-gate 		 */
6055aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6067c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6075aefb655Srie 
6087c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * LD -> LE
6117c478bd9Sstevel@tonic-gate 		 *
6127c478bd9Sstevel@tonic-gate 		 * Transition
6137c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
6147c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
6157c478bd9Sstevel@tonic-gate 		 *	0x0c
6167c478bd9Sstevel@tonic-gate 		 * To:
6177c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
6187c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
6197c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
6207c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
6217c478bd9Sstevel@tonic-gate 		 */
622051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
623051d39bbSrie 		    R_AMD64_NONE, arsp));
6247c478bd9Sstevel@tonic-gate 		offset -= 3;
6255aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
6267c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
6275aefb655Srie 
6287c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * LD->LE
6317c478bd9Sstevel@tonic-gate 		 *
6327c478bd9Sstevel@tonic-gate 		 * Transition:
6337c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
6347c478bd9Sstevel@tonic-gate 		 * To:
6357c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
6367c478bd9Sstevel@tonic-gate 		 */
637051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
638051d39bbSrie 		    R_AMD64_TPOFF32, arsp));
6397c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6407c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6417c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
647ba2be530Sab static uintptr_t
6485aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6497c478bd9Sstevel@tonic-gate {
650141040e8Srie 	Rel_desc	*arsp;
651141040e8Srie 	Rel_cache	*rcp;
65257ef7aa9SRod Evans 	Aliste		idx;
6537c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6541d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
6557c478bd9Sstevel@tonic-gate 
65657ef7aa9SRod Evans 	if (ofl->ofl_actrels)
6577010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6587010c12aSrie 
6597c478bd9Sstevel@tonic-gate 	/*
660141040e8Srie 	 * Process active relocations.
6617c478bd9Sstevel@tonic-gate 	 */
66257ef7aa9SRod Evans 	for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) {
6637c478bd9Sstevel@tonic-gate 		/* LINTED */
6647c478bd9Sstevel@tonic-gate 		for (arsp = (Rel_desc *)(rcp + 1);
6657c478bd9Sstevel@tonic-gate 		    arsp < rcp->rc_free; arsp++) {
666b3fbe5e6Sseizo 			uchar_t		*addr;
6677c478bd9Sstevel@tonic-gate 			Xword 		value;
6687c478bd9Sstevel@tonic-gate 			Sym_desc	*sdp;
6697c478bd9Sstevel@tonic-gate 			const char	*ifl_name;
6707c478bd9Sstevel@tonic-gate 			Xword		refaddr;
6717c478bd9Sstevel@tonic-gate 			int		moved = 0;
6727c478bd9Sstevel@tonic-gate 			Gotref		gref;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 			/*
6757c478bd9Sstevel@tonic-gate 			 * If the section this relocation is against has been
6767c478bd9Sstevel@tonic-gate 			 * discarded (-zignore), then discard (skip) the
6777c478bd9Sstevel@tonic-gate 			 * relocation itself.
6787c478bd9Sstevel@tonic-gate 			 */
6797c478bd9Sstevel@tonic-gate 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
6807c478bd9Sstevel@tonic-gate 			    ((arsp->rel_flags &
6817c478bd9Sstevel@tonic-gate 			    (FLG_REL_GOT | FLG_REL_BSS |
6827c478bd9Sstevel@tonic-gate 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
6835aefb655Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
6845aefb655Srie 				    M_MACH, arsp));
6857c478bd9Sstevel@tonic-gate 				continue;
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			/*
689d2d5cf7cSAli Bahrami 			 * We determine what the 'got reference'
6907c478bd9Sstevel@tonic-gate 			 * model (if required) is at this point.  This
6917c478bd9Sstevel@tonic-gate 			 * needs to be done before tls_fixup() since
6927c478bd9Sstevel@tonic-gate 			 * it may 'transition' our instructions.
6937c478bd9Sstevel@tonic-gate 			 *
6947c478bd9Sstevel@tonic-gate 			 * The got table entries have already been assigned,
6957c478bd9Sstevel@tonic-gate 			 * and we bind to those initial entries.
6967c478bd9Sstevel@tonic-gate 			 */
6977c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_DTLS)
6987c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSGD;
6997c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_MTLS)
7007c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSLD;
7017c478bd9Sstevel@tonic-gate 			else if (arsp->rel_flags & FLG_REL_STLS)
7027c478bd9Sstevel@tonic-gate 				gref = GOT_REF_TLSIE;
7037c478bd9Sstevel@tonic-gate 			else
7047c478bd9Sstevel@tonic-gate 				gref = GOT_REF_GENERIC;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			/*
7077c478bd9Sstevel@tonic-gate 			 * Perform any required TLS fixups.
7087c478bd9Sstevel@tonic-gate 			 */
7097c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7107c478bd9Sstevel@tonic-gate 				Fixupret	ret;
711141040e8Srie 
7125aefb655Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7137c478bd9Sstevel@tonic-gate 					return (S_ERROR);
7147c478bd9Sstevel@tonic-gate 				if (ret == FIX_DONE)
7157c478bd9Sstevel@tonic-gate 					continue;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 			/*
7197c478bd9Sstevel@tonic-gate 			 * If this is a relocation against a move table, or
7207c478bd9Sstevel@tonic-gate 			 * expanded move table, adjust the relocation entries.
7217c478bd9Sstevel@tonic-gate 			 */
7227c478bd9Sstevel@tonic-gate 			if (arsp->rel_move)
7235aefb655Srie 				ld_adj_movereloc(ofl, arsp);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 			sdp = arsp->rel_sym;
7267c478bd9Sstevel@tonic-gate 			refaddr = arsp->rel_roffset +
7277c478bd9Sstevel@tonic-gate 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
7307c478bd9Sstevel@tonic-gate 			    (arsp->rel_flags & FLG_REL_GOTCL))
7317c478bd9Sstevel@tonic-gate 				value = 0;
7327c478bd9Sstevel@tonic-gate 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
7337c478bd9Sstevel@tonic-gate 			    STT_SECTION) {
734141040e8Srie 				Sym_desc	*sym;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 				/*
7377c478bd9Sstevel@tonic-gate 				 * The value for a symbol pointing to a SECTION
7387c478bd9Sstevel@tonic-gate 				 * is based off of that sections position.
7397c478bd9Sstevel@tonic-gate 				 */
7407c478bd9Sstevel@tonic-gate 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
7417c478bd9Sstevel@tonic-gate 				    /* LINTED */
742bf2f215aSAli Bahrami 				    (sym = ld_am_I_partial(arsp,
743bf2f215aSAli Bahrami 				    arsp->rel_raddend))) {
7447c478bd9Sstevel@tonic-gate 					/*
745b26cc8daSAli Bahrami 					 * The symbol was moved, so adjust
746b26cc8daSAli Bahrami 					 * the value relative to the new
747b26cc8daSAli Bahrami 					 * section.
7487c478bd9Sstevel@tonic-gate 					 */
7497c478bd9Sstevel@tonic-gate 					value = sym->sd_sym->st_value;
7507c478bd9Sstevel@tonic-gate 					moved = 1;
751b26cc8daSAli Bahrami 
752b26cc8daSAli Bahrami 					/*
753b26cc8daSAli Bahrami 					 * The original raddend covers the
754b26cc8daSAli Bahrami 					 * displacement from the section start
755b26cc8daSAli Bahrami 					 * to the desired address. The value
756b26cc8daSAli Bahrami 					 * computed above gets us from the
757b26cc8daSAli Bahrami 					 * section start to the start of the
758b26cc8daSAli Bahrami 					 * symbol range. Adjust the old raddend
759b26cc8daSAli Bahrami 					 * to remove the offset from section
760b26cc8daSAli Bahrami 					 * start to symbol start, leaving the
761b26cc8daSAli Bahrami 					 * displacement within the range of
762b26cc8daSAli Bahrami 					 * the symbol.
763b26cc8daSAli Bahrami 					 */
764bf2f215aSAli Bahrami 					arsp->rel_raddend -=
765bf2f215aSAli Bahrami 					    sym->sd_osym->st_value;
7667c478bd9Sstevel@tonic-gate 				} else {
767141040e8Srie 					value = _elf_getxoff(
768141040e8Srie 					    sdp->sd_isc->is_indata);
7697c478bd9Sstevel@tonic-gate 					if (sdp->sd_isc->is_shdr->sh_flags &
7707c478bd9Sstevel@tonic-gate 					    SHF_ALLOC)
771de777a60Sab 						value +=
772de777a60Sab 						    sdp->sd_isc->is_osdesc->
773de777a60Sab 						    os_shdr->sh_addr;
7747c478bd9Sstevel@tonic-gate 				}
7757c478bd9Sstevel@tonic-gate 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
7767c478bd9Sstevel@tonic-gate 					value -= ofl->ofl_tlsphdr->p_vaddr;
7772926dd2eSrie 
7782926dd2eSrie 			} else if (IS_SIZE(arsp->rel_rtype)) {
7792926dd2eSrie 				/*
7802926dd2eSrie 				 * Size relocations require the symbols size.
7812926dd2eSrie 				 */
7822926dd2eSrie 				value = sdp->sd_sym->st_size;
783141040e8Srie 			} else {
7847c478bd9Sstevel@tonic-gate 				/*
7857010c12aSrie 				 * Else the value is the symbols value.
7867c478bd9Sstevel@tonic-gate 				 */
7877c478bd9Sstevel@tonic-gate 				value = sdp->sd_sym->st_value;
788141040e8Srie 			}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 			/*
7917c478bd9Sstevel@tonic-gate 			 * Relocation against the GLOBAL_OFFSET_TABLE.
7927c478bd9Sstevel@tonic-gate 			 */
7937c478bd9Sstevel@tonic-gate 			if (arsp->rel_flags & FLG_REL_GOT)
7947c478bd9Sstevel@tonic-gate 				arsp->rel_osdesc = ofl->ofl_osgot;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 			/*
7977c478bd9Sstevel@tonic-gate 			 * If loadable and not producing a relocatable object
7987c478bd9Sstevel@tonic-gate 			 * add the sections virtual address to the reference
7997c478bd9Sstevel@tonic-gate 			 * address.
8007c478bd9Sstevel@tonic-gate 			 */
8017c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
802141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
8037c478bd9Sstevel@tonic-gate 				refaddr += arsp->rel_isdesc->is_osdesc->
8047c478bd9Sstevel@tonic-gate 				    os_shdr->sh_addr;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * If this entry has a PLT assigned to it, it's
8087c478bd9Sstevel@tonic-gate 			 * value is actually the address of the PLT (and
8097c478bd9Sstevel@tonic-gate 			 * not the address of the function).
8107c478bd9Sstevel@tonic-gate 			 */
8117c478bd9Sstevel@tonic-gate 			if (IS_PLT(arsp->rel_rtype)) {
8127c478bd9Sstevel@tonic-gate 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
8135aefb655Srie 					value = ld_calc_plt_addr(sdp, ofl);
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 			/*
8177c478bd9Sstevel@tonic-gate 			 * Add relocations addend to value.  Add extra
8187c478bd9Sstevel@tonic-gate 			 * relocation addend if needed.
8197c478bd9Sstevel@tonic-gate 			 *
8207c478bd9Sstevel@tonic-gate 			 * Note: for GOT relative relocations on amd64
8217c478bd9Sstevel@tonic-gate 			 *	 we discard the addend.  It was relevant
8227c478bd9Sstevel@tonic-gate 			 *	 to the reference - not to the data item
8237c478bd9Sstevel@tonic-gate 			 *	 being referenced (ie: that -4 thing).
8247c478bd9Sstevel@tonic-gate 			 */
8257c478bd9Sstevel@tonic-gate 			if ((arsp->rel_flags & FLG_REL_GOT) == 0)
826bf2f215aSAli Bahrami 				value += arsp->rel_raddend;
8277c478bd9Sstevel@tonic-gate 
828141040e8Srie 			/*
829141040e8Srie 			 * Determine whether the value needs further adjustment.
830141040e8Srie 			 * Filter through the attributes of the relocation to
831141040e8Srie 			 * determine what adjustment is required.  Note, many
832141040e8Srie 			 * of the following cases are only applicable when a
833141040e8Srie 			 * .got is present.  As a .got is not generated when a
834141040e8Srie 			 * relocatable object is being built, any adjustments
835141040e8Srie 			 * that require a .got need to be skipped.
836141040e8Srie 			 */
837141040e8Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
838141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8397c478bd9Sstevel@tonic-gate 				Xword		R1addr;
8407c478bd9Sstevel@tonic-gate 				uintptr_t	R2addr;
8417c478bd9Sstevel@tonic-gate 				Word		gotndx;
8427c478bd9Sstevel@tonic-gate 				Gotndx		*gnp;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 				/*
8457c478bd9Sstevel@tonic-gate 				 * Perform relocation against GOT table.  Since
8467c478bd9Sstevel@tonic-gate 				 * this doesn't fit exactly into a relocation
8477c478bd9Sstevel@tonic-gate 				 * we place the appropriate byte in the GOT
8487c478bd9Sstevel@tonic-gate 				 * directly
8497c478bd9Sstevel@tonic-gate 				 *
8507c478bd9Sstevel@tonic-gate 				 * Calculate offset into GOT at which to apply
8517c478bd9Sstevel@tonic-gate 				 * the relocation.
8527c478bd9Sstevel@tonic-gate 				 */
85357ef7aa9SRod Evans 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
8547c478bd9Sstevel@tonic-gate 				    ofl, arsp);
8557c478bd9Sstevel@tonic-gate 				assert(gnp);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_DTPOFF64)
8587c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx + 1;
8597c478bd9Sstevel@tonic-gate 				else
8607c478bd9Sstevel@tonic-gate 					gotndx = gnp->gn_gotndx;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 				/*
8657c478bd9Sstevel@tonic-gate 				 * Add the GOTs data's offset.
8667c478bd9Sstevel@tonic-gate 				 */
8677c478bd9Sstevel@tonic-gate 				R2addr = R1addr + (uintptr_t)
8687c478bd9Sstevel@tonic-gate 				    arsp->rel_osdesc->os_outdata->d_buf;
8697c478bd9Sstevel@tonic-gate 
8705aefb655Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
871635216b6SRod Evans 				    ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
8727c478bd9Sstevel@tonic-gate 				    arsp->rel_rtype, R1addr, value,
8737c478bd9Sstevel@tonic-gate 				    arsp->rel_sname, arsp->rel_osdesc));
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 				/*
8767c478bd9Sstevel@tonic-gate 				 * And do it.
8777c478bd9Sstevel@tonic-gate 				 */
878f3324781Sab 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
879f3324781Sab 					*(Xword *)R2addr =
880ba2be530Sab 					    ld_bswap_Xword(value);
881f3324781Sab 				else
882f3324781Sab 					*(Xword *)R2addr = value;
8837c478bd9Sstevel@tonic-gate 				continue;
8847c478bd9Sstevel@tonic-gate 
885141040e8Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
886141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
8877c478bd9Sstevel@tonic-gate 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
888141040e8Srie 
889141040e8Srie 			} else if (IS_GOTPCREL(arsp->rel_rtype) &&
890141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
891141040e8Srie 				Gotndx *gnp;
892141040e8Srie 
8937c478bd9Sstevel@tonic-gate 				/*
8947c478bd9Sstevel@tonic-gate 				 * Calculation:
8957c478bd9Sstevel@tonic-gate 				 *	G + GOT + A - P
8967c478bd9Sstevel@tonic-gate 				 */
89757ef7aa9SRod Evans 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
8987c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
899141040e8Srie 				assert(gnp);
9007c478bd9Sstevel@tonic-gate 				value = (Xword)(ofl->ofl_osgot->os_shdr->
9017c478bd9Sstevel@tonic-gate 				    sh_addr) + ((Xword)gnp->gn_gotndx *
9027c478bd9Sstevel@tonic-gate 				    M_GOT_ENTSIZE) + arsp->rel_raddend -
9037c478bd9Sstevel@tonic-gate 				    refaddr;
904141040e8Srie 
905141040e8Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
906141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
907141040e8Srie 				value = (Xword)(ofl->ofl_osgot->os_shdr->
90854d82594Sseizo 				    sh_addr) - refaddr + arsp->rel_raddend;
909141040e8Srie 
9107c478bd9Sstevel@tonic-gate 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
911141040e8Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9127c478bd9Sstevel@tonic-gate 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9137c478bd9Sstevel@tonic-gate 				value -= refaddr;
914141040e8Srie 
9157c478bd9Sstevel@tonic-gate 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
916141040e8Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
917141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9187c478bd9Sstevel@tonic-gate 				Gotndx	*gnp;
9197c478bd9Sstevel@tonic-gate 
92057ef7aa9SRod Evans 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
9217c478bd9Sstevel@tonic-gate 				    ofl, arsp);
922141040e8Srie 				assert(gnp);
9237c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
924141040e8Srie 
925141040e8Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
926141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
927141040e8Srie 				Gotndx *gnp;
9287c478bd9Sstevel@tonic-gate 
92957ef7aa9SRod Evans 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
9307c478bd9Sstevel@tonic-gate 				    gref, ofl, arsp);
9317c478bd9Sstevel@tonic-gate 				assert(gnp);
9327c478bd9Sstevel@tonic-gate 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
933141040e8Srie 
934141040e8Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
935141040e8Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9367c478bd9Sstevel@tonic-gate 				Xword	tlsstatsize;
937141040e8Srie 
9387c478bd9Sstevel@tonic-gate 				/*
9397c478bd9Sstevel@tonic-gate 				 * This is the LE TLS reference model.  Static
9407c478bd9Sstevel@tonic-gate 				 * offset is hard-coded.
9417c478bd9Sstevel@tonic-gate 				 */
942141040e8Srie 				tlsstatsize =
943141040e8Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
9447c478bd9Sstevel@tonic-gate 				    M_TLSSTATALIGN);
9457c478bd9Sstevel@tonic-gate 				value = tlsstatsize - value;
946141040e8Srie 
9477c478bd9Sstevel@tonic-gate 				/*
948141040e8Srie 				 * Since this code is fixed up, it assumes a
949141040e8Srie 				 * negative offset that can be added to the
950141040e8Srie 				 * thread pointer.
9517c478bd9Sstevel@tonic-gate 				 */
9527c478bd9Sstevel@tonic-gate 				if (arsp->rel_rtype == R_AMD64_TPOFF32)
9537c478bd9Sstevel@tonic-gate 					value = -value;
9547c478bd9Sstevel@tonic-gate 			}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_file)
9577c478bd9Sstevel@tonic-gate 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
9587c478bd9Sstevel@tonic-gate 			else
9597c478bd9Sstevel@tonic-gate 				ifl_name = MSG_INTL(MSG_STR_NULL);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 			/*
9627c478bd9Sstevel@tonic-gate 			 * Make sure we have data to relocate.  Compiler and
9637c478bd9Sstevel@tonic-gate 			 * assembler developers have been known to generate
9647c478bd9Sstevel@tonic-gate 			 * relocations against invalid sections (normally .bss),
9657c478bd9Sstevel@tonic-gate 			 * so for their benefit give them sufficient information
9667c478bd9Sstevel@tonic-gate 			 * to help analyze the problem.  End users should never
9677c478bd9Sstevel@tonic-gate 			 * see this.
9687c478bd9Sstevel@tonic-gate 			 */
9697c478bd9Sstevel@tonic-gate 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
970de777a60Sab 				Conv_inv_buf_t inv_buf;
971de777a60Sab 
9725aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9735aefb655Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
974de777a60Sab 				    conv_reloc_amd64_type(arsp->rel_rtype,
975de777a60Sab 				    0, &inv_buf), ifl_name,
976de777a60Sab 				    demangle(arsp->rel_sname),
9774a8d0ea7SAli Bahrami 				    EC_WORD(arsp->rel_isdesc->is_scnndx),
9787c478bd9Sstevel@tonic-gate 				    arsp->rel_isdesc->is_name);
9797c478bd9Sstevel@tonic-gate 				return (S_ERROR);
9807c478bd9Sstevel@tonic-gate 			}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 			/*
9837c478bd9Sstevel@tonic-gate 			 * Get the address of the data item we need to modify.
9847c478bd9Sstevel@tonic-gate 			 */
985b3fbe5e6Sseizo 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
986b3fbe5e6Sseizo 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
987b3fbe5e6Sseizo 			    is_indata));
9887c478bd9Sstevel@tonic-gate 
989635216b6SRod Evans 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
9905aefb655Srie 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
9915aefb655Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
9927c478bd9Sstevel@tonic-gate 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
9937c478bd9Sstevel@tonic-gate 
9945aefb655Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
9957c478bd9Sstevel@tonic-gate 			    ofl->ofl_size) || (arsp->rel_roffset >
9967c478bd9Sstevel@tonic-gate 			    arsp->rel_osdesc->os_shdr->sh_size)) {
997de777a60Sab 				int		class;
998de777a60Sab 				Conv_inv_buf_t inv_buf;
9997c478bd9Sstevel@tonic-gate 
1000b3fbe5e6Sseizo 				if (((uintptr_t)addr -
10015aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10027c478bd9Sstevel@tonic-gate 					class = ERR_FATAL;
10037c478bd9Sstevel@tonic-gate 				else
10047c478bd9Sstevel@tonic-gate 					class = ERR_WARNING;
10057c478bd9Sstevel@tonic-gate 
10065aefb655Srie 				eprintf(ofl->ofl_lml, class,
10075aefb655Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
1008de777a60Sab 				    conv_reloc_amd64_type(arsp->rel_rtype,
1009de777a60Sab 				    0, &inv_buf), ifl_name,
10104a8d0ea7SAli Bahrami 				    EC_WORD(arsp->rel_isdesc->is_scnndx),
1011de777a60Sab 				    arsp->rel_isdesc->is_name,
10127c478bd9Sstevel@tonic-gate 				    demangle(arsp->rel_sname),
10137c478bd9Sstevel@tonic-gate 				    EC_ADDR((uintptr_t)addr -
10145aefb655Srie 				    (uintptr_t)ofl->ofl_nehdr));
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 				if (class == ERR_FATAL) {
10177c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
10187c478bd9Sstevel@tonic-gate 					continue;
10197c478bd9Sstevel@tonic-gate 				}
10207c478bd9Sstevel@tonic-gate 			}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 			/*
10237c478bd9Sstevel@tonic-gate 			 * The relocation is additive.  Ignore the previous
10247c478bd9Sstevel@tonic-gate 			 * symbol value if this local partial symbol is
10257c478bd9Sstevel@tonic-gate 			 * expanded.
10267c478bd9Sstevel@tonic-gate 			 */
10277c478bd9Sstevel@tonic-gate 			if (moved)
10287c478bd9Sstevel@tonic-gate 				value -= *addr;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 			/*
1031f3324781Sab 			 * If '-z noreloc' is specified - skip the do_reloc_ld
10327c478bd9Sstevel@tonic-gate 			 * stage.
10337c478bd9Sstevel@tonic-gate 			 */
1034f3324781Sab 			if (OFL_DO_RELOC(ofl)) {
1035f3324781Sab 				/*
1036f3324781Sab 				 * If this is a PROGBITS section and the
1037f3324781Sab 				 * running linker has a different byte order
1038f3324781Sab 				 * than the target host, tell do_reloc_ld()
1039f3324781Sab 				 * to swap bytes.
1040f3324781Sab 				 */
1041f3324781Sab 				if (do_reloc_ld((uchar_t)arsp->rel_rtype,
10425aefb655Srie 				    addr, &value, arsp->rel_sname, ifl_name,
1043f3324781Sab 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
10445aefb655Srie 				    ofl->ofl_lml) == 0)
10457c478bd9Sstevel@tonic-gate 					return_code = S_ERROR;
10467c478bd9Sstevel@tonic-gate 			}
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 	return (return_code);
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
1052ba2be530Sab static uintptr_t
10535aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10547c478bd9Sstevel@tonic-gate {
1055141040e8Srie 	Rel_desc	*orsp;
1056141040e8Srie 	Rel_cache	*rcp;
1057141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
105857ef7aa9SRod Evans 	static size_t	nextsize = 0;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/*
10617c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
10627c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10637c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
10647c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
10657c478bd9Sstevel@tonic-gate 	 */
1066635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
10677c478bd9Sstevel@tonic-gate 		return (1);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/*
107057ef7aa9SRod Evans 	 * Obtain the new available relocation cache entry.
10717c478bd9Sstevel@tonic-gate 	 */
107257ef7aa9SRod Evans 	if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize,
107357ef7aa9SRod Evans 	    REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR)
107457ef7aa9SRod Evans 		return (S_ERROR);
10757c478bd9Sstevel@tonic-gate 
107657ef7aa9SRod Evans 	orsp = rcp->rc_free;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/*
10797c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
10807c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
10817c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
10827c478bd9Sstevel@tonic-gate 	 */
10837c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10847c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
10857c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 		/*
10887c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
10897c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
10907c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
10917c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10940bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10957c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10967c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10977c478bd9Sstevel@tonic-gate 			else
10987c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1099141040e8Srie 		} else {
1100141040e8Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11017c478bd9Sstevel@tonic-gate 
1102c1c6f601Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11037c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11047c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11057c478bd9Sstevel@tonic-gate 			}
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	*orsp = *rsp;
11107c478bd9Sstevel@tonic-gate 	orsp->rel_flags |= flags;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	rcp->rc_free++;
11137c478bd9Sstevel@tonic-gate 	ofl->ofl_outrelscnt++;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
11167c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
11177c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
11187c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
11197c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
11207c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
11217c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
11227c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
11237c478bd9Sstevel@tonic-gate 	else
11247c478bd9Sstevel@tonic-gate 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
11277c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
11317c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
11327c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
11337c478bd9Sstevel@tonic-gate 	 */
11347c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
11357c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11367c478bd9Sstevel@tonic-gate 
1137141040e8Srie 	/*
1138141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1139141040e8Srie 	 */
1140141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1141141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1142141040e8Srie 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11477c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11505aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11517c478bd9Sstevel@tonic-gate 	}
11525aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11535aefb655Srie 	    M_MACH, orsp));
11547c478bd9Sstevel@tonic-gate 	return (1);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate /*
11587c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11597c478bd9Sstevel@tonic-gate  */
1160ba2be530Sab static uintptr_t
11615aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11627c478bd9Sstevel@tonic-gate {
11631d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
11647c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11650bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11667c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
11707c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
11717c478bd9Sstevel@tonic-gate 	 * then
11727c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
11737c478bd9Sstevel@tonic-gate 	 * fi
11747c478bd9Sstevel@tonic-gate 	 */
11757c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11762926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
11777c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11787c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
11797c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11807c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11817c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		/*
11847c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11857c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
11867c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
11877c478bd9Sstevel@tonic-gate 		 */
11887c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11891d9df23bSab 			return (ld_add_outrel(0, rsp, ofl));
11907c478bd9Sstevel@tonic-gate 		}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
11935aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11947c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11957c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
11967c478bd9Sstevel@tonic-gate 		return (1);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
1199b3fbe5e6Sseizo 	/*
1200b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1201b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1202b3fbe5e6Sseizo 	 * message.
1203b3fbe5e6Sseizo 	 *
1204b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1205b3fbe5e6Sseizo 	 *	a) it's undefined
1206b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1207b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1208b3fbe5e6Sseizo 	 *
1209b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1210b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1211b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1212b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1213b3fbe5e6Sseizo 	 */
12147c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1215b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1216b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1217b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1218de777a60Sab 		Conv_inv_buf_t inv_buf;
1219de777a60Sab 
1220b3fbe5e6Sseizo 		/*
1221b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1222b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1223b3fbe5e6Sseizo 		 * can not be resolved.
1224b3fbe5e6Sseizo 		 */
1225b3fbe5e6Sseizo 		if (rsp->rel_osdesc &&
1226b3fbe5e6Sseizo 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1227b3fbe5e6Sseizo 			return (0);
12285aefb655Srie 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
12295aefb655Srie 		    MSG_INTL(MSG_REL_EXTERNSYM),
1230de777a60Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
12317c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
12327c478bd9Sstevel@tonic-gate 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
12337c478bd9Sstevel@tonic-gate 		return (1);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12387c478bd9Sstevel@tonic-gate 	 */
12395aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 
1243ba2be530Sab static uintptr_t
12445aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12477c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12481d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12497c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	/*
1252d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1253d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12547c478bd9Sstevel@tonic-gate 	 */
1255d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12567c478bd9Sstevel@tonic-gate 		/*
1257d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12587c478bd9Sstevel@tonic-gate 		 */
12597c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12607c478bd9Sstevel@tonic-gate 
1261d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12627c478bd9Sstevel@tonic-gate 			/*
1263d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12647c478bd9Sstevel@tonic-gate 			 */
126557ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
126657ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
12677c478bd9Sstevel@tonic-gate 
1268d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1269d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1270d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1271d326b23bSrie 					return (S_ERROR);
1272d326b23bSrie 			}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 			/*
1275d326b23bSrie 			 * IE access model.
12767c478bd9Sstevel@tonic-gate 			 */
12777c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
12785aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 			/*
1281d326b23bSrie 			 * Fixups are required for other executable models.
12827c478bd9Sstevel@tonic-gate 			 */
12835aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12847c478bd9Sstevel@tonic-gate 			    rsp, ofl));
12857c478bd9Sstevel@tonic-gate 		}
1286d326b23bSrie 
12877c478bd9Sstevel@tonic-gate 		/*
1288d326b23bSrie 		 * LE access model.
12897c478bd9Sstevel@tonic-gate 		 */
12907c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
12915aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1292d326b23bSrie 
12935aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12945aefb655Srie 		    rsp, ofl));
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/*
1298d326b23bSrie 	 * Building a shared object.
1299d326b23bSrie 	 *
1300d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13017c478bd9Sstevel@tonic-gate 	 */
130257ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
130357ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1304d326b23bSrie 
1305d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
130657ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
13077c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1308d326b23bSrie 
13095aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
131057ef7aa9SRod Evans 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
131157ef7aa9SRod Evans 	    ofl, rsp)) == NULL)) {
1312d326b23bSrie 
1313d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1314d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1315d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13167c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13205aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13217c478bd9Sstevel@tonic-gate 
13225aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
1326ba2be530Sab static uintptr_t
132757ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
132857ef7aa9SRod Evans     Rel_desc *rsp, Sym_desc *sdp)
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	Xword		raddend;
133157ef7aa9SRod Evans 	Gotndx		gn, *gnp;
133257ef7aa9SRod Evans 	Aliste		idx;
13337c478bd9Sstevel@tonic-gate 	uint_t		gotents;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
133657ef7aa9SRod Evans 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
13377c478bd9Sstevel@tonic-gate 		return (1);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13407c478bd9Sstevel@tonic-gate 		gotents = 2;
13417c478bd9Sstevel@tonic-gate 	else
13427c478bd9Sstevel@tonic-gate 		gotents = 1;
13437c478bd9Sstevel@tonic-gate 
134457ef7aa9SRod Evans 	gn.gn_addend = raddend;
134557ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
134657ef7aa9SRod Evans 	gn.gn_gotref = gref;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
135157ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
135257ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
135357ef7aa9SRod Evans 				return (S_ERROR);
135457ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
135557ef7aa9SRod Evans 			ofl->ofl_tlsldgotndx = gnp;
135657ef7aa9SRod Evans 		}
13577c478bd9Sstevel@tonic-gate 		return (1);
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
136057ef7aa9SRod Evans 	idx = 0;
136157ef7aa9SRod Evans 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
136257ef7aa9SRod Evans 		if (gnp->gn_addend > raddend)
136357ef7aa9SRod Evans 			break;
13647c478bd9Sstevel@tonic-gate 	}
136557ef7aa9SRod Evans 
136657ef7aa9SRod Evans 	/*
136757ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
136857ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
136957ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
137057ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
137157ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
137257ef7aa9SRod Evans 	 */
137357ef7aa9SRod Evans 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
137457ef7aa9SRod Evans 	    AL_CNT_SDP_GOT, idx) == NULL)
137557ef7aa9SRod Evans 		return (S_ERROR);
137657ef7aa9SRod Evans 
13777c478bd9Sstevel@tonic-gate 	return (1);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
1380ba2be530Sab static void
13815aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
13847c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1385141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate 
1388b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
13897c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
13907c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
13917c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
13927c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
13937c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
13947c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
13957c478bd9Sstevel@tonic-gate };
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
13997c478bd9Sstevel@tonic-gate  */
1400ba2be530Sab static uintptr_t
1401d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14027c478bd9Sstevel@tonic-gate {
1403ba2be530Sab 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1404ba2be530Sab 
14057c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1406d326b23bSrie 		Sym_desc	*sdp;
14077c478bd9Sstevel@tonic-gate 
14085aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1409635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1410d326b23bSrie 			uchar_t	*genptr;
1411d326b23bSrie 
1412d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14137c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14147c478bd9Sstevel@tonic-gate 			/* LINTED */
14157c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
1416ba2be530Sab 			if (bswap)
1417ba2be530Sab 				/* LINTED */
1418ba2be530Sab 				*(Xword *)genptr =
1419ba2be530Sab 				    /* LINTED */
1420ba2be530Sab 				    ld_bswap_Xword(*(Xword *)genptr);
14217c478bd9Sstevel@tonic-gate 		}
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
14257c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14267c478bd9Sstevel@tonic-gate 	 * entry is:
14277c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14287c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14297c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
14307c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
14317c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
14327c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
14337c478bd9Sstevel@tonic-gate 	 */
1434f3324781Sab 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1435d326b23bSrie 		uchar_t	*pltent;
1436d326b23bSrie 		Xword	val1;
14377c478bd9Sstevel@tonic-gate 
1438b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14397c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14407c478bd9Sstevel@tonic-gate 
1441f3324781Sab 		/*
1442f3324781Sab 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1443f3324781Sab 		 * stage.
1444f3324781Sab 		 */
1445f3324781Sab 		if (!OFL_DO_RELOC(ofl))
1446f3324781Sab 			return (1);
1447f3324781Sab 
14487c478bd9Sstevel@tonic-gate 		/*
14497c478bd9Sstevel@tonic-gate 		 * filin:
14507c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
14517c478bd9Sstevel@tonic-gate 		 *
14527c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
14537c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
14547c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
14557c478bd9Sstevel@tonic-gate 		 */
14567c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1457de777a60Sab 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1458de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
14597c478bd9Sstevel@tonic-gate 
1460f3324781Sab 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
1461f3324781Sab 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1462f3324781Sab 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1463f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1464f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1465f3324781Sab 			return (S_ERROR);
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		/*
14697c478bd9Sstevel@tonic-gate 		 * filin:
14707c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
14717c478bd9Sstevel@tonic-gate 		 */
14727c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1473de777a60Sab 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1474de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1475f3324781Sab 
1476f3324781Sab 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08],
1477f3324781Sab 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1478f3324781Sab 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1479f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1480f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1481f3324781Sab 			return (S_ERROR);
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 	}
1484f3324781Sab 
14857c478bd9Sstevel@tonic-gate 	return (1);
14867c478bd9Sstevel@tonic-gate }
1487ba2be530Sab 
1488ba2be530Sab 
1489ba2be530Sab 
1490ba2be530Sab /*
1491ba2be530Sab  * Template for generating "void (*)(void)" function
1492ba2be530Sab  */
1493ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1494ba2be530Sab /* 0x00 */	0x55,				/* pushq  %rbp */
1495ba2be530Sab /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1496ba2be530Sab /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1497ba2be530Sab /* 0x07 */	0x5d,				/* popq   %rbp */
1498ba2be530Sab /* 0x08 */	0xc3				/* ret */
1499ba2be530Sab };
1500ba2be530Sab 
1501ba2be530Sab 
15023c573fccSAli Bahrami /*
15033c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15043c573fccSAli Bahrami  *
15053c573fccSAli Bahrami  * entry:
15063c573fccSAli Bahrami  *
15073c573fccSAli Bahrami  *	base - base address of section being filled
15083c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15093c573fccSAli Bahrami  *	cnt - # bytes to be filled
15103c573fccSAli Bahrami  *
15113c573fccSAli Bahrami  * exit:
15123c573fccSAli Bahrami  *	The fill has been completed.
15133c573fccSAli Bahrami  */
15143c573fccSAli Bahrami static void
15153c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
15163c573fccSAli Bahrami {
15173c573fccSAli Bahrami 	/*
15183c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
15193c573fccSAli Bahrami 	 * There are no alignment constraints.
15203c573fccSAli Bahrami 	 */
15213c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
15223c573fccSAli Bahrami }
15233c573fccSAli Bahrami 
15243c573fccSAli Bahrami 
1525ba2be530Sab /*
1526ba2be530Sab  * Return the ld_targ definition for this target.
1527ba2be530Sab  */
1528ba2be530Sab const Target *
1529ba2be530Sab ld_targ_init_x86(void)
1530ba2be530Sab {
1531ba2be530Sab 	static const Target _ld_targ = {
1532ba2be530Sab 		{			/* Target_mach */
1533ba2be530Sab 			M_MACH,			/* m_mach */
1534ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1535ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1536ba2be530Sab 			M_CLASS,		/* m_class */
1537ba2be530Sab 			M_DATA,			/* m_data */
1538ba2be530Sab 
1539ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1540ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1541bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1542ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
1543*69112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1544ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1545ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1546ba2be530Sab 
1547ba2be530Sab 			/* Relocation type codes */
1548ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1549ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1550ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1551ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1552ba2be530Sab 			M_R_NUM,		/* m_r_num */
1553ba2be530Sab 			M_R_NONE,		/* m_r_none */
1554ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1555ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1556ba2be530Sab 
1557ba2be530Sab 			/* Relocation related constants */
1558ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1559ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1560ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1561ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1562ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1563ba2be530Sab 
1564ba2be530Sab 			/* GOT related constants */
1565ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1566ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1567ba2be530Sab 
1568ba2be530Sab 			/* PLT related constants */
1569ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1570ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1571ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1572ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1573ba2be530Sab 
15747e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
15757e16fca0SAli Bahrami 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
15767e16fca0SAli Bahrami 
1577ba2be530Sab 			M_DT_REGISTER,		/* m_dt_register */
1578ba2be530Sab 		},
1579ba2be530Sab 		{			/* Target_machid */
1580ba2be530Sab 			M_ID_ARRAY,		/* id_array */
1581ba2be530Sab 			M_ID_BSS,		/* id_bss */
1582ba2be530Sab 			M_ID_CAP,		/* id_cap */
1583ba2be530Sab 			M_ID_DATA,		/* id_data */
1584ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1585ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1586ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1587ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1588ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1589ba2be530Sab 			M_ID_GOT,		/* id_got */
1590ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1591ba2be530Sab 			M_ID_HASH,		/* id_hash */
1592ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1593ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1594ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1595ba2be530Sab 			M_ID_NOTE,		/* id_note */
1596ba2be530Sab 			M_ID_NULL,		/* id_null */
1597ba2be530Sab 			M_ID_PLT,		/* id_plt */
1598ba2be530Sab 			M_ID_REL,		/* id_rel */
1599ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1600ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1601ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1602ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1603ba2be530Sab 			M_ID_TEXT,		/* id_text */
1604ba2be530Sab 			M_ID_TLS,		/* id_tls */
1605ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1606ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1607ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
16087e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1609ba2be530Sab 			M_ID_USER,		/* id_user */
1610ba2be530Sab 			M_ID_VERSION,		/* id_version */
1611ba2be530Sab 		},
1612ba2be530Sab 		{			/* Target_nullfunc */
1613ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1614ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1615ba2be530Sab 		},
16163c573fccSAli Bahrami 		{			/* Target_fillfunc */
16173c573fccSAli Bahrami 			execfill		/* ff_execfill */
16183c573fccSAli Bahrami 		},
1619ba2be530Sab 		{			/* Target_machrel */
1620ba2be530Sab 			reloc_table,
1621ba2be530Sab 
1622ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1623ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1624ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1625ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1626ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1627ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1628ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1629ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1630ba2be530Sab 			NULL,			/* mr_reloc_register */
1631ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1632ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1633ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1634ba2be530Sab 			NULL,			/* mr_assign_got */
163557ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1636ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1637ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1638ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1639ba2be530Sab 			NULL,			/* mr_allocate_got */
1640ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1641ba2be530Sab 		},
1642ba2be530Sab 		{			/* Target_machsym */
1643ba2be530Sab 			NULL,			/* ms_reg_check */
1644ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1645ba2be530Sab 			NULL,			/* ms_is_regsym */
1646ba2be530Sab 			NULL,			/* ms_reg_find */
1647ba2be530Sab 			NULL			/* ms_reg_enter */
1648ba2be530Sab 		}
1649ba2be530Sab 	};
1650ba2be530Sab 
1651ba2be530Sab 	return (&_ld_targ);
1652ba2be530Sab }
1653