15aefb655Srie /*
25aefb655Srie  * CDDL HEADER START
35aefb655Srie  *
45aefb655Srie  * 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.
75aefb655Srie  *
85aefb655Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95aefb655Srie  * or http://www.opensolaris.org/os/licensing.
105aefb655Srie  * See the License for the specific language governing permissions
115aefb655Srie  * and limitations under the License.
125aefb655Srie  *
135aefb655Srie  * When distributing Covered Code, include this CDDL HEADER in each
145aefb655Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155aefb655Srie  * If applicable, add the following below this CDDL HEADER, with the
165aefb655Srie  * fields enclosed by brackets "[]" replaced with your own identifying
175aefb655Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
185aefb655Srie  *
195aefb655Srie  * CDDL HEADER END
205aefb655Srie  */
215aefb655Srie 
225aefb655Srie /*
23bf994817SAli Bahrami  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
245aefb655Srie  */
255aefb655Srie 
26ba2be530Sab196087 /* Get the x86 version of the relocation engine */
27ba2be530Sab196087 #define	DO_RELOC_LIBLD_X86
28ba2be530Sab196087 
295aefb655Srie #include	<string.h>
305aefb655Srie #include	<stdio.h>
315aefb655Srie #include	<strings.h>
325aefb655Srie #include	<sys/elf_amd64.h>
335aefb655Srie #include	<debug.h>
345aefb655Srie #include	<reloc.h>
35ba2be530Sab196087 #include	<i386/machdep_x86.h>
365aefb655Srie #include	"msg.h"
375aefb655Srie #include	"_libld.h"
385aefb655Srie 
3957ef7aa9SRod Evans /*
40bf994817SAli Bahrami  * This module uses do_reloc_ld() to execute several synthesized relocations.
41bf994817SAli Bahrami  * That function expects to be passed two things that we need to construct
42bf994817SAli Bahrami  * here:
43bf994817SAli Bahrami  *
44bf994817SAli Bahrami  * 1)	A Rel_desc descriptor for each relocation type, from which the
45bf994817SAli Bahrami  *	rel_rtype field, and nothing else, is obtained. This is easily
46bf994817SAli Bahrami  *	handled by constructing the necessary descriptors.
47bf994817SAli Bahrami  *
48bf994817SAli Bahrami  * 2)	A function, which called with the Rel_desc descriptor, returns
49bf994817SAli Bahrami  *	a string representing the name of the symbol associated with
50bf994817SAli Bahrami  *	the descriptor. The usual function for this is ld_reloc_sym_name().
51bf994817SAli Bahrami  *	However, that function will not work in this case, as these synthetic
52bf994817SAli Bahrami  *	relocations do not have an associated symbol. We supply the
53bf994817SAli Bahrami  *	syn_rdesc_sym_name() function to simply return the fixed name.
54bf994817SAli Bahrami  */
55bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_gotpcrel = {
56bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_GOTPCREL };
57bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_32 = {
58bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_32 };
59bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_pc32 = {
60bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_PC32 };
61bf994817SAli Bahrami 
62bf994817SAli Bahrami /*ARGSUSED*/
63bf994817SAli Bahrami static const char *
syn_rdesc_sym_name(Rel_desc * rdesc)64bf994817SAli Bahrami syn_rdesc_sym_name(Rel_desc *rdesc)
65bf994817SAli Bahrami {
66bf994817SAli Bahrami 	return (MSG_ORIG(MSG_SYM_PLTENT));
67bf994817SAli Bahrami }
68bf994817SAli Bahrami 
69bf994817SAli Bahrami /*
7057ef7aa9SRod Evans  * Search the GOT index list for a GOT entry with a matching reference and the
7157ef7aa9SRod Evans  * proper addend.
7257ef7aa9SRod Evans  */
7357ef7aa9SRod Evans static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)7457ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
7557ef7aa9SRod Evans {
7657ef7aa9SRod Evans 	Aliste	idx;
7757ef7aa9SRod Evans 	Gotndx	*gnp;
78ba2be530Sab196087 
7957ef7aa9SRod Evans 	assert(rdesc != 0);
80ba2be530Sab196087 
8157ef7aa9SRod Evans 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
8257ef7aa9SRod Evans 		return (ofl->ofl_tlsldgotndx);
8357ef7aa9SRod Evans 
8457ef7aa9SRod Evans 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
8557ef7aa9SRod Evans 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
8657ef7aa9SRod Evans 		    (gnp->gn_gotref == gref)) {
8757ef7aa9SRod Evans 			return (gnp);
8857ef7aa9SRod Evans 		}
8957ef7aa9SRod Evans 	}
9057ef7aa9SRod Evans 	return (NULL);
9157ef7aa9SRod Evans }
9257ef7aa9SRod Evans 
9357ef7aa9SRod Evans static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)9457ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
9557ef7aa9SRod Evans {
9657ef7aa9SRod Evans 	Os_desc		*osp = ofl->ofl_osgot;
9757ef7aa9SRod Evans 	Sym_desc	*sdp = rdesc->rel_sym;
9857ef7aa9SRod Evans 	Xword		gotndx;
9957ef7aa9SRod Evans 	Gotref		gref;
10057ef7aa9SRod Evans 	Gotndx		*gnp;
10157ef7aa9SRod Evans 
10257ef7aa9SRod Evans 	if (rdesc->rel_flags & FLG_REL_DTLS)
10357ef7aa9SRod Evans 		gref = GOT_REF_TLSGD;
10457ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_MTLS)
10557ef7aa9SRod Evans 		gref = GOT_REF_TLSLD;
10657ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_STLS)
10757ef7aa9SRod Evans 		gref = GOT_REF_TLSIE;
10857ef7aa9SRod Evans 	else
10957ef7aa9SRod Evans 		gref = GOT_REF_GENERIC;
11057ef7aa9SRod Evans 
11157ef7aa9SRod Evans 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
11257ef7aa9SRod Evans 	assert(gnp);
11357ef7aa9SRod Evans 
11457ef7aa9SRod Evans 	gotndx = (Xword)gnp->gn_gotndx;
11557ef7aa9SRod Evans 
11657ef7aa9SRod Evans 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
11757ef7aa9SRod Evans 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
11857ef7aa9SRod Evans 		gotndx++;
11957ef7aa9SRod Evans 
12057ef7aa9SRod Evans 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
12157ef7aa9SRod Evans }
122ba2be530Sab196087 
123ba2be530Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)124bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
1255aefb655Srie {
1265aefb655Srie 	Rela	*rel = (Rela *)reloc;
1275aefb655Srie 
1285aefb655Srie 	/* LINTED */
129ba2be530Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
1305aefb655Srie 	reld->rel_roffset = rel->r_offset;
1315aefb655Srie 	reld->rel_raddend = rel->r_addend;
132bf994817SAli Bahrami 	*typedata = 0;
1335aefb655Srie 
1345aefb655Srie 	reld->rel_flags |= FLG_REL_RELA;
1355aefb655Srie 
1365aefb655Srie 	return ((Word)ELF_R_SYM(rel->r_info));
1375aefb655Srie }
1385aefb655Srie 
139ba2be530Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1405aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1415aefb655Srie {
1425aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1435aefb655Srie }
1445aefb655Srie 
145ba2be530Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1465aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1475aefb655Srie {
1485aefb655Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1495aefb655Srie 		/*
1505aefb655Srie 		 * Create this entry if we are going to create a PLT table.
1515aefb655Srie 		 */
1525aefb655Srie 		if (ofl->ofl_pltcnt)
1535aefb655Srie 			(*cnt)++;		/* DT_PLTGOT */
1545aefb655Srie 	}
1555aefb655Srie }
1565aefb655Srie 
157ba2be530Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1585aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1595aefb655Srie {
1605aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1615aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
1625aefb655Srie 		if (ofl->ofl_osgot)
1635aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1645aefb655Srie 		else
1655aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
1665aefb655Srie 		(*dyn)++;
1675aefb655Srie 	}
1685aefb655Srie }
1695aefb655Srie 
170ba2be530Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1715aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1725aefb655Srie {
1735aefb655Srie 	Xword	value;
1745aefb655Srie 
1755aefb655Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1765aefb655Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1775aefb655Srie 	return (value);
1785aefb655Srie }
1795aefb655Srie 
1805aefb655Srie /*
1815aefb655Srie  *  Build a single plt entry - code is:
1825aefb655Srie  *	JMP	*name1@GOTPCREL(%rip)
1835aefb655Srie  *	PUSHL	$index
1845aefb655Srie  *	JMP	.PLT0
1855aefb655Srie  */
1865aefb655Srie static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
1875aefb655Srie /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1885aefb655Srie /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
1895aefb655Srie /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
1905aefb655Srie /* 0x10 */
1915aefb655Srie };
1925aefb655Srie 
1935aefb655Srie static uintptr_t
plt_entry(Ofl_desc * ofl,Sym_desc * sdp)1945aefb655Srie plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
1955aefb655Srie {
1965aefb655Srie 	uchar_t		*plt0, *pltent, *gotent;
1975aefb655Srie 	Sword		plt_off;
1985aefb655Srie 	Word		got_off;
1995aefb655Srie 	Xword		val1;
200ba2be530Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
2015aefb655Srie 
2025aefb655Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2035aefb655Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
2045aefb655Srie 	    M_PLT_ENTSIZE);
2055aefb655Srie 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
2065aefb655Srie 	pltent = plt0 + plt_off;
2075aefb655Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
2085aefb655Srie 
2095aefb655Srie 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
2105aefb655Srie 	/*
2115aefb655Srie 	 * Fill in the got entry with the address of the next instruction.
2125aefb655Srie 	 */
2135aefb655Srie 	/* LINTED */
2145aefb655Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
2155aefb655Srie 	    M_PLT_INSSIZE;
216ba2be530Sab196087 	if (bswap)
217ba2be530Sab196087 		/* LINTED */
218ba2be530Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
2195aefb655Srie 
2205aefb655Srie 	/*
221f3324781Sab196087 	 * If '-z noreloc' is specified - skip the do_reloc_ld
222f3324781Sab196087 	 * stage.
223f3324781Sab196087 	 */
224f3324781Sab196087 	if (!OFL_DO_RELOC(ofl))
225f3324781Sab196087 		return (1);
226f3324781Sab196087 
227f3324781Sab196087 	/*
2285aefb655Srie 	 * patchup:
2295aefb655Srie 	 *	jmpq	*name1@gotpcrel(%rip)
2305aefb655Srie 	 *
2315aefb655Srie 	 * NOTE: 0x06 represents next instruction.
2325aefb655Srie 	 */
2335aefb655Srie 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
2345aefb655Srie 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
2355aefb655Srie 
236bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02], &val1,
237bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
238bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
2391007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
240f3324781Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2415aefb655Srie 		return (S_ERROR);
2425aefb655Srie 	}
2435aefb655Srie 
2445aefb655Srie 	/*
2455aefb655Srie 	 * patchup:
2465aefb655Srie 	 *	pushq	$pltndx
2475aefb655Srie 	 */
2485aefb655Srie 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
249f3324781Sab196087 
250bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_32, &pltent[0x07], &val1,
251bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
252bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
2531007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
254f3324781Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2555aefb655Srie 		return (S_ERROR);
2565aefb655Srie 	}
2575aefb655Srie 
2585aefb655Srie 	/*
2595aefb655Srie 	 * patchup:
2605aefb655Srie 	 *	jmpq	.plt0(%rip)
261f3324781Sab196087 	 * NOTE: 0x10 represents next instruction. The rather complex
262f3324781Sab196087 	 * series of casts is necessary to sign extend an offset into
263f3324781Sab196087 	 * a 64-bit value while satisfying various compiler error
264f3324781Sab196087 	 * checks.  Handle with care.
2655aefb655Srie 	 */
2665aefb655Srie 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
2675aefb655Srie 	    (uintptr_t)(&pltent[0x10])));
2685aefb655Srie 
269bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_pc32, &pltent[0x0c], &val1,
270bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
271bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
2721007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
273f3324781Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2745aefb655Srie 		return (S_ERROR);
2755aefb655Srie 	}
276f3324781Sab196087 
2775aefb655Srie 	return (1);
2785aefb655Srie }
2795aefb655Srie 
280ba2be530Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)2811007fd6fSAli Bahrami ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2825aefb655Srie {
2835aefb655Srie 	Os_desc *	relosp, * osp = 0;
2845aefb655Srie 	Word		ndx;
2855aefb655Srie 	Xword		roffset, value;
2865aefb655Srie 	Sxword		raddend;
2875aefb655Srie 	Rela		rea;
2885aefb655Srie 	char		*relbits;
2895aefb655Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2905aefb655Srie 	int		sectmoved = 0;
2915aefb655Srie 
2925aefb655Srie 	raddend = orsp->rel_raddend;
2935aefb655Srie 	sdp = orsp->rel_sym;
2945aefb655Srie 
2955aefb655Srie 	/*
2965aefb655Srie 	 * If the section this relocation is against has been discarded
2975aefb655Srie 	 * (-zignore), then also discard (skip) the relocation itself.
2985aefb655Srie 	 */
2995aefb655Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
3005aefb655Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
3015aefb655Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
3025aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
3035aefb655Srie 		return (1);
3045aefb655Srie 	}
3055aefb655Srie 
3065aefb655Srie 	/*
3075aefb655Srie 	 * If this is a relocation against a move table, or expanded move
3085aefb655Srie 	 * table, adjust the relocation entries.
3095aefb655Srie 	 */
310bf994817SAli Bahrami 	if (RELAUX_GET_MOVE(orsp))
3115aefb655Srie 		ld_adj_movereloc(ofl, orsp);
3125aefb655Srie 
3135aefb655Srie 	/*
3145aefb655Srie 	 * If this is a relocation against a section then we need to adjust the
3155aefb655Srie 	 * raddend field to compensate for the new position of the input section
3165aefb655Srie 	 * within the new output section.
3175aefb655Srie 	 */
3185aefb655Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
31957ef7aa9SRod Evans 		if (ofl->ofl_parsyms &&
3205aefb655Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
3215aefb655Srie 		    /* LINTED */
3225aefb655Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
3235aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
3245aefb655Srie 			sectmoved = 1;
3255aefb655Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
3265aefb655Srie 				raddend = psym->sd_sym->st_value;
3275aefb655Srie 			else
3285aefb655Srie 				raddend = psym->sd_sym->st_value -
3295aefb655Srie 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3305aefb655Srie 			/* LINTED */
3315aefb655Srie 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
3325aefb655Srie 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3335aefb655Srie 				raddend +=
3345aefb655Srie 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3355aefb655Srie 		} else {
3365aefb655Srie 			/* LINTED */
3375aefb655Srie 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
3385aefb655Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3395aefb655Srie 				raddend +=
3405aefb655Srie 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
3415aefb655Srie 		}
3425aefb655Srie 	}
3435aefb655Srie 
3445aefb655Srie 	value = sdp->sd_sym->st_value;
3455aefb655Srie 
3465aefb655Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
3475aefb655Srie 		/*
3485aefb655Srie 		 * Note: for GOT relative relocations on amd64
3495aefb655Srie 		 *	 we discard the addend.  It was relevant
3505aefb655Srie 		 *	 to the reference - not to the data item
3515aefb655Srie 		 *	 being referenced (ie: that -4 thing).
3525aefb655Srie 		 */
3535aefb655Srie 		raddend = 0;
3545aefb655Srie 		osp = ofl->ofl_osgot;
3555aefb655Srie 		roffset = ld_calc_got_offset(orsp, ofl);
3565aefb655Srie 
3575aefb655Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
3585aefb655Srie 		/*
3595aefb655Srie 		 * Note that relocations for PLT's actually
3605aefb655Srie 		 * cause a relocation againt the GOT.
3615aefb655Srie 		 */
3625aefb655Srie 		osp = ofl->ofl_osplt;
3635aefb655Srie 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
3645aefb655Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
3655aefb655Srie 		raddend = 0;
3665aefb655Srie 		if (plt_entry(ofl, sdp) == S_ERROR)
3675aefb655Srie 			return (S_ERROR);
3685aefb655Srie 
3695aefb655Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
3705aefb655Srie 		/*
3715aefb655Srie 		 * This must be a R_AMD64_COPY.  For these set the roffset to
3725aefb655Srie 		 * point to the new symbols location.
3735aefb655Srie 		 */
3745aefb655Srie 		osp = ofl->ofl_isbss->is_osdesc;
3755aefb655Srie 		roffset = value;
3765aefb655Srie 
3775aefb655Srie 		/*
3785aefb655Srie 		 * The raddend doesn't mean anything in a R_SPARC_COPY
3795aefb655Srie 		 * relocation.  Null it out because it can confuse people.
3805aefb655Srie 		 */
3815aefb655Srie 		raddend = 0;
3825aefb655Srie 	} else {
383bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(orsp);
3845aefb655Srie 
3855aefb655Srie 		/*
3865aefb655Srie 		 * Calculate virtual offset of reference point; equals offset
3875aefb655Srie 		 * into section + vaddr of section for loadable sections, or
3885aefb655Srie 		 * offset plus section displacement for nonloadable sections.
3895aefb655Srie 		 */
3905aefb655Srie 		roffset = orsp->rel_roffset +
3915aefb655Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3925aefb655Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3935aefb655Srie 			roffset += orsp->rel_isdesc->is_osdesc->
3945aefb655Srie 			    os_shdr->sh_addr;
3955aefb655Srie 	}
3965aefb655Srie 
3975aefb655Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3985aefb655Srie 		relosp = ofl->ofl_osrel;
3995aefb655Srie 
4005aefb655Srie 	/*
4015aefb655Srie 	 * Assign the symbols index for the output relocation.  If the
4025aefb655Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
4035aefb655Srie 	 * the output sections symbols index.  Otherwise the index can be
4045aefb655Srie 	 * derived from the symbols index itself.
4055aefb655Srie 	 */
4065aefb655Srie 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
4075aefb655Srie 		ndx = STN_UNDEF;
4085aefb655Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
4095aefb655Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
4105aefb655Srie 		if (sectmoved == 0) {
4115aefb655Srie 			/*
4125aefb655Srie 			 * Check for a null input section. This can
4135aefb655Srie 			 * occur if this relocation references a symbol
4145aefb655Srie 			 * generated by sym_add_sym().
4155aefb655Srie 			 */
41657ef7aa9SRod Evans 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
41757ef7aa9SRod Evans 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
4185aefb655Srie 			else
4195aefb655Srie 				ndx = sdp->sd_shndx;
4205aefb655Srie 		} else
42135450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
4225aefb655Srie 	} else
4235aefb655Srie 		ndx = sdp->sd_symndx;
4245aefb655Srie 
4255aefb655Srie 	/*
4265aefb655Srie 	 * Add the symbols 'value' to the addend field.
4275aefb655Srie 	 */
4285aefb655Srie 	if (orsp->rel_flags & FLG_REL_ADVAL)
4295aefb655Srie 		raddend += value;
4305aefb655Srie 
4315aefb655Srie 	/*
4327010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
4337010c12aSrie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
4345aefb655Srie 	 */
4355aefb655Srie 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
4365aefb655Srie 		raddend = 0;
4375aefb655Srie 
4380bc0887eSRichard Lowe 	if ((orsp->rel_rtype != M_R_NONE) &&
4390bc0887eSRichard Lowe 	    (orsp->rel_rtype != M_R_RELATIVE)) {
4400bc0887eSRichard Lowe 		if (ndx == 0) {
4410bc0887eSRichard Lowe 			Conv_inv_buf_t	inv_buf;
4420bc0887eSRichard Lowe 			Is_desc *isp = orsp->rel_isdesc;
4430bc0887eSRichard Lowe 
4440bc0887eSRichard Lowe 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
4450bc0887eSRichard Lowe 			    conv_reloc_type(ofl->ofl_nehdr->e_machine,
4460bc0887eSRichard Lowe 			    orsp->rel_rtype, 0, &inv_buf),
4470bc0887eSRichard Lowe 			    isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
4480bc0887eSRichard Lowe 			    isp->is_name, EC_XWORD(roffset));
4490bc0887eSRichard Lowe 			return (S_ERROR);
4500bc0887eSRichard Lowe 		}
4510bc0887eSRichard Lowe 	}
4525aefb655Srie 
4535aefb655Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4545aefb655Srie 	rea.r_offset = roffset;
4555aefb655Srie 	rea.r_addend = raddend;
4565aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
457bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
4585aefb655Srie 
4595aefb655Srie 	/*
4605aefb655Srie 	 * Assert we haven't walked off the end of our relocation table.
4615aefb655Srie 	 */
4625aefb655Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4635aefb655Srie 
4640bc0887eSRichard Lowe 	relbits = (char *)relosp->os_outdata->d_buf;
4650bc0887eSRichard Lowe 
4665aefb655Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
4675aefb655Srie 	    (char *)&rea, sizeof (Rela));
4685aefb655Srie 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4695aefb655Srie 
4705aefb655Srie 	/*
4715aefb655Srie 	 * Determine if this relocation is against a non-writable, allocatable
4725aefb655Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
4735aefb655Srie 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4745aefb655Srie 	 * result in modifications to the .got.
4755aefb655Srie 	 */
4765aefb655Srie 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4775aefb655Srie 		osp = ofl->ofl_osgot;
4785aefb655Srie 
4791007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
4805aefb655Srie 	return (1);
4815aefb655Srie }
4825aefb655Srie 
4835aefb655Srie /*
4845aefb655Srie  * amd64 Instructions for TLS processing
4855aefb655Srie  */
4865aefb655Srie static uchar_t tlsinstr_gd_ie[] = {
4875aefb655Srie 	/*
4885aefb655Srie 	 *	0x00 movq %fs:0, %rax
4895aefb655Srie 	 */
4905aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
4915aefb655Srie 	0x00, 0x00, 0x00, 0x00,
4925aefb655Srie 	/*
4935aefb655Srie 	 *	0x09 addq x@gottpoff(%rip), %rax
4945aefb655Srie 	 */
4955aefb655Srie 	0x48, 0x03, 0x05, 0x00, 0x00,
4965aefb655Srie 	0x00, 0x00
4975aefb655Srie };
4985aefb655Srie 
4995aefb655Srie static uchar_t tlsinstr_gd_le[] = {
5005aefb655Srie 	/*
5015aefb655Srie 	 *	0x00 movq %fs:0, %rax
5025aefb655Srie 	 */
5035aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
5045aefb655Srie 	0x00, 0x00, 0x00, 0x00,
5055aefb655Srie 	/*
5065aefb655Srie 	 *	0x09 leaq x@gottpoff(%rip), %rax
5075aefb655Srie 	 */
5085aefb655Srie 	0x48, 0x8d, 0x80, 0x00, 0x00,
5095aefb655Srie 	0x00, 0x00
5105aefb655Srie };
5115aefb655Srie 
5125aefb655Srie static uchar_t tlsinstr_ld_le[] = {
5135aefb655Srie 	/*
5145aefb655Srie 	 * .byte 0x66
5155aefb655Srie 	 */
5165aefb655Srie 	0x66,
5175aefb655Srie 	/*
5185aefb655Srie 	 * .byte 0x66
5195aefb655Srie 	 */
5205aefb655Srie 	0x66,
5215aefb655Srie 	/*
5225aefb655Srie 	 * .byte 0x66
5235aefb655Srie 	 */
5245aefb655Srie 	0x66,
5255aefb655Srie 	/*
5265aefb655Srie 	 * movq %fs:0, %rax
5275aefb655Srie 	 */
5285aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
5295aefb655Srie 	0x00, 0x00, 0x00, 0x00
5305aefb655Srie };
5315aefb655Srie 
53249f9b365SRichard Lowe #define	REX_B		0x1
53349f9b365SRichard Lowe #define	REX_X		0x2
53449f9b365SRichard Lowe #define	REX_R		0x4
53549f9b365SRichard Lowe #define	REX_W		0x8
53649f9b365SRichard Lowe #define	REX_PREFIX	0x40
53749f9b365SRichard Lowe 
53849f9b365SRichard Lowe #define	REX_RW		(REX_PREFIX | REX_R | REX_W)
53949f9b365SRichard Lowe #define	REX_BW		(REX_PREFIX | REX_B | REX_W)
54049f9b365SRichard Lowe #define	REX_BRW		(REX_PREFIX | REX_B | REX_R | REX_W)
54149f9b365SRichard Lowe 
54249f9b365SRichard Lowe #define	REG_ESP		0x4
54349f9b365SRichard Lowe 
54449f9b365SRichard Lowe #define	INSN_ADDMR	0x03	/* addq mem,reg */
54549f9b365SRichard Lowe #define	INSN_ADDIR	0x81	/* addq imm,reg */
54649f9b365SRichard Lowe #define	INSN_MOVMR	0x8b	/* movq mem,reg */
54749f9b365SRichard Lowe #define	INSN_MOVIR	0xc7	/* movq imm,reg */
54849f9b365SRichard Lowe #define	INSN_LEA	0x8d	/* leaq mem,reg */
5495aefb655Srie 
5505aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)5515aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
5525aefb655Srie {
5535aefb655Srie 	Sym_desc	*sdp = arsp->rel_sym;
5545aefb655Srie 	Word		rtype = arsp->rel_rtype;
5555aefb655Srie 	uchar_t		*offset;
5565aefb655Srie 
5575aefb655Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
5585aefb655Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
559bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
5605aefb655Srie 
56149f9b365SRichard Lowe 	/*
56249f9b365SRichard Lowe 	 * Note that in certain of the original insn sequences below, the
56349f9b365SRichard Lowe 	 * instructions are not necessarily adjacent
56449f9b365SRichard Lowe 	 */
5655aefb655Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
5665aefb655Srie 		/*
5675aefb655Srie 		 * IE reference model
5685aefb655Srie 		 */
5695aefb655Srie 		switch (rtype) {
5705aefb655Srie 		case R_AMD64_TLSGD:
5715aefb655Srie 			/*
5725aefb655Srie 			 *  GD -> IE
5735aefb655Srie 			 *
5745aefb655Srie 			 * Transition:
5755aefb655Srie 			 *	0x00 .byte 0x66
5765aefb655Srie 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5775aefb655Srie 			 *	0x08 .word 0x6666
5785aefb655Srie 			 *	0x0a rex64
5795aefb655Srie 			 *	0x0b call __tls_get_addr@plt
5805aefb655Srie 			 *	0x10
5815aefb655Srie 			 * To:
5825aefb655Srie 			 *	0x00 movq %fs:0, %rax
5835aefb655Srie 			 *	0x09 addq x@gottpoff(%rip), %rax
5845aefb655Srie 			 *	0x10
5855aefb655Srie 			 */
5865aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
587bf994817SAli Bahrami 			    R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
5885aefb655Srie 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5895aefb655Srie 			arsp->rel_roffset += 8;
5905aefb655Srie 			arsp->rel_raddend = (Sxword)-4;
5915aefb655Srie 
5925aefb655Srie 			/*
593051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
5945aefb655Srie 			 * sequence.
5955aefb655Srie 			 */
5965aefb655Srie 			offset -= 4;
5975aefb655Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
5985aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5995aefb655Srie 			return (FIX_RELOC);
6005aefb655Srie 
6015aefb655Srie 		case R_AMD64_PLT32:
6025aefb655Srie 			/*
603051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
6045aefb655Srie 			 */
6055aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
606bf994817SAli Bahrami 			    R_AMD64_NONE, arsp, ld_reloc_sym_name));
6075aefb655Srie 			return (FIX_DONE);
6085aefb655Srie 		}
6095aefb655Srie 	}
6105aefb655Srie 
6115aefb655Srie 	/*
6125aefb655Srie 	 * LE reference model
6135aefb655Srie 	 */
6145aefb655Srie 	switch (rtype) {
6155aefb655Srie 	case R_AMD64_TLSGD:
6165aefb655Srie 		/*
6175aefb655Srie 		 * GD -> LE
6185aefb655Srie 		 *
6195aefb655Srie 		 * Transition:
6205aefb655Srie 		 *	0x00 .byte 0x66
6215aefb655Srie 		 *	0x01 leaq x@tlsgd(%rip), %rdi
6225aefb655Srie 		 *	0x08 .word 0x6666
6235aefb655Srie 		 *	0x0a rex64
6245aefb655Srie 		 *	0x0b call __tls_get_addr@plt
6255aefb655Srie 		 *	0x10
6265aefb655Srie 		 * To:
6275aefb655Srie 		 *	0x00 movq %fs:0, %rax
6285aefb655Srie 		 *	0x09 leaq x@tpoff(%rax), %rax
6295aefb655Srie 		 *	0x10
6305aefb655Srie 		 */
6315aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
632bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6335aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6345aefb655Srie 		arsp->rel_roffset += 8;
6355aefb655Srie 		arsp->rel_raddend = 0;
6365aefb655Srie 
6375aefb655Srie 		/*
638051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
6395aefb655Srie 		 */
6405aefb655Srie 		offset -= 4;
6415aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6425aefb655Srie 		return (FIX_RELOC);
6435aefb655Srie 
64449f9b365SRichard Lowe 	case R_AMD64_GOTTPOFF: {
6455aefb655Srie 		/*
6465aefb655Srie 		 * IE -> LE
6475aefb655Srie 		 *
64849f9b365SRichard Lowe 		 * Transition 1:
64949f9b365SRichard Lowe 		 *	movq %fs:0, %reg
65049f9b365SRichard Lowe 		 *	addq x@gottpoff(%rip), %reg
6515aefb655Srie 		 * To:
65249f9b365SRichard Lowe 		 *	movq %fs:0, %reg
65349f9b365SRichard Lowe 		 *	leaq x@tpoff(%reg), %reg
65449f9b365SRichard Lowe 		 *
65549f9b365SRichard Lowe 		 * Transition (as a special case):
65649f9b365SRichard Lowe 		 *	movq %fs:0, %r12/%rsp
65749f9b365SRichard Lowe 		 *	addq x@gottpoff(%rip), %r12/%rsp
65849f9b365SRichard Lowe 		 * To:
65949f9b365SRichard Lowe 		 *	movq %fs:0, %r12/%rsp
66049f9b365SRichard Lowe 		 *	addq x@tpoff(%rax), %r12/%rsp
66149f9b365SRichard Lowe 		 *
66249f9b365SRichard Lowe 		 * Transition 2:
66349f9b365SRichard Lowe 		 *	movq x@gottpoff(%rip), %reg
66449f9b365SRichard Lowe 		 *	movq %fs:(%reg), %reg
66549f9b365SRichard Lowe 		 * To:
66649f9b365SRichard Lowe 		 *	movq x@tpoff(%reg), %reg
66749f9b365SRichard Lowe 		 *	movq %fs:(%reg), %reg
6685aefb655Srie 		 */
66949f9b365SRichard Lowe 		Conv_inv_buf_t	inv_buf;
67049f9b365SRichard Lowe 		uint8_t reg;		/* Register */
67149f9b365SRichard Lowe 
67249f9b365SRichard Lowe 		offset -= 3;
67349f9b365SRichard Lowe 
67449f9b365SRichard Lowe 		reg = offset[2] >> 3; /* Encoded dest. reg. operand */
67549f9b365SRichard Lowe 
676051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
677bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6785aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6795aefb655Srie 		arsp->rel_raddend = 0;
6805aefb655Srie 
6815aefb655Srie 		/*
68249f9b365SRichard Lowe 		 * This is transition 2, and the special case of form 1 where
68349f9b365SRichard Lowe 		 * a normal transition would index %rsp or %r12 and need a SIB
68449f9b365SRichard Lowe 		 * byte in the leaq for which we lack space
6855aefb655Srie 		 */
68649f9b365SRichard Lowe 		if ((offset[1] == INSN_MOVMR) ||
68749f9b365SRichard Lowe 		    ((offset[1] == INSN_ADDMR) && (reg == REG_ESP))) {
6885aefb655Srie 			/*
68949f9b365SRichard Lowe 			 * If we needed an extra bit of MOD.reg to refer to
69049f9b365SRichard Lowe 			 * this register as the dest of the original movq we
69149f9b365SRichard Lowe 			 * need an extra bit of MOD.rm to refer to it in the
69249f9b365SRichard Lowe 			 * dest of the replacement movq or addq.
6935aefb655Srie 			 */
69449f9b365SRichard Lowe 			if (offset[0] == REX_RW)
69549f9b365SRichard Lowe 				offset[0] = REX_BW;
6965aefb655Srie 
69749f9b365SRichard Lowe 			offset[1] = (offset[1] == INSN_MOVMR) ?
69849f9b365SRichard Lowe 			    INSN_MOVIR : INSN_ADDIR;
69949f9b365SRichard Lowe 			offset[2] = 0xc0 | reg;
70049f9b365SRichard Lowe 
70149f9b365SRichard Lowe 			return (FIX_RELOC);
70249f9b365SRichard Lowe 		} else if (offset[1] == INSN_ADDMR) {
70349f9b365SRichard Lowe 			/*
70449f9b365SRichard Lowe 			 * If we needed an extra bit of MOD.reg to refer to
70549f9b365SRichard Lowe 			 * this register in the dest of the addq we need an
70649f9b365SRichard Lowe 			 * extra bit of both MOD.reg and MOD.rm to refer to it
70749f9b365SRichard Lowe 			 * in the source and dest of the leaq
70849f9b365SRichard Lowe 			 */
70949f9b365SRichard Lowe 			if (offset[0] == REX_RW)
71049f9b365SRichard Lowe 				offset[0] = REX_BRW;
71149f9b365SRichard Lowe 
71249f9b365SRichard Lowe 			offset[1] = INSN_LEA;
71349f9b365SRichard Lowe 			offset[2] = 0x80 | (reg << 3) | reg;
71449f9b365SRichard Lowe 
71549f9b365SRichard Lowe 			return (FIX_RELOC);
71649f9b365SRichard Lowe 		}
71749f9b365SRichard Lowe 
71849f9b365SRichard Lowe 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
71949f9b365SRichard Lowe 		    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
72049f9b365SRichard Lowe 		    arsp->rel_isdesc->is_file->ifl_name,
72149f9b365SRichard Lowe 		    ld_reloc_sym_name(arsp),
72249f9b365SRichard Lowe 		    arsp->rel_isdesc->is_name,
72349f9b365SRichard Lowe 		    EC_OFF(arsp->rel_roffset));
72449f9b365SRichard Lowe 		return (FIX_ERROR);
72549f9b365SRichard Lowe 	}
7265aefb655Srie 	case R_AMD64_TLSLD:
7275aefb655Srie 		/*
7285aefb655Srie 		 * LD -> LE
7295aefb655Srie 		 *
7305aefb655Srie 		 * Transition
7315aefb655Srie 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
7325aefb655Srie 		 *	0x07 call __tls_get_addr@plt
7335aefb655Srie 		 *	0x0c
7345aefb655Srie 		 * To:
7355aefb655Srie 		 *	0x00 .byte 0x66
7365aefb655Srie 		 *	0x01 .byte 0x66
7375aefb655Srie 		 *	0x02 .byte 0x66
7385aefb655Srie 		 *	0x03 movq %fs:0, %rax
7395aefb655Srie 		 */
740051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
741bf994817SAli Bahrami 		    R_AMD64_NONE, arsp, ld_reloc_sym_name));
7425aefb655Srie 		offset -= 3;
7435aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
7445aefb655Srie 		return (FIX_DONE);
7455aefb655Srie 
7465aefb655Srie 	case R_AMD64_DTPOFF32:
7475aefb655Srie 		/*
7485aefb655Srie 		 * LD->LE
7495aefb655Srie 		 *
7505aefb655Srie 		 * Transition:
7515aefb655Srie 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
7525aefb655Srie 		 * To:
7535aefb655Srie 		 *	0x00 leaq x1@tpoff(%rax), %rcx
7545aefb655Srie 		 */
755051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
756bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
7575aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
7585aefb655Srie 		return (FIX_RELOC);
7595aefb655Srie 	}
7605aefb655Srie 
7615aefb655Srie 	return (FIX_RELOC);
7625aefb655Srie }
7635aefb655Srie 
764ba2be530Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7655aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7665aefb655Srie {
7675aefb655Srie 	Rel_desc	*arsp;
768bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
76957ef7aa9SRod Evans 	Aliste		idx;
7705aefb655Srie 	uintptr_t	return_code = 1;
7711d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
7725aefb655Srie 
773bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7745aefb655Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7757010c12aSrie 
7765aefb655Srie 	/*
7775aefb655Srie 	 * Process active relocations.
7785aefb655Srie 	 */
779bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
7805aefb655Srie 		uchar_t		*addr;
7815aefb655Srie 		Xword		value;
7825aefb655Srie 		Sym_desc	*sdp;
7835aefb655Srie 		const char	*ifl_name;
7845aefb655Srie 		Xword		refaddr;
7855aefb655Srie 		int		moved = 0;
7865aefb655Srie 		Gotref		gref;
787bf994817SAli Bahrami 		Os_desc		*osp;
7885aefb655Srie 
7895aefb655Srie 		/*
790bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
791bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
7925aefb655Srie 		 */
7935aefb655Srie 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
794bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
7955aefb655Srie 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
796bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
7975aefb655Srie 			continue;
7985aefb655Srie 		}
7995aefb655Srie 
8005aefb655Srie 		/*
801bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
802bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
803bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
8045aefb655Srie 		 *
8055aefb655Srie 		 * The got table entries have already been assigned,
8065aefb655Srie 		 * and we bind to those initial entries.
8075aefb655Srie 		 */
8085aefb655Srie 		if (arsp->rel_flags & FLG_REL_DTLS)
8095aefb655Srie 			gref = GOT_REF_TLSGD;
8105aefb655Srie 		else if (arsp->rel_flags & FLG_REL_MTLS)
8115aefb655Srie 			gref = GOT_REF_TLSLD;
8125aefb655Srie 		else if (arsp->rel_flags & FLG_REL_STLS)
8135aefb655Srie 			gref = GOT_REF_TLSIE;
8145aefb655Srie 		else
8155aefb655Srie 			gref = GOT_REF_GENERIC;
8165aefb655Srie 
8175aefb655Srie 		/*
8185aefb655Srie 		 * Perform any required TLS fixups.
8195aefb655Srie 		 */
8205aefb655Srie 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
8215aefb655Srie 			Fixupret	ret;
8225aefb655Srie 
8235aefb655Srie 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
8245aefb655Srie 				return (S_ERROR);
8255aefb655Srie 			if (ret == FIX_DONE)
8265aefb655Srie 				continue;
8275aefb655Srie 		}
8285aefb655Srie 
8295aefb655Srie 		/*
8305aefb655Srie 		 * If this is a relocation against a move table, or
8315aefb655Srie 		 * expanded move table, adjust the relocation entries.
8325aefb655Srie 		 */
833bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
8345aefb655Srie 			ld_adj_movereloc(ofl, arsp);
8355aefb655Srie 
8365aefb655Srie 		sdp = arsp->rel_sym;
8375aefb655Srie 		refaddr = arsp->rel_roffset +
8385aefb655Srie 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
8395aefb655Srie 
8405aefb655Srie 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
8415aefb655Srie 		    (arsp->rel_flags & FLG_REL_GOTCL))
8425aefb655Srie 			value = 0;
843bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8445aefb655Srie 			Sym_desc	*sym;
8455aefb655Srie 
8465aefb655Srie 			/*
8475aefb655Srie 			 * The value for a symbol pointing to a SECTION
8485aefb655Srie 			 * is based off of that sections position.
8495aefb655Srie 			 */
8505aefb655Srie 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
8515aefb655Srie 			    /* LINTED */
852bf994817SAli Bahrami 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
8535aefb655Srie 				/*
854bf994817SAli Bahrami 				 * The symbol was moved, so adjust the value
855bf994817SAli Bahrami 				 * relative to the new section.
8565aefb655Srie 				 */
8575aefb655Srie 				value = sym->sd_sym->st_value;
8585aefb655Srie 				moved = 1;
859b26cc8daSAli Bahrami 
860b26cc8daSAli Bahrami 				/*
861bf994817SAli Bahrami 				 * The original raddend covers the displacement
862bf994817SAli Bahrami 				 * from the section start to the desired
863bf994817SAli Bahrami 				 * address. The value computed above gets us
864bf994817SAli Bahrami 				 * from the section start to the start of the
865bf994817SAli Bahrami 				 * symbol range. Adjust the old raddend to
866bf994817SAli Bahrami 				 * remove the offset from section start to
867bf994817SAli Bahrami 				 * symbol start, leaving the displacement
868bf994817SAli Bahrami 				 * within the range of the symbol.
869b26cc8daSAli Bahrami 				 */
870bf994817SAli Bahrami 				arsp->rel_raddend -= sym->sd_osym->st_value;
8715aefb655Srie 			} else {
872bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
873bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
874bf994817SAli Bahrami 					value += sdp->sd_isc->is_osdesc->
8755aefb655Srie 					    os_shdr->sh_addr;
8765aefb655Srie 			}
8775aefb655Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8785aefb655Srie 				value -= ofl->ofl_tlsphdr->p_vaddr;
8792926dd2eSrie 
8802926dd2eSrie 		} else if (IS_SIZE(arsp->rel_rtype)) {
8812926dd2eSrie 			/*
8822926dd2eSrie 			 * Size relocations require the symbols size.
8832926dd2eSrie 			 */
8842926dd2eSrie 			value = sdp->sd_sym->st_size;
88508278a5eSRod Evans 
88608278a5eSRod Evans 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
88708278a5eSRod Evans 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
8885aefb655Srie 			/*
889bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
890bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
891bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
892bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
8935aefb655Srie 			 */
89408278a5eSRod Evans 			value = ld_calc_plt_addr(sdp, ofl);
89508278a5eSRod Evans 		} else
8965aefb655Srie 			value = sdp->sd_sym->st_value;
8975aefb655Srie 
8985aefb655Srie 		/*
8995aefb655Srie 		 * Relocation against the GLOBAL_OFFSET_TABLE.
9005aefb655Srie 		 */
901bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
902bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
903bf994817SAli Bahrami 			return (S_ERROR);
904bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
9055aefb655Srie 
9065aefb655Srie 		/*
907bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
908bf994817SAli Bahrami 		 * sections virtual address to the reference address.
9095aefb655Srie 		 */
9105aefb655Srie 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
9115aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0))
9125aefb655Srie 			refaddr += arsp->rel_isdesc->is_osdesc->
9135aefb655Srie 			    os_shdr->sh_addr;
9145aefb655Srie 
9155aefb655Srie 		/*
916bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
917bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
9185aefb655Srie 		 */
9195aefb655Srie 		if (IS_PLT(arsp->rel_rtype)) {
9205aefb655Srie 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
9215aefb655Srie 				value = ld_calc_plt_addr(sdp, ofl);
9225aefb655Srie 		}
9235aefb655Srie 
9245aefb655Srie 		/*
9255aefb655Srie 		 * Add relocations addend to value.  Add extra
9265aefb655Srie 		 * relocation addend if needed.
9275aefb655Srie 		 *
928bf994817SAli Bahrami 		 * Note: For GOT relative relocations on amd64 we discard the
929bf994817SAli Bahrami 		 * addend.  It was relevant to the reference - not to the
930bf994817SAli Bahrami 		 * data item being referenced (ie: that -4 thing).
9315aefb655Srie 		 */
9325aefb655Srie 		if ((arsp->rel_flags & FLG_REL_GOT) == 0)
933bf2f215aSAli Bahrami 			value += arsp->rel_raddend;
9345aefb655Srie 
9355aefb655Srie 		/*
936bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
937bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
938bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
939bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
940bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
941bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
9425aefb655Srie 		 */
9435aefb655Srie 		if ((arsp->rel_flags & FLG_REL_GOT) &&
9445aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9455aefb655Srie 			Xword		R1addr;
9465aefb655Srie 			uintptr_t	R2addr;
9475aefb655Srie 			Word		gotndx;
9485aefb655Srie 			Gotndx		*gnp;
9495aefb655Srie 
9505aefb655Srie 			/*
951bf994817SAli Bahrami 			 * Perform relocation against GOT table. Since this
952bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
953bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
9545aefb655Srie 			 *
9555aefb655Srie 			 * Calculate offset into GOT at which to apply
9565aefb655Srie 			 * the relocation.
9575aefb655Srie 			 */
958bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
9595aefb655Srie 			assert(gnp);
9605aefb655Srie 
9615aefb655Srie 			if (arsp->rel_rtype == R_AMD64_DTPOFF64)
9625aefb655Srie 				gotndx = gnp->gn_gotndx + 1;
9635aefb655Srie 			else
9645aefb655Srie 				gotndx = gnp->gn_gotndx;
9655aefb655Srie 
9665aefb655Srie 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9675aefb655Srie 
9685aefb655Srie 			/*
9695aefb655Srie 			 * Add the GOTs data's offset.
9705aefb655Srie 			 */
971bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
9725aefb655Srie 
973bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
974bf994817SAli Bahrami 			    M_MACH, SHT_RELA, arsp, R1addr, value,
975bf994817SAli Bahrami 			    ld_reloc_sym_name));
9765aefb655Srie 
9775aefb655Srie 			/*
9785aefb655Srie 			 * And do it.
9795aefb655Srie 			 */
980f3324781Sab196087 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
981bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
982f3324781Sab196087 			else
9835aefb655Srie 				*(Xword *)R2addr = value;
9845aefb655Srie 			continue;
9855aefb655Srie 
9865aefb655Srie 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
9875aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9885aefb655Srie 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
9895aefb655Srie 
9905aefb655Srie 		} else if (IS_GOTPCREL(arsp->rel_rtype) &&
9915aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9925aefb655Srie 			Gotndx *gnp;
9935aefb655Srie 
9945aefb655Srie 			/*
9955aefb655Srie 			 * Calculation:
9965aefb655Srie 			 *	G + GOT + A - P
9975aefb655Srie 			 */
998bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
9995aefb655Srie 			assert(gnp);
1000bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr-> sh_addr) +
1001bf994817SAli Bahrami 			    ((Xword)gnp->gn_gotndx * M_GOT_ENTSIZE) +
1002bf994817SAli Bahrami 			    arsp->rel_raddend - refaddr;
10035aefb655Srie 
10045aefb655Srie 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
10055aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10065aefb655Srie 			value = (Xword)(ofl->ofl_osgot->os_shdr->
10075aefb655Srie 			    sh_addr) - refaddr + arsp->rel_raddend;
10085aefb655Srie 
10095aefb655Srie 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
10105aefb655Srie 		    (((flags & FLG_OF_RELOBJ) == 0) ||
1011bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
10125aefb655Srie 			value -= refaddr;
10135aefb655Srie 
10145aefb655Srie 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
10155aefb655Srie 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
10165aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10175aefb655Srie 			Gotndx	*gnp;
10185aefb655Srie 
1019bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
10205aefb655Srie 			assert(gnp);
10215aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10225aefb655Srie 
10235aefb655Srie 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
10245aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10255aefb655Srie 			Gotndx *gnp;
10265aefb655Srie 
1027bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
10285aefb655Srie 			assert(gnp);
10295aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10305aefb655Srie 
10315aefb655Srie 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
10325aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10335aefb655Srie 			Xword	tlsstatsize;
10345aefb655Srie 
10355aefb655Srie 			/*
10365aefb655Srie 			 * This is the LE TLS reference model.  Static
10375aefb655Srie 			 * offset is hard-coded.
10385aefb655Srie 			 */
1039bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
10405aefb655Srie 			    M_TLSSTATALIGN);
10415aefb655Srie 			value = tlsstatsize - value;
10425aefb655Srie 
10435aefb655Srie 			/*
1044bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a negative
1045bf994817SAli Bahrami 			 * offset that can be added to the thread pointer.
10465aefb655Srie 			 */
10475aefb655Srie 			if (arsp->rel_rtype == R_AMD64_TPOFF32)
10485aefb655Srie 				value = -value;
10495aefb655Srie 		}
10505aefb655Srie 
10515aefb655Srie 		if (arsp->rel_isdesc->is_file)
10525aefb655Srie 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10535aefb655Srie 		else
10545aefb655Srie 			ifl_name = MSG_INTL(MSG_STR_NULL);
10555aefb655Srie 
10565aefb655Srie 		/*
1057bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1058bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1059bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1060bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1061bf994817SAli Bahrami 		 * End users should never see this.
10625aefb655Srie 		 */
10635aefb655Srie 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1064de777a60Sab196087 			Conv_inv_buf_t inv_buf;
1065de777a60Sab196087 
10661007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1067bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1068bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
10694a8d0ea7SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
10705aefb655Srie 			    arsp->rel_isdesc->is_name);
10715aefb655Srie 			return (S_ERROR);
10725aefb655Srie 		}
10735aefb655Srie 
10745aefb655Srie 		/*
10755aefb655Srie 		 * Get the address of the data item we need to modify.
10765aefb655Srie 		 */
10775aefb655Srie 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1078bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
10795aefb655Srie 
1080635216b6SRod Evans 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1081bf994817SAli Bahrami 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1082bf994817SAli Bahrami 		    ld_reloc_sym_name));
1083bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
10845aefb655Srie 
10855aefb655Srie 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
10865aefb655Srie 		    ofl->ofl_size) || (arsp->rel_roffset >
1087bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
10885aefb655Srie 			int		class;
1089de777a60Sab196087 			Conv_inv_buf_t inv_buf;
10905aefb655Srie 
1091bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1092bf994817SAli Bahrami 			    ofl->ofl_size)
10935aefb655Srie 				class = ERR_FATAL;
10945aefb655Srie 			else
10955aefb655Srie 				class = ERR_WARNING;
10965aefb655Srie 
10971007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1098bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1099bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1100bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
11015aefb655Srie 			    EC_ADDR((uintptr_t)addr -
11025aefb655Srie 			    (uintptr_t)ofl->ofl_nehdr));
11035aefb655Srie 
11045aefb655Srie 			if (class == ERR_FATAL) {
11055aefb655Srie 				return_code = S_ERROR;
11065aefb655Srie 				continue;
11075aefb655Srie 			}
11085aefb655Srie 		}
11095aefb655Srie 
11105aefb655Srie 		/*
1111bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1112bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
11135aefb655Srie 		 */
11145aefb655Srie 		if (moved)
11155aefb655Srie 			value -= *addr;
11165aefb655Srie 
11175aefb655Srie 		/*
1118bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
11195aefb655Srie 		 */
1120f3324781Sab196087 		if (OFL_DO_RELOC(ofl)) {
1121f3324781Sab196087 			/*
1122bf994817SAli Bahrami 			 * If this is a PROGBITS section and the running linker
1123bf994817SAli Bahrami 			 * has a different byte order than the target host,
1124bf994817SAli Bahrami 			 * tell do_reloc_ld() to swap bytes.
1125f3324781Sab196087 			 */
1126bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1127bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
11281007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
11291007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
11305aefb655Srie 				return_code = S_ERROR;
11315aefb655Srie 			}
11325aefb655Srie 		}
11331007fd6fSAli Bahrami 	}
11345aefb655Srie 	return (return_code);
11355aefb655Srie }
11365aefb655Srie 
1137ba2be530Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)11385aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11395aefb655Srie {
11405aefb655Srie 	Rel_desc	*orsp;
11415aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
11425aefb655Srie 
11435aefb655Srie 	/*
11445aefb655Srie 	 * Static executables *do not* want any relocations against them.
11455aefb655Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11465aefb655Srie 	 * symbol in a static executable, it's best to disable them here
11475aefb655Srie 	 * instead of through out the relocation code.
11485aefb655Srie 	 */
1149635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11505aefb655Srie 		return (1);
11515aefb655Srie 
11525aefb655Srie 	/*
11530bc0887eSRichard Lowe 	 * If the symbol will be reduced, we can't leave outstanding
11540bc0887eSRichard Lowe 	 * relocations against it, as nothing will ever be able to satisfy them
11550bc0887eSRichard Lowe 	 * (and the symbol won't be in .dynsym
11560bc0887eSRichard Lowe 	 */
11570bc0887eSRichard Lowe 	if ((sdp != NULL) &&
11580bc0887eSRichard Lowe 	    (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
11590bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_NONE) &&
11600bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_RELATIVE)) {
11610bc0887eSRichard Lowe 		if (ld_sym_reducable(ofl, sdp))
11620bc0887eSRichard Lowe 			return (1);
11630bc0887eSRichard Lowe 	}
11640bc0887eSRichard Lowe 
11650bc0887eSRichard Lowe 	/*
11665aefb655Srie 	 * If we are adding a output relocation against a section
11675aefb655Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11685aefb655Srie 	 * will be added to the .dynsym symbol table.
11695aefb655Srie 	 */
11705aefb655Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11715aefb655Srie 	    ((flags & FLG_REL_SCNNDX) ||
11725aefb655Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11735aefb655Srie 
11745aefb655Srie 		/*
11755aefb655Srie 		 * If this is a COMMON symbol - no output section
11765aefb655Srie 		 * exists yet - (it's created as part of sym_validate()).
11775aefb655Srie 		 * So - we mark here that when it's created it should
11785aefb655Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11795aefb655Srie 		 */
11805aefb655Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11810bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11825aefb655Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11835aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11845aefb655Srie 			else
11855aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11865aefb655Srie 		} else {
118708278a5eSRod Evans 			Os_desc *osp;
118808278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11895aefb655Srie 
119008278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
119108278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11925aefb655Srie 				ofl->ofl_dynshdrcnt++;
11935aefb655Srie 				osp->os_flags |= FLG_OS_OUTREL;
11945aefb655Srie 			}
11955aefb655Srie 		}
11965aefb655Srie 	}
11975aefb655Srie 
1198bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1199bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1200bf994817SAli Bahrami 		return (S_ERROR);
12015aefb655Srie 
12025aefb655Srie 	if (flags & FLG_REL_GOT)
12035aefb655Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
12045aefb655Srie 	else if (flags & FLG_REL_PLT)
12055aefb655Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
12065aefb655Srie 	else if (flags & FLG_REL_BSS)
12075aefb655Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
12085aefb655Srie 	else if (flags & FLG_REL_NOINFO)
12095aefb655Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
12105aefb655Srie 	else
1211bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
12125aefb655Srie 
12135aefb655Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
12145aefb655Srie 		ofl->ofl_relocrelcnt++;
12155aefb655Srie 
12165aefb655Srie 	/*
12175aefb655Srie 	 * We don't perform sorting on PLT relocations because
12185aefb655Srie 	 * they have already been assigned a PLT index and if we
12195aefb655Srie 	 * were to sort them we would have to re-assign the plt indexes.
12205aefb655Srie 	 */
12215aefb655Srie 	if (!(flags & FLG_REL_PLT))
12225aefb655Srie 		ofl->ofl_reloccnt++;
12235aefb655Srie 
12245aefb655Srie 	/*
12255aefb655Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
12265aefb655Srie 	 */
12275aefb655Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12285aefb655Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12295aefb655Srie 
12305aefb655Srie 	/*
12315aefb655Srie 	 * Identify and possibly warn of a displacement relocation.
12325aefb655Srie 	 */
12335aefb655Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
12345aefb655Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12355aefb655Srie 
12365aefb655Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12375aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12385aefb655Srie 	}
12395aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
12405aefb655Srie 	    M_MACH, orsp));
12415aefb655Srie 	return (1);
12425aefb655Srie }
12435aefb655Srie 
12445aefb655Srie /*
12455aefb655Srie  * process relocation for a LOCAL symbol
12465aefb655Srie  */
1247ba2be530Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)12485aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12495aefb655Srie {
12501d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12515aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
12520bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12535aefb655Srie 	Word		ortype = rsp->rel_rtype;
12545aefb655Srie 
12555aefb655Srie 	/*
12565aefb655Srie 	 * if ((shared object) and (not pc relative relocation) and
12575aefb655Srie 	 *    (not against ABS symbol))
12585aefb655Srie 	 * then
12595aefb655Srie 	 *	build R_AMD64_RELATIVE
12605aefb655Srie 	 * fi
12615aefb655Srie 	 */
12625aefb655Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12632926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12645aefb655Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12655aefb655Srie 	    !(rsp->rel_isdesc != NULL &&
12665aefb655Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12675aefb655Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12685aefb655Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12695aefb655Srie 
12705aefb655Srie 		/*
12715aefb655Srie 		 * R_AMD64_RELATIVE updates a 64bit address, if this
12725aefb655Srie 		 * relocation isn't a 64bit binding then we can not
12735aefb655Srie 		 * simplify it to a RELATIVE relocation.
12745aefb655Srie 		 */
12755aefb655Srie 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
12761d9df23bSab196087 			return (ld_add_outrel(0, rsp, ofl));
12775aefb655Srie 		}
12785aefb655Srie 
12795aefb655Srie 		rsp->rel_rtype = R_AMD64_RELATIVE;
12805aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
12815aefb655Srie 			return (S_ERROR);
12825aefb655Srie 		rsp->rel_rtype = ortype;
12835aefb655Srie 		return (1);
12845aefb655Srie 	}
12855aefb655Srie 
12865aefb655Srie 	/*
12875aefb655Srie 	 * If the relocation is against a 'non-allocatable' section
12885aefb655Srie 	 * and we can not resolve it now - then give a warning
12895aefb655Srie 	 * message.
12905aefb655Srie 	 *
12915aefb655Srie 	 * We can not resolve the symbol if either:
12925aefb655Srie 	 *	a) it's undefined
12935aefb655Srie 	 *	b) it's defined in a shared library and a
12945aefb655Srie 	 *	   COPY relocation hasn't moved it to the executable
12955aefb655Srie 	 *
12965aefb655Srie 	 * Note: because we process all of the relocations against the
12975aefb655Srie 	 *	text segment before any others - we know whether
12985aefb655Srie 	 *	or not a copy relocation will be generated before
12995aefb655Srie 	 *	we get here (see reloc_init()->reloc_segments()).
13005aefb655Srie 	 */
13015aefb655Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
13025aefb655Srie 	    ((shndx == SHN_UNDEF) ||
13035aefb655Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
13045aefb655Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1305de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
1306bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1307de777a60Sab196087 
13085aefb655Srie 		/*
13095aefb655Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
13105aefb655Srie 		 * section - then silently ignore that the relocation
13115aefb655Srie 		 * can not be resolved.
13125aefb655Srie 		 */
1313bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
13145aefb655Srie 			return (0);
13151007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1316de777a60Sab196087 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
13175aefb655Srie 		    rsp->rel_isdesc->is_file->ifl_name,
1318bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
13195aefb655Srie 		return (1);
13205aefb655Srie 	}
13215aefb655Srie 
13225aefb655Srie 	/*
13235aefb655Srie 	 * Perform relocation.
13245aefb655Srie 	 */
1325*fb8f92baSToomas Soome 	return (ld_add_actrel(0, rsp, ofl));
13265aefb655Srie }
13275aefb655Srie 
13285aefb655Srie 
1329ba2be530Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)13305aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13315aefb655Srie {
13325aefb655Srie 	Word		rtype = rsp->rel_rtype;
13335aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
13341d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
13355aefb655Srie 	Gotndx		*gnp;
13365aefb655Srie 
13375aefb655Srie 	/*
1338d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1339d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
13405aefb655Srie 	 */
1341d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13425aefb655Srie 		/*
1343d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
13445aefb655Srie 		 */
13455aefb655Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13465aefb655Srie 
1347d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13485aefb655Srie 			/*
1349d326b23bSrie 			 * Assign a GOT entry for static TLS references.
13505aefb655Srie 			 */
135157ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135257ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
1353d326b23bSrie 
1354d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1355d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1356d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
13575aefb655Srie 					return (S_ERROR);
13585aefb655Srie 			}
1359d326b23bSrie 
1360d326b23bSrie 			/*
1361d326b23bSrie 			 * IE access model.
1362d326b23bSrie 			 */
13635aefb655Srie 			if (IS_TLS_IE(rtype))
13645aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13655aefb655Srie 
13665aefb655Srie 			/*
1367d326b23bSrie 			 * Fixups are required for other executable models.
13685aefb655Srie 			 */
13695aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13705aefb655Srie 			    rsp, ofl));
13715aefb655Srie 		}
1372d326b23bSrie 
13735aefb655Srie 		/*
1374d326b23bSrie 		 * LE access model.
13755aefb655Srie 		 */
13765aefb655Srie 		if (IS_TLS_LE(rtype))
13775aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1378d326b23bSrie 
13795aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13805aefb655Srie 		    rsp, ofl));
13815aefb655Srie 	}
13825aefb655Srie 
13835aefb655Srie 	/*
1384d326b23bSrie 	 * Building a shared object.
1385d326b23bSrie 	 *
1386d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13875aefb655Srie 	 */
138857ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
138957ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1390d326b23bSrie 
1391d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1392*fb8f92baSToomas Soome 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR)
13935aefb655Srie 			return (S_ERROR);
1394d326b23bSrie 
13955aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
139657ef7aa9SRod Evans 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
139757ef7aa9SRod Evans 	    ofl, rsp)) == NULL)) {
1398d326b23bSrie 
1399d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1400d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1401d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
14025aefb655Srie 			return (S_ERROR);
14035aefb655Srie 	}
14045aefb655Srie 
14055aefb655Srie 	if (IS_TLS_LD(rtype))
14065aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14075aefb655Srie 
14085aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14095aefb655Srie }
14105aefb655Srie 
14115aefb655Srie /* ARGSUSED5 */
1412ba2be530Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)141357ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
14145aefb655Srie     Rel_desc *rsp, Sym_desc *sdp)
14155aefb655Srie {
14165aefb655Srie 	Xword		raddend;
141757ef7aa9SRod Evans 	Gotndx		gn, *gnp;
141857ef7aa9SRod Evans 	Aliste		idx;
14195aefb655Srie 	uint_t		gotents;
14205aefb655Srie 
14215aefb655Srie 	raddend = rsp->rel_raddend;
142257ef7aa9SRod Evans 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
14235aefb655Srie 		return (1);
14245aefb655Srie 
14255aefb655Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14265aefb655Srie 		gotents = 2;
14275aefb655Srie 	else
14285aefb655Srie 		gotents = 1;
14295aefb655Srie 
143057ef7aa9SRod Evans 	gn.gn_addend = raddend;
143157ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
143257ef7aa9SRod Evans 	gn.gn_gotref = gref;
14335aefb655Srie 
14345aefb655Srie 	ofl->ofl_gotcnt += gotents;
14355aefb655Srie 
14365aefb655Srie 	if (gref == GOT_REF_TLSLD) {
143757ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
143857ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
143957ef7aa9SRod Evans 				return (S_ERROR);
144057ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
14415aefb655Srie 			ofl->ofl_tlsldgotndx = gnp;
144257ef7aa9SRod Evans 		}
14435aefb655Srie 		return (1);
14445aefb655Srie 	}
14455aefb655Srie 
144657ef7aa9SRod Evans 	idx = 0;
144757ef7aa9SRod Evans 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
144857ef7aa9SRod Evans 		if (gnp->gn_addend > raddend)
144957ef7aa9SRod Evans 			break;
14505aefb655Srie 	}
145157ef7aa9SRod Evans 
145257ef7aa9SRod Evans 	/*
145357ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
145457ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
145557ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
145657ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
145757ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
145857ef7aa9SRod Evans 	 */
145957ef7aa9SRod Evans 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
146057ef7aa9SRod Evans 	    AL_CNT_SDP_GOT, idx) == NULL)
146157ef7aa9SRod Evans 		return (S_ERROR);
146257ef7aa9SRod Evans 
14635aefb655Srie 	return (1);
14645aefb655Srie }
14655aefb655Srie 
1466ba2be530Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)14675aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14685aefb655Srie {
14695aefb655Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14705aefb655Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14715aefb655Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14725aefb655Srie }
14735aefb655Srie 
14745aefb655Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14755aefb655Srie /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14765aefb655Srie /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14775aefb655Srie /* 0x0c NOP */			0x90,
14785aefb655Srie /* 0x0d NOP */			0x90,
14795aefb655Srie /* 0x0e NOP */			0x90,
14805aefb655Srie /* 0x0f NOP */			0x90
14815aefb655Srie };
14825aefb655Srie 
14835aefb655Srie /*
14845aefb655Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14855aefb655Srie  */
1486ba2be530Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)14875aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl)
14885aefb655Srie {
1489ba2be530Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1490ba2be530Sab196087 
14915aefb655Srie 	if (ofl->ofl_osgot) {
14925aefb655Srie 		Sym_desc	*sdp;
14935aefb655Srie 
14945aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1495635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1496d326b23bSrie 			uchar_t	*genptr;
1497d326b23bSrie 
1498d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14995aefb655Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15005aefb655Srie 			/* LINTED */
15015aefb655Srie 			*(Xword *)genptr = sdp->sd_sym->st_value;
1502ba2be530Sab196087 			if (bswap)
1503ba2be530Sab196087 				/* LINTED */
1504ba2be530Sab196087 				*(Xword *)genptr =
1505ba2be530Sab196087 				    /* LINTED */
1506ba2be530Sab196087 				    ld_bswap_Xword(*(Xword *)genptr);
15075aefb655Srie 		}
15085aefb655Srie 	}
15095aefb655Srie 
15105aefb655Srie 	/*
15115aefb655Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15125aefb655Srie 	 * entry is:
15135aefb655Srie 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
15145aefb655Srie 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
15155aefb655Srie 	 *	0x0c NOP
15165aefb655Srie 	 *	0x0d NOP
15175aefb655Srie 	 *	0x0e NOP
15185aefb655Srie 	 *	0x0f NOP
15195aefb655Srie 	 */
1520f3324781Sab196087 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15215aefb655Srie 		uchar_t	*pltent;
15225aefb655Srie 		Xword	val1;
15235aefb655Srie 
15245aefb655Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15255aefb655Srie 		bcopy(plt0_template, pltent, sizeof (plt0_template));
15265aefb655Srie 
15275aefb655Srie 		/*
1528f3324781Sab196087 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1529f3324781Sab196087 		 * stage.
1530f3324781Sab196087 		 */
1531f3324781Sab196087 		if (!OFL_DO_RELOC(ofl))
1532f3324781Sab196087 			return (1);
1533f3324781Sab196087 
1534f3324781Sab196087 		/*
15355aefb655Srie 		 * filin:
15365aefb655Srie 		 *	PUSHQ GOT + 8(%rip)
15375aefb655Srie 		 *
15385aefb655Srie 		 * Note: 0x06 below represents the offset to the
15395aefb655Srie 		 *	 next instruction - which is what %rip will
15405aefb655Srie 		 *	 be pointing at.
15415aefb655Srie 		 */
15425aefb655Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15435aefb655Srie 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
15445aefb655Srie 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
15455aefb655Srie 
1546bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
1547bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1548bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15491007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
15505aefb655Srie 			return (S_ERROR);
15515aefb655Srie 		}
15525aefb655Srie 
15535aefb655Srie 		/*
15545aefb655Srie 		 * filin:
15555aefb655Srie 		 *  JMP	*GOT+16(%rip)
15565aefb655Srie 		 */
15575aefb655Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15585aefb655Srie 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
15595aefb655Srie 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1560f3324781Sab196087 
1561bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
1562bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1563bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15641007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
15655aefb655Srie 			return (S_ERROR);
15665aefb655Srie 		}
15675aefb655Srie 	}
1568f3324781Sab196087 
15695aefb655Srie 	return (1);
15705aefb655Srie }
1571ba2be530Sab196087 
1572ba2be530Sab196087 
1573ba2be530Sab196087 
1574ba2be530Sab196087 /*
1575ba2be530Sab196087  * Template for generating "void (*)(void)" function
1576ba2be530Sab196087  */
1577ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1578ba2be530Sab196087 /* 0x00 */	0x55,				/* pushq  %rbp */
1579ba2be530Sab196087 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1580ba2be530Sab196087 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1581ba2be530Sab196087 /* 0x07 */	0x5d,				/* popq   %rbp */
1582ba2be530Sab196087 /* 0x08 */	0xc3				/* ret */
1583ba2be530Sab196087 };
1584ba2be530Sab196087 
1585ba2be530Sab196087 
1586ba2be530Sab196087 /*
15873c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15883c573fccSAli Bahrami  *
15893c573fccSAli Bahrami  * entry:
15903c573fccSAli Bahrami  *
15913c573fccSAli Bahrami  *	base - base address of section being filled
15923c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15933c573fccSAli Bahrami  *	cnt - # bytes to be filled
15943c573fccSAli Bahrami  *
15953c573fccSAli Bahrami  * exit:
15963c573fccSAli Bahrami  *	The fill has been completed.
15973c573fccSAli Bahrami  */
15983c573fccSAli Bahrami static void
execfill(void * base,off_t off,size_t cnt)15993c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
16003c573fccSAli Bahrami {
16013c573fccSAli Bahrami 	/*
16023c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
16033c573fccSAli Bahrami 	 * There are no alignment constraints.
16043c573fccSAli Bahrami 	 */
16053c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
16063c573fccSAli Bahrami }
16073c573fccSAli Bahrami 
16083c573fccSAli Bahrami 
16093c573fccSAli Bahrami /*
1610ba2be530Sab196087  * Return the ld_targ definition for this target.
1611ba2be530Sab196087  */
1612ba2be530Sab196087 const Target *
ld_targ_init_x86(void)1613ba2be530Sab196087 ld_targ_init_x86(void)
1614ba2be530Sab196087 {
1615ba2be530Sab196087 	static const Target _ld_targ = {
1616ba2be530Sab196087 		{			/* Target_mach */
1617ba2be530Sab196087 			M_MACH,			/* m_mach */
1618ba2be530Sab196087 			M_MACHPLUS,		/* m_machplus */
1619ba2be530Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
1620ba2be530Sab196087 			M_CLASS,		/* m_class */
1621ba2be530Sab196087 			M_DATA,			/* m_data */
1622ba2be530Sab196087 
1623ba2be530Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
1624ba2be530Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
1625bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1626ba2be530Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
162769112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1628ba2be530Sab196087 			M_WORD_ALIGN,		/* m_word_align */
1629ba2be530Sab196087 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1630ba2be530Sab196087 
1631ba2be530Sab196087 			/* Relocation type codes */
1632ba2be530Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1633ba2be530Sab196087 			M_R_COPY,		/* m_r_copy */
1634ba2be530Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1635ba2be530Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1636ba2be530Sab196087 			M_R_NUM,		/* m_r_num */
1637ba2be530Sab196087 			M_R_NONE,		/* m_r_none */
1638ba2be530Sab196087 			M_R_RELATIVE,		/* m_r_relative */
1639ba2be530Sab196087 			M_R_REGISTER,		/* m_r_register */
1640ba2be530Sab196087 
1641ba2be530Sab196087 			/* Relocation related constants */
1642ba2be530Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1643ba2be530Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1644ba2be530Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1645ba2be530Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1646ba2be530Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1647ba2be530Sab196087 
1648ba2be530Sab196087 			/* GOT related constants */
1649ba2be530Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
1650ba2be530Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
1651ba2be530Sab196087 
1652ba2be530Sab196087 			/* PLT related constants */
1653ba2be530Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
1654ba2be530Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1655ba2be530Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1656ba2be530Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1657ba2be530Sab196087 
16587e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16597e16fca0SAli Bahrami 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
16607e16fca0SAli Bahrami 
1661ba2be530Sab196087 			M_DT_REGISTER,		/* m_dt_register */
1662ba2be530Sab196087 		},
1663ba2be530Sab196087 		{			/* Target_machid */
1664ba2be530Sab196087 			M_ID_ARRAY,		/* id_array */
1665ba2be530Sab196087 			M_ID_BSS,		/* id_bss */
1666ba2be530Sab196087 			M_ID_CAP,		/* id_cap */
166708278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
166808278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1669ba2be530Sab196087 			M_ID_DATA,		/* id_data */
1670ba2be530Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
1671ba2be530Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
1672ba2be530Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
1673ba2be530Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
1674ba2be530Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1675ba2be530Sab196087 			M_ID_GOT,		/* id_got */
1676ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1677ba2be530Sab196087 			M_ID_HASH,		/* id_hash */
1678ba2be530Sab196087 			M_ID_INTERP,		/* id_interp */
1679ba2be530Sab196087 			M_ID_LBSS,		/* id_lbss */
1680ba2be530Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
1681ba2be530Sab196087 			M_ID_NOTE,		/* id_note */
1682ba2be530Sab196087 			M_ID_NULL,		/* id_null */
1683ba2be530Sab196087 			M_ID_PLT,		/* id_plt */
1684ba2be530Sab196087 			M_ID_REL,		/* id_rel */
1685ba2be530Sab196087 			M_ID_STRTAB,		/* id_strtab */
1686ba2be530Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
1687ba2be530Sab196087 			M_ID_SYMTAB,		/* id_symtab */
1688ba2be530Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1689ba2be530Sab196087 			M_ID_TEXT,		/* id_text */
1690ba2be530Sab196087 			M_ID_TLS,		/* id_tls */
1691ba2be530Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
1692ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
1693ba2be530Sab196087 			M_ID_UNWIND,		/* id_unwind */
16947e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1695ba2be530Sab196087 			M_ID_USER,		/* id_user */
1696ba2be530Sab196087 			M_ID_VERSION,		/* id_version */
1697ba2be530Sab196087 		},
1698ba2be530Sab196087 		{			/* Target_nullfunc */
1699ba2be530Sab196087 			nullfunc_tmpl,		/* nf_template */
1700ba2be530Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
1701ba2be530Sab196087 		},
17023c573fccSAli Bahrami 		{			/* Target_fillfunc */
17033c573fccSAli Bahrami 			execfill		/* ff_execfill */
17043c573fccSAli Bahrami 		},
1705ba2be530Sab196087 		{			/* Target_machrel */
1706ba2be530Sab196087 			reloc_table,
1707ba2be530Sab196087 
1708ba2be530Sab196087 			ld_init_rel,		/* mr_init_rel */
1709ba2be530Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
1710ba2be530Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1711ba2be530Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1712ba2be530Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1713ba2be530Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
1714ba2be530Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
1715ba2be530Sab196087 			ld_add_outrel,		/* mr_add_outrel */
1716ba2be530Sab196087 			NULL,			/* mr_reloc_register */
1717ba2be530Sab196087 			ld_reloc_local,		/* mr_reloc_local */
1718ba2be530Sab196087 			NULL,			/* mr_reloc_GOTOP */
1719ba2be530Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
1720ba2be530Sab196087 			NULL,			/* mr_assign_got */
172157ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1722ba2be530Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
1723ba2be530Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1724ba2be530Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1725ba2be530Sab196087 			NULL,			/* mr_allocate_got */
1726ba2be530Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1727ba2be530Sab196087 		},
1728ba2be530Sab196087 		{			/* Target_machsym */
1729ba2be530Sab196087 			NULL,			/* ms_reg_check */
1730ba2be530Sab196087 			NULL,			/* ms_mach_sym_typecheck */
1731ba2be530Sab196087 			NULL,			/* ms_is_regsym */
1732ba2be530Sab196087 			NULL,			/* ms_reg_find */
1733ba2be530Sab196087 			NULL			/* ms_reg_enter */
1734ba2be530Sab196087 		}
1735ba2be530Sab196087 	};
1736ba2be530Sab196087 
1737ba2be530Sab196087 	return (&_ld_targ);
1738ba2be530Sab196087 }
1739