xref: /onnv-gate/usr/src/uts/intel/amd64/krtld/doreloc.c (revision 12155:50d9d4aeb501)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * 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.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211169Srie 
220Sstevel@tonic-gate /*
23*12155SAli.Bahrami@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #if	defined(_KERNEL)
270Sstevel@tonic-gate #include	<sys/types.h>
280Sstevel@tonic-gate #include	"reloc.h"
290Sstevel@tonic-gate #else
306206Sab196087 #define	ELF_TARGET_AMD64
316206Sab196087 #if defined(DO_RELOC_LIBLD)
326206Sab196087 #undef DO_RELOC_LIBLD
336206Sab196087 #define	DO_RELOC_LIBLD_X86
346206Sab196087 #endif
350Sstevel@tonic-gate #include	<stdio.h>
360Sstevel@tonic-gate #include	"sgs.h"
370Sstevel@tonic-gate #include	"machdep.h"
380Sstevel@tonic-gate #include	"libld.h"
390Sstevel@tonic-gate #include	"reloc.h"
400Sstevel@tonic-gate #include	"conv.h"
410Sstevel@tonic-gate #include	"msg.h"
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
456206Sab196087  * We need to build this code differently when it is used for
466206Sab196087  * cross linking:
476206Sab196087  *	- Data alignment requirements can differ from those
486206Sab196087  *		of the running system, so we can't access data
496206Sab196087  *		in units larger than a byte
506206Sab196087  *	- We have to include code to do byte swapping when the
516206Sab196087  *		target and linker host use different byte ordering,
526206Sab196087  *		but such code is a waste when running natively.
536206Sab196087  */
546206Sab196087 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
556206Sab196087 #define	DORELOC_NATIVE
566206Sab196087 #endif
576206Sab196087 
586206Sab196087 /*
591169Srie  * This table represents the current relocations that do_reloc() is able to
601169Srie  * process.  The relocations below that are marked SPECIAL are relocations that
611169Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate const Rel_entry	reloc_table[R_AMD64_NUM] = {
646206Sab196087 /* R_AMD64_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
656206Sab196087 /* R_AMD64_64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
666206Sab196087 /* R_AMD64_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
676206Sab196087 /* R_AMD64_GOT32 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
686206Sab196087 /* R_AMD64_PLT32 */	{0, FLG_RE_PCREL | FLG_RE_PLTREL |
696206Sab196087 			    FLG_RE_VERIFY | FLG_RE_SIGN, 4, 0, 0},
706206Sab196087 /* R_AMD64_COPY */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
716206Sab196087 /* R_AMD64_GLOB_DAT */	{0, FLG_RE_NOTREL, 8, 0, 0},
726206Sab196087 /* R_AMD64_JUMP_SLOT */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
736206Sab196087 /* R_AMD64_RELATIVE */	{0, FLG_RE_NOTREL, 8, 0, 0},
746206Sab196087 /* R_AMD64_GOTPCREL */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD, 4, 0, 0},
756206Sab196087 /* R_AMD64_32 */	{0, FLG_RE_NOTREL, 4, 0, 0},
766206Sab196087 /* R_AMD64_32S */	{0, FLG_RE_NOTREL, 4, 0, 0},
776206Sab196087 /* R_AMD64_16 */	{0, FLG_RE_NOTREL, 2, 0, 0},
786206Sab196087 /* R_AMD64_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
796206Sab196087 /* R_AMD64_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
806206Sab196087 /* R_AMD64_PC8 */	{0, FLG_RE_PCREL, 1, 0, 0},
816206Sab196087 /* R_AMD64_DTPMOD64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
826206Sab196087 /* R_AMD64_DTPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
836206Sab196087 /* R_AMD64_TPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
846206Sab196087 /* R_AMD64_TLSGD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSGD,
856206Sab196087 			    4, 0, 0},
866206Sab196087 /* R_AMD64_TLSLD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSLD,
876206Sab196087 			    4, 0, 0},
886206Sab196087 /* R_AMD64_DTPOFF32 */	{0, FLG_RE_TLSLD, 4},
896206Sab196087 /* R_AMD64_GOTTPOFF */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSIE,
906206Sab196087 			    4, 0, 0},
916206Sab196087 /* R_AMD64_TPOFF32 */	{0, FLG_RE_TLSLE, 4, 0, 0},
926206Sab196087 /* R_AMD64_PC64 */	{0, FLG_RE_PCREL, 8, 0, 0},
936206Sab196087 /* R_AMD64_GOTOFF64 */	{0, FLG_RE_GOTREL, 8, 0, 0},
946206Sab196087 /* R_AMD64_GOTPC32 */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND,
956206Sab196087 			    4, 0, 0},
966206Sab196087 /* R_AMD64_GOT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
976206Sab196087 /* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0, 0, 0},
986206Sab196087 /* R_AMD64_GOTPC6 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
996206Sab196087 /* R_AMD64_GOTPLT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
1006206Sab196087 /* R_AMD64_PLTOFF64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
1016206Sab196087 /* R_AMD64_SIZE32 */	{0, FLG_RE_SIZE, 4, 0, 0},
1026206Sab196087 /* R_AMD64_SIZE64 */	{0, FLG_RE_SIZE, 8, 0, 0}
1030Sstevel@tonic-gate };
1042850Srie #if	(R_AMD64_NUM != (R_AMD64_SIZE64 + 1))
1050Sstevel@tonic-gate #error	"R_AMD64_NUM has grown"
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Write a single relocated value to its reference location.
1101169Srie  * We assume we wish to add the relocation amount, value, to the
1110Sstevel@tonic-gate  * value of the address already present at the offset.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * R_AMD64_NONE		 0	none		none
1160Sstevel@tonic-gate  * R_AMD64_64		 1	word64		S + A
1170Sstevel@tonic-gate  * R_AMD64_PC32		 2	word64		S + A
1180Sstevel@tonic-gate  * R_AMD64_GOT32	 3	word32		G + A
1190Sstevel@tonic-gate  * R_AMD64_PLT32	 4	word32		L + A - P
1200Sstevel@tonic-gate  * R_AMD64_COPY		 5	none		none
1210Sstevel@tonic-gate  * R_AMD64_GLOB_DAT	 6	word64		S
1220Sstevel@tonic-gate  * R_AMD64_JUMP_SLOT	 7	word64		S
1230Sstevel@tonic-gate  * R_AMD64_RELATIVE	 8	word64		B + A
1240Sstevel@tonic-gate  * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
1250Sstevel@tonic-gate  * R_AMD64_32		10	word32		S + A
1260Sstevel@tonic-gate  * R_AMD64_32S		11	word32		S + A
1270Sstevel@tonic-gate  * R_AMD64_16		12	word16		S + A
1280Sstevel@tonic-gate  * R_AMD64_PC16		13	word16		S + A - P
1290Sstevel@tonic-gate  * R_AMD64_8		14	word8		S + A
1300Sstevel@tonic-gate  * R_AMD64_PC8		15	word8		S + A - P
1310Sstevel@tonic-gate  * R_AMD64_DTPMOD64	16	word64
1320Sstevel@tonic-gate  * R_AMD64_DTPOFF64	17	word64
1330Sstevel@tonic-gate  * R_AMD64_TPOFF64	18	word64
1340Sstevel@tonic-gate  * R_AMD64_TLSGD	19	word32
1350Sstevel@tonic-gate  * R_AMD64_TLSLD	20	word32
1360Sstevel@tonic-gate  * R_AMD64_DTPOFF32	21	word32
1370Sstevel@tonic-gate  * R_AMD64_GOTTPOFF	22	word32
1380Sstevel@tonic-gate  * R_AMD64_TPOFF32	23	word32
1390Sstevel@tonic-gate  * R_AMD64_PC64		24	word32		S + A - P
1400Sstevel@tonic-gate  * R_AMD64_GOTOFF64	25	word32		S + A - GOT
1410Sstevel@tonic-gate  * R_AMD64_GOTPC32	26	word32		GOT + A - P
1421169Srie  * R_AMD64_GOT64	27			reserved for future expansion
1431169Srie  * R_AMD64_GOTPCREL64	28			reserved for future expansion
1441169Srie  * R_AMD64_GOTPC64	29			reserved for future expansion
1451169Srie  * R_AMD64_GOTPLT64	30			reserved for future expansion
1461169Srie  * R_AMD64_PLTOFF64	31			reserved for future expansion
1472850Srie  * R_AMD64_SIZE32	32	word32		Z + A
1482850Srie  * R_AMD64_SIZE64	33	word64		Z + A
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * Relocation calculations:
1510Sstevel@tonic-gate  *	A	Represents the addend used to compute the value of the
1520Sstevel@tonic-gate  *		relocatable field.
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  *	B	Represents the base address at which a shared objects has
1550Sstevel@tonic-gate  *		been loaded into memory during executaion.  Generally, a
1560Sstevel@tonic-gate  *		shared objects is built with a 0 base virtual address,
1570Sstevel@tonic-gate  *		but the execution address will be different.
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  *	G	Represents the offset into the global offset table
1600Sstevel@tonic-gate  *		at which the relocation entry's symbol will reside
1610Sstevel@tonic-gate  *		during execution.
1620Sstevel@tonic-gate  *
1630Sstevel@tonic-gate  *	GOT	Rrepresents the address of the global offset table.
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  *	L	Represents the place (section offset or address) of
1660Sstevel@tonic-gate  *		the Procedure Linkage Table entry for a symbol.
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *	P	Represents the place (section offset or address) of the
1690Sstevel@tonic-gate  *		storage unit being relocated (computed using r_offset).
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  *	S	Represents the value of the symbol whose index resides
1720Sstevel@tonic-gate  *		in the relocation entry.
1732850Srie  *
1742850Srie  *	Z	the size of the symbol whose index resides in the relocation
1752850Srie  *		entry
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate 
178238Sseizo #define	HIBITS	0xffffffff80000000ULL
1790Sstevel@tonic-gate 
1805189Sab196087 #if defined(_KERNEL)
1815189Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
1825189Sab196087 int
do_reloc_krtld(uchar_t rtype,uchar_t * off,Xword * value,const char * sym,const char * file)1835189Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1845189Sab196087     const char *file)
1855189Sab196087 #elif defined(DO_RELOC_LIBLD)
1866206Sab196087 /*ARGSUSED5*/
1870Sstevel@tonic-gate int
188*12155SAli.Bahrami@Sun.COM do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
189*12155SAli.Bahrami@Sun.COM     rel_desc_sname_func_t rel_desc_sname_func,
1905189Sab196087     const char *file, int bswap, void *lml)
1915189Sab196087 #else
1925189Sab196087 int
1935189Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1941618Srie     const char *file, void *lml)
1955189Sab196087 #endif
1960Sstevel@tonic-gate {
197*12155SAli.Bahrami@Sun.COM #ifdef DO_RELOC_LIBLD
198*12155SAli.Bahrami@Sun.COM #define	sym (* rel_desc_sname_func)(rdesc)
199*12155SAli.Bahrami@Sun.COM 	uchar_t	rtype = rdesc->rel_rtype;
200*12155SAli.Bahrami@Sun.COM #endif
2010Sstevel@tonic-gate 	const Rel_entry	*rep;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	rep = &reloc_table[rtype];
2040Sstevel@tonic-gate 
2051169Srie 	switch (rep->re_fsize) {
2061169Srie 	case 1:
2071169Srie 		/* LINTED */
2081169Srie 		*((uchar_t *)off) = (uchar_t)(*value);
2091169Srie 		break;
2106206Sab196087 
2111169Srie 	case 2:
2126206Sab196087 #if defined(DORELOC_NATIVE)
2131169Srie 		/* LINTED */
2141169Srie 		*((Half *)off) = (Half)(*value);
2156206Sab196087 #else
2166206Sab196087 		{
2176206Sab196087 			Half	v = (Half)(*value);
2186206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
2196206Sab196087 
2206206Sab196087 			if (bswap) {
2216206Sab196087 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
2226206Sab196087 			} else {
2236206Sab196087 				UL_ASSIGN_HALF(off, v_bytes);
2246206Sab196087 			}
2256206Sab196087 		}
2266206Sab196087 #endif
2271169Srie 		break;
2286206Sab196087 
2291169Srie 	case 4:
2300Sstevel@tonic-gate 		/*
2311169Srie 		 * The amd64 psABI requires that we perform the following
2321169Srie 		 * verifications:
2330Sstevel@tonic-gate 		 *
2341169Srie 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
2351169Srie 		 *    computed value to 32bits.  Verify that the generated value
2361169Srie 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
2371169Srie 		 *    extends) to the original 64-bit value.
2380Sstevel@tonic-gate 		 *
2391169Srie 		 * Also, the following relocations are all 32 bit PC relative
2401169Srie 		 * references.  Validate that the value being written will fit
2411169Srie 		 * in the field provided.
2420Sstevel@tonic-gate 		 *
2431169Srie 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
2440Sstevel@tonic-gate 		 */
2450Sstevel@tonic-gate 		if (rtype == R_AMD64_32) {
2460Sstevel@tonic-gate 			/*
2471169Srie 			 * Verify that this value will 'zero-extend', this
2481169Srie 			 * requires that the upper 33bits all be 'zero'.
2490Sstevel@tonic-gate 			 */
2500Sstevel@tonic-gate 			if ((*value & HIBITS) != 0) {
2510Sstevel@tonic-gate 				/*
2521169Srie 				 * To keep chkmsg() happy:
2530Sstevel@tonic-gate 				 *  MSG_INTL(MSG_REL_NOFIT)
2540Sstevel@tonic-gate 				 */
2551618Srie 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
2560Sstevel@tonic-gate 				return (0);
2570Sstevel@tonic-gate 			}
2581169Srie 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
2591169Srie 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
2600Sstevel@tonic-gate 			/*
2611169Srie 			 * Verify that this value will properly sign extend.
2621169Srie 			 * This is true of the upper 33bits are all either
2631169Srie 			 * 'zero' or all 'one'.
2640Sstevel@tonic-gate 			 */
2650Sstevel@tonic-gate 			if (((*value & HIBITS) != HIBITS) &&
2660Sstevel@tonic-gate 			    ((*value & HIBITS) != 0)) {
2670Sstevel@tonic-gate 				/*
2681169Srie 				 * To keep chkmsg() happy:
2690Sstevel@tonic-gate 				 *  MSG_INTL(MSG_REL_NOFIT)
2700Sstevel@tonic-gate 				 */
2711618Srie 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
2720Sstevel@tonic-gate 				return (0);
2730Sstevel@tonic-gate 			}
2740Sstevel@tonic-gate 		}
2756206Sab196087 
2766206Sab196087 #if defined(DORELOC_NATIVE)
2770Sstevel@tonic-gate 		/* LINTED */
2780Sstevel@tonic-gate 		*((Word *)off) += *value;
2796206Sab196087 #else
2806206Sab196087 		{
2816206Sab196087 			Word	v;
2826206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
2836206Sab196087 
2846206Sab196087 			if (bswap) {
2856206Sab196087 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
2866206Sab196087 				v += *value;
2876206Sab196087 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
2886206Sab196087 			} else {
2896206Sab196087 				UL_ASSIGN_WORD(v_bytes, off);
2906206Sab196087 				v += *value;
2916206Sab196087 				UL_ASSIGN_WORD(off, v_bytes);
2926206Sab196087 			}
2936206Sab196087 		}
2946206Sab196087 #endif
2951169Srie 		break;
2966206Sab196087 
2971169Srie 	case 8:
2986206Sab196087 #if defined(DORELOC_NATIVE)
2991169Srie 		/* LINTED */
3000Sstevel@tonic-gate 		*((Xword *)off) += *value;
3016206Sab196087 #else
3026206Sab196087 		{
3036206Sab196087 			Xword	v;
3046206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
3056206Sab196087 
3066206Sab196087 			if (bswap) {
3076206Sab196087 				UL_ASSIGN_BSWAP_XWORD(v_bytes, off);
3086206Sab196087 				v += *value;
3096206Sab196087 				UL_ASSIGN_BSWAP_XWORD(off, v_bytes);
3106206Sab196087 			} else {
3116206Sab196087 				UL_ASSIGN_XWORD(v_bytes, off);
3126206Sab196087 				v += *value;
3136206Sab196087 				UL_ASSIGN_XWORD(off, v_bytes);
3146206Sab196087 			}
3156206Sab196087 		}
3166206Sab196087 #endif
3171169Srie 		break;
3181169Srie 	default:
3191169Srie 		/*
3201169Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
3211169Srie 		 */
3221618Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
3230Sstevel@tonic-gate 		return (0);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	return (1);
326*12155SAli.Bahrami@Sun.COM 
327*12155SAli.Bahrami@Sun.COM #ifdef DO_RELOC_LIBLD
328*12155SAli.Bahrami@Sun.COM #undef sym
329*12155SAli.Bahrami@Sun.COM #endif
3300Sstevel@tonic-gate }
331