17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21141040e8Srie 
227c478bd9Sstevel@tonic-gate /*
23bf994817SAli Bahrami  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26ba2be530Sab /* Get the x86 version of the relocation engine */
27ba2be530Sab #define	DO_RELOC_LIBLD_X86
28ba2be530Sab 
297c478bd9Sstevel@tonic-gate #include	<string.h>
307c478bd9Sstevel@tonic-gate #include	<stdio.h>
317c478bd9Sstevel@tonic-gate #include	<strings.h>
327c478bd9Sstevel@tonic-gate #include	<sys/elf_amd64.h>
337c478bd9Sstevel@tonic-gate #include	<debug.h>
347c478bd9Sstevel@tonic-gate #include	<reloc.h>
35ba2be530Sab #include	<i386/machdep_x86.h>
365aefb655Srie #include	"msg.h"
375aefb655Srie #include	"_libld.h"
38ba2be530Sab 
39bf994817SAli Bahrami /*
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 
6957ef7aa9SRod Evans /*
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;
7857ef7aa9SRod Evans 
7957ef7aa9SRod Evans 	assert(rdesc != 0);
8057ef7aa9SRod Evans 
8157ef7aa9SRod Evans 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
8257ef7aa9SRod Evans 		return (ofl->ofl_tlsldgotndx);
83ba2be530Sab 
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 }
927c478bd9Sstevel@tonic-gate 
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 }
122ba2be530Sab 
123ba2be530Sab static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)124bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
1257c478bd9Sstevel@tonic-gate {
12657ef7aa9SRod Evans 	Rela	*rel = (Rela *)reloc;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* LINTED */
129ba2be530Sab 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
1307c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
1317c478bd9Sstevel@tonic-gate 	reld->rel_raddend = rel->r_addend;
132bf994817SAli Bahrami 	*typedata = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	reld->rel_flags |= FLG_REL_RELA;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
139ba2be530Sab static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1405aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1417c478bd9Sstevel@tonic-gate {
1425aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
145ba2be530Sab static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1465aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
1517c478bd9Sstevel@tonic-gate 		 */
1527c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
1537c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
157ba2be530Sab static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1585aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1597c478bd9Sstevel@tonic-gate {
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)++;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
170ba2be530Sab static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1715aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	Xword	value;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1767c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1777c478bd9Sstevel@tonic-gate 	return (value);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
1827c478bd9Sstevel@tonic-gate  *	JMP	*name1@GOTPCREL(%rip)
1837c478bd9Sstevel@tonic-gate  *	PUSHL	$index
1847c478bd9Sstevel@tonic-gate  *	JMP	.PLT0
1857c478bd9Sstevel@tonic-gate  */
186b3fbe5e6Sseizo static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
1877c478bd9Sstevel@tonic-gate /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1887c478bd9Sstevel@tonic-gate /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
1897c478bd9Sstevel@tonic-gate /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
1907c478bd9Sstevel@tonic-gate /* 0x10 */
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static uintptr_t
plt_entry(Ofl_desc * ofl,Sym_desc * sdp)1947c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
1957c478bd9Sstevel@tonic-gate {
196b3fbe5e6Sseizo 	uchar_t		*plt0, *pltent, *gotent;
1977c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1987c478bd9Sstevel@tonic-gate 	Word		got_off;
1997c478bd9Sstevel@tonic-gate 	Xword		val1;
200ba2be530Sab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2037c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
2047c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
205b3fbe5e6Sseizo 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
2067c478bd9Sstevel@tonic-gate 	pltent = plt0 + plt_off;
207b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 	/* LINTED */
214b3fbe5e6Sseizo 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
215b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
216ba2be530Sab 	if (bswap)
217ba2be530Sab 		/* LINTED */
218ba2be530Sab 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
2197c478bd9Sstevel@tonic-gate 
220f3324781Sab 	/*
221f3324781Sab 	 * If '-z noreloc' is specified - skip the do_reloc_ld
222f3324781Sab 	 * stage.
223f3324781Sab 	 */
224f3324781Sab 	if (!OFL_DO_RELOC(ofl))
225f3324781Sab 		return (1);
226f3324781Sab 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * patchup:
2297c478bd9Sstevel@tonic-gate 	 *	jmpq	*name1@gotpcrel(%rip)
2307c478bd9Sstevel@tonic-gate 	 *
2317c478bd9Sstevel@tonic-gate 	 * NOTE: 0x06 represents next instruction.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
234de777a60Sab 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
2357c478bd9Sstevel@tonic-gate 
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),
240f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
241f3324781Sab 		return (S_ERROR);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * patchup:
2467c478bd9Sstevel@tonic-gate 	 *	pushq	$pltndx
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
249f3324781Sab 
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),
254f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
255f3324781Sab 		return (S_ERROR);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * patchup:
2607c478bd9Sstevel@tonic-gate 	 *	jmpq	.plt0(%rip)
261f3324781Sab 	 * NOTE: 0x10 represents next instruction. The rather complex
262f3324781Sab 	 * series of casts is necessary to sign extend an offset into
263f3324781Sab 	 * a 64-bit value while satisfying various compiler error
264f3324781Sab 	 * checks.  Handle with care.
2657c478bd9Sstevel@tonic-gate 	 */
266b3fbe5e6Sseizo 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
267b3fbe5e6Sseizo 	    (uintptr_t)(&pltent[0x10])));
268b3fbe5e6Sseizo 
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),
273f3324781Sab 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
274f3324781Sab 		return (S_ERROR);
2757c478bd9Sstevel@tonic-gate 	}
276f3324781Sab 
2777c478bd9Sstevel@tonic-gate 	return (1);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
280ba2be530Sab 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)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
2847c478bd9Sstevel@tonic-gate 	Word		ndx;
2857c478bd9Sstevel@tonic-gate 	Xword		roffset, value;
2867c478bd9Sstevel@tonic-gate 	Sxword		raddend;
2877c478bd9Sstevel@tonic-gate 	Rela		rea;
2887c478bd9Sstevel@tonic-gate 	char		*relbits;
2897c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2907c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	raddend = orsp->rel_raddend;
2937c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
2977c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
3007c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
3017c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
3025aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
3037c478bd9Sstevel@tonic-gate 		return (1);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
3087c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
3097c478bd9Sstevel@tonic-gate 	 */
310bf994817SAli Bahrami 	if (RELAUX_GET_MOVE(orsp))
3115aefb655Srie 		ld_adj_movereloc(ofl, orsp);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section then we need to adjust the
3157c478bd9Sstevel@tonic-gate 	 * raddend field to compensate for the new position of the input section
3167c478bd9Sstevel@tonic-gate 	 * within the new output section.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
31957ef7aa9SRod Evans 		if (ofl->ofl_parsyms &&
3207c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
3217c478bd9Sstevel@tonic-gate 		    /* LINTED */
3225aefb655Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
3235aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
3247c478bd9Sstevel@tonic-gate 			sectmoved = 1;
3257c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
3267c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value;
3277c478bd9Sstevel@tonic-gate 			else
3287c478bd9Sstevel@tonic-gate 				raddend = psym->sd_sym->st_value -
3297c478bd9Sstevel@tonic-gate 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3307c478bd9Sstevel@tonic-gate 			/* LINTED */
3317c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
3327c478bd9Sstevel@tonic-gate 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3337c478bd9Sstevel@tonic-gate 				raddend +=
334de777a60Sab 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3357c478bd9Sstevel@tonic-gate 		} else {
3367c478bd9Sstevel@tonic-gate 			/* LINTED */
3377c478bd9Sstevel@tonic-gate 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
3387c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3397c478bd9Sstevel@tonic-gate 				raddend +=
340de777a60Sab 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * Note: for GOT relative relocations on amd64
3497c478bd9Sstevel@tonic-gate 		 *	 we discard the addend.  It was relevant
3507c478bd9Sstevel@tonic-gate 		 *	 to the reference - not to the data item
3517c478bd9Sstevel@tonic-gate 		 *	 being referenced (ie: that -4 thing).
3527c478bd9Sstevel@tonic-gate 		 */
3537c478bd9Sstevel@tonic-gate 		raddend = 0;
3547c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3555aefb655Srie 		roffset = ld_calc_got_offset(orsp, ofl);
3565aefb655Srie 
3577c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
3587c478bd9Sstevel@tonic-gate 		/*
3597c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
3607c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
3637c478bd9Sstevel@tonic-gate 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
3647c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
3657c478bd9Sstevel@tonic-gate 		raddend = 0;
3667c478bd9Sstevel@tonic-gate 		if (plt_entry(ofl, sdp) == S_ERROR)
3677c478bd9Sstevel@tonic-gate 			return (S_ERROR);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
3707c478bd9Sstevel@tonic-gate 		/*
3717c478bd9Sstevel@tonic-gate 		 * This must be a R_AMD64_COPY.  For these set the roffset to
3727c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
3737c478bd9Sstevel@tonic-gate 		 */
3747c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
3757c478bd9Sstevel@tonic-gate 		roffset = value;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		/*
3787c478bd9Sstevel@tonic-gate 		 * The raddend doesn't mean anything in a R_SPARC_COPY
3797c478bd9Sstevel@tonic-gate 		 * relocation.  Null it out because it can confuse people.
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		raddend = 0;
3827c478bd9Sstevel@tonic-gate 	} else {
383bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(orsp);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
3877c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
3887c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
3897c478bd9Sstevel@tonic-gate 		 */
3907c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
3917c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3927c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3937c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
3947c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3987c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
4027c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
4037c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
4047c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
4077c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
4087c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
4097c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
4107c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
4117c478bd9Sstevel@tonic-gate 			/*
4127c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
4137c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
4147c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
4157c478bd9Sstevel@tonic-gate 			 */
41657ef7aa9SRod Evans 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
41757ef7aa9SRod Evans 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
4187c478bd9Sstevel@tonic-gate 			else
4197c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
4207c478bd9Sstevel@tonic-gate 		} else
42135450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
4227c478bd9Sstevel@tonic-gate 	} else
4237c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Add the symbols 'value' to the addend field.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_ADVAL)
4297c478bd9Sstevel@tonic-gate 		raddend += value;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327010c12aSrie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
4337010c12aSrie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
4367c478bd9Sstevel@tonic-gate 		raddend = 0;
4377c478bd9Sstevel@tonic-gate 
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 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4547c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
4557c478bd9Sstevel@tonic-gate 	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)));
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4637c478bd9Sstevel@tonic-gate 
4640bc0887eSRichard Lowe 	relbits = (char *)relosp->os_outdata->d_buf;
4650bc0887eSRichard Lowe 
4667c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
4677c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rela));
4687c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
4727c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
4737c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4747c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4777c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
4787c478bd9Sstevel@tonic-gate 
4791007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
4807c478bd9Sstevel@tonic-gate 	return (1);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * amd64 Instructions for TLS processing
4857c478bd9Sstevel@tonic-gate  */
486b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4917c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 *	0x09 addq x@gottpoff(%rip), %rax
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	0x48, 0x03, 0x05, 0x00, 0x00,
4967c478bd9Sstevel@tonic-gate 	0x00, 0x00
4977c478bd9Sstevel@tonic-gate };
4987c478bd9Sstevel@tonic-gate 
499b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
5047c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
5057c478bd9Sstevel@tonic-gate 	/*
5067c478bd9Sstevel@tonic-gate 	 *	0x09 leaq x@gottpoff(%rip), %rax
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	0x48, 0x8d, 0x80, 0x00, 0x00,
5097c478bd9Sstevel@tonic-gate 	0x00, 0x00
5107c478bd9Sstevel@tonic-gate };
5117c478bd9Sstevel@tonic-gate 
512b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = {
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	0x66,
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	0x66,
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	0x66,
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * movq %fs:0, %rax
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
5297c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00
5307c478bd9Sstevel@tonic-gate };
5317c478bd9Sstevel@tonic-gate 
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 */
5497c478bd9Sstevel@tonic-gate 
5505aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)5515aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
5547c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
555b3fbe5e6Sseizo 	uchar_t		*offset;
5567c478bd9Sstevel@tonic-gate 
557b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
558b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
559bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
5607c478bd9Sstevel@tonic-gate 
56149f9b365SRichard Lowe 	/*
56249f9b365SRichard Lowe 	 * Note that in certain of the original insn sequences below, the
56349f9b365SRichard Lowe 	 * instructions are not necessarily adjacent
56449f9b365SRichard Lowe 	 */
5657c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * IE reference model
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		switch (rtype) {
5707c478bd9Sstevel@tonic-gate 		case R_AMD64_TLSGD:
5717c478bd9Sstevel@tonic-gate 			/*
5727c478bd9Sstevel@tonic-gate 			 *  GD -> IE
5737c478bd9Sstevel@tonic-gate 			 *
5747c478bd9Sstevel@tonic-gate 			 * Transition:
5757c478bd9Sstevel@tonic-gate 			 *	0x00 .byte 0x66
5767c478bd9Sstevel@tonic-gate 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5777c478bd9Sstevel@tonic-gate 			 *	0x08 .word 0x6666
5787c478bd9Sstevel@tonic-gate 			 *	0x0a rex64
5797c478bd9Sstevel@tonic-gate 			 *	0x0b call __tls_get_addr@plt
5807c478bd9Sstevel@tonic-gate 			 *	0x10
5817c478bd9Sstevel@tonic-gate 			 * To:
5827c478bd9Sstevel@tonic-gate 			 *	0x00 movq %fs:0, %rax
5837c478bd9Sstevel@tonic-gate 			 *	0x09 addq x@gottpoff(%rip), %rax
5847c478bd9Sstevel@tonic-gate 			 *	0x10
5857c478bd9Sstevel@tonic-gate 			 */
5865aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
587bf994817SAli Bahrami 			    R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
5887c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5897c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
5907c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
5915aefb655Srie 
5927c478bd9Sstevel@tonic-gate 			/*
593051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
5947c478bd9Sstevel@tonic-gate 			 * sequence.
5957c478bd9Sstevel@tonic-gate 			 */
5967c478bd9Sstevel@tonic-gate 			offset -= 4;
5977c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
5985aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5997c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6005aefb655Srie 
6017c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
6027c478bd9Sstevel@tonic-gate 			/*
603051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
6047c478bd9Sstevel@tonic-gate 			 */
6055aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
606bf994817SAli Bahrami 			    R_AMD64_NONE, arsp, ld_reloc_sym_name));
6077c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * LE reference model
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	switch (rtype) {
6157c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
6167c478bd9Sstevel@tonic-gate 		/*
6177c478bd9Sstevel@tonic-gate 		 * GD -> LE
6187c478bd9Sstevel@tonic-gate 		 *
6197c478bd9Sstevel@tonic-gate 		 * Transition:
6207c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
6217c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
6227c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
6237c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
6247c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
6257c478bd9Sstevel@tonic-gate 		 *	0x10
6267c478bd9Sstevel@tonic-gate 		 * To:
6277c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
6287c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
6297c478bd9Sstevel@tonic-gate 		 *	0x10
6307c478bd9Sstevel@tonic-gate 		 */
6315aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
632bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6337c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6347c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
6357c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6365aefb655Srie 
6377c478bd9Sstevel@tonic-gate 		/*
638051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
6397c478bd9Sstevel@tonic-gate 		 */
6407c478bd9Sstevel@tonic-gate 		offset -= 4;
6415aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6427c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6435aefb655Srie 
64449f9b365SRichard Lowe 	case R_AMD64_GOTTPOFF: {
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * IE -> LE
6477c478bd9Sstevel@tonic-gate 		 *
64849f9b365SRichard Lowe 		 * Transition 1:
64949f9b365SRichard Lowe 		 *	movq %fs:0, %reg
65049f9b365SRichard Lowe 		 *	addq x@gottpoff(%rip), %reg
6517c478bd9Sstevel@tonic-gate 		 * 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
6687c478bd9Sstevel@tonic-gate 		 */
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));
6787c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6797c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6805aefb655Srie 
6817c478bd9Sstevel@tonic-gate 		/*
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
6857c478bd9Sstevel@tonic-gate 		 */
68649f9b365SRichard Lowe 		if ((offset[1] == INSN_MOVMR) ||
68749f9b365SRichard Lowe 		    ((offset[1] == INSN_ADDMR) && (reg == REG_ESP))) {
68849f9b365SRichard Lowe 			/*
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.
69349f9b365SRichard Lowe 			 */
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;
7005aefb655Srie 
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 	}
7267c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
7277c478bd9Sstevel@tonic-gate 		/*
7287c478bd9Sstevel@tonic-gate 		 * LD -> LE
7297c478bd9Sstevel@tonic-gate 		 *
7307c478bd9Sstevel@tonic-gate 		 * Transition
7317c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
7327c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
7337c478bd9Sstevel@tonic-gate 		 *	0x0c
7347c478bd9Sstevel@tonic-gate 		 * To:
7357c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
7367c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
7377c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
7387c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
7397c478bd9Sstevel@tonic-gate 		 */
740051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
741bf994817SAli Bahrami 		    R_AMD64_NONE, arsp, ld_reloc_sym_name));
7427c478bd9Sstevel@tonic-gate 		offset -= 3;
7435aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
7447c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
7455aefb655Srie 
7467c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
7477c478bd9Sstevel@tonic-gate 		/*
7487c478bd9Sstevel@tonic-gate 		 * LD->LE
7497c478bd9Sstevel@tonic-gate 		 *
7507c478bd9Sstevel@tonic-gate 		 * Transition:
7517c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
7527c478bd9Sstevel@tonic-gate 		 * To:
7537c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
7547c478bd9Sstevel@tonic-gate 		 */
755051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
756bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
7577c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
7587c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
764ba2be530Sab static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7655aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7667c478bd9Sstevel@tonic-gate {
767141040e8Srie 	Rel_desc	*arsp;
768bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
76957ef7aa9SRod Evans 	Aliste		idx;
7707c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
7711d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
7727c478bd9Sstevel@tonic-gate 
773bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7747010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7757010c12aSrie 
7767c478bd9Sstevel@tonic-gate 	/*
777141040e8Srie 	 * Process active relocations.
7787c478bd9Sstevel@tonic-gate 	 */
779bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
780bf994817SAli Bahrami 		uchar_t		*addr;
7813c30f56dSRichard Lowe 		Xword		value;
782bf994817SAli Bahrami 		Sym_desc	*sdp;
783bf994817SAli Bahrami 		const char	*ifl_name;
784bf994817SAli Bahrami 		Xword		refaddr;
785bf994817SAli Bahrami 		int		moved = 0;
786bf994817SAli Bahrami 		Gotref		gref;
787bf994817SAli Bahrami 		Os_desc		*osp;
7887c478bd9Sstevel@tonic-gate 
789bf994817SAli Bahrami 		/*
790bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
791bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
792bf994817SAli Bahrami 		 */
793bf994817SAli Bahrami 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
794bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
795bf994817SAli Bahrami 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
796bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
797bf994817SAli Bahrami 			continue;
798bf994817SAli Bahrami 		}
7997c478bd9Sstevel@tonic-gate 
800bf994817SAli Bahrami 		/*
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.
804bf994817SAli Bahrami 		 *
805bf994817SAli Bahrami 		 * The got table entries have already been assigned,
806bf994817SAli Bahrami 		 * and we bind to those initial entries.
807bf994817SAli Bahrami 		 */
808bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_DTLS)
809bf994817SAli Bahrami 			gref = GOT_REF_TLSGD;
810bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_MTLS)
811bf994817SAli Bahrami 			gref = GOT_REF_TLSLD;
812bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_STLS)
813bf994817SAli Bahrami 			gref = GOT_REF_TLSIE;
814bf994817SAli Bahrami 		else
815bf994817SAli Bahrami 			gref = GOT_REF_GENERIC;
8167c478bd9Sstevel@tonic-gate 
817bf994817SAli Bahrami 		/*
818bf994817SAli Bahrami 		 * Perform any required TLS fixups.
819bf994817SAli Bahrami 		 */
820bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
821bf994817SAli Bahrami 			Fixupret	ret;
822141040e8Srie 
823bf994817SAli Bahrami 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
824bf994817SAli Bahrami 				return (S_ERROR);
825bf994817SAli Bahrami 			if (ret == FIX_DONE)
826bf994817SAli Bahrami 				continue;
827bf994817SAli Bahrami 		}
8287c478bd9Sstevel@tonic-gate 
829bf994817SAli Bahrami 		/*
830bf994817SAli Bahrami 		 * If this is a relocation against a move table, or
831bf994817SAli Bahrami 		 * expanded move table, adjust the relocation entries.
832bf994817SAli Bahrami 		 */
833bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
834bf994817SAli Bahrami 			ld_adj_movereloc(ofl, arsp);
8357c478bd9Sstevel@tonic-gate 
836bf994817SAli Bahrami 		sdp = arsp->rel_sym;
837bf994817SAli Bahrami 		refaddr = arsp->rel_roffset +
838bf994817SAli Bahrami 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
8397c478bd9Sstevel@tonic-gate 
840bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
841bf994817SAli Bahrami 		    (arsp->rel_flags & FLG_REL_GOTCL))
842bf994817SAli Bahrami 			value = 0;
843bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
844bf994817SAli Bahrami 			Sym_desc	*sym;
8457c478bd9Sstevel@tonic-gate 
846bf994817SAli Bahrami 			/*
847bf994817SAli Bahrami 			 * The value for a symbol pointing to a SECTION
848bf994817SAli Bahrami 			 * is based off of that sections position.
849bf994817SAli Bahrami 			 */
850bf994817SAli Bahrami 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
851bf994817SAli Bahrami 			    /* LINTED */
852bf994817SAli Bahrami 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
8537c478bd9Sstevel@tonic-gate 				/*
854bf994817SAli Bahrami 				 * The symbol was moved, so adjust the value
855bf994817SAli Bahrami 				 * relative to the new section.
8562926dd2eSrie 				 */
857bf994817SAli Bahrami 				value = sym->sd_sym->st_value;
858bf994817SAli Bahrami 				moved = 1;
85908278a5eSRod Evans 
8607c478bd9Sstevel@tonic-gate 				/*
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.
8697c478bd9Sstevel@tonic-gate 				 */
870bf994817SAli Bahrami 				arsp->rel_raddend -= sym->sd_osym->st_value;
871bf994817SAli Bahrami 			} 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->
875bf994817SAli Bahrami 					    os_shdr->sh_addr;
876bf994817SAli Bahrami 			}
877bf994817SAli Bahrami 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
878bf994817SAli Bahrami 				value -= ofl->ofl_tlsphdr->p_vaddr;
8797c478bd9Sstevel@tonic-gate 
880bf994817SAli Bahrami 		} else if (IS_SIZE(arsp->rel_rtype)) {
8817c478bd9Sstevel@tonic-gate 			/*
882bf994817SAli Bahrami 			 * Size relocations require the symbols size.
8837c478bd9Sstevel@tonic-gate 			 */
884bf994817SAli Bahrami 			value = sdp->sd_sym->st_size;
8857c478bd9Sstevel@tonic-gate 
886bf994817SAli Bahrami 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
887bf994817SAli Bahrami 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
8887c478bd9Sstevel@tonic-gate 			/*
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.
8937c478bd9Sstevel@tonic-gate 			 */
894bf994817SAli Bahrami 			value = ld_calc_plt_addr(sdp, ofl);
895bf994817SAli Bahrami 		} else
896bf994817SAli Bahrami 			value = sdp->sd_sym->st_value;
8977c478bd9Sstevel@tonic-gate 
898bf994817SAli Bahrami 		/*
899bf994817SAli Bahrami 		 * Relocation against the GLOBAL_OFFSET_TABLE.
900bf994817SAli Bahrami 		 */
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);
9057c478bd9Sstevel@tonic-gate 
906bf994817SAli Bahrami 		/*
907bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
908bf994817SAli Bahrami 		 * sections virtual address to the reference address.
909bf994817SAli Bahrami 		 */
910bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
911bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0))
912bf994817SAli Bahrami 			refaddr += arsp->rel_isdesc->is_osdesc->
913bf994817SAli Bahrami 			    os_shdr->sh_addr;
9147c478bd9Sstevel@tonic-gate 
915bf994817SAli Bahrami 		/*
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).
918bf994817SAli Bahrami 		 */
919bf994817SAli Bahrami 		if (IS_PLT(arsp->rel_rtype)) {
920bf994817SAli Bahrami 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
921bf994817SAli Bahrami 				value = ld_calc_plt_addr(sdp, ofl);
922bf994817SAli Bahrami 		}
9237c478bd9Sstevel@tonic-gate 
924bf994817SAli Bahrami 		/*
925bf994817SAli Bahrami 		 * Add relocations addend to value.  Add extra
926bf994817SAli Bahrami 		 * relocation addend if needed.
927bf994817SAli Bahrami 		 *
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).
931bf994817SAli Bahrami 		 */
932bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) == 0)
933bf994817SAli Bahrami 			value += arsp->rel_raddend;
9347c478bd9Sstevel@tonic-gate 
935bf994817SAli Bahrami 		/*
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.
942bf994817SAli Bahrami 		 */
943bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
944bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
945bf994817SAli Bahrami 			Xword		R1addr;
946bf994817SAli Bahrami 			uintptr_t	R2addr;
947bf994817SAli Bahrami 			Word		gotndx;
948bf994817SAli Bahrami 			Gotndx		*gnp;
9497c478bd9Sstevel@tonic-gate 
950bf994817SAli Bahrami 			/*
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
954bf994817SAli Bahrami 			 *
955bf994817SAli Bahrami 			 * Calculate offset into GOT at which to apply
956bf994817SAli Bahrami 			 * the relocation.
957bf994817SAli Bahrami 			 */
958bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
959bf994817SAli Bahrami 			assert(gnp);
9607c478bd9Sstevel@tonic-gate 
961bf994817SAli Bahrami 			if (arsp->rel_rtype == R_AMD64_DTPOFF64)
962bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx + 1;
963bf994817SAli Bahrami 			else
964bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx;
9657c478bd9Sstevel@tonic-gate 
966bf994817SAli Bahrami 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9677c478bd9Sstevel@tonic-gate 
968bf994817SAli Bahrami 			/*
969bf994817SAli Bahrami 			 * Add the GOTs data's offset.
970bf994817SAli Bahrami 			 */
971bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
972141040e8Srie 
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));
976141040e8Srie 
977bf994817SAli Bahrami 			/*
978bf994817SAli Bahrami 			 * And do it.
979bf994817SAli Bahrami 			 */
980bf994817SAli Bahrami 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
981bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
982bf994817SAli Bahrami 			else
983bf994817SAli Bahrami 				*(Xword *)R2addr = value;
984bf994817SAli Bahrami 			continue;
985141040e8Srie 
986bf994817SAli Bahrami 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
987bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
988bf994817SAli Bahrami 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
989141040e8Srie 
990bf994817SAli Bahrami 		} else if (IS_GOTPCREL(arsp->rel_rtype) &&
991bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
992bf994817SAli Bahrami 			Gotndx *gnp;
9937c478bd9Sstevel@tonic-gate 
994bf994817SAli Bahrami 			/*
995bf994817SAli Bahrami 			 * Calculation:
996bf994817SAli Bahrami 			 *	G + GOT + A - P
997bf994817SAli Bahrami 			 */
998bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
999bf994817SAli Bahrami 			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;
1003bf994817SAli Bahrami 
1004bf994817SAli Bahrami 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
1005bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1006bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr->
1007bf994817SAli Bahrami 			    sh_addr) - refaddr + arsp->rel_raddend;
1008bf994817SAli Bahrami 
1009bf994817SAli Bahrami 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
1010bf994817SAli Bahrami 		    (((flags & FLG_OF_RELOBJ) == 0) ||
1011bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
1012bf994817SAli Bahrami 			value -= refaddr;
1013bf994817SAli Bahrami 
1014