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 *
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 *
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
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
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
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
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
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
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
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
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 
438*0bc0887eSRichard Lowe 	if ((orsp->rel_rtype != M_R_NONE) &&
439*0bc0887eSRichard Lowe 	    (orsp->rel_rtype != M_R_RELATIVE)) {
440*0bc0887eSRichard Lowe 		if (ndx == 0) {
441*0bc0887eSRichard Lowe 			Conv_inv_buf_t	inv_buf;
442*0bc0887eSRichard Lowe 			Is_desc *isp = orsp->rel_isdesc;
443*0bc0887eSRichard Lowe 
444*0bc0887eSRichard Lowe 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
445*0bc0887eSRichard Lowe 			    conv_reloc_type(ofl->ofl_nehdr->e_machine,
446*0bc0887eSRichard Lowe 			    orsp->rel_rtype, 0, &inv_buf),
447*0bc0887eSRichard Lowe 			    isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
448*0bc0887eSRichard Lowe 			    isp->is_name, EC_XWORD(roffset));
449*0bc0887eSRichard Lowe 			return (S_ERROR);
450*0bc0887eSRichard Lowe 		}
451*0bc0887eSRichard 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 
464*0bc0887eSRichard Lowe 	relbits = (char *)relosp->os_outdata->d_buf;
465*0bc0887eSRichard 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
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
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 
1014bf994817SAli Bahrami 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
1015bf994817SAli Bahrami 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1016bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1017bf994817SAli Bahrami 			Gotndx	*gnp;
1018bf994817SAli Bahrami 
1019bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1020bf994817SAli Bahrami 			assert(gnp);
1021bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1022bf994817SAli Bahrami 
1023bf994817SAli Bahrami 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1024bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1025bf994817SAli Bahrami 			Gotndx *gnp;
1026bf994817SAli Bahrami 
1027bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1028bf994817SAli Bahrami 			assert(gnp);
1029bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1030bf994817SAli Bahrami 
1031bf994817SAli Bahrami 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1032bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1033bf994817SAli Bahrami 			Xword	tlsstatsize;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			/*
1036bf994817SAli Bahrami 			 * This is the LE TLS reference model.  Static
1037bf994817SAli Bahrami 			 * offset is hard-coded.
10387c478bd9Sstevel@tonic-gate 			 */
1039bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
1040bf994817SAli Bahrami 			    M_TLSSTATALIGN);
1041bf994817SAli Bahrami 			value = tlsstatsize - value;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 			/*
1044bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a negative
1045bf994817SAli Bahrami 			 * offset that can be added to the thread pointer.
10467c478bd9Sstevel@tonic-gate 			 */
1047bf994817SAli Bahrami 			if (arsp->rel_rtype == R_AMD64_TPOFF32)
1048bf994817SAli Bahrami 				value = -value;
1049bf994817SAli Bahrami 		}
10507c478bd9Sstevel@tonic-gate 
1051bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_file)
1052bf994817SAli Bahrami 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1053bf994817SAli Bahrami 		else
1054bf994817SAli Bahrami 			ifl_name = MSG_INTL(MSG_STR_NULL);
1055bf994817SAli Bahrami 
1056bf994817SAli Bahrami 		/*
1057bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1058bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1059bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1060bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1061bf994817SAli Bahrami 		 * End users should never see this.
1062bf994817SAli Bahrami 		 */
1063bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1064bf994817SAli Bahrami 			Conv_inv_buf_t inv_buf;
1065bf994817SAli Bahrami 
10661007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1067bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1068bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
1069bf994817SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
1070bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name);
1071bf994817SAli Bahrami 			return (S_ERROR);
1072bf994817SAli Bahrami 		}
1073bf994817SAli Bahrami 
1074bf994817SAli Bahrami 		/*
1075bf994817SAli Bahrami 		 * Get the address of the data item we need to modify.
1076bf994817SAli Bahrami 		 */
1077bf994817SAli Bahrami 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1078bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1079bf994817SAli Bahrami 
1080bf994817SAli Bahrami 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1081bf994817SAli Bahrami 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1082bf994817SAli Bahrami 		    ld_reloc_sym_name));
1083bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
1084bf994817SAli Bahrami 
1085bf994817SAli Bahrami 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1086bf994817SAli Bahrami 		    ofl->ofl_size) || (arsp->rel_roffset >
1087bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
1088bf994817SAli Bahrami 			int		class;
1089bf994817SAli Bahrami 			Conv_inv_buf_t inv_buf;
1090bf994817SAli Bahrami 
1091bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1092bf994817SAli Bahrami 			    ofl->ofl_size)
1093bf994817SAli Bahrami 				class = ERR_FATAL;
1094bf994817SAli Bahrami 			else
1095bf994817SAli Bahrami 				class = ERR_WARNING;
1096bf994817SAli Bahrami 
10971007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1098bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1099bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1100bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1101bf994817SAli Bahrami 			    EC_ADDR((uintptr_t)addr -
1102bf994817SAli Bahrami 			    (uintptr_t)ofl->ofl_nehdr));
1103bf994817SAli Bahrami 
1104bf994817SAli Bahrami 			if (class == ERR_FATAL) {
1105bf994817SAli Bahrami 				return_code = S_ERROR;
1106bf994817SAli Bahrami 				continue;
11077c478bd9Sstevel@tonic-gate 			}
1108bf994817SAli Bahrami 		}
11097c478bd9Sstevel@tonic-gate 
1110bf994817SAli Bahrami 		/*
1111bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1112bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
1113bf994817SAli Bahrami 		 */
1114bf994817SAli Bahrami 		if (moved)
1115bf994817SAli Bahrami 			value -= *addr;
11167c478bd9Sstevel@tonic-gate 
1117bf994817SAli Bahrami 		/*
1118bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1119bf994817SAli Bahrami 		 */
1120bf994817SAli Bahrami 		if (OFL_DO_RELOC(ofl)) {
11217c478bd9Sstevel@tonic-gate 			/*
1122bf994817SAli Bahrami 			 * If this is a PROGBITS section and the running linker
1123bf994817SAli Bahrami 			 * has a different byte order than the target host,
1124bf994817SAli Bahrami 			 * tell do_reloc_ld() to swap bytes.
11257c478bd9Sstevel@tonic-gate 			 */
1126bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1127bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
11281007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
11291007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
1130bf994817SAli Bahrami 				return_code = S_ERROR;
11311007fd6fSAli Bahrami 			}
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	return (return_code);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
1137ba2be530Sab static uintptr_t
11385aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11397c478bd9Sstevel@tonic-gate {
1140141040e8Srie 	Rel_desc	*orsp;
1141141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
11457c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11467c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
11477c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
11487c478bd9Sstevel@tonic-gate 	 */
1149635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11507c478bd9Sstevel@tonic-gate 		return (1);
11517c478bd9Sstevel@tonic-gate 
1152*0bc0887eSRichard Lowe 	/*
1153*0bc0887eSRichard Lowe 	 * If the symbol will be reduced, we can't leave outstanding
1154*0bc0887eSRichard Lowe 	 * relocations against it, as nothing will ever be able to satisfy them
1155*0bc0887eSRichard Lowe 	 * (and the symbol won't be in .dynsym
1156*0bc0887eSRichard Lowe 	 */
1157*0bc0887eSRichard Lowe 	if ((sdp != NULL) &&
1158*0bc0887eSRichard Lowe 	    (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
1159*0bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_NONE) &&
1160*0bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_RELATIVE)) {
1161*0bc0887eSRichard Lowe 		if (ld_sym_reducable(ofl, sdp))
1162*0bc0887eSRichard Lowe 			return (1);
1163*0bc0887eSRichard Lowe 	}
1164*0bc0887eSRichard Lowe 
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
11677c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
11687c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11717c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
11727c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		/*
11757c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
11767c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
11777c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
11787c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11810bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11827c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11837c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11847c478bd9Sstevel@tonic-gate 			else
11857c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1186141040e8Srie 		} else {
118708278a5eSRod Evans 			Os_desc *osp;
118808278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11897c478bd9Sstevel@tonic-gate 
119008278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
119108278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11927c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11937c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11947c478bd9Sstevel@tonic-gate 			}
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
1198bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1199bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1200bf994817SAli Bahrami 		return (S_ERROR);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
12037c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
12047c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
12057c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
12067c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
12077c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
12087c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
12097c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
12107c478bd9Sstevel@tonic-gate 	else
1211bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
12147c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	/*
12177c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
12187c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
12197c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
12227c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
12237c478bd9Sstevel@tonic-gate 
1224141040e8Srie 	/*
1225141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1226141040e8Srie 	 */
1227141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1228141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1229141040e8Srie 
12307c478bd9Sstevel@tonic-gate 	/*
12317c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
12327c478bd9Sstevel@tonic-gate 	 */
12337c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
12347c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12375aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12387c478bd9Sstevel@tonic-gate 	}
12395aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
12405aefb655Srie 	    M_MACH, orsp));
12417c478bd9Sstevel@tonic-gate 	return (1);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate /*
12457c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
12467c478bd9Sstevel@tonic-gate  */
1247ba2be530Sab static uintptr_t
12485aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12497c478bd9Sstevel@tonic-gate {
12501d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12517c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12520bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12537c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
12577c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
12587c478bd9Sstevel@tonic-gate 	 * then
12597c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
12607c478bd9Sstevel@tonic-gate 	 * fi
12617c478bd9Sstevel@tonic-gate 	 */
12627c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12632926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12647c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12657c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
12667c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12677c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12687c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
12727c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
12737c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
12761d9df23bSab 			return (ld_add_outrel(0, rsp, ofl));
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
12805aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
12817c478bd9Sstevel@tonic-gate 			return (S_ERROR);
12827c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
12837c478bd9Sstevel@tonic-gate 		return (1);
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
1286b3fbe5e6Sseizo 	/*
1287b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1288b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1289b3fbe5e6Sseizo 	 * message.
1290b3fbe5e6Sseizo 	 *
1291b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1292b3fbe5e6Sseizo 	 *	a) it's undefined
1293b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1294b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1295b3fbe5e6Sseizo 	 *
1296b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1297b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1298b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1299b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1300b3fbe5e6Sseizo 	 */
13017c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1302b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1303b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1304b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1305bf994817SAli Bahrami 		Conv_inv_buf_t	inv_buf;
1306bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1307de777a60Sab 
1308b3fbe5e6Sseizo 		/*
1309b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1310b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1311b3fbe5e6Sseizo 		 * can not be resolved.
1312b3fbe5e6Sseizo 		 */
1313bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1314b3fbe5e6Sseizo 			return (0);
13151007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1316de777a60Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
13177c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
1318bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
13197c478bd9Sstevel@tonic-gate 		return (1);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Perform relocation.
13247c478bd9Sstevel@tonic-gate 	 */
13255aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 
1329ba2be530Sab static uintptr_t
13305aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
13337c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
13341d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
13357c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
1338d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1339d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
13407c478bd9Sstevel@tonic-gate 	 */
1341d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13427c478bd9Sstevel@tonic-gate 		/*
1343d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
13447c478bd9Sstevel@tonic-gate 		 */
13457c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13467c478bd9Sstevel@tonic-gate 
1347d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13487c478bd9Sstevel@tonic-gate 			/*
1349d326b23bSrie 			 * Assign a GOT entry for static TLS references.
13507c478bd9Sstevel@tonic-gate 			 */
135157ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135257ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
13537c478bd9Sstevel@tonic-gate 
1354d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1355d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1356d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1357d326b23bSrie 					return (S_ERROR);
1358d326b23bSrie 			}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 			/*
1361d326b23bSrie 			 * IE access model.
13627c478bd9Sstevel@tonic-gate 			 */
13637c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
13645aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 			/*
1367d326b23bSrie 			 * Fixups are required for other executable models.
13687c478bd9Sstevel@tonic-gate 			 */
13695aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13707c478bd9Sstevel@tonic-gate 			    rsp, ofl));
13717c478bd9Sstevel@tonic-gate 		}
1372d326b23bSrie 
13737c478bd9Sstevel@tonic-gate 		/*
1374d326b23bSrie 		 * LE access model.
13757c478bd9Sstevel@tonic-gate 		 */
13767c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
13775aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1378d326b23bSrie 
13795aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13805aefb655Srie 		    rsp, ofl));
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	/*
1384d326b23bSrie 	 * Building a shared object.
1385d326b23bSrie 	 *
1386d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13877c478bd9Sstevel@tonic-gate 	 */
138857ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
138957ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1390d326b23bSrie 
1391d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
139257ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
13937c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1394d326b23bSrie 
13955aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
139657ef7aa9SRod Evans 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
139757ef7aa9SRod Evans 	    ofl, rsp)) == NULL)) {
1398d326b23bSrie 
1399d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1400d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1401d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
14027c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
14065aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14077c478bd9Sstevel@tonic-gate 
14085aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
1412ba2be530Sab static uintptr_t
141357ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
141457ef7aa9SRod Evans     Rel_desc *rsp, Sym_desc *sdp)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	Xword		raddend;
141757ef7aa9SRod Evans 	Gotndx		gn, *gnp;
141857ef7aa9SRod Evans 	Aliste		idx;
14197c478bd9Sstevel@tonic-gate 	uint_t		gotents;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
142257ef7aa9SRod Evans 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
14237c478bd9Sstevel@tonic-gate 		return (1);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14267c478bd9Sstevel@tonic-gate 		gotents = 2;
14277c478bd9Sstevel@tonic-gate 	else
14287c478bd9Sstevel@tonic-gate 		gotents = 1;
14297c478bd9Sstevel@tonic-gate 
143057ef7aa9SRod Evans 	gn.gn_addend = raddend;
143157ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
143257ef7aa9SRod Evans 	gn.gn_gotref = gref;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
143757ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
143857ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
143957ef7aa9SRod Evans 				return (S_ERROR);
144057ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
144157ef7aa9SRod Evans 			ofl->ofl_tlsldgotndx = gnp;
144257ef7aa9SRod Evans 		}
14437c478bd9Sstevel@tonic-gate 		return (1);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
144657ef7aa9SRod Evans 	idx = 0;
144757ef7aa9SRod Evans 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
144857ef7aa9SRod Evans 		if (gnp->gn_addend > raddend)
144957ef7aa9SRod Evans 			break;
14507c478bd9Sstevel@tonic-gate 	}
145157ef7aa9SRod Evans 
145257ef7aa9SRod Evans 	/*
145357ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
145457ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
145557ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
145657ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
145757ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
145857ef7aa9SRod Evans 	 */
145957ef7aa9SRod Evans 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
146057ef7aa9SRod Evans 	    AL_CNT_SDP_GOT, idx) == NULL)
146157ef7aa9SRod Evans 		return (S_ERROR);
146257ef7aa9SRod Evans 
14637c478bd9Sstevel@tonic-gate 	return (1);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
1466ba2be530Sab static void
14675aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14707c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1471141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
1474b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
14757c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
14767c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
14777c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
14787c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
14797c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
14807c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
14817c478bd9Sstevel@tonic-gate };
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate /*
14847c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14857c478bd9Sstevel@tonic-gate  */
1486ba2be530Sab static uintptr_t
1487d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14887c478bd9Sstevel@tonic-gate {
1489ba2be530Sab 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1490ba2be530Sab 
14917c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1492d326b23bSrie 		Sym_desc	*sdp;
14937c478bd9Sstevel@tonic-gate 
14945aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1495635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1496d326b23bSrie 			uchar_t	*genptr;
1497d326b23bSrie 
1498d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14997c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15007c478bd9Sstevel@tonic-gate 			/* LINTED */
15017c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
1502ba2be530Sab 			if (bswap)
1503ba2be530Sab 				/* LINTED */
1504ba2be530Sab 				*(Xword *)genptr =
1505ba2be530Sab 				    /* LINTED */
1506ba2be530Sab 				    ld_bswap_Xword(*(Xword *)genptr);
15077c478bd9Sstevel@tonic-gate 		}
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/*
15117c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
15127c478bd9Sstevel@tonic-gate 	 * entry is:
15137c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
15147c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
15157c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
15167c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
15177c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
15187c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
15197c478bd9Sstevel@tonic-gate 	 */
1520f3324781Sab 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1521d326b23bSrie 		uchar_t	*pltent;
1522d326b23bSrie 		Xword	val1;
15237c478bd9Sstevel@tonic-gate 
1524b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15257c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
15267c478bd9Sstevel@tonic-gate 
1527f3324781Sab 		/*
1528f3324781Sab 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1529f3324781Sab 		 * stage.
1530f3324781Sab 		 */
1531f3324781Sab 		if (!OFL_DO_RELOC(ofl))
1532f3324781Sab 			return (1);
1533f3324781Sab 
15347c478bd9Sstevel@tonic-gate 		/*
15357c478bd9Sstevel@tonic-gate 		 * filin:
15367c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
15377c478bd9Sstevel@tonic-gate 		 *
15387c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
15397c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
15407c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
15417c478bd9Sstevel@tonic-gate 		 */
15427c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1543de777a60Sab 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1544de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
15457c478bd9Sstevel@tonic-gate 
1546bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
1547bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1548bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15491007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
1550f3324781Sab 			return (S_ERROR);
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		/*
15547c478bd9Sstevel@tonic-gate 		 * filin:
15557c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
15567c478bd9Sstevel@tonic-gate 		 */
15577c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1558de777a60Sab 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1559de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1560f3324781Sab 
1561bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
1562bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1563bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
15641007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
1565f3324781Sab 			return (S_ERROR);
15667c478bd9Sstevel@tonic-gate 		}
15677c478bd9Sstevel@tonic-gate 	}
1568f3324781Sab 
15697c478bd9Sstevel@tonic-gate 	return (1);
15707c478bd9Sstevel@tonic-gate }
1571ba2be530Sab 
1572ba2be530Sab 
1573ba2be530Sab 
1574ba2be530Sab /*
1575ba2be530Sab  * Template for generating "void (*)(void)" function
1576ba2be530Sab  */
1577ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1578ba2be530Sab /* 0x00 */	0x55,				/* pushq  %rbp */
1579ba2be530Sab /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1580ba2be530Sab /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1581ba2be530Sab /* 0x07 */	0x5d,				/* popq   %rbp */
1582ba2be530Sab /* 0x08 */	0xc3				/* ret */
1583ba2be530Sab };
1584ba2be530Sab 
1585ba2be530Sab 
15863c573fccSAli Bahrami /*
15873c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15883c573fccSAli Bahrami  *
15893c573fccSAli Bahrami  * entry:
15903c573fccSAli Bahrami  *
15913c573fccSAli Bahrami  *	base - base address of section being filled
15923c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15933c573fccSAli Bahrami  *	cnt - # bytes to be filled
15943c573fccSAli Bahrami  *
15953c573fccSAli Bahrami  * exit:
15963c573fccSAli Bahrami  *	The fill has been completed.
15973c573fccSAli Bahrami  */
15983c573fccSAli Bahrami static void
15993c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
16003c573fccSAli Bahrami {
16013c573fccSAli Bahrami 	/*
16023c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
16033c573fccSAli Bahrami 	 * There are no alignment constraints.
16043c573fccSAli Bahrami 	 */
16053c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
16063c573fccSAli Bahrami }
16073c573fccSAli Bahrami 
16083c573fccSAli Bahrami 
1609ba2be530Sab /*
1610ba2be530Sab  * Return the ld_targ definition for this target.
1611ba2be530Sab  */
1612ba2be530Sab const Target *
1613ba2be530Sab ld_targ_init_x86(void)
1614ba2be530Sab {
1615ba2be530Sab 	static const Target _ld_targ = {
1616ba2be530Sab 		{			/* Target_mach */
1617ba2be530Sab 			M_MACH,			/* m_mach */
1618ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1619ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1620ba2be530Sab 			M_CLASS,		/* m_class */
1621ba2be530Sab 			M_DATA,			/* m_data */
1622ba2be530Sab 
1623ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1624ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1625bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1626ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
162769112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1628ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1629ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1630ba2be530Sab 
1631ba2be530Sab 			/* Relocation type codes */
1632ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1633ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1634ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1635ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1636ba2be530Sab 			M_R_NUM,		/* m_r_num */
1637ba2be530Sab 			M_R_NONE,		/* m_r_none */
1638ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1639ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1640ba2be530Sab 
1641ba2be530Sab 			/* Relocation related constants */
1642ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1643ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1644ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1645ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1646ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1647ba2be530Sab 
1648ba2be530Sab 			/* GOT related constants */
1649ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1650ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1651ba2be530Sab 
1652ba2be530Sab 			/* PLT related constants */
1653ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1654ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1655ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1656ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1657ba2be530Sab 
16587e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16597e16fca0SAli Bahrami 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
16607e16fca0SAli Bahrami 
1661ba2be530Sab 			M_DT_REGISTER,		/* m_dt_register */
1662ba2be530Sab 		},
1663ba2be530Sab 		{			/* Target_machid */
1664ba2be530Sab 			M_ID_ARRAY,		/* id_array */
1665ba2be530Sab 			M_ID_BSS,		/* id_bss */
1666ba2be530Sab 			M_ID_CAP,		/* id_cap */
166708278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
166808278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1669ba2be530Sab 			M_ID_DATA,		/* id_data */
1670ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1671ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1672ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1673ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1674ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1675ba2be530Sab 			M_ID_GOT,		/* id_got */
1676ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1677ba2be530Sab 			M_ID_HASH,		/* id_hash */
1678ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1679ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1680ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1681ba2be530Sab 			M_ID_NOTE,		/* id_note */
1682ba2be530Sab 			M_ID_NULL,		/* id_null */
1683ba2be530Sab 			M_ID_PLT,		/* id_plt */
1684ba2be530Sab 			M_ID_REL,		/* id_rel */
1685ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1686ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1687ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1688ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1689ba2be530Sab 			M_ID_TEXT,		/* id_text */
1690ba2be530Sab 			M_ID_TLS,		/* id_tls */
1691ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1692ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1693ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
16947e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1695ba2be530Sab 			M_ID_USER,		/* id_user */
1696ba2be530Sab 			M_ID_VERSION,		/* id_version */
1697ba2be530Sab 		},
1698ba2be530Sab 		{			/* Target_nullfunc */
1699ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1700ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1701ba2be530Sab 		},
17023c573fccSAli Bahrami 		{			/* Target_fillfunc */
17033c573fccSAli Bahrami 			execfill		/* ff_execfill */
17043c573fccSAli Bahrami 		},
1705ba2be530Sab 		{			/* Target_machrel */
1706ba2be530Sab 			reloc_table,
1707ba2be530Sab 
1708ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1709ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1710ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1711ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1712ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1713ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1714ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1715ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1716ba2be530Sab 			NULL,			/* mr_reloc_register */
1717ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1718ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1719ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1720ba2be530Sab 			NULL,			/* mr_assign_got */
172157ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1722ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1723ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1724ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1725ba2be530Sab 			NULL,			/* mr_allocate_got */
1726ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1727ba2be530Sab 		},
1728ba2be530Sab 		{			/* Target_machsym */
1729ba2be530Sab 			NULL,			/* ms_reg_check */
1730ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1731ba2be530Sab 			NULL,			/* ms_is_regsym */
1732ba2be530Sab 			NULL,			/* ms_reg_find */
1733ba2be530Sab 			NULL			/* ms_reg_enter */
1734ba2be530Sab 		}
1735ba2be530Sab 	};
1736ba2be530Sab 
1737ba2be530Sab 	return (&_ld_targ);
1738ba2be530Sab }
1739