17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21141040e8Srie 
227c478bd9Sstevel@tonic-gate /*
23*bf994817SAli 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 
39*bf994817SAli Bahrami /*
40*bf994817SAli Bahrami  * This module uses do_reloc_ld() to execute several synthesized relocations.
41*bf994817SAli Bahrami  * That function expects to be passed two things that we need to construct
42*bf994817SAli Bahrami  * here:
43*bf994817SAli Bahrami  *
44*bf994817SAli Bahrami  * 1)	A Rel_desc descriptor for each relocation type, from which the
45*bf994817SAli Bahrami  *	rel_rtype field, and nothing else, is obtained. This is easily
46*bf994817SAli Bahrami  *	handled by constructing the necessary descriptors.
47*bf994817SAli Bahrami  *
48*bf994817SAli Bahrami  * 2)	A function, which called with the Rel_desc descriptor, returns
49*bf994817SAli Bahrami  *	a string representing the name of the symbol associated with
50*bf994817SAli Bahrami  *	the descriptor. The usual function for this is ld_reloc_sym_name().
51*bf994817SAli Bahrami  *	However, that function will not work in this case, as these synthetic
52*bf994817SAli Bahrami  *	relocations do not have an associated symbol. We supply the
53*bf994817SAli Bahrami  *	syn_rdesc_sym_name() function to simply return the fixed name.
54*bf994817SAli Bahrami  */
55*bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_gotpcrel = {
56*bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_GOTPCREL };
57*bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_32 = {
58*bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_32 };
59*bf994817SAli Bahrami static Rel_desc rdesc_r_amd64_pc32 = {
60*bf994817SAli Bahrami     NULL, NULL, NULL, 0, 0, 0, R_AMD64_PC32 };
61*bf994817SAli Bahrami 
62*bf994817SAli Bahrami /*ARGSUSED*/
63*bf994817SAli Bahrami static const char *
64*bf994817SAli Bahrami syn_rdesc_sym_name(Rel_desc *rdesc)
65*bf994817SAli Bahrami {
66*bf994817SAli Bahrami 	return (MSG_ORIG(MSG_SYM_PLTENT));
67*bf994817SAli Bahrami }
68*bf994817SAli 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
124*bf994817SAli 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;
132*bf994817SAli 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 
236*bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02], &val1,
237*bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
238*bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
239f3324781Sab 		eprintf(ofl->ofl_lml, 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 
250*bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_32, &pltent[0x07], &val1,
251*bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
252*bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
253f3324781Sab 		eprintf(ofl->ofl_lml, 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 
269*bf994817SAli Bahrami 	if (do_reloc_ld(&rdesc_r_amd64_pc32, &pltent[0x0c], &val1,
270*bf994817SAli Bahrami 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
271*bf994817SAli Bahrami 	    ofl->ofl_lml) == 0) {
272f3324781Sab 		eprintf(ofl->ofl_lml, 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
2815aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
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 	 */
310*bf994817SAli 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 {
383*bf994817SAli 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 
4387c478bd9Sstevel@tonic-gate 	relbits = (char *)relosp->os_outdata->d_buf;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4417c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
4427c478bd9Sstevel@tonic-gate 	rea.r_addend = raddend;
4435aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
444*bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
4527c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rela));
4537c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
4577c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
4587c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4597c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
4607c478bd9Sstevel@tonic-gate 	 */
4617c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4627c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
4637c478bd9Sstevel@tonic-gate 
4645aefb655Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
4657c478bd9Sstevel@tonic-gate 	return (1);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * amd64 Instructions for TLS processing
4707c478bd9Sstevel@tonic-gate  */
471b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4767c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 *	0x09 addq x@gottpoff(%rip), %rax
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	0x48, 0x03, 0x05, 0x00, 0x00,
4817c478bd9Sstevel@tonic-gate 	0x00, 0x00
4827c478bd9Sstevel@tonic-gate };
4837c478bd9Sstevel@tonic-gate 
484b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 *	0x00 movq %fs:0, %rax
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
4897c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00,
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 *	0x09 leaq x@gottpoff(%rip), %rax
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	0x48, 0x8d, 0x80, 0x00, 0x00,
4947c478bd9Sstevel@tonic-gate 	0x00, 0x00
4957c478bd9Sstevel@tonic-gate };
4967c478bd9Sstevel@tonic-gate 
497b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = {
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	0x66,
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	0x66,
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * .byte 0x66
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 	0x66,
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * movq %fs:0, %rax
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	0x64, 0x48, 0x8b, 0x04, 0x25,
5147c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00, 0x00
5157c478bd9Sstevel@tonic-gate };
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 
5185aefb655Srie static Fixupret
5195aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
5227c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
523b3fbe5e6Sseizo 	uchar_t		*offset;
5247c478bd9Sstevel@tonic-gate 
525b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
526b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
527*bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * IE reference model
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		switch (rtype) {
5347c478bd9Sstevel@tonic-gate 		case R_AMD64_TLSGD:
5357c478bd9Sstevel@tonic-gate 			/*
5367c478bd9Sstevel@tonic-gate 			 *  GD -> IE
5377c478bd9Sstevel@tonic-gate 			 *
5387c478bd9Sstevel@tonic-gate 			 * Transition:
5397c478bd9Sstevel@tonic-gate 			 *	0x00 .byte 0x66
5407c478bd9Sstevel@tonic-gate 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5417c478bd9Sstevel@tonic-gate 			 *	0x08 .word 0x6666
5427c478bd9Sstevel@tonic-gate 			 *	0x0a rex64
5437c478bd9Sstevel@tonic-gate 			 *	0x0b call __tls_get_addr@plt
5447c478bd9Sstevel@tonic-gate 			 *	0x10
5457c478bd9Sstevel@tonic-gate 			 * To:
5467c478bd9Sstevel@tonic-gate 			 *	0x00 movq %fs:0, %rax
5477c478bd9Sstevel@tonic-gate 			 *	0x09 addq x@gottpoff(%rip), %rax
5487c478bd9Sstevel@tonic-gate 			 *	0x10
5497c478bd9Sstevel@tonic-gate 			 */
5505aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
551*bf994817SAli Bahrami 			    R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
5527c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5537c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 8;
5547c478bd9Sstevel@tonic-gate 			arsp->rel_raddend = (Sxword)-4;
5555aefb655Srie 
5567c478bd9Sstevel@tonic-gate 			/*
557051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
5587c478bd9Sstevel@tonic-gate 			 * sequence.
5597c478bd9Sstevel@tonic-gate 			 */
5607c478bd9Sstevel@tonic-gate 			offset -= 4;
5617c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
5625aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5637c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5645aefb655Srie 
5657c478bd9Sstevel@tonic-gate 		case R_AMD64_PLT32:
5667c478bd9Sstevel@tonic-gate 			/*
567051d39bbSrie 			 * Fixup done via the TLS_GD relocation.
5687c478bd9Sstevel@tonic-gate 			 */
5695aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
570*bf994817SAli Bahrami 			    R_AMD64_NONE, arsp, ld_reloc_sym_name));
5717c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * LE reference model
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	switch (rtype) {
5797c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSGD:
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * GD -> LE
5827c478bd9Sstevel@tonic-gate 		 *
5837c478bd9Sstevel@tonic-gate 		 * Transition:
5847c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
5857c478bd9Sstevel@tonic-gate 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5867c478bd9Sstevel@tonic-gate 		 *	0x08 .word 0x6666
5877c478bd9Sstevel@tonic-gate 		 *	0x0a rex64
5887c478bd9Sstevel@tonic-gate 		 *	0x0b call __tls_get_addr@plt
5897c478bd9Sstevel@tonic-gate 		 *	0x10
5907c478bd9Sstevel@tonic-gate 		 * To:
5917c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
5927c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
5937c478bd9Sstevel@tonic-gate 		 *	0x10
5947c478bd9Sstevel@tonic-gate 		 */
5955aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
596*bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
5977c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
5987c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 8;
5997c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6005aefb655Srie 
6017c478bd9Sstevel@tonic-gate 		/*
602051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		offset -= 4;
6055aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6067c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6075aefb655Srie 
6087c478bd9Sstevel@tonic-gate 	case R_AMD64_GOTTPOFF:
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * IE -> LE
6117c478bd9Sstevel@tonic-gate 		 *
6127c478bd9Sstevel@tonic-gate 		 * Transition:
6137c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
6147c478bd9Sstevel@tonic-gate 		 *	0x09 addq x@gottopoff(%rip), %rax
6157c478bd9Sstevel@tonic-gate 		 *	0x10
6167c478bd9Sstevel@tonic-gate 		 * To:
6177c478bd9Sstevel@tonic-gate 		 *	0x00 movq %fs:0, %rax
6187c478bd9Sstevel@tonic-gate 		 *	0x09 leaq x@tpoff(%rax), %rax
6197c478bd9Sstevel@tonic-gate 		 *	0x10
6207c478bd9Sstevel@tonic-gate 		 */
621051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
622*bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6237c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6247c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6255aefb655Srie 
6267c478bd9Sstevel@tonic-gate 		/*
627051d39bbSrie 		 * Adjust 'offset' to beginning of instruction sequence.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		offset -= 12;
6305aefb655Srie 
6317c478bd9Sstevel@tonic-gate 		/*
632051d39bbSrie 		 * Same code sequence used in the GD -> LE transition.
6337c478bd9Sstevel@tonic-gate 		 */
6345aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6357c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6365aefb655Srie 
6377c478bd9Sstevel@tonic-gate 	case R_AMD64_TLSLD:
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * LD -> LE
6407c478bd9Sstevel@tonic-gate 		 *
6417c478bd9Sstevel@tonic-gate 		 * Transition
6427c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
6437c478bd9Sstevel@tonic-gate 		 *	0x07 call __tls_get_addr@plt
6447c478bd9Sstevel@tonic-gate 		 *	0x0c
6457c478bd9Sstevel@tonic-gate 		 * To:
6467c478bd9Sstevel@tonic-gate 		 *	0x00 .byte 0x66
6477c478bd9Sstevel@tonic-gate 		 *	0x01 .byte 0x66
6487c478bd9Sstevel@tonic-gate 		 *	0x02 .byte 0x66
6497c478bd9Sstevel@tonic-gate 		 *	0x03 movq %fs:0, %rax
6507c478bd9Sstevel@tonic-gate 		 */
651051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
652*bf994817SAli Bahrami 		    R_AMD64_NONE, arsp, ld_reloc_sym_name));
6537c478bd9Sstevel@tonic-gate 		offset -= 3;
6545aefb655Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
6557c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
6565aefb655Srie 
6577c478bd9Sstevel@tonic-gate 	case R_AMD64_DTPOFF32:
6587c478bd9Sstevel@tonic-gate 		/*
6597c478bd9Sstevel@tonic-gate 		 * LD->LE
6607c478bd9Sstevel@tonic-gate 		 *
6617c478bd9Sstevel@tonic-gate 		 * Transition:
6627c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
6637c478bd9Sstevel@tonic-gate 		 * To:
6647c478bd9Sstevel@tonic-gate 		 *	0x00 leaq x1@tpoff(%rax), %rcx
6657c478bd9Sstevel@tonic-gate 		 */
666051d39bbSrie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
667*bf994817SAli Bahrami 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6687c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_AMD64_TPOFF32;
6697c478bd9Sstevel@tonic-gate 		arsp->rel_raddend = 0;
6707c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
676ba2be530Sab static uintptr_t
6775aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
6787c478bd9Sstevel@tonic-gate {
679141040e8Srie 	Rel_desc	*arsp;
680*bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
68157ef7aa9SRod Evans 	Aliste		idx;
6827c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
6831d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
6847c478bd9Sstevel@tonic-gate 
685*bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
6867010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6877010c12aSrie 
6887c478bd9Sstevel@tonic-gate 	/*
689141040e8Srie 	 * Process active relocations.
6907c478bd9Sstevel@tonic-gate 	 */
691*bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
692*bf994817SAli Bahrami 		uchar_t		*addr;
693*bf994817SAli Bahrami 		Xword 		value;
694*bf994817SAli Bahrami 		Sym_desc	*sdp;
695*bf994817SAli Bahrami 		const char	*ifl_name;
696*bf994817SAli Bahrami 		Xword		refaddr;
697*bf994817SAli Bahrami 		int		moved = 0;
698*bf994817SAli Bahrami 		Gotref		gref;
699*bf994817SAli Bahrami 		Os_desc		*osp;
7007c478bd9Sstevel@tonic-gate 
701*bf994817SAli Bahrami 		/*
702*bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
703*bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
704*bf994817SAli Bahrami 		 */
705*bf994817SAli Bahrami 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
706*bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
707*bf994817SAli Bahrami 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
708*bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
709*bf994817SAli Bahrami 			continue;
710*bf994817SAli Bahrami 		}
7117c478bd9Sstevel@tonic-gate 
712*bf994817SAli Bahrami 		/*
713*bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
714*bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
715*bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
716*bf994817SAli Bahrami 		 *
717*bf994817SAli Bahrami 		 * The got table entries have already been assigned,
718*bf994817SAli Bahrami 		 * and we bind to those initial entries.
719*bf994817SAli Bahrami 		 */
720*bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_DTLS)
721*bf994817SAli Bahrami 			gref = GOT_REF_TLSGD;
722*bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_MTLS)
723*bf994817SAli Bahrami 			gref = GOT_REF_TLSLD;
724*bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_STLS)
725*bf994817SAli Bahrami 			gref = GOT_REF_TLSIE;
726*bf994817SAli Bahrami 		else
727*bf994817SAli Bahrami 			gref = GOT_REF_GENERIC;
7287c478bd9Sstevel@tonic-gate 
729*bf994817SAli Bahrami 		/*
730*bf994817SAli Bahrami 		 * Perform any required TLS fixups.
731*bf994817SAli Bahrami 		 */
732*bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
733*bf994817SAli Bahrami 			Fixupret	ret;
734141040e8Srie 
735*bf994817SAli Bahrami 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
736*bf994817SAli Bahrami 				return (S_ERROR);
737*bf994817SAli Bahrami 			if (ret == FIX_DONE)
738*bf994817SAli Bahrami 				continue;
739*bf994817SAli Bahrami 		}
7407c478bd9Sstevel@tonic-gate 
741*bf994817SAli Bahrami 		/*
742*bf994817SAli Bahrami 		 * If this is a relocation against a move table, or
743*bf994817SAli Bahrami 		 * expanded move table, adjust the relocation entries.
744*bf994817SAli Bahrami 		 */
745*bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
746*bf994817SAli Bahrami 			ld_adj_movereloc(ofl, arsp);
7477c478bd9Sstevel@tonic-gate 
748*bf994817SAli Bahrami 		sdp = arsp->rel_sym;
749*bf994817SAli Bahrami 		refaddr = arsp->rel_roffset +
750*bf994817SAli Bahrami 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7517c478bd9Sstevel@tonic-gate 
752*bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
753*bf994817SAli Bahrami 		    (arsp->rel_flags & FLG_REL_GOTCL))
754*bf994817SAli Bahrami 			value = 0;
755*bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
756*bf994817SAli Bahrami 			Sym_desc	*sym;
7577c478bd9Sstevel@tonic-gate 
758*bf994817SAli Bahrami 			/*
759*bf994817SAli Bahrami 			 * The value for a symbol pointing to a SECTION
760*bf994817SAli Bahrami 			 * is based off of that sections position.
761*bf994817SAli Bahrami 			 */
762*bf994817SAli Bahrami 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
763*bf994817SAli Bahrami 			    /* LINTED */
764*bf994817SAli Bahrami 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
7657c478bd9Sstevel@tonic-gate 				/*
766*bf994817SAli Bahrami 				 * The symbol was moved, so adjust the value
767*bf994817SAli Bahrami 				 * relative to the new section.
7682926dd2eSrie 				 */
769*bf994817SAli Bahrami 				value = sym->sd_sym->st_value;
770*bf994817SAli Bahrami 				moved = 1;
77108278a5eSRod Evans 
7727c478bd9Sstevel@tonic-gate 				/*
773*bf994817SAli Bahrami 				 * The original raddend covers the displacement
774*bf994817SAli Bahrami 				 * from the section start to the desired
775*bf994817SAli Bahrami 				 * address. The value computed above gets us
776*bf994817SAli Bahrami 				 * from the section start to the start of the
777*bf994817SAli Bahrami 				 * symbol range. Adjust the old raddend to
778*bf994817SAli Bahrami 				 * remove the offset from section start to
779*bf994817SAli Bahrami 				 * symbol start, leaving the displacement
780*bf994817SAli Bahrami 				 * within the range of the symbol.
7817c478bd9Sstevel@tonic-gate 				 */
782*bf994817SAli Bahrami 				arsp->rel_raddend -= sym->sd_osym->st_value;
783*bf994817SAli Bahrami 			} else {
784*bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
785*bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
786*bf994817SAli Bahrami 					value += sdp->sd_isc->is_osdesc->
787*bf994817SAli Bahrami 					    os_shdr->sh_addr;
788*bf994817SAli Bahrami 			}
789*bf994817SAli Bahrami 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
790*bf994817SAli Bahrami 				value -= ofl->ofl_tlsphdr->p_vaddr;
7917c478bd9Sstevel@tonic-gate 
792*bf994817SAli Bahrami 		} else if (IS_SIZE(arsp->rel_rtype)) {
7937c478bd9Sstevel@tonic-gate 			/*
794*bf994817SAli Bahrami 			 * Size relocations require the symbols size.
7957c478bd9Sstevel@tonic-gate 			 */
796*bf994817SAli Bahrami 			value = sdp->sd_sym->st_size;
7977c478bd9Sstevel@tonic-gate 
798*bf994817SAli Bahrami 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
799*bf994817SAli Bahrami 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
8007c478bd9Sstevel@tonic-gate 			/*
801*bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
802*bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
803*bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
804*bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
8057c478bd9Sstevel@tonic-gate 			 */
806*bf994817SAli Bahrami 			value = ld_calc_plt_addr(sdp, ofl);
807*bf994817SAli Bahrami 		} else
808*bf994817SAli Bahrami 			value = sdp->sd_sym->st_value;
8097c478bd9Sstevel@tonic-gate 
810*bf994817SAli Bahrami 		/*
811*bf994817SAli Bahrami 		 * Relocation against the GLOBAL_OFFSET_TABLE.
812*bf994817SAli Bahrami 		 */
813*bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
814*bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
815*bf994817SAli Bahrami 			return (S_ERROR);
816*bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
8177c478bd9Sstevel@tonic-gate 
818*bf994817SAli Bahrami 		/*
819*bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
820*bf994817SAli Bahrami 		 * sections virtual address to the reference address.
821*bf994817SAli Bahrami 		 */
822*bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
823*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0))
824*bf994817SAli Bahrami 			refaddr += arsp->rel_isdesc->is_osdesc->
825*bf994817SAli Bahrami 			    os_shdr->sh_addr;
8267c478bd9Sstevel@tonic-gate 
827*bf994817SAli Bahrami 		/*
828*bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
829*bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
830*bf994817SAli Bahrami 		 */
831*bf994817SAli Bahrami 		if (IS_PLT(arsp->rel_rtype)) {
832*bf994817SAli Bahrami 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
833*bf994817SAli Bahrami 				value = ld_calc_plt_addr(sdp, ofl);
834*bf994817SAli Bahrami 		}
8357c478bd9Sstevel@tonic-gate 
836*bf994817SAli Bahrami 		/*
837*bf994817SAli Bahrami 		 * Add relocations addend to value.  Add extra
838*bf994817SAli Bahrami 		 * relocation addend if needed.
839*bf994817SAli Bahrami 		 *
840*bf994817SAli Bahrami 		 * Note: For GOT relative relocations on amd64 we discard the
841*bf994817SAli Bahrami 		 * addend.  It was relevant to the reference - not to the
842*bf994817SAli Bahrami 		 * data item being referenced (ie: that -4 thing).
843*bf994817SAli Bahrami 		 */
844*bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) == 0)
845*bf994817SAli Bahrami 			value += arsp->rel_raddend;
8467c478bd9Sstevel@tonic-gate 
847*bf994817SAli Bahrami 		/*
848*bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
849*bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
850*bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
851*bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
852*bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
853*bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
854*bf994817SAli Bahrami 		 */
855*bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
856*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
857*bf994817SAli Bahrami 			Xword		R1addr;
858*bf994817SAli Bahrami 			uintptr_t	R2addr;
859*bf994817SAli Bahrami 			Word		gotndx;
860*bf994817SAli Bahrami 			Gotndx		*gnp;
8617c478bd9Sstevel@tonic-gate 
862*bf994817SAli Bahrami 			/*
863*bf994817SAli Bahrami 			 * Perform relocation against GOT table. Since this
864*bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
865*bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
866*bf994817SAli Bahrami 			 *
867*bf994817SAli Bahrami 			 * Calculate offset into GOT at which to apply
868*bf994817SAli Bahrami 			 * the relocation.
869*bf994817SAli Bahrami 			 */
870*bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
871*bf994817SAli Bahrami 			assert(gnp);
8727c478bd9Sstevel@tonic-gate 
873*bf994817SAli Bahrami 			if (arsp->rel_rtype == R_AMD64_DTPOFF64)
874*bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx + 1;
875*bf994817SAli Bahrami 			else
876*bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx;
8777c478bd9Sstevel@tonic-gate 
878*bf994817SAli Bahrami 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8797c478bd9Sstevel@tonic-gate 
880*bf994817SAli Bahrami 			/*
881*bf994817SAli Bahrami 			 * Add the GOTs data's offset.
882*bf994817SAli Bahrami 			 */
883*bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
884141040e8Srie 
885*bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
886*bf994817SAli Bahrami 			    M_MACH, SHT_RELA, arsp, R1addr, value,
887*bf994817SAli Bahrami 			    ld_reloc_sym_name));
888141040e8Srie 
889*bf994817SAli Bahrami 			/*
890*bf994817SAli Bahrami 			 * And do it.
891*bf994817SAli Bahrami 			 */
892*bf994817SAli Bahrami 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
893*bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
894*bf994817SAli Bahrami 			else
895*bf994817SAli Bahrami 				*(Xword *)R2addr = value;
896*bf994817SAli Bahrami 			continue;
897141040e8Srie 
898*bf994817SAli Bahrami 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
899*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
900*bf994817SAli Bahrami 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
901141040e8Srie 
902*bf994817SAli Bahrami 		} else if (IS_GOTPCREL(arsp->rel_rtype) &&
903*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
904*bf994817SAli Bahrami 			Gotndx *gnp;
9057c478bd9Sstevel@tonic-gate 
906*bf994817SAli Bahrami 			/*
907*bf994817SAli Bahrami 			 * Calculation:
908*bf994817SAli Bahrami 			 *	G + GOT + A - P
909*bf994817SAli Bahrami 			 */
910*bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
911*bf994817SAli Bahrami 			assert(gnp);
912*bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr-> sh_addr) +
913*bf994817SAli Bahrami 			    ((Xword)gnp->gn_gotndx * M_GOT_ENTSIZE) +
914*bf994817SAli Bahrami 			    arsp->rel_raddend - refaddr;
915*bf994817SAli Bahrami 
916*bf994817SAli Bahrami 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
917*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
918*bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr->
919*bf994817SAli Bahrami 			    sh_addr) - refaddr + arsp->rel_raddend;
920*bf994817SAli Bahrami 
921*bf994817SAli Bahrami 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
922*bf994817SAli Bahrami 		    (((flags & FLG_OF_RELOBJ) == 0) ||
923*bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
924*bf994817SAli Bahrami 			value -= refaddr;
925*bf994817SAli Bahrami 
926*bf994817SAli Bahrami 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
927*bf994817SAli Bahrami 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
928*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
929*bf994817SAli Bahrami 			Gotndx	*gnp;
930*bf994817SAli Bahrami 
931*bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
932*bf994817SAli Bahrami 			assert(gnp);
933*bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
934*bf994817SAli Bahrami 
935*bf994817SAli Bahrami 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
936*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
937*bf994817SAli Bahrami 			Gotndx *gnp;
938*bf994817SAli Bahrami 
939*bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
940*bf994817SAli Bahrami 			assert(gnp);
941*bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
942*bf994817SAli Bahrami 
943*bf994817SAli Bahrami 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
944*bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
945*bf994817SAli Bahrami 			Xword	tlsstatsize;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 			/*
948*bf994817SAli Bahrami 			 * This is the LE TLS reference model.  Static
949*bf994817SAli Bahrami 			 * offset is hard-coded.
9507c478bd9Sstevel@tonic-gate 			 */
951*bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
952*bf994817SAli Bahrami 			    M_TLSSTATALIGN);
953*bf994817SAli Bahrami 			value = tlsstatsize - value;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 			/*
956*bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a negative
957*bf994817SAli Bahrami 			 * offset that can be added to the thread pointer.
9587c478bd9Sstevel@tonic-gate 			 */
959*bf994817SAli Bahrami 			if (arsp->rel_rtype == R_AMD64_TPOFF32)
960*bf994817SAli Bahrami 				value = -value;
961*bf994817SAli Bahrami 		}
9627c478bd9Sstevel@tonic-gate 
963*bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_file)
964*bf994817SAli Bahrami 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
965*bf994817SAli Bahrami 		else
966*bf994817SAli Bahrami 			ifl_name = MSG_INTL(MSG_STR_NULL);
967*bf994817SAli Bahrami 
968*bf994817SAli Bahrami 		/*
969*bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
970*bf994817SAli Bahrami 		 * developers have been known to generate relocations against
971*bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
972*bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
973*bf994817SAli Bahrami 		 * End users should never see this.
974*bf994817SAli Bahrami 		 */
975*bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
976*bf994817SAli Bahrami 			Conv_inv_buf_t inv_buf;
977*bf994817SAli Bahrami 
978*bf994817SAli Bahrami 			eprintf(ofl->ofl_lml, ERR_FATAL,
979*bf994817SAli Bahrami 			    MSG_INTL(MSG_REL_EMPTYSEC),
980*bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
981*bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
982*bf994817SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
983*bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name);
984*bf994817SAli Bahrami 			return (S_ERROR);
985*bf994817SAli Bahrami 		}
986*bf994817SAli Bahrami 
987*bf994817SAli Bahrami 		/*
988*bf994817SAli Bahrami 		 * Get the address of the data item we need to modify.
989*bf994817SAli Bahrami 		 */
990*bf994817SAli Bahrami 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
991*bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
992*bf994817SAli Bahrami 
993*bf994817SAli Bahrami 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
994*bf994817SAli Bahrami 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
995*bf994817SAli Bahrami 		    ld_reloc_sym_name));
996*bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
997*bf994817SAli Bahrami 
998*bf994817SAli Bahrami 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
999*bf994817SAli Bahrami 		    ofl->ofl_size) || (arsp->rel_roffset >
1000*bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
1001*bf994817SAli Bahrami 			int		class;
1002*bf994817SAli Bahrami 			Conv_inv_buf_t inv_buf;
1003*bf994817SAli Bahrami 
1004*bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1005*bf994817SAli Bahrami 			    ofl->ofl_size)
1006*bf994817SAli Bahrami 				class = ERR_FATAL;
1007*bf994817SAli Bahrami 			else
1008*bf994817SAli Bahrami 				class = ERR_WARNING;
1009*bf994817SAli Bahrami 
1010*bf994817SAli Bahrami 			eprintf(ofl->ofl_lml, class,
1011*bf994817SAli Bahrami 			    MSG_INTL(MSG_REL_INVALOFFSET),
1012*bf994817SAli Bahrami 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1013*bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1014*bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1015*bf994817SAli Bahrami 			    EC_ADDR((uintptr_t)addr -
1016*bf994817SAli Bahrami 			    (uintptr_t)ofl->ofl_nehdr));
1017*bf994817SAli Bahrami 
1018*bf994817SAli Bahrami 			if (class == ERR_FATAL) {
1019*bf994817SAli Bahrami 				return_code = S_ERROR;
1020*bf994817SAli Bahrami 				continue;
10217c478bd9Sstevel@tonic-gate 			}
1022*bf994817SAli Bahrami 		}
10237c478bd9Sstevel@tonic-gate 
1024*bf994817SAli Bahrami 		/*
1025*bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1026*bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
1027*bf994817SAli Bahrami 		 */
1028*bf994817SAli Bahrami 		if (moved)
1029*bf994817SAli Bahrami 			value -= *addr;
10307c478bd9Sstevel@tonic-gate 
1031*bf994817SAli Bahrami 		/*
1032*bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1033*bf994817SAli Bahrami 		 */
1034*bf994817SAli Bahrami 		if (OFL_DO_RELOC(ofl)) {
10357c478bd9Sstevel@tonic-gate 			/*
1036*bf994817SAli Bahrami 			 * If this is a PROGBITS section and the running linker
1037*bf994817SAli Bahrami 			 * has a different byte order than the target host,
1038*bf994817SAli Bahrami 			 * tell do_reloc_ld() to swap bytes.
10397c478bd9Sstevel@tonic-gate 			 */
1040*bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1041*bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1042*bf994817SAli Bahrami 			    ofl->ofl_lml) == 0)
1043*bf994817SAli Bahrami 				return_code = S_ERROR;
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 	return (return_code);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
1049ba2be530Sab static uintptr_t
10505aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10517c478bd9Sstevel@tonic-gate {
1052141040e8Srie 	Rel_desc	*orsp;
1053141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
10577c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10587c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
10597c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
10607c478bd9Sstevel@tonic-gate 	 */
1061635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
10627c478bd9Sstevel@tonic-gate 		return (1);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
10667c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
10677c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10707c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
10717c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		/*
10747c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
10757c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
10767c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
10777c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
10787c478bd9Sstevel@tonic-gate 		 */
10797c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10800bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10817c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10827c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10837c478bd9Sstevel@tonic-gate 			else
10847c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1085141040e8Srie 		} else {
108608278a5eSRod Evans 			Os_desc *osp;
108708278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
10887c478bd9Sstevel@tonic-gate 
108908278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
109008278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
10917c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
10927c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
1097*bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1098*bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1099*bf994817SAli Bahrami 		return (S_ERROR);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
11027c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
11037c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
11047c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
11057c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
11067c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
11077c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
11087c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
11097c478bd9Sstevel@tonic-gate 	else
1110*bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
11137c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
11177c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
11187c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
11197c478bd9Sstevel@tonic-gate 	 */
11207c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
11217c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
11227c478bd9Sstevel@tonic-gate 
1123141040e8Srie 	/*
1124141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1125141040e8Srie 	 */
1126141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1127141040e8Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1128141040e8Srie 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
11337c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11365aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11377c478bd9Sstevel@tonic-gate 	}
11385aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11395aefb655Srie 	    M_MACH, orsp));
11407c478bd9Sstevel@tonic-gate 	return (1);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
11457c478bd9Sstevel@tonic-gate  */
1146ba2be530Sab static uintptr_t
11475aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11487c478bd9Sstevel@tonic-gate {
11491d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
11507c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
11510bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11527c478bd9Sstevel@tonic-gate 	Word		ortype = rsp->rel_rtype;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
11557c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
11567c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
11577c478bd9Sstevel@tonic-gate 	 * then
11587c478bd9Sstevel@tonic-gate 	 *	build R_AMD64_RELATIVE
11597c478bd9Sstevel@tonic-gate 	 * fi
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11622926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
11637c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11647c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
11657c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11667c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11677c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		/*
11707c478bd9Sstevel@tonic-gate 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11717c478bd9Sstevel@tonic-gate 		 * relocation isn't a 64bit binding then we can not
11727c478bd9Sstevel@tonic-gate 		 * simplify it to a RELATIVE relocation.
11737c478bd9Sstevel@tonic-gate 		 */
11747c478bd9Sstevel@tonic-gate 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11751d9df23bSab 			return (ld_add_outrel(0, rsp, ofl));
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_AMD64_RELATIVE;
11795aefb655Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11807c478bd9Sstevel@tonic-gate 			return (S_ERROR);
11817c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
11827c478bd9Sstevel@tonic-gate 		return (1);
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
1185b3fbe5e6Sseizo 	/*
1186b3fbe5e6Sseizo 	 * If the relocation is against a 'non-allocatable' section
1187b3fbe5e6Sseizo 	 * and we can not resolve it now - then give a warning
1188b3fbe5e6Sseizo 	 * message.
1189b3fbe5e6Sseizo 	 *
1190b3fbe5e6Sseizo 	 * We can not resolve the symbol if either:
1191b3fbe5e6Sseizo 	 *	a) it's undefined
1192b3fbe5e6Sseizo 	 *	b) it's defined in a shared library and a
1193b3fbe5e6Sseizo 	 *	   COPY relocation hasn't moved it to the executable
1194b3fbe5e6Sseizo 	 *
1195b3fbe5e6Sseizo 	 * Note: because we process all of the relocations against the
1196b3fbe5e6Sseizo 	 *	text segment before any others - we know whether
1197b3fbe5e6Sseizo 	 *	or not a copy relocation will be generated before
1198b3fbe5e6Sseizo 	 *	we get here (see reloc_init()->reloc_segments()).
1199b3fbe5e6Sseizo 	 */
12007c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1201b3fbe5e6Sseizo 	    ((shndx == SHN_UNDEF) ||
1202b3fbe5e6Sseizo 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1203b3fbe5e6Sseizo 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1204*bf994817SAli Bahrami 		Conv_inv_buf_t	inv_buf;
1205*bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1206de777a60Sab 
1207b3fbe5e6Sseizo 		/*
1208b3fbe5e6Sseizo 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1209b3fbe5e6Sseizo 		 * section - then silently ignore that the relocation
1210b3fbe5e6Sseizo 		 * can not be resolved.
1211b3fbe5e6Sseizo 		 */
1212*bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1213b3fbe5e6Sseizo 			return (0);
12145aefb655Srie 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
12155aefb655Srie 		    MSG_INTL(MSG_REL_EXTERNSYM),
1216de777a60Sab 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
12177c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
1218*bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
12197c478bd9Sstevel@tonic-gate 		return (1);
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * Perform relocation.
12247c478bd9Sstevel@tonic-gate 	 */
12255aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 
1229ba2be530Sab static uintptr_t
12305aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
12337c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12341d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12357c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	/*
1238d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1239d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12407c478bd9Sstevel@tonic-gate 	 */
1241d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12427c478bd9Sstevel@tonic-gate 		/*
1243d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12447c478bd9Sstevel@tonic-gate 		 */
12457c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12467c478bd9Sstevel@tonic-gate 
1247d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12487c478bd9Sstevel@tonic-gate 			/*
1249d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12507c478bd9Sstevel@tonic-gate 			 */
125157ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
125257ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
12537c478bd9Sstevel@tonic-gate 
1254d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1255d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1256d326b23bSrie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1257d326b23bSrie 					return (S_ERROR);
1258d326b23bSrie 			}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 			/*
1261d326b23bSrie 			 * IE access model.
12627c478bd9Sstevel@tonic-gate 			 */
12637c478bd9Sstevel@tonic-gate 			if (IS_TLS_IE(rtype))
12645aefb655Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 			/*
1267d326b23bSrie 			 * Fixups are required for other executable models.
12687c478bd9Sstevel@tonic-gate 			 */
12695aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12707c478bd9Sstevel@tonic-gate 			    rsp, ofl));
12717c478bd9Sstevel@tonic-gate 		}
1272d326b23bSrie 
12737c478bd9Sstevel@tonic-gate 		/*
1274d326b23bSrie 		 * LE access model.
12757c478bd9Sstevel@tonic-gate 		 */
12767c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype))
12775aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1278d326b23bSrie 
12795aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12805aefb655Srie 		    rsp, ofl));
12817c478bd9Sstevel@tonic-gate 	}
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	/*
1284d326b23bSrie 	 * Building a shared object.
1285d326b23bSrie 	 *
1286d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
12877c478bd9Sstevel@tonic-gate 	 */
128857ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
128957ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1290d326b23bSrie 
1291d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
129257ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
12937c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1294d326b23bSrie 
12955aefb655Srie 	} else if (IS_TLS_GD(rtype) &&
129657ef7aa9SRod Evans 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
129757ef7aa9SRod Evans 	    ofl, rsp)) == NULL)) {
1298d326b23bSrie 
1299d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1300d326b23bSrie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1301d326b23bSrie 		    R_AMD64_DTPOFF64) == S_ERROR)
13027c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
13065aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13077c478bd9Sstevel@tonic-gate 
13085aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate /* ARGSUSED5 */
1312ba2be530Sab static uintptr_t
131357ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
131457ef7aa9SRod Evans     Rel_desc *rsp, Sym_desc *sdp)
13157c478bd9Sstevel@tonic-gate {
13167c478bd9Sstevel@tonic-gate 	Xword		raddend;
131757ef7aa9SRod Evans 	Gotndx		gn, *gnp;
131857ef7aa9SRod Evans 	Aliste		idx;
13197c478bd9Sstevel@tonic-gate 	uint_t		gotents;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	raddend = rsp->rel_raddend;
132257ef7aa9SRod Evans 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
13237c478bd9Sstevel@tonic-gate 		return (1);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13267c478bd9Sstevel@tonic-gate 		gotents = 2;
13277c478bd9Sstevel@tonic-gate 	else
13287c478bd9Sstevel@tonic-gate 		gotents = 1;
13297c478bd9Sstevel@tonic-gate 
133057ef7aa9SRod Evans 	gn.gn_addend = raddend;
133157ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
133257ef7aa9SRod Evans 	gn.gn_gotref = gref;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
133757ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
133857ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
133957ef7aa9SRod Evans 				return (S_ERROR);
134057ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
134157ef7aa9SRod Evans 			ofl->ofl_tlsldgotndx = gnp;
134257ef7aa9SRod Evans 		}
13437c478bd9Sstevel@tonic-gate 		return (1);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
134657ef7aa9SRod Evans 	idx = 0;
134757ef7aa9SRod Evans 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
134857ef7aa9SRod Evans 		if (gnp->gn_addend > raddend)
134957ef7aa9SRod Evans 			break;
13507c478bd9Sstevel@tonic-gate 	}
135157ef7aa9SRod Evans 
135257ef7aa9SRod Evans 	/*
135357ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
135457ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
135557ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
135657ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
135757ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
135857ef7aa9SRod Evans 	 */
135957ef7aa9SRod Evans 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
136057ef7aa9SRod Evans 	    AL_CNT_SDP_GOT, idx) == NULL)
136157ef7aa9SRod Evans 		return (S_ERROR);
136257ef7aa9SRod Evans 
13637c478bd9Sstevel@tonic-gate 	return (1);
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate 
1366ba2be530Sab static void
13675aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
13707c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1371141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
1374b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = {
13757c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
13767c478bd9Sstevel@tonic-gate /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
13777c478bd9Sstevel@tonic-gate /* 0x0c NOP */			0x90,
13787c478bd9Sstevel@tonic-gate /* 0x0d NOP */			0x90,
13797c478bd9Sstevel@tonic-gate /* 0x0e NOP */			0x90,
13807c478bd9Sstevel@tonic-gate /* 0x0f NOP */			0x90
13817c478bd9Sstevel@tonic-gate };
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate /*
13847c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
13857c478bd9Sstevel@tonic-gate  */
1386ba2be530Sab static uintptr_t
1387d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
13887c478bd9Sstevel@tonic-gate {
1389ba2be530Sab 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1390ba2be530Sab 
13917c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1392d326b23bSrie 		Sym_desc	*sdp;
13937c478bd9Sstevel@tonic-gate 
13945aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1395635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1396d326b23bSrie 			uchar_t	*genptr;
1397d326b23bSrie 
1398d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
13997c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14007c478bd9Sstevel@tonic-gate 			/* LINTED */
14017c478bd9Sstevel@tonic-gate 			*(Xword *)genptr = sdp->sd_sym->st_value;
1402ba2be530Sab 			if (bswap)
1403ba2be530Sab 				/* LINTED */
1404ba2be530Sab 				*(Xword *)genptr =
1405ba2be530Sab 				    /* LINTED */
1406ba2be530Sab 				    ld_bswap_Xword(*(Xword *)genptr);
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
14127c478bd9Sstevel@tonic-gate 	 * entry is:
14137c478bd9Sstevel@tonic-gate 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14147c478bd9Sstevel@tonic-gate 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14157c478bd9Sstevel@tonic-gate 	 *	0x0c NOP
14167c478bd9Sstevel@tonic-gate 	 *	0x0d NOP
14177c478bd9Sstevel@tonic-gate 	 *	0x0e NOP
14187c478bd9Sstevel@tonic-gate 	 *	0x0f NOP
14197c478bd9Sstevel@tonic-gate 	 */
1420f3324781Sab 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1421d326b23bSrie 		uchar_t	*pltent;
1422d326b23bSrie 		Xword	val1;
14237c478bd9Sstevel@tonic-gate 
1424b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14257c478bd9Sstevel@tonic-gate 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14267c478bd9Sstevel@tonic-gate 
1427f3324781Sab 		/*
1428f3324781Sab 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1429f3324781Sab 		 * stage.
1430f3324781Sab 		 */
1431f3324781Sab 		if (!OFL_DO_RELOC(ofl))
1432f3324781Sab 			return (1);
1433f3324781Sab 
14347c478bd9Sstevel@tonic-gate 		/*
14357c478bd9Sstevel@tonic-gate 		 * filin:
14367c478bd9Sstevel@tonic-gate 		 *	PUSHQ GOT + 8(%rip)
14377c478bd9Sstevel@tonic-gate 		 *
14387c478bd9Sstevel@tonic-gate 		 * Note: 0x06 below represents the offset to the
14397c478bd9Sstevel@tonic-gate 		 *	 next instruction - which is what %rip will
14407c478bd9Sstevel@tonic-gate 		 *	 be pointing at.
14417c478bd9Sstevel@tonic-gate 		 */
14427c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1443de777a60Sab 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1444de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
14457c478bd9Sstevel@tonic-gate 
1446*bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
1447*bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1448*bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
1449f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1450f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1451f3324781Sab 			return (S_ERROR);
14527c478bd9Sstevel@tonic-gate 		}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 		/*
14557c478bd9Sstevel@tonic-gate 		 * filin:
14567c478bd9Sstevel@tonic-gate 		 *  JMP	*GOT+16(%rip)
14577c478bd9Sstevel@tonic-gate 		 */
14587c478bd9Sstevel@tonic-gate 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1459de777a60Sab 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1460de777a60Sab 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1461f3324781Sab 
1462*bf994817SAli Bahrami 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
1463*bf994817SAli Bahrami 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1464*bf994817SAli Bahrami 		    bswap, ofl->ofl_lml) == 0) {
1465f3324781Sab 			eprintf(ofl->ofl_lml, ERR_FATAL,
1466f3324781Sab 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1467f3324781Sab 			return (S_ERROR);
14687c478bd9Sstevel@tonic-gate 		}
14697c478bd9Sstevel@tonic-gate 	}
1470f3324781Sab 
14717c478bd9Sstevel@tonic-gate 	return (1);
14727c478bd9Sstevel@tonic-gate }
1473ba2be530Sab 
1474ba2be530Sab 
1475ba2be530Sab 
1476ba2be530Sab /*
1477ba2be530Sab  * Template for generating "void (*)(void)" function
1478ba2be530Sab  */
1479ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1480ba2be530Sab /* 0x00 */	0x55,				/* pushq  %rbp */
1481ba2be530Sab /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1482ba2be530Sab /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1483ba2be530Sab /* 0x07 */	0x5d,				/* popq   %rbp */
1484ba2be530Sab /* 0x08 */	0xc3				/* ret */
1485ba2be530Sab };
1486ba2be530Sab 
1487ba2be530Sab 
14883c573fccSAli Bahrami /*
14893c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
14903c573fccSAli Bahrami  *
14913c573fccSAli Bahrami  * entry:
14923c573fccSAli Bahrami  *
14933c573fccSAli Bahrami  *	base - base address of section being filled
14943c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
14953c573fccSAli Bahrami  *	cnt - # bytes to be filled
14963c573fccSAli Bahrami  *
14973c573fccSAli Bahrami  * exit:
14983c573fccSAli Bahrami  *	The fill has been completed.
14993c573fccSAli Bahrami  */
15003c573fccSAli Bahrami static void
15013c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
15023c573fccSAli Bahrami {
15033c573fccSAli Bahrami 	/*
15043c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
15053c573fccSAli Bahrami 	 * There are no alignment constraints.
15063c573fccSAli Bahrami 	 */
15073c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
15083c573fccSAli Bahrami }
15093c573fccSAli Bahrami 
15103c573fccSAli Bahrami 
1511ba2be530Sab /*
1512ba2be530Sab  * Return the ld_targ definition for this target.
1513ba2be530Sab  */
1514ba2be530Sab const Target *
1515ba2be530Sab ld_targ_init_x86(void)
1516ba2be530Sab {
1517ba2be530Sab 	static const Target _ld_targ = {
1518ba2be530Sab 		{			/* Target_mach */
1519ba2be530Sab 			M_MACH,			/* m_mach */
1520ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1521ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1522ba2be530Sab 			M_CLASS,		/* m_class */
1523ba2be530Sab 			M_DATA,			/* m_data */
1524ba2be530Sab 
1525ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1526ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1527bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1528ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
152969112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1530ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1531ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1532ba2be530Sab 
1533ba2be530Sab 			/* Relocation type codes */
1534ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1535ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1536ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1537ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1538ba2be530Sab 			M_R_NUM,		/* m_r_num */
1539ba2be530Sab 			M_R_NONE,		/* m_r_none */
1540ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1541ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1542ba2be530Sab 
1543ba2be530Sab 			/* Relocation related constants */
1544ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1545ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1546ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1547ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1548ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1549ba2be530Sab 
1550ba2be530Sab 			/* GOT related constants */
1551ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1552ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1553ba2be530Sab 
1554ba2be530Sab 			/* PLT related constants */
1555ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1556ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1557ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1558ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1559ba2be530Sab 
15607e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
15617e16fca0SAli Bahrami 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
15627e16fca0SAli Bahrami 
1563ba2be530Sab 			M_DT_REGISTER,		/* m_dt_register */
1564ba2be530Sab 		},
1565ba2be530Sab 		{			/* Target_machid */
1566ba2be530Sab 			M_ID_ARRAY,		/* id_array */
1567ba2be530Sab 			M_ID_BSS,		/* id_bss */
1568ba2be530Sab 			M_ID_CAP,		/* id_cap */
156908278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
157008278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1571ba2be530Sab 			M_ID_DATA,		/* id_data */
1572ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1573ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1574ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1575ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1576ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1577ba2be530Sab 			M_ID_GOT,		/* id_got */
1578ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1579ba2be530Sab 			M_ID_HASH,		/* id_hash */
1580ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1581ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1582ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1583ba2be530Sab 			M_ID_NOTE,		/* id_note */
1584ba2be530Sab 			M_ID_NULL,		/* id_null */
1585ba2be530Sab 			M_ID_PLT,		/* id_plt */
1586ba2be530Sab 			M_ID_REL,		/* id_rel */
1587ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1588ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1589ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1590ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1591ba2be530Sab 			M_ID_TEXT,		/* id_text */
1592ba2be530Sab 			M_ID_TLS,		/* id_tls */
1593ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1594ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1595ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
15967e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1597ba2be530Sab 			M_ID_USER,		/* id_user */
1598ba2be530Sab 			M_ID_VERSION,		/* id_version */
1599ba2be530Sab 		},
1600ba2be530Sab 		{			/* Target_nullfunc */
1601ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1602ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1603ba2be530Sab 		},
16043c573fccSAli Bahrami 		{			/* Target_fillfunc */
16053c573fccSAli Bahrami 			execfill		/* ff_execfill */
16063c573fccSAli Bahrami 		},
1607ba2be530Sab 		{			/* Target_machrel */
1608ba2be530Sab 			reloc_table,
1609ba2be530Sab 
1610ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1611ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1612ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1613ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1614ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1615ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1616ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1617ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1618ba2be530Sab 			NULL,			/* mr_reloc_register */
1619ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1620ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1621ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1622ba2be530Sab 			NULL,			/* mr_assign_got */
162357ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1624ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1625ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1626ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1627ba2be530Sab 			NULL,			/* mr_allocate_got */
1628ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1629ba2be530Sab 		},
1630ba2be530Sab 		{			/* Target_machsym */
1631ba2be530Sab 			NULL,			/* ms_reg_check */
1632ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1633ba2be530Sab 			NULL,			/* ms_is_regsym */
1634ba2be530Sab 			NULL,			/* ms_reg_find */
1635ba2be530Sab 			NULL			/* ms_reg_enter */
1636ba2be530Sab 		}
1637ba2be530Sab 	};
1638ba2be530Sab 
1639ba2be530Sab 	return (&_ld_targ);
1640ba2be530Sab }
1641