xref: /onnv-gate/usr/src/cmd/sgs/libld/common/machrel.amd.c (revision 13074:787bf65954d0)
11618Srie /*
21618Srie  * CDDL HEADER START
31618Srie  *
41618Srie  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
71618Srie  *
81618Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91618Srie  * or http://www.opensolaris.org/os/licensing.
101618Srie  * See the License for the specific language governing permissions
111618Srie  * and limitations under the License.
121618Srie  *
131618Srie  * When distributing Covered Code, include this CDDL HEADER in each
141618Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151618Srie  * If applicable, add the following below this CDDL HEADER, with the
161618Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171618Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181618Srie  *
191618Srie  * CDDL HEADER END
201618Srie  */
211618Srie 
221618Srie /*
2312155SAli.Bahrami@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
241618Srie  */
251618Srie 
266206Sab196087 /* Get the x86 version of the relocation engine */
276206Sab196087 #define	DO_RELOC_LIBLD_X86
286206Sab196087 
291618Srie #include	<string.h>
301618Srie #include	<stdio.h>
311618Srie #include	<strings.h>
321618Srie #include	<sys/elf_amd64.h>
331618Srie #include	<debug.h>
341618Srie #include	<reloc.h>
356206Sab196087 #include	<i386/machdep_x86.h>
361618Srie #include	"msg.h"
371618Srie #include	"_libld.h"
381618Srie 
399131SRod.Evans@Sun.COM /*
4012155SAli.Bahrami@Sun.COM  * This module uses do_reloc_ld() to execute several synthesized relocations.
4112155SAli.Bahrami@Sun.COM  * That function expects to be passed two things that we need to construct
4212155SAli.Bahrami@Sun.COM  * here:
4312155SAli.Bahrami@Sun.COM  *
4412155SAli.Bahrami@Sun.COM  * 1)	A Rel_desc descriptor for each relocation type, from which the
4512155SAli.Bahrami@Sun.COM  *	rel_rtype field, and nothing else, is obtained. This is easily
4612155SAli.Bahrami@Sun.COM  *	handled by constructing the necessary descriptors.
4712155SAli.Bahrami@Sun.COM  *
4812155SAli.Bahrami@Sun.COM  * 2)	A function, which called with the Rel_desc descriptor, returns
4912155SAli.Bahrami@Sun.COM  *	a string representing the name of the symbol associated with
5012155SAli.Bahrami@Sun.COM  *	the descriptor. The usual function for this is ld_reloc_sym_name().
5112155SAli.Bahrami@Sun.COM  *	However, that function will not work in this case, as these synthetic
5212155SAli.Bahrami@Sun.COM  *	relocations do not have an associated symbol. We supply the
5312155SAli.Bahrami@Sun.COM  *	syn_rdesc_sym_name() function to simply return the fixed name.
5412155SAli.Bahrami@Sun.COM  */
5512155SAli.Bahrami@Sun.COM static Rel_desc rdesc_r_amd64_gotpcrel = {
5612155SAli.Bahrami@Sun.COM     NULL, NULL, NULL, 0, 0, 0, R_AMD64_GOTPCREL };
5712155SAli.Bahrami@Sun.COM static Rel_desc rdesc_r_amd64_32 = {
5812155SAli.Bahrami@Sun.COM     NULL, NULL, NULL, 0, 0, 0, R_AMD64_32 };
5912155SAli.Bahrami@Sun.COM static Rel_desc rdesc_r_amd64_pc32 = {
6012155SAli.Bahrami@Sun.COM     NULL, NULL, NULL, 0, 0, 0, R_AMD64_PC32 };
6112155SAli.Bahrami@Sun.COM 
6212155SAli.Bahrami@Sun.COM /*ARGSUSED*/
6312155SAli.Bahrami@Sun.COM static const char *
syn_rdesc_sym_name(Rel_desc * rdesc)6412155SAli.Bahrami@Sun.COM syn_rdesc_sym_name(Rel_desc *rdesc)
6512155SAli.Bahrami@Sun.COM {
6612155SAli.Bahrami@Sun.COM 	return (MSG_ORIG(MSG_SYM_PLTENT));
6712155SAli.Bahrami@Sun.COM }
6812155SAli.Bahrami@Sun.COM 
6912155SAli.Bahrami@Sun.COM /*
709131SRod.Evans@Sun.COM  * Search the GOT index list for a GOT entry with a matching reference and the
719131SRod.Evans@Sun.COM  * proper addend.
729131SRod.Evans@Sun.COM  */
739131SRod.Evans@Sun.COM static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)749131SRod.Evans@Sun.COM ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
759131SRod.Evans@Sun.COM {
769131SRod.Evans@Sun.COM 	Aliste	idx;
779131SRod.Evans@Sun.COM 	Gotndx	*gnp;
786206Sab196087 
799131SRod.Evans@Sun.COM 	assert(rdesc != 0);
809131SRod.Evans@Sun.COM 
819131SRod.Evans@Sun.COM 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
829131SRod.Evans@Sun.COM 		return (ofl->ofl_tlsldgotndx);
839131SRod.Evans@Sun.COM 
849131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
859131SRod.Evans@Sun.COM 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
869131SRod.Evans@Sun.COM 		    (gnp->gn_gotref == gref)) {
879131SRod.Evans@Sun.COM 			return (gnp);
889131SRod.Evans@Sun.COM 		}
899131SRod.Evans@Sun.COM 	}
909131SRod.Evans@Sun.COM 	return (NULL);
919131SRod.Evans@Sun.COM }
926206Sab196087 
939131SRod.Evans@Sun.COM static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)949131SRod.Evans@Sun.COM ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
959131SRod.Evans@Sun.COM {
969131SRod.Evans@Sun.COM 	Os_desc		*osp = ofl->ofl_osgot;
979131SRod.Evans@Sun.COM 	Sym_desc	*sdp = rdesc->rel_sym;
989131SRod.Evans@Sun.COM 	Xword		gotndx;
999131SRod.Evans@Sun.COM 	Gotref		gref;
1009131SRod.Evans@Sun.COM 	Gotndx		*gnp;
1019131SRod.Evans@Sun.COM 
1029131SRod.Evans@Sun.COM 	if (rdesc->rel_flags & FLG_REL_DTLS)
1039131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSGD;
1049131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1059131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSLD;
1069131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_STLS)
1079131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSIE;
1089131SRod.Evans@Sun.COM 	else
1099131SRod.Evans@Sun.COM 		gref = GOT_REF_GENERIC;
1109131SRod.Evans@Sun.COM 
1119131SRod.Evans@Sun.COM 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
1129131SRod.Evans@Sun.COM 	assert(gnp);
1139131SRod.Evans@Sun.COM 
1149131SRod.Evans@Sun.COM 	gotndx = (Xword)gnp->gn_gotndx;
1159131SRod.Evans@Sun.COM 
1169131SRod.Evans@Sun.COM 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1179131SRod.Evans@Sun.COM 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
1189131SRod.Evans@Sun.COM 		gotndx++;
1199131SRod.Evans@Sun.COM 
1209131SRod.Evans@Sun.COM 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
1219131SRod.Evans@Sun.COM }
1226206Sab196087 
1236206Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)12412155SAli.Bahrami@Sun.COM ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
1251618Srie {
1269131SRod.Evans@Sun.COM 	Rela	*rel = (Rela *)reloc;
1271618Srie 
1281618Srie 	/* LINTED */
1296206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
1301618Srie 	reld->rel_roffset = rel->r_offset;
1311618Srie 	reld->rel_raddend = rel->r_addend;
13212155SAli.Bahrami@Sun.COM 	*typedata = 0;
1331618Srie 
1341618Srie 	reld->rel_flags |= FLG_REL_RELA;
1351618Srie 
1361618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
1371618Srie }
1381618Srie 
1396206Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1401618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1411618Srie {
1421618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1431618Srie }
1441618Srie 
1456206Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1461618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1471618Srie {
1481618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1491618Srie 		/*
1501618Srie 		 * Create this entry if we are going to create a PLT table.
1511618Srie 		 */
1521618Srie 		if (ofl->ofl_pltcnt)
1531618Srie 			(*cnt)++;		/* DT_PLTGOT */
1541618Srie 	}
1551618Srie }
1561618Srie 
1576206Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1581618Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1591618Srie {
1601618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1611618Srie 		(*dyn)->d_tag = DT_PLTGOT;
1621618Srie 		if (ofl->ofl_osgot)
1631618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1641618Srie 		else
1651618Srie 			(*dyn)->d_un.d_ptr = 0;
1661618Srie 		(*dyn)++;
1671618Srie 	}
1681618Srie }
1691618Srie 
1706206Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1711618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1721618Srie {
1731618Srie 	Xword	value;
1741618Srie 
1751618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1761618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1771618Srie 	return (value);
1781618Srie }
1791618Srie 
1801618Srie /*
1811618Srie  *  Build a single plt entry - code is:
1821618Srie  *	JMP	*name1@GOTPCREL(%rip)
1831618Srie  *	PUSHL	$index
1841618Srie  *	JMP	.PLT0
1851618Srie  */
1861618Srie static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
1871618Srie /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1881618Srie /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
1891618Srie /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
1901618Srie /* 0x10 */
1911618Srie };
1921618Srie 
1931618Srie static uintptr_t
plt_entry(Ofl_desc * ofl,Sym_desc * sdp)1941618Srie plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
1951618Srie {
1961618Srie 	uchar_t		*plt0, *pltent, *gotent;
1971618Srie 	Sword		plt_off;
1981618Srie 	Word		got_off;
1991618Srie 	Xword		val1;
2006206Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
2011618Srie 
2021618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2031618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
2041618Srie 	    M_PLT_ENTSIZE);
2051618Srie 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
2061618Srie 	pltent = plt0 + plt_off;
2071618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
2081618Srie 
2091618Srie 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
2101618Srie 	/*
2111618Srie 	 * Fill in the got entry with the address of the next instruction.
2121618Srie 	 */
2131618Srie 	/* LINTED */
2141618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
2151618Srie 	    M_PLT_INSSIZE;
2166206Sab196087 	if (bswap)
2176206Sab196087 		/* LINTED */
2186206Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
2191618Srie 
2201618Srie 	/*
2215189Sab196087 	 * If '-z noreloc' is specified - skip the do_reloc_ld
2225189Sab196087 	 * stage.
2235189Sab196087 	 */
2245189Sab196087 	if (!OFL_DO_RELOC(ofl))
2255189Sab196087 		return (1);
2265189Sab196087 
2275189Sab196087 	/*
2281618Srie 	 * patchup:
2291618Srie 	 *	jmpq	*name1@gotpcrel(%rip)
2301618Srie 	 *
2311618Srie 	 * NOTE: 0x06 represents next instruction.
2321618Srie 	 */
2331618Srie 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
2344734Sab196087 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
2351618Srie 
23612155SAli.Bahrami@Sun.COM 	if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02], &val1,
23712155SAli.Bahrami@Sun.COM 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
23812155SAli.Bahrami@Sun.COM 	    ofl->ofl_lml) == 0) {
239*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
2405189Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2415189Sab196087 		return (S_ERROR);
2421618Srie 	}
2431618Srie 
2441618Srie 	/*
2451618Srie 	 * patchup:
2461618Srie 	 *	pushq	$pltndx
2471618Srie 	 */
2481618Srie 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
2495189Sab196087 
25012155SAli.Bahrami@Sun.COM 	if (do_reloc_ld(&rdesc_r_amd64_32, &pltent[0x07], &val1,
25112155SAli.Bahrami@Sun.COM 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
25212155SAli.Bahrami@Sun.COM 	    ofl->ofl_lml) == 0) {
253*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
2545189Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2555189Sab196087 		return (S_ERROR);
2561618Srie 	}
2571618Srie 
2581618Srie 	/*
2591618Srie 	 * patchup:
2601618Srie 	 *	jmpq	.plt0(%rip)
2615189Sab196087 	 * NOTE: 0x10 represents next instruction. The rather complex
2625189Sab196087 	 * series of casts is necessary to sign extend an offset into
2635189Sab196087 	 * a 64-bit value while satisfying various compiler error
2645189Sab196087 	 * checks.  Handle with care.
2651618Srie 	 */
2661618Srie 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
2671618Srie 	    (uintptr_t)(&pltent[0x10])));
2681618Srie 
26912155SAli.Bahrami@Sun.COM 	if (do_reloc_ld(&rdesc_r_amd64_pc32, &pltent[0x0c], &val1,
27012155SAli.Bahrami@Sun.COM 	    syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
27112155SAli.Bahrami@Sun.COM 	    ofl->ofl_lml) == 0) {
272*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
2735189Sab196087 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
2745189Sab196087 		return (S_ERROR);
2751618Srie 	}
2765189Sab196087 
2771618Srie 	return (1);
2781618Srie }
2791618Srie 
2806206Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)281*13074SAli.Bahrami@Oracle.COM ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2821618Srie {
2831618Srie 	Os_desc *	relosp, * osp = 0;
2841618Srie 	Word		ndx;
2851618Srie 	Xword		roffset, value;
2861618Srie 	Sxword		raddend;
2871618Srie 	Rela		rea;
2881618Srie 	char		*relbits;
2891618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2901618Srie 	int		sectmoved = 0;
2911618Srie 
2921618Srie 	raddend = orsp->rel_raddend;
2931618Srie 	sdp = orsp->rel_sym;
2941618Srie 
2951618Srie 	/*
2961618Srie 	 * If the section this relocation is against has been discarded
2971618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
2981618Srie 	 */
2991618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
3001618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
3011618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
3021618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
3031618Srie 		return (1);
3041618Srie 	}
3051618Srie 
3061618Srie 	/*
3071618Srie 	 * If this is a relocation against a move table, or expanded move
3081618Srie 	 * table, adjust the relocation entries.
3091618Srie 	 */
31012155SAli.Bahrami@Sun.COM 	if (RELAUX_GET_MOVE(orsp))
3111618Srie 		ld_adj_movereloc(ofl, orsp);
3121618Srie 
3131618Srie 	/*
3141618Srie 	 * If this is a relocation against a section then we need to adjust the
3151618Srie 	 * raddend field to compensate for the new position of the input section
3161618Srie 	 * within the new output section.
3171618Srie 	 */
3181618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
3199131SRod.Evans@Sun.COM 		if (ofl->ofl_parsyms &&
3201618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
3211618Srie 		    /* LINTED */
3221618Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
3231618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
3241618Srie 			sectmoved = 1;
3251618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
3261618Srie 				raddend = psym->sd_sym->st_value;
3271618Srie 			else
3281618Srie 				raddend = psym->sd_sym->st_value -
3291618Srie 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3301618Srie 			/* LINTED */
3311618Srie 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
3321618Srie 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3331618Srie 				raddend +=
3344734Sab196087 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
3351618Srie 		} else {
3361618Srie 			/* LINTED */
3371618Srie 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
3381618Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
3391618Srie 				raddend +=
3404734Sab196087 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
3411618Srie 		}
3421618Srie 	}
3431618Srie 
3441618Srie 	value = sdp->sd_sym->st_value;
3451618Srie 
3461618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
3471618Srie 		/*
3481618Srie 		 * Note: for GOT relative relocations on amd64
3491618Srie 		 *	 we discard the addend.  It was relevant
3501618Srie 		 *	 to the reference - not to the data item
3511618Srie 		 *	 being referenced (ie: that -4 thing).
3521618Srie 		 */
3531618Srie 		raddend = 0;
3541618Srie 		osp = ofl->ofl_osgot;
3551618Srie 		roffset = ld_calc_got_offset(orsp, ofl);
3561618Srie 
3571618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
3581618Srie 		/*
3591618Srie 		 * Note that relocations for PLT's actually
3601618Srie 		 * cause a relocation againt the GOT.
3611618Srie 		 */
3621618Srie 		osp = ofl->ofl_osplt;
3631618Srie 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
3641618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
3651618Srie 		raddend = 0;
3661618Srie 		if (plt_entry(ofl, sdp) == S_ERROR)
3671618Srie 			return (S_ERROR);
3681618Srie 
3691618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
3701618Srie 		/*
3711618Srie 		 * This must be a R_AMD64_COPY.  For these set the roffset to
3721618Srie 		 * point to the new symbols location.
3731618Srie 		 */
3741618Srie 		osp = ofl->ofl_isbss->is_osdesc;
3751618Srie 		roffset = value;
3761618Srie 
3771618Srie 		/*
3781618Srie 		 * The raddend doesn't mean anything in a R_SPARC_COPY
3791618Srie 		 * relocation.  Null it out because it can confuse people.
3801618Srie 		 */
3811618Srie 		raddend = 0;
3821618Srie 	} else {
38312155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(orsp);
3841618Srie 
3851618Srie 		/*
3861618Srie 		 * Calculate virtual offset of reference point; equals offset
3871618Srie 		 * into section + vaddr of section for loadable sections, or
3881618Srie 		 * offset plus section displacement for nonloadable sections.
3891618Srie 		 */
3901618Srie 		roffset = orsp->rel_roffset +
3911618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3921618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3931618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
3941618Srie 			    os_shdr->sh_addr;
3951618Srie 	}
3961618Srie 
3971618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3981618Srie 		relosp = ofl->ofl_osrel;
3991618Srie 
4001618Srie 	/*
4011618Srie 	 * Assign the symbols index for the output relocation.  If the
4021618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
4031618Srie 	 * the output sections symbols index.  Otherwise the index can be
4041618Srie 	 * derived from the symbols index itself.
4051618Srie 	 */
4061618Srie 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
4071618Srie 		ndx = STN_UNDEF;
4081618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
4091618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
4101618Srie 		if (sectmoved == 0) {
4111618Srie 			/*
4121618Srie 			 * Check for a null input section. This can
4131618Srie 			 * occur if this relocation references a symbol
4141618Srie 			 * generated by sym_add_sym().
4151618Srie 			 */
4169131SRod.Evans@Sun.COM 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
4179131SRod.Evans@Sun.COM 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
4181618Srie 			else
4191618Srie 				ndx = sdp->sd_shndx;
4201618Srie 		} else
4218159SAli.Bahrami@Sun.COM 			ndx = ofl->ofl_parexpnndx;
4221618Srie 	} else
4231618Srie 		ndx = sdp->sd_symndx;
4241618Srie 
4251618Srie 	/*
4261618Srie 	 * Add the symbols 'value' to the addend field.
4271618Srie 	 */
4281618Srie 	if (orsp->rel_flags & FLG_REL_ADVAL)
4291618Srie 		raddend += value;
4301618Srie 
4311618Srie 	/*
4322647Srie 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
4332647Srie 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
4341618Srie 	 */
4351618Srie 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
4361618Srie 		raddend = 0;
4371618Srie 
4381618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
4391618Srie 
4401618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
4411618Srie 	rea.r_offset = roffset;
4421618Srie 	rea.r_addend = raddend;
4431618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
44412155SAli.Bahrami@Sun.COM 	    ld_reloc_sym_name(orsp)));
4451618Srie 
4461618Srie 	/*
4471618Srie 	 * Assert we haven't walked off the end of our relocation table.
4481618Srie 	 */
4491618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
4501618Srie 
4511618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
4521618Srie 	    (char *)&rea, sizeof (Rela));
4531618Srie 	relosp->os_szoutrels += (Xword)sizeof (Rela);
4541618Srie 
4551618Srie 	/*
4561618Srie 	 * Determine if this relocation is against a non-writable, allocatable
4571618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
4581618Srie 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
4591618Srie 	 * result in modifications to the .got.
4601618Srie 	 */
4611618Srie 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
4621618Srie 		osp = ofl->ofl_osgot;
4631618Srie 
464*13074SAli.Bahrami@Oracle.COM 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
4651618Srie 	return (1);
4661618Srie }
4671618Srie 
4681618Srie /*
4691618Srie  * amd64 Instructions for TLS processing
4701618Srie  */
4711618Srie static uchar_t tlsinstr_gd_ie[] = {
4721618Srie 	/*
4731618Srie 	 *	0x00 movq %fs:0, %rax
4741618Srie 	 */
4751618Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
4761618Srie 	0x00, 0x00, 0x00, 0x00,
4771618Srie 	/*
4781618Srie 	 *	0x09 addq x@gottpoff(%rip), %rax
4791618Srie 	 */
4801618Srie 	0x48, 0x03, 0x05, 0x00, 0x00,
4811618Srie 	0x00, 0x00
4821618Srie };
4831618Srie 
4841618Srie static uchar_t tlsinstr_gd_le[] = {
4851618Srie 	/*
4861618Srie 	 *	0x00 movq %fs:0, %rax
4871618Srie 	 */
4881618Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
4891618Srie 	0x00, 0x00, 0x00, 0x00,
4901618Srie 	/*
4911618Srie 	 *	0x09 leaq x@gottpoff(%rip), %rax
4921618Srie 	 */
4931618Srie 	0x48, 0x8d, 0x80, 0x00, 0x00,
4941618Srie 	0x00, 0x00
4951618Srie };
4961618Srie 
4971618Srie static uchar_t tlsinstr_ld_le[] = {
4981618Srie 	/*
4991618Srie 	 * .byte 0x66
5001618Srie 	 */
5011618Srie 	0x66,
5021618Srie 	/*
5031618Srie 	 * .byte 0x66
5041618Srie 	 */
5051618Srie 	0x66,
5061618Srie 	/*
5071618Srie 	 * .byte 0x66
5081618Srie 	 */
5091618Srie 	0x66,
5101618Srie 	/*
5111618Srie 	 * movq %fs:0, %rax
5121618Srie 	 */
5131618Srie 	0x64, 0x48, 0x8b, 0x04, 0x25,
5141618Srie 	0x00, 0x00, 0x00, 0x00
5151618Srie };
5161618Srie 
5171618Srie 
5181618Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)5191618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
5201618Srie {
5211618Srie 	Sym_desc	*sdp = arsp->rel_sym;
5221618Srie 	Word		rtype = arsp->rel_rtype;
5231618Srie 	uchar_t		*offset;
5241618Srie 
5251618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
5261618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
52712155SAli.Bahrami@Sun.COM 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
5281618Srie 
5291618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
5301618Srie 		/*
5311618Srie 		 * IE reference model
5321618Srie 		 */
5331618Srie 		switch (rtype) {
5341618Srie 		case R_AMD64_TLSGD:
5351618Srie 			/*
5361618Srie 			 *  GD -> IE
5371618Srie 			 *
5381618Srie 			 * Transition:
5391618Srie 			 *	0x00 .byte 0x66
5401618Srie 			 *	0x01 leaq x@tlsgd(%rip), %rdi
5411618Srie 			 *	0x08 .word 0x6666
5421618Srie 			 *	0x0a rex64
5431618Srie 			 *	0x0b call __tls_get_addr@plt
5441618Srie 			 *	0x10
5451618Srie 			 * To:
5461618Srie 			 *	0x00 movq %fs:0, %rax
5471618Srie 			 *	0x09 addq x@gottpoff(%rip), %rax
5481618Srie 			 *	0x10
5491618Srie 			 */
5501618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
55112155SAli.Bahrami@Sun.COM 			    R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
5521618Srie 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
5531618Srie 			arsp->rel_roffset += 8;
5541618Srie 			arsp->rel_raddend = (Sxword)-4;
5551618Srie 
5561618Srie 			/*
5573304Srie 			 * Adjust 'offset' to beginning of instruction
5581618Srie 			 * sequence.
5591618Srie 			 */
5601618Srie 			offset -= 4;
5611618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
5621618Srie 			    sizeof (tlsinstr_gd_ie));
5631618Srie 			return (FIX_RELOC);
5641618Srie 
5651618Srie 		case R_AMD64_PLT32:
5661618Srie 			/*
5673304Srie 			 * Fixup done via the TLS_GD relocation.
5681618Srie 			 */
5691618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
57012155SAli.Bahrami@Sun.COM 			    R_AMD64_NONE, arsp, ld_reloc_sym_name));
5711618Srie 			return (FIX_DONE);
5721618Srie 		}
5731618Srie 	}
5741618Srie 
5751618Srie 	/*
5761618Srie 	 * LE reference model
5771618Srie 	 */
5781618Srie 	switch (rtype) {
5791618Srie 	case R_AMD64_TLSGD:
5801618Srie 		/*
5811618Srie 		 * GD -> LE
5821618Srie 		 *
5831618Srie 		 * Transition:
5841618Srie 		 *	0x00 .byte 0x66
5851618Srie 		 *	0x01 leaq x@tlsgd(%rip), %rdi
5861618Srie 		 *	0x08 .word 0x6666
5871618Srie 		 *	0x0a rex64
5881618Srie 		 *	0x0b call __tls_get_addr@plt
5891618Srie 		 *	0x10
5901618Srie 		 * To:
5911618Srie 		 *	0x00 movq %fs:0, %rax
5921618Srie 		 *	0x09 leaq x@tpoff(%rax), %rax
5931618Srie 		 *	0x10
5941618Srie 		 */
5951618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
59612155SAli.Bahrami@Sun.COM 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
5971618Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
5981618Srie 		arsp->rel_roffset += 8;
5991618Srie 		arsp->rel_raddend = 0;
6001618Srie 
6011618Srie 		/*
6023304Srie 		 * Adjust 'offset' to beginning of instruction sequence.
6031618Srie 		 */
6041618Srie 		offset -= 4;
6051618Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6061618Srie 		return (FIX_RELOC);
6071618Srie 
6081618Srie 	case R_AMD64_GOTTPOFF:
6091618Srie 		/*
6101618Srie 		 * IE -> LE
6111618Srie 		 *
6121618Srie 		 * Transition:
6131618Srie 		 *	0x00 movq %fs:0, %rax
6141618Srie 		 *	0x09 addq x@gottopoff(%rip), %rax
6151618Srie 		 *	0x10
6161618Srie 		 * To:
6171618Srie 		 *	0x00 movq %fs:0, %rax
6181618Srie 		 *	0x09 leaq x@tpoff(%rax), %rax
6191618Srie 		 *	0x10
6201618Srie 		 */
6213304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
62212155SAli.Bahrami@Sun.COM 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6231618Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6241618Srie 		arsp->rel_raddend = 0;
6251618Srie 
6261618Srie 		/*
6273304Srie 		 * Adjust 'offset' to beginning of instruction sequence.
6281618Srie 		 */
6291618Srie 		offset -= 12;
6301618Srie 
6311618Srie 		/*
6323304Srie 		 * Same code sequence used in the GD -> LE transition.
6331618Srie 		 */
6341618Srie 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
6351618Srie 		return (FIX_RELOC);
6361618Srie 
6371618Srie 	case R_AMD64_TLSLD:
6381618Srie 		/*
6391618Srie 		 * LD -> LE
6401618Srie 		 *
6411618Srie 		 * Transition
6421618Srie 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
6431618Srie 		 *	0x07 call __tls_get_addr@plt
6441618Srie 		 *	0x0c
6451618Srie 		 * To:
6461618Srie 		 *	0x00 .byte 0x66
6471618Srie 		 *	0x01 .byte 0x66
6481618Srie 		 *	0x02 .byte 0x66
6491618Srie 		 *	0x03 movq %fs:0, %rax
6501618Srie 		 */
6513304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
65212155SAli.Bahrami@Sun.COM 		    R_AMD64_NONE, arsp, ld_reloc_sym_name));
6531618Srie 		offset -= 3;
6541618Srie 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
6551618Srie 		return (FIX_DONE);
6561618Srie 
6571618Srie 	case R_AMD64_DTPOFF32:
6581618Srie 		/*
6591618Srie 		 * LD->LE
6601618Srie 		 *
6611618Srie 		 * Transition:
6621618Srie 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
6631618Srie 		 * To:
6641618Srie 		 *	0x00 leaq x1@tpoff(%rax), %rcx
6651618Srie 		 */
6663304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
66712155SAli.Bahrami@Sun.COM 		    R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
6681618Srie 		arsp->rel_rtype = R_AMD64_TPOFF32;
6691618Srie 		arsp->rel_raddend = 0;
6701618Srie 		return (FIX_RELOC);
6711618Srie 	}
6721618Srie 
6731618Srie 	return (FIX_RELOC);
6741618Srie }
6751618Srie 
6766206Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)6771618Srie ld_do_activerelocs(Ofl_desc *ofl)
6781618Srie {
6791618Srie 	Rel_desc	*arsp;
68012155SAli.Bahrami@Sun.COM 	Rel_cachebuf	*rcbp;
6819131SRod.Evans@Sun.COM 	Aliste		idx;
6821618Srie 	uintptr_t	return_code = 1;
6836299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
6841618Srie 
68512155SAli.Bahrami@Sun.COM 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
6862647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
6872647Srie 
6881618Srie 	/*
6891618Srie 	 * Process active relocations.
6901618Srie 	 */
69112155SAli.Bahrami@Sun.COM 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
69212155SAli.Bahrami@Sun.COM 		uchar_t		*addr;
69312155SAli.Bahrami@Sun.COM 		Xword 		value;
69412155SAli.Bahrami@Sun.COM 		Sym_desc	*sdp;
69512155SAli.Bahrami@Sun.COM 		const char	*ifl_name;
69612155SAli.Bahrami@Sun.COM 		Xword		refaddr;
69712155SAli.Bahrami@Sun.COM 		int		moved = 0;
69812155SAli.Bahrami@Sun.COM 		Gotref		gref;
69912155SAli.Bahrami@Sun.COM 		Os_desc		*osp;
7001618Srie 
70112155SAli.Bahrami@Sun.COM 		/*
70212155SAli.Bahrami@Sun.COM 		 * If the section this relocation is against has been discarded
70312155SAli.Bahrami@Sun.COM 		 * (-zignore), then discard (skip) the relocation itself.
70412155SAli.Bahrami@Sun.COM 		 */
70512155SAli.Bahrami@Sun.COM 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
70612155SAli.Bahrami@Sun.COM 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
70712155SAli.Bahrami@Sun.COM 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
70812155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
70912155SAli.Bahrami@Sun.COM 			continue;
71012155SAli.Bahrami@Sun.COM 		}
7111618Srie 
71212155SAli.Bahrami@Sun.COM 		/*
71312155SAli.Bahrami@Sun.COM 		 * We determine what the 'got reference' model (if required)
71412155SAli.Bahrami@Sun.COM 		 * is at this point.  This needs to be done before tls_fixup()
71512155SAli.Bahrami@Sun.COM 		 * since it may 'transition' our instructions.
71612155SAli.Bahrami@Sun.COM 		 *
71712155SAli.Bahrami@Sun.COM 		 * The got table entries have already been assigned,
71812155SAli.Bahrami@Sun.COM 		 * and we bind to those initial entries.
71912155SAli.Bahrami@Sun.COM 		 */
72012155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_DTLS)
72112155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSGD;
72212155SAli.Bahrami@Sun.COM 		else if (arsp->rel_flags & FLG_REL_MTLS)
72312155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSLD;
72412155SAli.Bahrami@Sun.COM 		else if (arsp->rel_flags & FLG_REL_STLS)
72512155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSIE;
72612155SAli.Bahrami@Sun.COM 		else
72712155SAli.Bahrami@Sun.COM 			gref = GOT_REF_GENERIC;
72812155SAli.Bahrami@Sun.COM 
72912155SAli.Bahrami@Sun.COM 		/*
73012155SAli.Bahrami@Sun.COM 		 * Perform any required TLS fixups.
73112155SAli.Bahrami@Sun.COM 		 */
73212155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
73312155SAli.Bahrami@Sun.COM 			Fixupret	ret;
7341618Srie 
73512155SAli.Bahrami@Sun.COM 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
73612155SAli.Bahrami@Sun.COM 				return (S_ERROR);
73712155SAli.Bahrami@Sun.COM 			if (ret == FIX_DONE)
73812155SAli.Bahrami@Sun.COM 				continue;
73912155SAli.Bahrami@Sun.COM 		}
7401618Srie 
74112155SAli.Bahrami@Sun.COM 		/*
74212155SAli.Bahrami@Sun.COM 		 * If this is a relocation against a move table, or
74312155SAli.Bahrami@Sun.COM 		 * expanded move table, adjust the relocation entries.
74412155SAli.Bahrami@Sun.COM 		 */
74512155SAli.Bahrami@Sun.COM 		if (RELAUX_GET_MOVE(arsp))
74612155SAli.Bahrami@Sun.COM 			ld_adj_movereloc(ofl, arsp);
74712155SAli.Bahrami@Sun.COM 
74812155SAli.Bahrami@Sun.COM 		sdp = arsp->rel_sym;
74912155SAli.Bahrami@Sun.COM 		refaddr = arsp->rel_roffset +
75012155SAli.Bahrami@Sun.COM 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
75112155SAli.Bahrami@Sun.COM 
75212155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
75312155SAli.Bahrami@Sun.COM 		    (arsp->rel_flags & FLG_REL_GOTCL))
75412155SAli.Bahrami@Sun.COM 			value = 0;
75512155SAli.Bahrami@Sun.COM 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
75612155SAli.Bahrami@Sun.COM 			Sym_desc	*sym;
7571618Srie 
7581618Srie 			/*
75912155SAli.Bahrami@Sun.COM 			 * The value for a symbol pointing to a SECTION
76012155SAli.Bahrami@Sun.COM 			 * is based off of that sections position.
7611618Srie 			 */
76212155SAli.Bahrami@Sun.COM 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
76312155SAli.Bahrami@Sun.COM 			    /* LINTED */
76412155SAli.Bahrami@Sun.COM 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
7651618Srie 				/*
76612155SAli.Bahrami@Sun.COM 				 * The symbol was moved, so adjust the value
76712155SAli.Bahrami@Sun.COM 				 * relative to the new section.
7682850Srie 				 */
76912155SAli.Bahrami@Sun.COM 				value = sym->sd_sym->st_value;
77012155SAli.Bahrami@Sun.COM 				moved = 1;
7711618Srie 
7721618Srie 				/*
77312155SAli.Bahrami@Sun.COM 				 * The original raddend covers the displacement
77412155SAli.Bahrami@Sun.COM 				 * from the section start to the desired
77512155SAli.Bahrami@Sun.COM 				 * address. The value computed above gets us
77612155SAli.Bahrami@Sun.COM 				 * from the section start to the start of the
77712155SAli.Bahrami@Sun.COM 				 * symbol range. Adjust the old raddend to
77812155SAli.Bahrami@Sun.COM 				 * remove the offset from section start to
77912155SAli.Bahrami@Sun.COM 				 * symbol start, leaving the displacement
78012155SAli.Bahrami@Sun.COM 				 * within the range of the symbol.
7811618Srie 				 */
78212155SAli.Bahrami@Sun.COM 				arsp->rel_raddend -= sym->sd_osym->st_value;
78312155SAli.Bahrami@Sun.COM 			} else {
78412155SAli.Bahrami@Sun.COM 				value = _elf_getxoff(sdp->sd_isc->is_indata);
78512155SAli.Bahrami@Sun.COM 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
78612155SAli.Bahrami@Sun.COM 					value += sdp->sd_isc->is_osdesc->
78712155SAli.Bahrami@Sun.COM 					    os_shdr->sh_addr;
78812155SAli.Bahrami@Sun.COM 			}
78912155SAli.Bahrami@Sun.COM 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
79012155SAli.Bahrami@Sun.COM 				value -= ofl->ofl_tlsphdr->p_vaddr;
7911618Srie 
79212155SAli.Bahrami@Sun.COM 		} else if (IS_SIZE(arsp->rel_rtype)) {
79312155SAli.Bahrami@Sun.COM 			/*
79412155SAli.Bahrami@Sun.COM 			 * Size relocations require the symbols size.
79512155SAli.Bahrami@Sun.COM 			 */
79612155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_size;
7971618Srie 
79812155SAli.Bahrami@Sun.COM 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
79912155SAli.Bahrami@Sun.COM 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
80012155SAli.Bahrami@Sun.COM 			/*
80112155SAli.Bahrami@Sun.COM 			 * If relocation is against a capabilities symbol, we
80212155SAli.Bahrami@Sun.COM 			 * need to jump to an associated PLT, so that at runtime
80312155SAli.Bahrami@Sun.COM 			 * ld.so.1 is involved to determine the best binding
80412155SAli.Bahrami@Sun.COM 			 * choice. Otherwise, the value is the symbols value.
80512155SAli.Bahrami@Sun.COM 			 */
80612155SAli.Bahrami@Sun.COM 			value = ld_calc_plt_addr(sdp, ofl);
80712155SAli.Bahrami@Sun.COM 		} else
80812155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_value;
8091618Srie 
81012155SAli.Bahrami@Sun.COM 		/*
81112155SAli.Bahrami@Sun.COM 		 * Relocation against the GLOBAL_OFFSET_TABLE.
81212155SAli.Bahrami@Sun.COM 		 */
81312155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
81412155SAli.Bahrami@Sun.COM 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
81512155SAli.Bahrami@Sun.COM 			return (S_ERROR);
81612155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(arsp);
8171618Srie 
81812155SAli.Bahrami@Sun.COM 		/*
81912155SAli.Bahrami@Sun.COM 		 * If loadable and not producing a relocatable object add the
82012155SAli.Bahrami@Sun.COM 		 * sections virtual address to the reference address.
82112155SAli.Bahrami@Sun.COM 		 */
82212155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
82312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0))
82412155SAli.Bahrami@Sun.COM 			refaddr += arsp->rel_isdesc->is_osdesc->
82512155SAli.Bahrami@Sun.COM 			    os_shdr->sh_addr;
8261618Srie 
82712155SAli.Bahrami@Sun.COM 		/*
82812155SAli.Bahrami@Sun.COM 		 * If this entry has a PLT assigned to it, its value is actually
82912155SAli.Bahrami@Sun.COM 		 * the address of the PLT (and not the address of the function).
83012155SAli.Bahrami@Sun.COM 		 */
83112155SAli.Bahrami@Sun.COM 		if (IS_PLT(arsp->rel_rtype)) {
83212155SAli.Bahrami@Sun.COM 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
83312155SAli.Bahrami@Sun.COM 				value = ld_calc_plt_addr(sdp, ofl);
83412155SAli.Bahrami@Sun.COM 		}
8351618Srie 
83612155SAli.Bahrami@Sun.COM 		/*
83712155SAli.Bahrami@Sun.COM 		 * Add relocations addend to value.  Add extra
83812155SAli.Bahrami@Sun.COM 		 * relocation addend if needed.
83912155SAli.Bahrami@Sun.COM 		 *
84012155SAli.Bahrami@Sun.COM 		 * Note: For GOT relative relocations on amd64 we discard the
84112155SAli.Bahrami@Sun.COM 		 * addend.  It was relevant to the reference - not to the
84212155SAli.Bahrami@Sun.COM 		 * data item being referenced (ie: that -4 thing).
84312155SAli.Bahrami@Sun.COM 		 */
84412155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) == 0)
84512155SAli.Bahrami@Sun.COM 			value += arsp->rel_raddend;
8461618Srie 
84712155SAli.Bahrami@Sun.COM 		/*
84812155SAli.Bahrami@Sun.COM 		 * Determine whether the value needs further adjustment. Filter
84912155SAli.Bahrami@Sun.COM 		 * through the attributes of the relocation to determine what
85012155SAli.Bahrami@Sun.COM 		 * adjustment is required.  Note, many of the following cases
85112155SAli.Bahrami@Sun.COM 		 * are only applicable when a .got is present.  As a .got is
85212155SAli.Bahrami@Sun.COM 		 * not generated when a relocatable object is being built,
85312155SAli.Bahrami@Sun.COM 		 * any adjustments that require a .got need to be skipped.
85412155SAli.Bahrami@Sun.COM 		 */
85512155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
85612155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
85712155SAli.Bahrami@Sun.COM 			Xword		R1addr;
85812155SAli.Bahrami@Sun.COM 			uintptr_t	R2addr;
85912155SAli.Bahrami@Sun.COM 			Word		gotndx;
86012155SAli.Bahrami@Sun.COM 			Gotndx		*gnp;
8611618Srie 
86212155SAli.Bahrami@Sun.COM 			/*
86312155SAli.Bahrami@Sun.COM 			 * Perform relocation against GOT table. Since this
86412155SAli.Bahrami@Sun.COM 			 * doesn't fit exactly into a relocation we place the
86512155SAli.Bahrami@Sun.COM 			 * appropriate byte in the GOT directly
86612155SAli.Bahrami@Sun.COM 			 *
86712155SAli.Bahrami@Sun.COM 			 * Calculate offset into GOT at which to apply
86812155SAli.Bahrami@Sun.COM 			 * the relocation.
86912155SAli.Bahrami@Sun.COM 			 */
87012155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
87112155SAli.Bahrami@Sun.COM 			assert(gnp);
87212155SAli.Bahrami@Sun.COM 
87312155SAli.Bahrami@Sun.COM 			if (arsp->rel_rtype == R_AMD64_DTPOFF64)
87412155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx + 1;
87512155SAli.Bahrami@Sun.COM 			else
87612155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx;
87712155SAli.Bahrami@Sun.COM 
87812155SAli.Bahrami@Sun.COM 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
8791618Srie 
88012155SAli.Bahrami@Sun.COM 			/*
88112155SAli.Bahrami@Sun.COM 			 * Add the GOTs data's offset.
88212155SAli.Bahrami@Sun.COM 			 */
88312155SAli.Bahrami@Sun.COM 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
88412155SAli.Bahrami@Sun.COM 
88512155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
88612155SAli.Bahrami@Sun.COM 			    M_MACH, SHT_RELA, arsp, R1addr, value,
88712155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name));
8881618Srie 
88912155SAli.Bahrami@Sun.COM 			/*
89012155SAli.Bahrami@Sun.COM 			 * And do it.
89112155SAli.Bahrami@Sun.COM 			 */
89212155SAli.Bahrami@Sun.COM 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
89312155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = ld_bswap_Xword(value);
8941618Srie 			else
89512155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = value;
89612155SAli.Bahrami@Sun.COM 			continue;
89712155SAli.Bahrami@Sun.COM 
89812155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
89912155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
90012155SAli.Bahrami@Sun.COM 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
90112155SAli.Bahrami@Sun.COM 
90212155SAli.Bahrami@Sun.COM 		} else if (IS_GOTPCREL(arsp->rel_rtype) &&
90312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
90412155SAli.Bahrami@Sun.COM 			Gotndx *gnp;
9051618Srie 
9061618Srie 			/*
90712155SAli.Bahrami@Sun.COM 			 * Calculation:
90812155SAli.Bahrami@Sun.COM 			 *	G + GOT + A - P
9091618Srie 			 */
91012155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
91112155SAli.Bahrami@Sun.COM 			assert(gnp);
91212155SAli.Bahrami@Sun.COM 			value = (Xword)(ofl->ofl_osgot->os_shdr-> sh_addr) +
91312155SAli.Bahrami@Sun.COM 			    ((Xword)gnp->gn_gotndx * M_GOT_ENTSIZE) +
91412155SAli.Bahrami@Sun.COM 			    arsp->rel_raddend - refaddr;
91512155SAli.Bahrami@Sun.COM 
91612155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
91712155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
91812155SAli.Bahrami@Sun.COM 			value = (Xword)(ofl->ofl_osgot->os_shdr->
91912155SAli.Bahrami@Sun.COM 			    sh_addr) - refaddr + arsp->rel_raddend;
92012155SAli.Bahrami@Sun.COM 
92112155SAli.Bahrami@Sun.COM 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
92212155SAli.Bahrami@Sun.COM 		    (((flags & FLG_OF_RELOBJ) == 0) ||
92312155SAli.Bahrami@Sun.COM 		    (osp == sdp->sd_isc->is_osdesc))) {
92412155SAli.Bahrami@Sun.COM 			value -= refaddr;
92512155SAli.Bahrami@Sun.COM 
92612155SAli.Bahrami@Sun.COM 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
92712155SAli.Bahrami@Sun.COM 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
92812155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
92912155SAli.Bahrami@Sun.COM 			Gotndx	*gnp;
9304734Sab196087 
93112155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
93212155SAli.Bahrami@Sun.COM 			assert(gnp);
93312155SAli.Bahrami@Sun.COM 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
93412155SAli.Bahrami@Sun.COM 
93512155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
93612155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
93712155SAli.Bahrami@Sun.COM 			Gotndx *gnp;
93812155SAli.Bahrami@Sun.COM 
93912155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
94012155SAli.Bahrami@Sun.COM 			assert(gnp);
94112155SAli.Bahrami@Sun.COM 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
94212155SAli.Bahrami@Sun.COM 
94312155SAli.Bahrami@Sun.COM 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
94412155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
94512155SAli.Bahrami@Sun.COM 			Xword	tlsstatsize;
94612155SAli.Bahrami@Sun.COM 
94712155SAli.Bahrami@Sun.COM 			/*
94812155SAli.Bahrami@Sun.COM 			 * This is the LE TLS reference model.  Static
94912155SAli.Bahrami@Sun.COM 			 * offset is hard-coded.
95012155SAli.Bahrami@Sun.COM 			 */
95112155SAli.Bahrami@Sun.COM 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
95212155SAli.Bahrami@Sun.COM 			    M_TLSSTATALIGN);
95312155SAli.Bahrami@Sun.COM 			value = tlsstatsize - value;
9541618Srie 
9551618Srie 			/*
95612155SAli.Bahrami@Sun.COM 			 * Since this code is fixed up, it assumes a negative
95712155SAli.Bahrami@Sun.COM 			 * offset that can be added to the thread pointer.
9581618Srie 			 */
95912155SAli.Bahrami@Sun.COM 			if (arsp->rel_rtype == R_AMD64_TPOFF32)
96012155SAli.Bahrami@Sun.COM 				value = -value;
96112155SAli.Bahrami@Sun.COM 		}
9621618Srie 
96312155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_file)
96412155SAli.Bahrami@Sun.COM 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
96512155SAli.Bahrami@Sun.COM 		else
96612155SAli.Bahrami@Sun.COM 			ifl_name = MSG_INTL(MSG_STR_NULL);
9671618Srie 
96812155SAli.Bahrami@Sun.COM 		/*
96912155SAli.Bahrami@Sun.COM 		 * Make sure we have data to relocate.  Compiler and assembler
97012155SAli.Bahrami@Sun.COM 		 * developers have been known to generate relocations against
97112155SAli.Bahrami@Sun.COM 		 * invalid sections (normally .bss), so for their benefit give
97212155SAli.Bahrami@Sun.COM 		 * them sufficient information to help analyze the problem.
97312155SAli.Bahrami@Sun.COM 		 * End users should never see this.
97412155SAli.Bahrami@Sun.COM 		 */
97512155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
97612155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t inv_buf;
9771618Srie 
978*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
97912155SAli.Bahrami@Sun.COM 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
98012155SAli.Bahrami@Sun.COM 			    ifl_name, ld_reloc_sym_name(arsp),
98112155SAli.Bahrami@Sun.COM 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
98212155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name);
98312155SAli.Bahrami@Sun.COM 			return (S_ERROR);
98412155SAli.Bahrami@Sun.COM 		}
98512155SAli.Bahrami@Sun.COM 
98612155SAli.Bahrami@Sun.COM 		/*
98712155SAli.Bahrami@Sun.COM 		 * Get the address of the data item we need to modify.
98812155SAli.Bahrami@Sun.COM 		 */
98912155SAli.Bahrami@Sun.COM 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
99012155SAli.Bahrami@Sun.COM 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
99112155SAli.Bahrami@Sun.COM 
99212155SAli.Bahrami@Sun.COM 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
99312155SAli.Bahrami@Sun.COM 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
99412155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name));
99512155SAli.Bahrami@Sun.COM 		addr += (uintptr_t)osp->os_outdata->d_buf;
9961618Srie 
99712155SAli.Bahrami@Sun.COM 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
99812155SAli.Bahrami@Sun.COM 		    ofl->ofl_size) || (arsp->rel_roffset >
99912155SAli.Bahrami@Sun.COM 		    osp->os_shdr->sh_size)) {
100012155SAli.Bahrami@Sun.COM 			int		class;
100112155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t inv_buf;
100212155SAli.Bahrami@Sun.COM 
100312155SAli.Bahrami@Sun.COM 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
100412155SAli.Bahrami@Sun.COM 			    ofl->ofl_size)
100512155SAli.Bahrami@Sun.COM 				class = ERR_FATAL;
100612155SAli.Bahrami@Sun.COM 			else
100712155SAli.Bahrami@Sun.COM 				class = ERR_WARNING;
100812155SAli.Bahrami@Sun.COM 
1009*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
101012155SAli.Bahrami@Sun.COM 			    conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
101112155SAli.Bahrami@Sun.COM 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
101212155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
101312155SAli.Bahrami@Sun.COM 			    EC_ADDR((uintptr_t)addr -
101412155SAli.Bahrami@Sun.COM 			    (uintptr_t)ofl->ofl_nehdr));
10151618Srie 
101612155SAli.Bahrami@Sun.COM 			if (class == ERR_FATAL) {
101712155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
101812155SAli.Bahrami@Sun.COM 				continue;
10191618Srie 			}
102012155SAli.Bahrami@Sun.COM 		}
10211618Srie 
102212155SAli.Bahrami@Sun.COM 		/*
102312155SAli.Bahrami@Sun.COM 		 * The relocation is additive.  Ignore the previous symbol
102412155SAli.Bahrami@Sun.COM 		 * value if this local partial symbol is expanded.
102512155SAli.Bahrami@Sun.COM 		 */
102612155SAli.Bahrami@Sun.COM 		if (moved)
102712155SAli.Bahrami@Sun.COM 			value -= *addr;
102812155SAli.Bahrami@Sun.COM 
102912155SAli.Bahrami@Sun.COM 		/*
103012155SAli.Bahrami@Sun.COM 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
103112155SAli.Bahrami@Sun.COM 		 */
103212155SAli.Bahrami@Sun.COM 		if (OFL_DO_RELOC(ofl)) {
10331618Srie 			/*
103412155SAli.Bahrami@Sun.COM 			 * If this is a PROGBITS section and the running linker
103512155SAli.Bahrami@Sun.COM 			 * has a different byte order than the target host,
103612155SAli.Bahrami@Sun.COM 			 * tell do_reloc_ld() to swap bytes.
10371618Srie 			 */
103812155SAli.Bahrami@Sun.COM 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
103912155SAli.Bahrami@Sun.COM 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1040*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_lml) == 0) {
1041*13074SAli.Bahrami@Oracle.COM 				ofl->ofl_flags |= FLG_OF_FATAL;
104212155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
1043*13074SAli.Bahrami@Oracle.COM 			}
10441618Srie 		}
10451618Srie 	}
10461618Srie 	return (return_code);
10471618Srie }
10481618Srie 
10496206Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)10501618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
10511618Srie {
10521618Srie 	Rel_desc	*orsp;
10531618Srie 	Sym_desc	*sdp = rsp->rel_sym;
10541618Srie 
10551618Srie 	/*
10561618Srie 	 * Static executables *do not* want any relocations against them.
10571618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
10581618Srie 	 * symbol in a static executable, it's best to disable them here
10591618Srie 	 * instead of through out the relocation code.
10601618Srie 	 */
106110792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_EXEC(ofl))
10621618Srie 		return (1);
10631618Srie 
10641618Srie 	/*
10651618Srie 	 * If we are adding a output relocation against a section
10661618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
10671618Srie 	 * will be added to the .dynsym symbol table.
10681618Srie 	 */
10691618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
10701618Srie 	    ((flags & FLG_REL_SCNNDX) ||
10711618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
10721618Srie 
10731618Srie 		/*
10741618Srie 		 * If this is a COMMON symbol - no output section
10751618Srie 		 * exists yet - (it's created as part of sym_validate()).
10761618Srie 		 * So - we mark here that when it's created it should
10771618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
10781618Srie 		 */
10791618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10801682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
10811618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
10821618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
10831618Srie 			else
10841618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
10851618Srie 		} else {
108611827SRod.Evans@Sun.COM 			Os_desc *osp;
108711827SRod.Evans@Sun.COM 			Is_desc *isp = sdp->sd_isc;
10881618Srie 
108911827SRod.Evans@Sun.COM 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
109011827SRod.Evans@Sun.COM 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
10911618Srie 				ofl->ofl_dynshdrcnt++;
10921618Srie 				osp->os_flags |= FLG_OS_OUTREL;
10931618Srie 			}
10941618Srie 		}
10951618Srie 	}
10961618Srie 
109712155SAli.Bahrami@Sun.COM 	/* Enter it into the output relocation cache */
109812155SAli.Bahrami@Sun.COM 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
109912155SAli.Bahrami@Sun.COM 		return (S_ERROR);
11001618Srie 
11011618Srie 	if (flags & FLG_REL_GOT)
11021618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
11031618Srie 	else if (flags & FLG_REL_PLT)
11041618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
11051618Srie 	else if (flags & FLG_REL_BSS)
11061618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
11071618Srie 	else if (flags & FLG_REL_NOINFO)
11081618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
11091618Srie 	else
111012155SAli.Bahrami@Sun.COM 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
11111618Srie 
11121618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11131618Srie 		ofl->ofl_relocrelcnt++;
11141618Srie 
11151618Srie 	/*
11161618Srie 	 * We don't perform sorting on PLT relocations because
11171618Srie 	 * they have already been assigned a PLT index and if we
11181618Srie 	 * were to sort them we would have to re-assign the plt indexes.
11191618Srie 	 */
11201618Srie 	if (!(flags & FLG_REL_PLT))
11211618Srie 		ofl->ofl_reloccnt++;
11221618Srie 
11231618Srie 	/*
11241618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11251618Srie 	 */
11261618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11271618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11281618Srie 
11291618Srie 	/*
11301618Srie 	 * Identify and possibly warn of a displacement relocation.
11311618Srie 	 */
11321618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11331618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11341618Srie 
11351618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11361618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11371618Srie 	}
11381618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
11391618Srie 	    M_MACH, orsp));
11401618Srie 	return (1);
11411618Srie }
11421618Srie 
11431618Srie /*
11441618Srie  * process relocation for a LOCAL symbol
11451618Srie  */
11466206Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)11471618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
11481618Srie {
11496299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
11501618Srie 	Sym_desc	*sdp = rsp->rel_sym;
11511682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
11521618Srie 	Word		ortype = rsp->rel_rtype;
11531618Srie 
11541618Srie 	/*
11551618Srie 	 * if ((shared object) and (not pc relative relocation) and
11561618Srie 	 *    (not against ABS symbol))
11571618Srie 	 * then
11581618Srie 	 *	build R_AMD64_RELATIVE
11591618Srie 	 * fi
11601618Srie 	 */
11611618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
11622850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
11631618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
11641618Srie 	    !(rsp->rel_isdesc != NULL &&
11651618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
11661618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
11671618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
11681618Srie 
11691618Srie 		/*
11701618Srie 		 * R_AMD64_RELATIVE updates a 64bit address, if this
11711618Srie 		 * relocation isn't a 64bit binding then we can not
11721618Srie 		 * simplify it to a RELATIVE relocation.
11731618Srie 		 */
11741618Srie 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
11756299Sab196087 			return (ld_add_outrel(0, rsp, ofl));
11761618Srie 		}
11771618Srie 
11781618Srie 		rsp->rel_rtype = R_AMD64_RELATIVE;
11791618Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
11801618Srie 			return (S_ERROR);
11811618Srie 		rsp->rel_rtype = ortype;
11821618Srie 		return (1);
11831618Srie 	}
11841618Srie 
11851618Srie 	/*
11861618Srie 	 * If the relocation is against a 'non-allocatable' section
11871618Srie 	 * and we can not resolve it now - then give a warning
11881618Srie 	 * message.
11891618Srie 	 *
11901618Srie 	 * We can not resolve the symbol if either:
11911618Srie 	 *	a) it's undefined
11921618Srie 	 *	b) it's defined in a shared library and a
11931618Srie 	 *	   COPY relocation hasn't moved it to the executable
11941618Srie 	 *
11951618Srie 	 * Note: because we process all of the relocations against the
11961618Srie 	 *	text segment before any others - we know whether
11971618Srie 	 *	or not a copy relocation will be generated before
11981618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
11991618Srie 	 */
12001618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12011618Srie 	    ((shndx == SHN_UNDEF) ||
12021618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12031618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
120412155SAli.Bahrami@Sun.COM 		Conv_inv_buf_t	inv_buf;
120512155SAli.Bahrami@Sun.COM 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
12064734Sab196087 
12071618Srie 		/*
12081618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12091618Srie 		 * section - then silently ignore that the relocation
12101618Srie 		 * can not be resolved.
12111618Srie 		 */
121212155SAli.Bahrami@Sun.COM 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12131618Srie 			return (0);
1214*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12154734Sab196087 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
12161618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
121712155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name(rsp), osp->os_name);
12181618Srie 		return (1);
12191618Srie 	}
12201618Srie 
12211618Srie 	/*
12221618Srie 	 * Perform relocation.
12231618Srie 	 */
12241618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12251618Srie }
12261618Srie 
12271618Srie 
12286206Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)12291618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12301618Srie {
12311618Srie 	Word		rtype = rsp->rel_rtype;
12321618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12336299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12341618Srie 	Gotndx		*gnp;
12351618Srie 
12361618Srie 	/*
12372145Srie 	 * If we're building an executable - use either the IE or LE access
12382145Srie 	 * model.  If we're building a shared object process any IE model.
12391618Srie 	 */
12402145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12411618Srie 		/*
12422145Srie 		 * Set the DF_STATIC_TLS flag.
12431618Srie 		 */
12441618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12451618Srie 
12462145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12471618Srie 			/*
12482145Srie 			 * Assign a GOT entry for static TLS references.
12491618Srie 			 */
12509131SRod.Evans@Sun.COM 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
12519131SRod.Evans@Sun.COM 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
12522145Srie 
12532145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
12542145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
12552145Srie 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
12562145Srie 					return (S_ERROR);
12571618Srie 			}
12581618Srie 
12591618Srie 			/*
12602145Srie 			 * IE access model.
12611618Srie 			 */
12621618Srie 			if (IS_TLS_IE(rtype))
12631618Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12641618Srie 
12651618Srie 			/*
12662145Srie 			 * Fixups are required for other executable models.
12671618Srie 			 */
12681618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12691618Srie 			    rsp, ofl));
12701618Srie 		}
12712145Srie 
12721618Srie 		/*
12732145Srie 		 * LE access model.
12741618Srie 		 */
12751618Srie 		if (IS_TLS_LE(rtype))
12761618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
12772145Srie 
12781618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
12791618Srie 		    rsp, ofl));
12801618Srie 	}
12811618Srie 
12821618Srie 	/*
12832145Srie 	 * Building a shared object.
12842145Srie 	 *
12852145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
12861618Srie 	 */
12879131SRod.Evans@Sun.COM 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
12889131SRod.Evans@Sun.COM 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
12892145Srie 
12902145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
12919131SRod.Evans@Sun.COM 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
12921618Srie 			return (S_ERROR);
12932145Srie 
12942145Srie 	} else if (IS_TLS_GD(rtype) &&
12959131SRod.Evans@Sun.COM 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
12969131SRod.Evans@Sun.COM 	    ofl, rsp)) == NULL)) {
12972145Srie 
12982145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
12992145Srie 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
13002145Srie 		    R_AMD64_DTPOFF64) == S_ERROR)
13011618Srie 			return (S_ERROR);
13021618Srie 	}
13031618Srie 
13041618Srie 	if (IS_TLS_LD(rtype))
13051618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13061618Srie 
13071618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13081618Srie }
13091618Srie 
13101618Srie /* ARGSUSED5 */
13116206Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)13129131SRod.Evans@Sun.COM ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
13139131SRod.Evans@Sun.COM     Rel_desc *rsp, Sym_desc *sdp)
13141618Srie {
13151618Srie 	Xword		raddend;
13169131SRod.Evans@Sun.COM 	Gotndx		gn, *gnp;
13179131SRod.Evans@Sun.COM 	Aliste		idx;
13181618Srie 	uint_t		gotents;
13191618Srie 
13201618Srie 	raddend = rsp->rel_raddend;
13219131SRod.Evans@Sun.COM 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
13221618Srie 		return (1);
13231618Srie 
13241618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
13251618Srie 		gotents = 2;
13261618Srie 	else
13271618Srie 		gotents = 1;
13281618Srie 
13299131SRod.Evans@Sun.COM 	gn.gn_addend = raddend;
13309131SRod.Evans@Sun.COM 	gn.gn_gotndx = ofl->ofl_gotcnt;
13319131SRod.Evans@Sun.COM 	gn.gn_gotref = gref;
13321618Srie 
13331618Srie 	ofl->ofl_gotcnt += gotents;
13341618Srie 
13351618Srie 	if (gref == GOT_REF_TLSLD) {
13369131SRod.Evans@Sun.COM 		if (ofl->ofl_tlsldgotndx == NULL) {
13379131SRod.Evans@Sun.COM 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
13389131SRod.Evans@Sun.COM 				return (S_ERROR);
13399131SRod.Evans@Sun.COM 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
13409131SRod.Evans@Sun.COM 			ofl->ofl_tlsldgotndx = gnp;
13419131SRod.Evans@Sun.COM 		}
13421618Srie 		return (1);
13431618Srie 	}
13441618Srie 
13459131SRod.Evans@Sun.COM 	idx = 0;
13469131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
13479131SRod.Evans@Sun.COM 		if (gnp->gn_addend > raddend)
13489131SRod.Evans@Sun.COM 			break;
13491618Srie 	}
13509131SRod.Evans@Sun.COM 
13519131SRod.Evans@Sun.COM 	/*
13529131SRod.Evans@Sun.COM 	 * GOT indexes are maintained on an Alist, where there is typically
13539131SRod.Evans@Sun.COM 	 * only one index.  The usage of this list is to scan the list to find
13549131SRod.Evans@Sun.COM 	 * an index, and then apply that index immediately to a relocation.
13559131SRod.Evans@Sun.COM 	 * Thus there are no external references to these GOT index structures
13569131SRod.Evans@Sun.COM 	 * that can be compromised by the Alist being reallocated.
13579131SRod.Evans@Sun.COM 	 */
13589131SRod.Evans@Sun.COM 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
13599131SRod.Evans@Sun.COM 	    AL_CNT_SDP_GOT, idx) == NULL)
13609131SRod.Evans@Sun.COM 		return (S_ERROR);
13619131SRod.Evans@Sun.COM 
13621618Srie 	return (1);
13631618Srie }
13641618Srie 
13656206Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)13661618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
13671618Srie {
13681618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
13691618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
13701618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
13711618Srie }
13721618Srie 
13731618Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = {
13741618Srie /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
13751618Srie /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
13761618Srie /* 0x0c NOP */			0x90,
13771618Srie /* 0x0d NOP */			0x90,
13781618Srie /* 0x0e NOP */			0x90,
13791618Srie /* 0x0f NOP */			0x90
13801618Srie };
13811618Srie 
13821618Srie /*
13831618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
13841618Srie  */
13856206Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)13862145Srie ld_fillin_gotplt(Ofl_desc *ofl)
13871618Srie {
13886206Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
13896206Sab196087 
13901618Srie 	if (ofl->ofl_osgot) {
13912145Srie 		Sym_desc	*sdp;
13921618Srie 
13931618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
139410792SRod.Evans@Sun.COM 		    SYM_NOHASH, NULL, ofl)) != NULL) {
13952145Srie 			uchar_t	*genptr;
13962145Srie 
13972145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
13981618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
13991618Srie 			/* LINTED */
14001618Srie 			*(Xword *)genptr = sdp->sd_sym->st_value;
14016206Sab196087 			if (bswap)
14026206Sab196087 				/* LINTED */
14036206Sab196087 				*(Xword *)genptr =
14046206Sab196087 				    /* LINTED */
14056206Sab196087 				    ld_bswap_Xword(*(Xword *)genptr);
14061618Srie 		}
14071618Srie 	}
14081618Srie 
14091618Srie 	/*
14101618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
14111618Srie 	 * entry is:
14121618Srie 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
14131618Srie 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
14141618Srie 	 *	0x0c NOP
14151618Srie 	 *	0x0d NOP
14161618Srie 	 *	0x0e NOP
14171618Srie 	 *	0x0f NOP
14181618Srie 	 */
14195189Sab196087 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
14202145Srie 		uchar_t	*pltent;
14212145Srie 		Xword	val1;
14221618Srie 
14231618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14241618Srie 		bcopy(plt0_template, pltent, sizeof (plt0_template));
14251618Srie 
14261618Srie 		/*
14275189Sab196087 		 * If '-z noreloc' is specified - skip the do_reloc_ld
14285189Sab196087 		 * stage.
14295189Sab196087 		 */
14305189Sab196087 		if (!OFL_DO_RELOC(ofl))
14315189Sab196087 			return (1);
14325189Sab196087 
14335189Sab196087 		/*
14341618Srie 		 * filin:
14351618Srie 		 *	PUSHQ GOT + 8(%rip)
14361618Srie 		 *
14371618Srie 		 * Note: 0x06 below represents the offset to the
14381618Srie 		 *	 next instruction - which is what %rip will
14391618Srie 		 *	 be pointing at.
14401618Srie 		 */
14411618Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
14424734Sab196087 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
14434734Sab196087 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
14441618Srie 
144512155SAli.Bahrami@Sun.COM 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
144612155SAli.Bahrami@Sun.COM 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
144712155SAli.Bahrami@Sun.COM 		    bswap, ofl->ofl_lml) == 0) {
1448*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
14495189Sab196087 			return (S_ERROR);
14501618Srie 		}
14511618Srie 
14521618Srie 		/*
14531618Srie 		 * filin:
14541618Srie 		 *  JMP	*GOT+16(%rip)
14551618Srie 		 */
14561618Srie 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
14574734Sab196087 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
14584734Sab196087 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
14595189Sab196087 
146012155SAli.Bahrami@Sun.COM 		if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
146112155SAli.Bahrami@Sun.COM 		    &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
146212155SAli.Bahrami@Sun.COM 		    bswap, ofl->ofl_lml) == 0) {
1463*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
14645189Sab196087 			return (S_ERROR);
14651618Srie 		}
14661618Srie 	}
14675189Sab196087 
14681618Srie 	return (1);
14691618Srie }
14706206Sab196087 
14716206Sab196087 
14726206Sab196087 
14736206Sab196087 /*
14746206Sab196087  * Template for generating "void (*)(void)" function
14756206Sab196087  */
14766206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
14776206Sab196087 /* 0x00 */	0x55,				/* pushq  %rbp */
14786206Sab196087 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
14796206Sab196087 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
14806206Sab196087 /* 0x07 */	0x5d,				/* popq   %rbp */
14816206Sab196087 /* 0x08 */	0xc3				/* ret */
14826206Sab196087 };
14836206Sab196087 
14846206Sab196087 
14856206Sab196087 /*
148610809SAli.Bahrami@Sun.COM  * Function used to provide fill padding in SHF_EXECINSTR sections
148710809SAli.Bahrami@Sun.COM  *
148810809SAli.Bahrami@Sun.COM  * entry:
148910809SAli.Bahrami@Sun.COM  *
149010809SAli.Bahrami@Sun.COM  *	base - base address of section being filled
149110809SAli.Bahrami@Sun.COM  *	offset - starting offset for fill within memory referenced by base
149210809SAli.Bahrami@Sun.COM  *	cnt - # bytes to be filled
149310809SAli.Bahrami@Sun.COM  *
149410809SAli.Bahrami@Sun.COM  * exit:
149510809SAli.Bahrami@Sun.COM  *	The fill has been completed.
149610809SAli.Bahrami@Sun.COM  */
149710809SAli.Bahrami@Sun.COM static void
execfill(void * base,off_t off,size_t cnt)149810809SAli.Bahrami@Sun.COM execfill(void *base, off_t off, size_t cnt)
149910809SAli.Bahrami@Sun.COM {
150010809SAli.Bahrami@Sun.COM 	/*
150110809SAli.Bahrami@Sun.COM 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
150210809SAli.Bahrami@Sun.COM 	 * There are no alignment constraints.
150310809SAli.Bahrami@Sun.COM 	 */
150410809SAli.Bahrami@Sun.COM 	(void) memset(off + (char *)base, 0x90, cnt);
150510809SAli.Bahrami@Sun.COM }
150610809SAli.Bahrami@Sun.COM 
150710809SAli.Bahrami@Sun.COM 
150810809SAli.Bahrami@Sun.COM /*
15096206Sab196087  * Return the ld_targ definition for this target.
15106206Sab196087  */
15116206Sab196087 const Target *
ld_targ_init_x86(void)15126206Sab196087 ld_targ_init_x86(void)
15136206Sab196087 {
15146206Sab196087 	static const Target _ld_targ = {
15156206Sab196087 		{			/* Target_mach */
15166206Sab196087 			M_MACH,			/* m_mach */
15176206Sab196087 			M_MACHPLUS,		/* m_machplus */
15186206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
15196206Sab196087 			M_CLASS,		/* m_class */
15206206Sab196087 			M_DATA,			/* m_data */
15216206Sab196087 
15226206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
15236206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
15248501SRod.Evans@Sun.COM 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
15256206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
152611734SAli.Bahrami@Sun.COM 			M_STACK_PERM,		/* m_stack_perm */
15276206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
15286206Sab196087 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
15296206Sab196087 
15306206Sab196087 			/* Relocation type codes */
15316206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
15326206Sab196087 			M_R_COPY,		/* m_r_copy */
15336206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
15346206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
15356206Sab196087 			M_R_NUM,		/* m_r_num */
15366206Sab196087 			M_R_NONE,		/* m_r_none */
15376206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
15386206Sab196087 			M_R_REGISTER,		/* m_r_register */
15396206Sab196087 
15406206Sab196087 			/* Relocation related constants */
15416206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
15426206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
15436206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
15446206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
15456206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
15466206Sab196087 
15476206Sab196087 			/* GOT related constants */
15486206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
15496206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
15506206Sab196087 
15516206Sab196087 			/* PLT related constants */
15526206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
15536206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
15546206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
15556206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
15566206Sab196087 
15579085SAli.Bahrami@Sun.COM 			/* Section type of .eh_frame/.eh_frame_hdr sections */
15589085SAli.Bahrami@Sun.COM 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
15599085SAli.Bahrami@Sun.COM 
15606206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
15616206Sab196087 		},
15626206Sab196087 		{			/* Target_machid */
15636206Sab196087 			M_ID_ARRAY,		/* id_array */
15646206Sab196087 			M_ID_BSS,		/* id_bss */
15656206Sab196087 			M_ID_CAP,		/* id_cap */
156611827SRod.Evans@Sun.COM 			M_ID_CAPINFO,		/* id_capinfo */
156711827SRod.Evans@Sun.COM 			M_ID_CAPCHAIN,		/* id_capchain */
15686206Sab196087 			M_ID_DATA,		/* id_data */
15696206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
15706206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
15716206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
15726206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
15736206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
15746206Sab196087 			M_ID_GOT,		/* id_got */
15756206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
15766206Sab196087 			M_ID_HASH,		/* id_hash */
15776206Sab196087 			M_ID_INTERP,		/* id_interp */
15786206Sab196087 			M_ID_LBSS,		/* id_lbss */
15796206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
15806206Sab196087 			M_ID_NOTE,		/* id_note */
15816206Sab196087 			M_ID_NULL,		/* id_null */
15826206Sab196087 			M_ID_PLT,		/* id_plt */
15836206Sab196087 			M_ID_REL,		/* id_rel */
15846206Sab196087 			M_ID_STRTAB,		/* id_strtab */
15856206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
15866206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
15876206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
15886206Sab196087 			M_ID_TEXT,		/* id_text */
15896206Sab196087 			M_ID_TLS,		/* id_tls */
15906206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
15916206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
15926206Sab196087 			M_ID_UNWIND,		/* id_unwind */
15939085SAli.Bahrami@Sun.COM 			M_ID_UNWINDHDR,		/* id_unwindhdr */
15946206Sab196087 			M_ID_USER,		/* id_user */
15956206Sab196087 			M_ID_VERSION,		/* id_version */
15966206Sab196087 		},
15976206Sab196087 		{			/* Target_nullfunc */
15986206Sab196087 			nullfunc_tmpl,		/* nf_template */
15996206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
16006206Sab196087 		},
160110809SAli.Bahrami@Sun.COM 		{			/* Target_fillfunc */
160210809SAli.Bahrami@Sun.COM 			execfill		/* ff_execfill */
160310809SAli.Bahrami@Sun.COM 		},
16046206Sab196087 		{			/* Target_machrel */
16056206Sab196087 			reloc_table,
16066206Sab196087 
16076206Sab196087 			ld_init_rel,		/* mr_init_rel */
16086206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
16096206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
16106206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
16116206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
16126206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
16136206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
16146206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
16156206Sab196087 			NULL,			/* mr_reloc_register */
16166206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
16176206Sab196087 			NULL,			/* mr_reloc_GOTOP */
16186206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
16196206Sab196087 			NULL,			/* mr_assign_got */
16209131SRod.Evans@Sun.COM 			ld_find_got_ndx,	/* mr_find_got_ndx */
16216206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
16226206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
16236206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
16246206Sab196087 			NULL,			/* mr_allocate_got */
16256206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
16266206Sab196087 		},
16276206Sab196087 		{			/* Target_machsym */
16286206Sab196087 			NULL,			/* ms_reg_check */
16296206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
16306206Sab196087 			NULL,			/* ms_is_regsym */
16316206Sab196087 			NULL,			/* ms_reg_find */
16326206Sab196087 			NULL			/* ms_reg_enter */
16336206Sab196087 		}
16346206Sab196087 	};
16356206Sab196087 
16366206Sab196087 	return (&_ld_targ);
16376206Sab196087 }
1638