xref: /titanic_44/usr/src/cmd/sgs/libld/common/machrel.amd.c (revision 49f9b365248ee858ee91baa36eab27c5200f6dca)
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 
4385aefb655Srie 	relbits = (char *)relosp->os_outdata->d_buf;
4395aefb655Srie 
4405aefb655Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4415aefb655Srie 	rea.r_offset = roffset;
4425aefb655Srie 	rea.r_addend = raddend;
4435aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
444bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
4455aefb655Srie 
4465aefb655Srie 	/*
4475aefb655Srie 	 * Assert we haven't walked off the end of our relocation table.
4485aefb655Srie 	 */
4495aefb655Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4505aefb655Srie 
4515aefb655Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
4525aefb655Srie 	    (char *)&rea, sizeof (Rela));
4535aefb655Srie 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4545aefb655Srie 
4555aefb655Srie 	/*
4565aefb655Srie 	 * Determine if this relocation is against a non-writable, allocatable
4575aefb655Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
4585aefb655Srie 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4595aefb655Srie 	 * result in modifications to the .got.
4605aefb655Srie 	 */
4615aefb655Srie 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4625aefb655Srie 		osp = ofl->ofl_osgot;
4635aefb655Srie 
4641007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
4655aefb655Srie 	return (1);
4665aefb655Srie }
4675aefb655Srie 
4685aefb655Srie /*
4695aefb655Srie  * amd64 Instructions for TLS processing
4705aefb655Srie  */
4715aefb655Srie static uchar_t tlsinstr_gd_ie[] = {
4725aefb655Srie 	/*
4735aefb655Srie 	 *	0x00 movq %fs:0, %rax
4745aefb655Srie 	 */
4755aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
4765aefb655Srie 	0x00, 0x00, 0x00, 0x00,
4775aefb655Srie 	/*
4785aefb655Srie 	 *	0x09 addq x@gottpoff(%rip), %rax
4795aefb655Srie 	 */
4805aefb655Srie 	0x48, 0x03, 0x05, 0x00, 0x00,
4815aefb655Srie 	0x00, 0x00
4825aefb655Srie };
4835aefb655Srie 
4845aefb655Srie static uchar_t tlsinstr_gd_le[] = {
4855aefb655Srie 	/*
4865aefb655Srie 	 *	0x00 movq %fs:0, %rax
4875aefb655Srie 	 */
4885aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
4895aefb655Srie 	0x00, 0x00, 0x00, 0x00,
4905aefb655Srie 	/*
4915aefb655Srie 	 *	0x09 leaq x@gottpoff(%rip), %rax
4925aefb655Srie 	 */
4935aefb655Srie 	0x48, 0x8d, 0x80, 0x00, 0x00,
4945aefb655Srie 	0x00, 0x00
4955aefb655Srie };
4965aefb655Srie 
4975aefb655Srie static uchar_t tlsinstr_ld_le[] = {
4985aefb655Srie 	/*
4995aefb655Srie 	 * .byte 0x66
5005aefb655Srie 	 */
5015aefb655Srie 	0x66,
5025aefb655Srie 	/*
5035aefb655Srie 	 * .byte 0x66
5045aefb655Srie 	 */
5055aefb655Srie 	0x66,
5065aefb655Srie 	/*
5075aefb655Srie 	 * .byte 0x66
5085aefb655Srie 	 */
5095aefb655Srie 	0x66,
5105aefb655Srie 	/*
5115aefb655Srie 	 * movq %fs:0, %rax
5125aefb655Srie 	 */
5135aefb655Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
5145aefb655Srie 	0x00, 0x00, 0x00, 0x00
5155aefb655Srie };
5165aefb655Srie 
517*49f9b365SRichard Lowe #define	REX_B		0x1
518*49f9b365SRichard Lowe #define	REX_X		0x2
519*49f9b365SRichard Lowe #define	REX_R		0x4
520*49f9b365SRichard Lowe #define	REX_W		0x8
521*49f9b365SRichard Lowe #define	REX_PREFIX	0x40
522*49f9b365SRichard Lowe 
523*49f9b365SRichard Lowe #define	REX_RW		(REX_PREFIX | REX_R | REX_W)
524*49f9b365SRichard Lowe #define	REX_BW		(REX_PREFIX | REX_B | REX_W)
525*49f9b365SRichard Lowe #define	REX_BRW		(REX_PREFIX | REX_B | REX_R | REX_W)
526*49f9b365SRichard Lowe 
527*49f9b365SRichard Lowe #define	REG_ESP		0x4
528*49f9b365SRichard Lowe 
529*49f9b365SRichard Lowe #define	INSN_ADDMR	0x03	/* addq mem,reg */
530*49f9b365SRichard Lowe #define	INSN_ADDIR	0x81	/* addq imm,reg */
531*49f9b365SRichard Lowe #define	INSN_MOVMR	0x8b	/* movq mem,reg */
532*49f9b365SRichard Lowe #define	INSN_MOVIR	0xc7	/* movq imm,reg */
533*49f9b365SRichard Lowe #define	INSN_LEA	0x8d	/* leaq mem,reg */
5345aefb655Srie 
5355aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)5365aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
5375aefb655Srie {
5385aefb655Srie 	Sym_desc	*sdp = arsp->rel_sym;
5395aefb655Srie 	Word		rtype = arsp->rel_rtype;
5405aefb655Srie 	uchar_t		*offset;
5415aefb655Srie 
5425aefb655Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
5435aefb655Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
544bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
5455aefb655Srie 
546*49f9b365SRichard Lowe 	/*
547*49f9b365SRichard Lowe 	 * Note that in certain of the original insn sequences below, the
548*49f9b365SRichard Lowe 	 * instructions are not necessarily adjacent
549*49f9b365SRichard Lowe 	 */
5505aefb655Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
5515aefb655Srie 		/*
5525aefb655Srie 		 * IE reference model
5535aefb655Srie 		 */
5545aefb655Srie 		switch (rtype) {
5555aefb655Srie 		case R_AMD64_TLSGD:
5565aefb655Srie 			/*
5575aefb655Srie 			 *  GD -> IE
5585aefb655Srie 			 *
5595aefb655Srie 			 * Transition:
5605aefb655Srie 			 *	0x00 .byte 0x66
5615aefb655Srie 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5625aefb655Srie 			 *	0x08 .word 0x6666
5635aefb655Srie 			 *	0x0a rex64
5645aefb655Srie 			 *	0x0b call __tls_get_addr@plt
5655aefb655Srie 			 *	0x10
5665aefb655Srie 			 * To:
5675aefb655Srie 			 *	0x00 movq %fs:0, %rax
5685aefb655Srie 			 *	0x09 addq x@gottpoff(%rip), %rax
5695aefb655Srie 			 *	0x10
5705aefb655Srie 			 */
5715aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
572bf994817SAli Bahrami 			    R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
5735aefb655Srie 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5745aefb655Srie 			arsp->rel_roffset += 8;
5755aefb655Srie 			arsp->rel_raddend = (Sxword)-4;
5765aefb655Srie 
5775aefb655Srie 			/*
578051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
5795aefb655Srie 			 * sequence.
5805aefb655Srie 			 */
5815aefb655Srie 			offset -= 4;
5825aefb655Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
5835aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5845aefb655Srie 			return (FIX_RELOC);
5855aefb655Srie 
5865aefb655Srie 		case R_AMD64_PLT32:
5875aefb655Srie 			/*
588051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
5895aefb655Srie 			 */
5905aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
591bf994817SAli Bahrami 			    R_AMD64_NONE, arsp, ld_reloc_sym_name));
5925aefb655Srie 			return (FIX_DONE);
5935aefb655Srie 		}
5945aefb655Srie 	}
5955aefb655Srie 
5965aefb655Srie 	/*
5975aefb655Srie 	 * LE reference model
5985aefb655Srie 	 */
5995aefb655Srie 	switch (rtype) {
6005aefb655Srie 	case R_AMD64_TLSGD:
6015aefb655Srie 		/*
6025aefb655Srie 		 * GD -> LE
6035aefb655Srie 		 *
6045aefb655Srie 		 * Transition:
6055aefb655Srie 		 *	0x00 .byte 0x66
6065aefb655Srie 		 *	0x01 leaq x@tlsgd(%rip), %rdi
6075aefb655Srie 		 *	0x08 .word 0x6666
6085aefb655Srie 		 *	0x0a rex64
6095aefb655Srie 		 *	0x0b call __tls_get_addr@plt
6105aefb655Srie 		 *	0x10
6115aefb655Srie 		 * To:
6125aefb655Srie 		 *	0x00 movq %fs:0, %rax
6135aefb655Srie 		 *	0x09 leaq x@tpoff(%rax), %rax
6145aefb655Srie 		 *	0x10
6155aefb655Srie 		 */
6165aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
617bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6185aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6195aefb655Srie 		arsp->rel_roffset += 8;
6205aefb655Srie 		arsp->rel_raddend = 0;
6215aefb655Srie 
6225aefb655Srie 		/*
623051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
6245aefb655Srie 		 */
6255aefb655Srie 		offset -= 4;
6265aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6275aefb655Srie 		return (FIX_RELOC);
6285aefb655Srie 
629*49f9b365SRichard Lowe 	case R_AMD64_GOTTPOFF: {
6305aefb655Srie 		/*
6315aefb655Srie 		 * IE -> LE
6325aefb655Srie 		 *
633*49f9b365SRichard Lowe 		 * Transition 1:
634*49f9b365SRichard Lowe 		 *	movq %fs:0, %reg
635*49f9b365SRichard Lowe 		 *	addq x@gottpoff(%rip), %reg
6365aefb655Srie 		 * To:
637*49f9b365SRichard Lowe 		 *	movq %fs:0, %reg
638*49f9b365SRichard Lowe 		 *	leaq x@tpoff(%reg), %reg
639*49f9b365SRichard Lowe 		 *
640*49f9b365SRichard Lowe 		 * Transition (as a special case):
641*49f9b365SRichard Lowe 		 *	movq %fs:0, %r12/%rsp
642*49f9b365SRichard Lowe 		 *	addq x@gottpoff(%rip), %r12/%rsp
643*49f9b365SRichard Lowe 		 * To:
644*49f9b365SRichard Lowe 		 *	movq %fs:0, %r12/%rsp
645*49f9b365SRichard Lowe 		 *	addq x@tpoff(%rax), %r12/%rsp
646*49f9b365SRichard Lowe 		 *
647*49f9b365SRichard Lowe 		 * Transition 2:
648*49f9b365SRichard Lowe 		 *	movq x@gottpoff(%rip), %reg
649*49f9b365SRichard Lowe 		 *	movq %fs:(%reg), %reg
650*49f9b365SRichard Lowe 		 * To:
651*49f9b365SRichard Lowe 		 *	movq x@tpoff(%reg), %reg
652*49f9b365SRichard Lowe 		 *	movq %fs:(%reg), %reg
6535aefb655Srie 		 */
654*49f9b365SRichard Lowe 		Conv_inv_buf_t	inv_buf;
655*49f9b365SRichard Lowe 		uint8_t reg;		/* Register */
656*49f9b365SRichard Lowe 
657*49f9b365SRichard Lowe 		offset -= 3;
658*49f9b365SRichard Lowe 
659*49f9b365SRichard Lowe 		reg = offset[2] >> 3; /* Encoded dest. reg. operand */
660*49f9b365SRichard Lowe 
661051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
662bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6635aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6645aefb655Srie 		arsp->rel_raddend = 0;
6655aefb655Srie 
6665aefb655Srie 		/*
667*49f9b365SRichard Lowe 		 * This is transition 2, and the special case of form 1 where
668*49f9b365SRichard Lowe 		 * a normal transition would index %rsp or %r12 and need a SIB
669*49f9b365SRichard Lowe 		 * byte in the leaq for which we lack space
6705aefb655Srie 		 */
671*49f9b365SRichard Lowe 		if ((offset[1] == INSN_MOVMR) ||
672*49f9b365SRichard Lowe 		    ((offset[1] == INSN_ADDMR) && (reg == REG_ESP))) {
6735aefb655Srie 			/*
674*49f9b365SRichard Lowe 			 * If we needed an extra bit of MOD.reg to refer to
675*49f9b365SRichard Lowe 			 * this register as the dest of the original movq we
676*49f9b365SRichard Lowe 			 * need an extra bit of MOD.rm to refer to it in the
677*49f9b365SRichard Lowe 			 * dest of the replacement movq or addq.
6785aefb655Srie 			 */
679*49f9b365SRichard Lowe 			if (offset[0] == REX_RW)
680*49f9b365SRichard Lowe 				offset[0] = REX_BW;
6815aefb655Srie 
682*49f9b365SRichard Lowe 			offset[1] = (offset[1] == INSN_MOVMR) ?
683*49f9b365SRichard Lowe 			    INSN_MOVIR : INSN_ADDIR;
684*49f9b365SRichard Lowe 			offset[2] = 0xc0 | reg;
685*49f9b365SRichard Lowe 
686*49f9b365SRichard Lowe 			return (FIX_RELOC);
687*49f9b365SRichard Lowe 		} else if (offset[1] == INSN_ADDMR) {
688*49f9b365SRichard Lowe 			/*
689*49f9b365SRichard Lowe 			 * If we needed an extra bit of MOD.reg to refer to
690*49f9b365SRichard Lowe 			 * this register in the dest of the addq we need an
691*49f9b365SRichard Lowe 			 * extra bit of both MOD.reg and MOD.rm to refer to it
692*49f9b365SRichard Lowe 			 * in the source and dest of the leaq
693*49f9b365SRichard Lowe 			 */
694*49f9b365SRichard Lowe 			if (offset[0] == REX_RW)
695*49f9b365SRichard Lowe 				offset[0] = REX_BRW;
696*49f9b365SRichard Lowe 
697*49f9b365SRichard Lowe 			offset[1] = INSN_LEA;
698*49f9b365SRichard Lowe 			offset[2] = 0x80 | (reg << 3) | reg;
699*49f9b365SRichard Lowe 
700*49f9b365SRichard Lowe 			return (FIX_RELOC);
701*49f9b365SRichard Lowe 		}
702*49f9b365SRichard Lowe 
703*49f9b365SRichard Lowe 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
704*49f9b365SRichard Lowe 		    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
705*49f9b365SRichard Lowe 		    arsp->rel_isdesc->is_file->ifl_name,
706*49f9b365SRichard Lowe 		    ld_reloc_sym_name(arsp),
707*49f9b365SRichard Lowe 		    arsp->rel_isdesc->is_name,
708*49f9b365SRichard Lowe 		    EC_OFF(arsp->rel_roffset));
709*49f9b365SRichard Lowe 		return (FIX_ERROR);
710*49f9b365SRichard Lowe 	}
7115aefb655Srie 	case R_AMD64_TLSLD:
7125aefb655Srie 		/*
7135aefb655Srie 		 * LD -> LE
7145aefb655Srie 		 *
7155aefb655Srie 		 * Transition
7165aefb655Srie 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
7175aefb655Srie 		 *	0x07 call __tls_get_addr@plt
7185aefb655Srie 		 *	0x0c
7195aefb655Srie 		 * To:
7205aefb655Srie 		 *	0x00 .byte 0x66
7215aefb655Srie 		 *	0x01 .byte 0x66
7225aefb655Srie 		 *	0x02 .byte 0x66
7235aefb655Srie 		 *	0x03 movq %fs:0, %rax
7245aefb655Srie 		 */
725051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
726bf994817SAli Bahrami 		    R_AMD64_NONE, arsp, ld_reloc_sym_name));
7275aefb655Srie 		offset -= 3;
7285aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
7295aefb655Srie 		return (FIX_DONE);
7305aefb655Srie 
7315aefb655Srie 	case R_AMD64_DTPOFF32:
7325aefb655Srie 		/*
7335aefb655Srie 		 * LD->LE
7345aefb655Srie 		 *
7355aefb655Srie 		 * Transition:
7365aefb655Srie 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
7375aefb655Srie 		 * To:
7385aefb655Srie 		 *	0x00 leaq x1@tpoff(%rax), %rcx
7395aefb655Srie 		 */
740051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
741bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
7425aefb655Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
7435aefb655Srie 		arsp->rel_raddend = 0;
7445aefb655Srie 		return (FIX_RELOC);
7455aefb655Srie 	}
7465aefb655Srie 
7475aefb655Srie 	return (FIX_RELOC);
7485aefb655Srie }
7495aefb655Srie 
750ba2be530Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7515aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7525aefb655Srie {
7535aefb655Srie 	Rel_desc	*arsp;
754bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
75557ef7aa9SRod Evans 	Aliste		idx;
7565aefb655Srie 	uintptr_t	return_code = 1;
7571d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
7585aefb655Srie 
759bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7605aefb655Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7617010c12aSrie 
7625aefb655Srie 	/*
7635aefb655Srie 	 * Process active relocations.
7645aefb655Srie 	 */
765bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
7665aefb655Srie 		uchar_t		*addr;
7675aefb655Srie 		Xword 		value;
7685aefb655Srie 		Sym_desc	*sdp;
7695aefb655Srie 		const char	*ifl_name;
7705aefb655Srie 		Xword		refaddr;
7715aefb655Srie 		int		moved = 0;
7725aefb655Srie 		Gotref		gref;
773bf994817SAli Bahrami 		Os_desc		*osp;
7745aefb655Srie 
7755aefb655Srie 		/*
776bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
777bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
7785aefb655Srie 		 */
7795aefb655Srie 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
780bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
7815aefb655Srie 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
782bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
7835aefb655Srie 			continue;
7845aefb655Srie 		}
7855aefb655Srie 
7865aefb655Srie 		/*
787bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
788bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
789bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
7905aefb655Srie 		 *
7915aefb655Srie 		 * The got table entries have already been assigned,
7925aefb655Srie 		 * and we bind to those initial entries.
7935aefb655Srie 		 */
7945aefb655Srie 		if (arsp->rel_flags & FLG_REL_DTLS)
7955aefb655Srie 			gref = GOT_REF_TLSGD;
7965aefb655Srie 		else if (arsp->rel_flags & FLG_REL_MTLS)
7975aefb655Srie 			gref = GOT_REF_TLSLD;
7985aefb655Srie 		else if (arsp->rel_flags & FLG_REL_STLS)
7995aefb655Srie 			gref = GOT_REF_TLSIE;
8005aefb655Srie 		else
8015aefb655Srie 			gref = GOT_REF_GENERIC;
8025aefb655Srie 
8035aefb655Srie 		/*
8045aefb655Srie 		 * Perform any required TLS fixups.
8055aefb655Srie 		 */
8065aefb655Srie 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
8075aefb655Srie 			Fixupret	ret;
8085aefb655Srie 
8095aefb655Srie 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
8105aefb655Srie 				return (S_ERROR);
8115aefb655Srie 			if (ret == FIX_DONE)
8125aefb655Srie 				continue;
8135aefb655Srie 		}
8145aefb655Srie 
8155aefb655Srie 		/*
8165aefb655Srie 		 * If this is a relocation against a move table, or
8175aefb655Srie 		 * expanded move table, adjust the relocation entries.
8185aefb655Srie 		 */
819bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
8205aefb655Srie 			ld_adj_movereloc(ofl, arsp);
8215aefb655Srie 
8225aefb655Srie 		sdp = arsp->rel_sym;
8235aefb655Srie 		refaddr = arsp->rel_roffset +
8245aefb655Srie 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
8255aefb655Srie 
8265aefb655Srie 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
8275aefb655Srie 		    (arsp->rel_flags & FLG_REL_GOTCL))
8285aefb655Srie 			value = 0;
829bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8305aefb655Srie 			Sym_desc	*sym;
8315aefb655Srie 
8325aefb655Srie 			/*
8335aefb655Srie 			 * The value for a symbol pointing to a SECTION
8345aefb655Srie 			 * is based off of that sections position.
8355aefb655Srie 			 */
8365aefb655Srie 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
8375aefb655Srie 			    /* LINTED */
838bf994817SAli Bahrami 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
8395aefb655Srie 				/*
840bf994817SAli Bahrami 				 * The symbol was moved, so adjust the value
841bf994817SAli Bahrami 				 * relative to the new section.
8425aefb655Srie 				 */
8435aefb655Srie 				value = sym->sd_sym->st_value;
8445aefb655Srie 				moved = 1;
845b26cc8daSAli Bahrami 
846b26cc8daSAli Bahrami 				/*
847bf994817SAli Bahrami 				 * The original raddend covers the displacement
848bf994817SAli Bahrami 				 * from the section start to the desired
849bf994817SAli Bahrami 				 * address. The value computed above gets us
850bf994817SAli Bahrami 				 * from the section start to the start of the
851bf994817SAli Bahrami 				 * symbol range. Adjust the old raddend to
852bf994817SAli Bahrami 				 * remove the offset from section start to
853bf994817SAli Bahrami 				 * symbol start, leaving the displacement
854bf994817SAli Bahrami 				 * within the range of the symbol.
855b26cc8daSAli Bahrami 				 */
856bf994817SAli Bahrami 				arsp->rel_raddend -= sym->sd_osym->st_value;
8575aefb655Srie 			} else {
858bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
859bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
860bf994817SAli Bahrami 					value += sdp->sd_isc->is_osdesc->
8615aefb655Srie 					    os_shdr->sh_addr;
8625aefb655Srie 			}
8635aefb655Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8645aefb655Srie 				value -= ofl->ofl_tlsphdr->p_vaddr;
8652926dd2eSrie 
8662926dd2eSrie 		} else if (IS_SIZE(arsp->rel_rtype)) {
8672926dd2eSrie 			/*
8682926dd2eSrie 			 * Size relocations require the symbols size.
8692926dd2eSrie 			 */
8702926dd2eSrie 			value = sdp->sd_sym->st_size;
87108278a5eSRod Evans 
87208278a5eSRod Evans 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
87308278a5eSRod Evans 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
8745aefb655Srie 			/*
875bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
876bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
877bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
878bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
8795aefb655Srie 			 */
88008278a5eSRod Evans 			value = ld_calc_plt_addr(sdp, ofl);
88108278a5eSRod Evans 		} else
8825aefb655Srie 			value = sdp->sd_sym->st_value;
8835aefb655Srie 
8845aefb655Srie 		/*
8855aefb655Srie 		 * Relocation against the GLOBAL_OFFSET_TABLE.
8865aefb655Srie 		 */
887bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
888bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
889bf994817SAli Bahrami 			return (S_ERROR);
890bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
8915aefb655Srie 
8925aefb655Srie 		/*
893bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
894bf994817SAli Bahrami 		 * sections virtual address to the reference address.
8955aefb655Srie 		 */
8965aefb655Srie 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
8975aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0))
8985aefb655Srie 			refaddr += arsp->rel_isdesc->is_osdesc->
8995aefb655Srie 			    os_shdr->sh_addr;
9005aefb655Srie 
9015aefb655Srie 		/*
902bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
903bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
9045aefb655Srie 		 */
9055aefb655Srie 		if (IS_PLT(arsp->rel_rtype)) {
9065aefb655Srie 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
9075aefb655Srie 				value = ld_calc_plt_addr(sdp, ofl);
9085aefb655Srie 		}
9095aefb655Srie 
9105aefb655Srie 		/*
9115aefb655Srie 		 * Add relocations addend to value.  Add extra
9125aefb655Srie 		 * relocation addend if needed.
9135aefb655Srie 		 *
914bf994817SAli Bahrami 		 * Note: For GOT relative relocations on amd64 we discard the
915bf994817SAli Bahrami 		 * addend.  It was relevant to the reference - not to the
916bf994817SAli Bahrami 		 * data item being referenced (ie: that -4 thing).
9175aefb655Srie 		 */
9185aefb655Srie 		if ((arsp->rel_flags & FLG_REL_GOT) == 0)
919bf2f215aSAli Bahrami 			value += arsp->rel_raddend;
9205aefb655Srie 
9215aefb655Srie 		/*
922bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
923bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
924bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
925bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
926bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
927bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
9285aefb655Srie 		 */
9295aefb655Srie 		if ((arsp->rel_flags & FLG_REL_GOT) &&
9305aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9315aefb655Srie 			Xword		R1addr;
9325aefb655Srie 			uintptr_t	R2addr;
9335aefb655Srie 			Word		gotndx;
9345aefb655Srie 			Gotndx		*gnp;
9355aefb655Srie 
9365aefb655Srie 			/*
937bf994817SAli Bahrami 			 * Perform relocation against GOT table. Since this
938bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
939bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
9405aefb655Srie 			 *
9415aefb655Srie 			 * Calculate offset into GOT at which to apply
9425aefb655Srie 			 * the relocation.
9435aefb655Srie 			 */
944bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
9455aefb655Srie 			assert(gnp);
9465aefb655Srie 
9475aefb655Srie 			if (arsp->rel_rtype == R_AMD64_DTPOFF64)
9485aefb655Srie 				gotndx = gnp->gn_gotndx + 1;
9495aefb655Srie 			else
9505aefb655Srie 				gotndx = gnp->gn_gotndx;
9515aefb655Srie 
9525aefb655Srie 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9535aefb655Srie 
9545aefb655Srie 			/*
9555aefb655Srie 			 * Add the GOTs data's offset.
9565aefb655Srie 			 */
957bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
9585aefb655Srie 
959bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
960bf994817SAli Bahrami 			    M_MACH, SHT_RELA, arsp, R1addr, value,
961bf994817SAli Bahrami 			    ld_reloc_sym_name));
9625aefb655Srie 
9635aefb655Srie 			/*
9645aefb655Srie 			 * And do it.
9655aefb655Srie 			 */
966f3324781Sab196087 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
967bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
968f3324781Sab196087 			else
9695aefb655Srie 				*(Xword *)R2addr = value;
9705aefb655Srie 			continue;
9715aefb655Srie 
9725aefb655Srie 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
9735aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9745aefb655Srie 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
9755aefb655Srie 
9765aefb655Srie 		} else if (IS_GOTPCREL(arsp->rel_rtype) &&
9775aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9785aefb655Srie 			Gotndx *gnp;
9795aefb655Srie 
9805aefb655Srie 			/*
9815aefb655Srie 			 * Calculation:
9825aefb655Srie 			 *	G + GOT + A - P
9835aefb655Srie 			 */
984bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
9855aefb655Srie 			assert(gnp);
986bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr-> sh_addr) +
987bf994817SAli Bahrami 			    ((Xword)gnp->gn_gotndx * M_GOT_ENTSIZE) +
988bf994817SAli Bahrami 			    arsp->rel_raddend - refaddr;
9895aefb655Srie 
9905aefb655Srie 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
9915aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9925aefb655Srie 			value = (Xword)(ofl->ofl_osgot->os_shdr->
9935aefb655Srie 			    sh_addr) - refaddr + arsp->rel_raddend;
9945aefb655Srie 
9955aefb655Srie 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9965aefb655Srie 		    (((flags & FLG_OF_RELOBJ) == 0) ||
997bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
9985aefb655Srie 			value -= refaddr;
9995aefb655Srie 
10005aefb655Srie 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
10015aefb655Srie 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
10025aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10035aefb655Srie 			Gotndx	*gnp;
10045aefb655Srie 
1005bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
10065aefb655Srie 			assert(gnp);
10075aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10085aefb655Srie 
10095aefb655Srie 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
10105aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10115aefb655Srie 			Gotndx *gnp;
10125aefb655Srie 
1013bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
10145aefb655Srie 			assert(gnp);
10155aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10165aefb655Srie 
10175aefb655Srie 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
10185aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
10195aefb655Srie 			Xword	tlsstatsize;
10205aefb655Srie 
10215aefb655Srie 			/*
10225aefb655Srie 			 * This is the LE TLS reference model.  Static
10235aefb655Srie 			 * offset is hard-coded.
10245aefb655Srie 			 */
1025bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
10265aefb655Srie 			    M_TLSSTATALIGN);
10275aefb655Srie 			value = tlsstatsize - value;
10285aefb655Srie 
10295aefb655Srie 			/*
1030bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a negative
1031bf994817SAli Bahrami 			 * offset that can be added to the thread pointer.
10325aefb655Srie 			 */
10335aefb655Srie 			if (arsp->rel_rtype == R_AMD64_TPOFF32)
10345aefb655Srie 				value = -value;
10355aefb655Srie 		}
10365aefb655Srie 
10375aefb655Srie 		if (arsp->rel_isdesc->is_file)
10385aefb655Srie 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10395aefb655Srie 		else
10405aefb655Srie 			ifl_name = MSG_INTL(MSG_STR_NULL);
10415aefb655Srie 
10425aefb655Srie 		/*
1043bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1044bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1045bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1046bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1047bf994817SAli Bahrami 		 * End users should never see this.
10485aefb655Srie 		 */
10495aefb655Srie 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1050de777a60Sab196087 			Conv_inv_buf_t inv_buf;
1051de777a60Sab196087 
10521007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1053bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1054bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
10554a8d0ea7SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
10565aefb655Srie 			    arsp->rel_isdesc->is_name);
10575aefb655Srie 			return (S_ERROR);
10585aefb655Srie 		}
10595aefb655Srie 
10605aefb655Srie 		/*
10615aefb655Srie 		 * Get the address of the data item we need to modify.
10625aefb655Srie 		 */
10635aefb655Srie 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1064bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
10655aefb655Srie 
1066635216b6SRod Evans 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1067bf994817SAli Bahrami 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1068bf994817SAli Bahrami 		    ld_reloc_sym_name));
1069bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
10705aefb655Srie 
10715aefb655Srie 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
10725aefb655Srie 		    ofl->ofl_size) || (arsp->rel_roffset >
1073bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
10745aefb655Srie 			int		class;
1075de777a60Sab196087 			Conv_inv_buf_t inv_buf;
10765aefb655Srie 
1077bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1078bf994817SAli Bahrami 			    ofl->ofl_size)
10795aefb655Srie 				class = ERR_FATAL;
10805aefb655Srie 			else
10815aefb655Srie 				class = ERR_WARNING;
10825aefb655Srie 
10831007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1084bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1085bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1086bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
10875aefb655Srie 			    EC_ADDR((uintptr_t)addr -
10885aefb655Srie 			    (uintptr_t)ofl->ofl_nehdr));
10895aefb655Srie 
10905aefb655Srie 			if (class == ERR_FATAL) {
10915aefb655Srie 				return_code = S_ERROR;
10925aefb655Srie 				continue;
10935aefb655Srie 			}
10945aefb655Srie 		}
10955aefb655Srie 
10965aefb655Srie 		/*
1097bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1098bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
10995aefb655Srie 		 */
11005aefb655Srie 		if (moved)
11015aefb655Srie 			value -= *addr;
11025aefb655Srie 
11035aefb655Srie 		/*
1104bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
11055aefb655Srie 		 */
1106f3324781Sab196087 		if (OFL_DO_RELOC(ofl)) {
1107f3324781Sab196087 			/*
1108bf994817SAli Bahrami 			 * If this is a PROGBITS section and the running linker
1109bf994817SAli Bahrami 			 * has a different byte order than the target host,
1110bf994817SAli Bahrami 			 * tell do_reloc_ld() to swap bytes.
1111f3324781Sab196087 			 */
1112bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1113bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
11141007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
11151007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
11165aefb655Srie 				return_code = S_ERROR;
11175aefb655Srie 			}
11185aefb655Srie 		}
11191007fd6fSAli Bahrami 	}
11205aefb655Srie 	return (return_code);
11215aefb655Srie }
11225aefb655Srie 
1123ba2be530Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)11245aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11255aefb655Srie {
11265aefb655Srie 	Rel_desc	*orsp;
11275aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
11285aefb655Srie 
11295aefb655Srie 	/*
11305aefb655Srie 	 * Static executables *do not* want any relocations against them.
11315aefb655Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11325aefb655Srie 	 * symbol in a static executable, it's best to disable them here
11335aefb655Srie 	 * instead of through out the relocation code.
11345aefb655Srie 	 */
1135635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11365aefb655Srie 		return (1);
11375aefb655Srie 
11385aefb655Srie 	/*
11395aefb655Srie 	 * If we are adding a output relocation against a section
11405aefb655Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11415aefb655Srie 	 * will be added to the .dynsym symbol table.
11425aefb655Srie 	 */
11435aefb655Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11445aefb655Srie 	    ((flags & FLG_REL_SCNNDX) ||
11455aefb655Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11465aefb655Srie 
11475aefb655Srie 		/*
11485aefb655Srie 		 * If this is a COMMON symbol - no output section
11495aefb655Srie 		 * exists yet - (it's created as part of sym_validate()).
11505aefb655Srie 		 * So - we mark here that when it's created it should
11515aefb655Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11525aefb655Srie 		 */
11535aefb655Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11540bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11555aefb655Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11565aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11575aefb655Srie 			else
11585aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11595aefb655Srie 		} else {
116008278a5eSRod Evans 			Os_desc *osp;
116108278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11625aefb655Srie 
116308278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
116408278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11655aefb655Srie 				ofl->ofl_dynshdrcnt++;
11665aefb655Srie 				osp->os_flags |= FLG_OS_OUTREL;
11675aefb655Srie 			}
11685aefb655Srie 		}
11695aefb655Srie 	}
11705aefb655Srie 
1171bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1172bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1173bf994817SAli Bahrami 		return (S_ERROR);
11745aefb655Srie 
11755aefb655Srie 	if (flags & FLG_REL_GOT)
11765aefb655Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
11775aefb655Srie 	else if (flags & FLG_REL_PLT)
11785aefb655Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
11795aefb655Srie 	else if (flags & FLG_REL_BSS)
11805aefb655Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
11815aefb655Srie 	else if (flags & FLG_REL_NOINFO)
11825aefb655Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
11835aefb655Srie 	else
1184bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
11855aefb655Srie 
11865aefb655Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11875aefb655Srie 		ofl->ofl_relocrelcnt++;
11885aefb655Srie 
11895aefb655Srie 	/*
11905aefb655Srie 	 * We don't perform sorting on PLT relocations because
11915aefb655Srie 	 * they have already been assigned a PLT index and if we
11925aefb655Srie 	 * were to sort them we would have to re-assign the plt indexes.
11935aefb655Srie 	 */
11945aefb655Srie 	if (!(flags & FLG_REL_PLT))
11955aefb655Srie 		ofl->ofl_reloccnt++;
11965aefb655Srie 
11975aefb655Srie 	/*
11985aefb655Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11995aefb655Srie 	 */
12005aefb655Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12015aefb655Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12025aefb655Srie 
12035aefb655Srie 	/*
12045aefb655Srie 	 * Identify and possibly warn of a displacement relocation.
12055aefb655Srie 	 */
12065aefb655Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
12075aefb655Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12085aefb655Srie 
12095aefb655Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12105aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12115aefb655Srie 	}
12125aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
12135aefb655Srie 	    M_MACH, orsp));
12145aefb655Srie 	return (1);
12155aefb655Srie }
12165aefb655Srie 
12175aefb655Srie /*
12185aefb655Srie  * process relocation for a LOCAL symbol
12195aefb655Srie  */
1220ba2be530Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)12215aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12225aefb655Srie {
12231d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12245aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
12250bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12265aefb655Srie 	Word		ortype = rsp->rel_rtype;
12275aefb655Srie 
12285aefb655Srie 	/*
12295aefb655Srie 	 * if ((shared object) and (not pc relative relocation) and
12305aefb655Srie 	 *    (not against ABS symbol))
12315aefb655Srie 	 * then
12325aefb655Srie 	 *	build R_AMD64_RELATIVE
12335aefb655Srie 	 * fi
12345aefb655Srie 	 */
12355aefb655Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12362926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12375aefb655Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12385aefb655Srie 	    !(rsp->rel_isdesc != NULL &&
12395aefb655Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12405aefb655Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12415aefb655Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12425aefb655Srie 
12435aefb655Srie 		/*
12445aefb655Srie 		 * R_AMD64_RELATIVE updates a 64bit address, if this
12455aefb655Srie 		 * relocation isn't a 64bit binding then we can not
12465aefb655Srie 		 * simplify it to a RELATIVE relocation.
12475aefb655Srie 		 */
12485aefb655Srie 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
12491d9df23bSab196087 			return (ld_add_outrel(0, rsp, ofl));
12505aefb655Srie 		}
12515aefb655Srie 
12525aefb655Srie 		rsp->rel_rtype = R_AMD64_RELATIVE;
12535aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
12545aefb655Srie 			return (S_ERROR);
12555aefb655Srie 		rsp->rel_rtype = ortype;
12565aefb655Srie 		return (1);
12575aefb655Srie 	}
12585aefb655Srie 
12595aefb655Srie 	/*
12605aefb655Srie 	 * If the relocation is against a 'non-allocatable' section
12615aefb655Srie 	 * and we can not resolve it now - then give a warning
12625aefb655Srie 	 * message.
12635aefb655Srie 	 *
12645aefb655Srie 	 * We can not resolve the symbol if either:
12655aefb655Srie 	 *	a) it's undefined
12665aefb655Srie 	 *	b) it's defined in a shared library and a
12675aefb655Srie 	 *	   COPY relocation hasn't moved it to the executable
12685aefb655Srie 	 *
12695aefb655Srie 	 * Note: because we process all of the relocations against the
12705aefb655Srie 	 *	text segment before any others - we know whether
12715aefb655Srie 	 *	or not a copy relocation will be generated before
12725aefb655Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12735aefb655Srie 	 */
12745aefb655Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12755aefb655Srie 	    ((shndx == SHN_UNDEF) ||
12765aefb655Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12775aefb655Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1278de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
1279bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1280de777a60Sab196087 
12815aefb655Srie 		/*
12825aefb655Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12835aefb655Srie 		 * section - then silently ignore that the relocation
12845aefb655Srie 		 * can not be resolved.
12855aefb655Srie 		 */
1286bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12875aefb655Srie 			return (0);
12881007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1289de777a60Sab196087 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
12905aefb655Srie 		    rsp->rel_isdesc->is_file->ifl_name,
1291bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
12925aefb655Srie 		return (1);
12935aefb655Srie 	}
12945aefb655Srie 
12955aefb655Srie 	/*
12965aefb655Srie 	 * Perform relocation.
12975aefb655Srie 	 */
12985aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12995aefb655Srie }
13005aefb655Srie 
13015aefb655Srie 
1302ba2be530Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)13035aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13045aefb655Srie {
13055aefb655Srie 	Word		rtype = rsp->rel_rtype;
13065aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
13071d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
13085aefb655Srie 	Gotndx		*gnp;
13095aefb655Srie 
13105aefb655Srie 	/*
1311d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1312d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
13135aefb655Srie 	 */
1314d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13155aefb655Srie 		/*
1316d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
13175aefb655Srie 		 */
13185aefb655Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13195aefb655Srie 
1320d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13215aefb655Srie 			/*
1322d326b23bSrie 			 * Assign a GOT entry for static TLS references.
13235aefb655Srie 			 */
132457ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
132557ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
1326d326b23bSrie 
1327d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1328d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1329d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
13305aefb655Srie 					return (S_ERROR);
13315aefb655Srie 			}
1332d326b23bSrie 
1333d326b23bSrie 			/*
1334d326b23bSrie 			 * IE access model.
1335d326b23bSrie 			 */
13365aefb655Srie 			if (IS_TLS_IE(rtype))
13375aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13385aefb655Srie 
13395aefb655Srie 			/*
1340d326b23bSrie 			 * Fixups are required for other executable models.
13415aefb655Srie 			 */
13425aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13435aefb655Srie 			    rsp, ofl));
13445aefb655Srie 		}
1345d326b23bSrie 
13465aefb655Srie 		/*
1347d326b23bSrie 		 * LE access model.
13485aefb655Srie 		 */
13495aefb655Srie 		if (IS_TLS_LE(rtype))
13505aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1351d326b23bSrie 
13525aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13535aefb655Srie 		    rsp, ofl));
13545aefb655Srie 	}
13555aefb655Srie 
13565aefb655Srie 	/*
1357d326b23bSrie 	 * Building a shared object.
1358d326b23bSrie 	 *
1359d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13605aefb655Srie 	 */
136157ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
136257ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1363d326b23bSrie 
1364d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
136557ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
13665aefb655Srie 			return (S_ERROR);
1367d326b23bSrie 
13685aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
136957ef7aa9SRod Evans 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
137057ef7aa9SRod Evans 	    ofl, rsp)) == NULL)) {
1371d326b23bSrie 
1372d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1373d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1374d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13755aefb655Srie 			return (S_ERROR);
13765aefb655Srie 	}
13775aefb655Srie 
13785aefb655Srie 	if (IS_TLS_LD(rtype))
13795aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13805aefb655Srie 
13815aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13825aefb655Srie }
13835aefb655Srie 
13845aefb655Srie /* ARGSUSED5 */
1385ba2be530Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)138657ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
13875aefb655Srie     Rel_desc *rsp, Sym_desc *sdp)
13885aefb655Srie {
13895aefb655Srie 	Xword		raddend;
139057ef7aa9SRod Evans 	Gotndx		gn, *gnp;
139157ef7aa9SRod Evans 	Aliste		idx;
13925aefb655Srie 	uint_t		gotents;
13935aefb655Srie 
13945aefb655Srie 	raddend = rsp->rel_raddend;
139557ef7aa9SRod Evans 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
13965aefb655Srie 		return (1);
13975aefb655Srie 
13985aefb655Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13995aefb655Srie 		gotents = 2;
14005aefb655Srie 	else
14015aefb655Srie 		gotents = 1;
14025aefb655Srie 
140357ef7aa9SRod Evans 	gn.gn_addend = raddend;
140457ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
140557ef7aa9SRod Evans 	gn.gn_gotref = gref;
14065aefb655Srie 
14075aefb655Srie 	ofl->ofl_gotcnt += gotents;
14085aefb655Srie 
14095aefb655Srie 	if (gref == GOT_REF_TLSLD) {
141057ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
141157ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
141257ef7aa9SRod Evans 				return (S_ERROR);
141357ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
14145aefb655Srie 			ofl->ofl_tlsldgotndx = gnp;
141557ef7aa9SRod Evans 		}
14165aefb655Srie 		return (1);
14175aefb655Srie 	}
14185aefb655Srie 
141957ef7aa9SRod Evans 	idx = 0;
142057ef7aa9SRod Evans 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
142157ef7aa9SRod Evans 		if (gnp->gn_addend > raddend)
142257ef7aa9SRod Evans 			break;
14235aefb655Srie 	}
142457ef7aa9SRod Evans 
142557ef7aa9SRod Evans 	/*
142657ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
142757ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
142857ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
142957ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
143057ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
143157ef7aa9SRod Evans 	 */
143257ef7aa9SRod Evans 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
143357ef7aa9SRod Evans 	    AL_CNT_SDP_GOT, idx) == NULL)
143457ef7aa9SRod Evans 		return (S_ERROR);
143557ef7aa9SRod Evans 
14365aefb655Srie 	return (1);
14375aefb655Srie }
14385aefb655Srie 
1439ba2be530Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)14405aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14415aefb655Srie {
14425aefb655Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14435aefb655Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14445aefb655Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14455aefb655Srie }
14465aefb655Srie 
14475aefb655Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14485aefb655Srie /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14495aefb655Srie /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14505aefb655Srie /* 0x0c NOP */			0x90,
14515aefb655Srie /* 0x0d NOP */			0x90,
14525aefb655Srie /* 0x0e NOP */			0x90,
14535aefb655Srie /* 0x0f NOP */			0x90
14545aefb655Srie };
14555aefb655Srie 
14565aefb655Srie /*
14575aefb655Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14585aefb655Srie  */
1459ba2be530Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)14605aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl)
14615aefb655Srie {
1462ba2be530Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1463ba2be530Sab196087 
14645aefb655Srie 	if (ofl->ofl_osgot) {
14655aefb655Srie 		Sym_desc	*sdp;
14665aefb655Srie 
14675aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1468635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1469d326b23bSrie 			uchar_t	*genptr;
1470d326b23bSrie 
1471d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14725aefb655Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14735aefb655Srie 			/* LINTED */
14745aefb655Srie 			*(Xword *)genptr = sdp->sd_sym->st_value;
1475ba2be530Sab196087 			if (bswap)
1476ba2be530Sab196087 				/* LINTED */
1477ba2be530Sab196087 				*(Xword *)genptr =
1478ba2be530Sab196087 				    /* LINTED */
1479ba2be530Sab196087 				    ld_bswap_Xword(*(Xword *)genptr);
14805aefb655Srie 		}
14815aefb655Srie 	}
14825aefb655Srie 
14835aefb655Srie 	/*
14845aefb655Srie 	 * Fill in the reserved slot in the procedure linkage table the first
14855aefb655Srie 	 * entry is:
14865aefb655Srie 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14875aefb655Srie 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14885aefb655Srie 	 *	0x0c NOP
14895aefb655Srie 	 *	0x0d NOP
14905aefb655Srie 	 *	0x0e NOP
14915aefb655Srie 	 *	0x0f NOP
14925aefb655Srie 	 */
1493f3324781Sab196087 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
14945aefb655Srie 		uchar_t	*pltent;
14955aefb655Srie 		Xword	val1;
14965aefb655Srie 
14975aefb655Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14985aefb655Srie 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14995aefb655Srie 
15005aefb655Srie 		/*
1501f3324781Sab196087 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1502f3324781Sab196087 		 * stage.
1503f3324781Sab196087 		 */
1504f3324781Sab196087 		if (!OFL_DO_RELOC(ofl))
1505f3324781Sab196087 			return (1);
1506f3324781Sab196087 
1507f3324781Sab196087 		/*
15085aefb655Srie 		 * filin:
15095aefb655Srie 		 *	PUSHQ GOT + 8(%rip)
15105aefb655Srie 		 *
15115aefb655Srie 		 * Note: 0x06 below represents the offset to the
15125aefb655Srie 		 *	 next instruction - which is what %rip will
15135aefb655Srie 		 *	 be pointing at.
15145aefb655Srie 		 */
15155aefb655Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15165aefb655Srie 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
15175aefb655Srie 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
15185aefb655Srie 
1519bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
1520bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1521bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15221007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
15235aefb655Srie 			return (S_ERROR);
15245aefb655Srie 		}
15255aefb655Srie 
15265aefb655Srie 		/*
15275aefb655Srie 		 * filin:
15285aefb655Srie 		 *  JMP	*GOT+16(%rip)
15295aefb655Srie 		 */
15305aefb655Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
15315aefb655Srie 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
15325aefb655Srie 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1533f3324781Sab196087 
1534bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
1535bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1536bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15371007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
15385aefb655Srie 			return (S_ERROR);
15395aefb655Srie 		}
15405aefb655Srie 	}
1541f3324781Sab196087 
15425aefb655Srie 	return (1);
15435aefb655Srie }
1544ba2be530Sab196087 
1545ba2be530Sab196087 
1546ba2be530Sab196087 
1547ba2be530Sab196087 /*
1548ba2be530Sab196087  * Template for generating "void (*)(void)" function
1549ba2be530Sab196087  */
1550ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1551ba2be530Sab196087 /* 0x00 */	0x55,				/* pushq  %rbp */
1552ba2be530Sab196087 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1553ba2be530Sab196087 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1554ba2be530Sab196087 /* 0x07 */	0x5d,				/* popq   %rbp */
1555ba2be530Sab196087 /* 0x08 */	0xc3				/* ret */
1556ba2be530Sab196087 };
1557ba2be530Sab196087 
1558ba2be530Sab196087 
1559ba2be530Sab196087 /*
15603c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15613c573fccSAli Bahrami  *
15623c573fccSAli Bahrami  * entry:
15633c573fccSAli Bahrami  *
15643c573fccSAli Bahrami  *	base - base address of section being filled
15653c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15663c573fccSAli Bahrami  *	cnt - # bytes to be filled
15673c573fccSAli Bahrami  *
15683c573fccSAli Bahrami  * exit:
15693c573fccSAli Bahrami  *	The fill has been completed.
15703c573fccSAli Bahrami  */
15713c573fccSAli Bahrami static void
execfill(void * base,off_t off,size_t cnt)15723c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
15733c573fccSAli Bahrami {
15743c573fccSAli Bahrami 	/*
15753c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
15763c573fccSAli Bahrami 	 * There are no alignment constraints.
15773c573fccSAli Bahrami 	 */
15783c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
15793c573fccSAli Bahrami }
15803c573fccSAli Bahrami 
15813c573fccSAli Bahrami 
15823c573fccSAli Bahrami /*
1583ba2be530Sab196087  * Return the ld_targ definition for this target.
1584ba2be530Sab196087  */
1585ba2be530Sab196087 const Target *
ld_targ_init_x86(void)1586ba2be530Sab196087 ld_targ_init_x86(void)
1587ba2be530Sab196087 {
1588ba2be530Sab196087 	static const Target _ld_targ = {
1589ba2be530Sab196087 		{			/* Target_mach */
1590ba2be530Sab196087 			M_MACH,			/* m_mach */
1591ba2be530Sab196087 			M_MACHPLUS,		/* m_machplus */
1592ba2be530Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
1593ba2be530Sab196087 			M_CLASS,		/* m_class */
1594ba2be530Sab196087 			M_DATA,			/* m_data */
1595ba2be530Sab196087 
1596ba2be530Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
1597ba2be530Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
1598bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1599ba2be530Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
160069112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1601ba2be530Sab196087 			M_WORD_ALIGN,		/* m_word_align */
1602ba2be530Sab196087 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1603ba2be530Sab196087 
1604ba2be530Sab196087 			/* Relocation type codes */
1605ba2be530Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1606ba2be530Sab196087 			M_R_COPY,		/* m_r_copy */
1607ba2be530Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1608ba2be530Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1609ba2be530Sab196087 			M_R_NUM,		/* m_r_num */
1610ba2be530Sab196087 			M_R_NONE,		/* m_r_none */
1611ba2be530Sab196087 			M_R_RELATIVE,		/* m_r_relative */
1612ba2be530Sab196087 			M_R_REGISTER,		/* m_r_register */
1613ba2be530Sab196087 
1614ba2be530Sab196087 			/* Relocation related constants */
1615ba2be530Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1616ba2be530Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1617ba2be530Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1618ba2be530Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1619ba2be530Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1620ba2be530Sab196087 
1621ba2be530Sab196087 			/* GOT related constants */
1622ba2be530Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
1623ba2be530Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
1624ba2be530Sab196087 
1625ba2be530Sab196087 			/* PLT related constants */
1626ba2be530Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
1627ba2be530Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1628ba2be530Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1629ba2be530Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1630ba2be530Sab196087 
16317e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16327e16fca0SAli Bahrami 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
16337e16fca0SAli Bahrami 
1634ba2be530Sab196087 			M_DT_REGISTER,		/* m_dt_register */
1635ba2be530Sab196087 		},
1636ba2be530Sab196087 		{			/* Target_machid */
1637ba2be530Sab196087 			M_ID_ARRAY,		/* id_array */
1638ba2be530Sab196087 			M_ID_BSS,		/* id_bss */
1639ba2be530Sab196087 			M_ID_CAP,		/* id_cap */
164008278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
164108278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1642ba2be530Sab196087 			M_ID_DATA,		/* id_data */
1643ba2be530Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
1644ba2be530Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
1645ba2be530Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
1646ba2be530Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
1647ba2be530Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1648ba2be530Sab196087 			M_ID_GOT,		/* id_got */
1649ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1650ba2be530Sab196087 			M_ID_HASH,		/* id_hash */
1651ba2be530Sab196087 			M_ID_INTERP,		/* id_interp */
1652ba2be530Sab196087 			M_ID_LBSS,		/* id_lbss */
1653ba2be530Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
1654ba2be530Sab196087 			M_ID_NOTE,		/* id_note */
1655ba2be530Sab196087 			M_ID_NULL,		/* id_null */
1656ba2be530Sab196087 			M_ID_PLT,		/* id_plt */
1657ba2be530Sab196087 			M_ID_REL,		/* id_rel */
1658ba2be530Sab196087 			M_ID_STRTAB,		/* id_strtab */
1659ba2be530Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
1660ba2be530Sab196087 			M_ID_SYMTAB,		/* id_symtab */
1661ba2be530Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1662ba2be530Sab196087 			M_ID_TEXT,		/* id_text */
1663ba2be530Sab196087 			M_ID_TLS,		/* id_tls */
1664ba2be530Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
1665ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
1666ba2be530Sab196087 			M_ID_UNWIND,		/* id_unwind */
16677e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1668ba2be530Sab196087 			M_ID_USER,		/* id_user */
1669ba2be530Sab196087 			M_ID_VERSION,		/* id_version */
1670ba2be530Sab196087 		},
1671ba2be530Sab196087 		{			/* Target_nullfunc */
1672ba2be530Sab196087 			nullfunc_tmpl,		/* nf_template */
1673ba2be530Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
1674ba2be530Sab196087 		},
16753c573fccSAli Bahrami 		{			/* Target_fillfunc */
16763c573fccSAli Bahrami 			execfill		/* ff_execfill */
16773c573fccSAli Bahrami 		},
1678ba2be530Sab196087 		{			/* Target_machrel */
1679ba2be530Sab196087 			reloc_table,
1680ba2be530Sab196087 
1681ba2be530Sab196087 			ld_init_rel,		/* mr_init_rel */
1682ba2be530Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
1683ba2be530Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1684ba2be530Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1685ba2be530Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1686ba2be530Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
1687ba2be530Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
1688ba2be530Sab196087 			ld_add_outrel,		/* mr_add_outrel */
1689ba2be530Sab196087 			NULL,			/* mr_reloc_register */
1690ba2be530Sab196087 			ld_reloc_local,		/* mr_reloc_local */
1691ba2be530Sab196087 			NULL,			/* mr_reloc_GOTOP */
1692ba2be530Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
1693ba2be530Sab196087 			NULL,			/* mr_assign_got */
169457ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1695ba2be530Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
1696ba2be530Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1697ba2be530Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1698ba2be530Sab196087 			NULL,			/* mr_allocate_got */
1699ba2be530Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1700ba2be530Sab196087 		},
1701ba2be530Sab196087 		{			/* Target_machsym */
1702ba2be530Sab196087 			NULL,			/* ms_reg_check */
1703ba2be530Sab196087 			NULL,			/* ms_mach_sym_typecheck */
1704ba2be530Sab196087 			NULL,			/* ms_is_regsym */
1705ba2be530Sab196087 			NULL,			/* ms_reg_find */
1706ba2be530Sab196087 			NULL			/* ms_reg_enter */
1707ba2be530Sab196087 		}
1708ba2be530Sab196087 	};
1709ba2be530Sab196087 
1710ba2be530Sab196087 	return (&_ld_targ);
1711ba2be530Sab196087 }
1712