xref: /onnv-gate/usr/src/uts/intel/amd64/krtld/doreloc.c (revision 6206:6b0ed502a8e7)
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*6206Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #if	defined(_KERNEL)
300Sstevel@tonic-gate #include	<sys/types.h>
310Sstevel@tonic-gate #include	"reloc.h"
320Sstevel@tonic-gate #else
33*6206Sab196087 #define	ELF_TARGET_AMD64
34*6206Sab196087 #if defined(DO_RELOC_LIBLD)
35*6206Sab196087 #undef DO_RELOC_LIBLD
36*6206Sab196087 #define	DO_RELOC_LIBLD_X86
37*6206Sab196087 #endif
380Sstevel@tonic-gate #include	<stdio.h>
390Sstevel@tonic-gate #include	"sgs.h"
400Sstevel@tonic-gate #include	"machdep.h"
410Sstevel@tonic-gate #include	"libld.h"
420Sstevel@tonic-gate #include	"reloc.h"
430Sstevel@tonic-gate #include	"conv.h"
440Sstevel@tonic-gate #include	"msg.h"
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
48*6206Sab196087  * We need to build this code differently when it is used for
49*6206Sab196087  * cross linking:
50*6206Sab196087  *	- Data alignment requirements can differ from those
51*6206Sab196087  *		of the running system, so we can't access data
52*6206Sab196087  *		in units larger than a byte
53*6206Sab196087  *	- We have to include code to do byte swapping when the
54*6206Sab196087  *		target and linker host use different byte ordering,
55*6206Sab196087  *		but such code is a waste when running natively.
56*6206Sab196087  */
57*6206Sab196087 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
58*6206Sab196087 #define	DORELOC_NATIVE
59*6206Sab196087 #endif
60*6206Sab196087 
61*6206Sab196087 /*
621169Srie  * This table represents the current relocations that do_reloc() is able to
631169Srie  * process.  The relocations below that are marked SPECIAL are relocations that
641169Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate const Rel_entry	reloc_table[R_AMD64_NUM] = {
67*6206Sab196087 /* R_AMD64_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
68*6206Sab196087 /* R_AMD64_64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
69*6206Sab196087 /* R_AMD64_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
70*6206Sab196087 /* R_AMD64_GOT32 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
71*6206Sab196087 /* R_AMD64_PLT32 */	{0, FLG_RE_PCREL | FLG_RE_PLTREL |
72*6206Sab196087 			    FLG_RE_VERIFY | FLG_RE_SIGN, 4, 0, 0},
73*6206Sab196087 /* R_AMD64_COPY */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
74*6206Sab196087 /* R_AMD64_GLOB_DAT */	{0, FLG_RE_NOTREL, 8, 0, 0},
75*6206Sab196087 /* R_AMD64_JUMP_SLOT */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
76*6206Sab196087 /* R_AMD64_RELATIVE */	{0, FLG_RE_NOTREL, 8, 0, 0},
77*6206Sab196087 /* R_AMD64_GOTPCREL */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD, 4, 0, 0},
78*6206Sab196087 /* R_AMD64_32 */	{0, FLG_RE_NOTREL, 4, 0, 0},
79*6206Sab196087 /* R_AMD64_32S */	{0, FLG_RE_NOTREL, 4, 0, 0},
80*6206Sab196087 /* R_AMD64_16 */	{0, FLG_RE_NOTREL, 2, 0, 0},
81*6206Sab196087 /* R_AMD64_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
82*6206Sab196087 /* R_AMD64_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
83*6206Sab196087 /* R_AMD64_PC8 */	{0, FLG_RE_PCREL, 1, 0, 0},
84*6206Sab196087 /* R_AMD64_DTPMOD64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
85*6206Sab196087 /* R_AMD64_DTPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
86*6206Sab196087 /* R_AMD64_TPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
87*6206Sab196087 /* R_AMD64_TLSGD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSGD,
88*6206Sab196087 			    4, 0, 0},
89*6206Sab196087 /* R_AMD64_TLSLD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSLD,
90*6206Sab196087 			    4, 0, 0},
91*6206Sab196087 /* R_AMD64_DTPOFF32 */	{0, FLG_RE_TLSLD, 4},
92*6206Sab196087 /* R_AMD64_GOTTPOFF */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSIE,
93*6206Sab196087 			    4, 0, 0},
94*6206Sab196087 /* R_AMD64_TPOFF32 */	{0, FLG_RE_TLSLE, 4, 0, 0},
95*6206Sab196087 /* R_AMD64_PC64 */	{0, FLG_RE_PCREL, 8, 0, 0},
96*6206Sab196087 /* R_AMD64_GOTOFF64 */	{0, FLG_RE_GOTREL, 8, 0, 0},
97*6206Sab196087 /* R_AMD64_GOTPC32 */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND,
98*6206Sab196087 			    4, 0, 0},
99*6206Sab196087 /* R_AMD64_GOT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
100*6206Sab196087 /* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0, 0, 0},
101*6206Sab196087 /* R_AMD64_GOTPC6 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102*6206Sab196087 /* R_AMD64_GOTPLT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
103*6206Sab196087 /* R_AMD64_PLTOFF64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
104*6206Sab196087 /* R_AMD64_SIZE32 */	{0, FLG_RE_SIZE, 4, 0, 0},
105*6206Sab196087 /* R_AMD64_SIZE64 */	{0, FLG_RE_SIZE, 8, 0, 0}
1060Sstevel@tonic-gate };
1072850Srie #if	(R_AMD64_NUM != (R_AMD64_SIZE64 + 1))
1080Sstevel@tonic-gate #error	"R_AMD64_NUM has grown"
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Write a single relocated value to its reference location.
1131169Srie  * We assume we wish to add the relocation amount, value, to the
1140Sstevel@tonic-gate  * value of the address already present at the offset.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  * R_AMD64_NONE		 0	none		none
1190Sstevel@tonic-gate  * R_AMD64_64		 1	word64		S + A
1200Sstevel@tonic-gate  * R_AMD64_PC32		 2	word64		S + A
1210Sstevel@tonic-gate  * R_AMD64_GOT32	 3	word32		G + A
1220Sstevel@tonic-gate  * R_AMD64_PLT32	 4	word32		L + A - P
1230Sstevel@tonic-gate  * R_AMD64_COPY		 5	none		none
1240Sstevel@tonic-gate  * R_AMD64_GLOB_DAT	 6	word64		S
1250Sstevel@tonic-gate  * R_AMD64_JUMP_SLOT	 7	word64		S
1260Sstevel@tonic-gate  * R_AMD64_RELATIVE	 8	word64		B + A
1270Sstevel@tonic-gate  * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
1280Sstevel@tonic-gate  * R_AMD64_32		10	word32		S + A
1290Sstevel@tonic-gate  * R_AMD64_32S		11	word32		S + A
1300Sstevel@tonic-gate  * R_AMD64_16		12	word16		S + A
1310Sstevel@tonic-gate  * R_AMD64_PC16		13	word16		S + A - P
1320Sstevel@tonic-gate  * R_AMD64_8		14	word8		S + A
1330Sstevel@tonic-gate  * R_AMD64_PC8		15	word8		S + A - P
1340Sstevel@tonic-gate  * R_AMD64_DTPMOD64	16	word64
1350Sstevel@tonic-gate  * R_AMD64_DTPOFF64	17	word64
1360Sstevel@tonic-gate  * R_AMD64_TPOFF64	18	word64
1370Sstevel@tonic-gate  * R_AMD64_TLSGD	19	word32
1380Sstevel@tonic-gate  * R_AMD64_TLSLD	20	word32
1390Sstevel@tonic-gate  * R_AMD64_DTPOFF32	21	word32
1400Sstevel@tonic-gate  * R_AMD64_GOTTPOFF	22	word32
1410Sstevel@tonic-gate  * R_AMD64_TPOFF32	23	word32
1420Sstevel@tonic-gate  * R_AMD64_PC64		24	word32		S + A - P
1430Sstevel@tonic-gate  * R_AMD64_GOTOFF64	25	word32		S + A - GOT
1440Sstevel@tonic-gate  * R_AMD64_GOTPC32	26	word32		GOT + A - P
1451169Srie  * R_AMD64_GOT64	27			reserved for future expansion
1461169Srie  * R_AMD64_GOTPCREL64	28			reserved for future expansion
1471169Srie  * R_AMD64_GOTPC64	29			reserved for future expansion
1481169Srie  * R_AMD64_GOTPLT64	30			reserved for future expansion
1491169Srie  * R_AMD64_PLTOFF64	31			reserved for future expansion
1502850Srie  * R_AMD64_SIZE32	32	word32		Z + A
1512850Srie  * R_AMD64_SIZE64	33	word64		Z + A
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * Relocation calculations:
1540Sstevel@tonic-gate  *	A	Represents the addend used to compute the value of the
1550Sstevel@tonic-gate  *		relocatable field.
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  *	B	Represents the base address at which a shared objects has
1580Sstevel@tonic-gate  *		been loaded into memory during executaion.  Generally, a
1590Sstevel@tonic-gate  *		shared objects is built with a 0 base virtual address,
1600Sstevel@tonic-gate  *		but the execution address will be different.
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  *	G	Represents the offset into the global offset table
1630Sstevel@tonic-gate  *		at which the relocation entry's symbol will reside
1640Sstevel@tonic-gate  *		during execution.
1650Sstevel@tonic-gate  *
1660Sstevel@tonic-gate  *	GOT	Rrepresents the address of the global offset table.
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *	L	Represents the place (section offset or address) of
1690Sstevel@tonic-gate  *		the Procedure Linkage Table entry for a symbol.
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  *	P	Represents the place (section offset or address) of the
1720Sstevel@tonic-gate  *		storage unit being relocated (computed using r_offset).
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  *	S	Represents the value of the symbol whose index resides
1750Sstevel@tonic-gate  *		in the relocation entry.
1762850Srie  *
1772850Srie  *	Z	the size of the symbol whose index resides in the relocation
1782850Srie  *		entry
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate 
181238Sseizo #define	HIBITS	0xffffffff80000000ULL
1820Sstevel@tonic-gate 
1835189Sab196087 #if defined(_KERNEL)
1845189Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
1855189Sab196087 int
1865189Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1875189Sab196087     const char *file)
1885189Sab196087 #elif defined(DO_RELOC_LIBLD)
189*6206Sab196087 /*ARGSUSED5*/
1900Sstevel@tonic-gate int
1915189Sab196087 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1925189Sab196087     const char *file, int bswap, void *lml)
1935189Sab196087 #else
1945189Sab196087 int
1955189Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1961618Srie     const char *file, void *lml)
1975189Sab196087 #endif
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	const Rel_entry	*rep;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	rep = &reloc_table[rtype];
2020Sstevel@tonic-gate 
2031169Srie 	switch (rep->re_fsize) {
2041169Srie 	case 1:
2051169Srie 		/* LINTED */
2061169Srie 		*((uchar_t *)off) = (uchar_t)(*value);
2071169Srie 		break;
208*6206Sab196087 
2091169Srie 	case 2:
210*6206Sab196087 #if defined(DORELOC_NATIVE)
2111169Srie 		/* LINTED */
2121169Srie 		*((Half *)off) = (Half)(*value);
213*6206Sab196087 #else
214*6206Sab196087 		{
215*6206Sab196087 			Half	v = (Half)(*value);
216*6206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
217*6206Sab196087 
218*6206Sab196087 			if (bswap) {
219*6206Sab196087 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
220*6206Sab196087 			} else {
221*6206Sab196087 				UL_ASSIGN_HALF(off, v_bytes);
222*6206Sab196087 			}
223*6206Sab196087 		}
224*6206Sab196087 #endif
2251169Srie 		break;
226*6206Sab196087 
2271169Srie 	case 4:
2280Sstevel@tonic-gate 		/*
2291169Srie 		 * The amd64 psABI requires that we perform the following
2301169Srie 		 * verifications:
2310Sstevel@tonic-gate 		 *
2321169Srie 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
2331169Srie 		 *    computed value to 32bits.  Verify that the generated value
2341169Srie 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
2351169Srie 		 *    extends) to the original 64-bit value.
2360Sstevel@tonic-gate 		 *
2371169Srie 		 * Also, the following relocations are all 32 bit PC relative
2381169Srie 		 * references.  Validate that the value being written will fit
2391169Srie 		 * in the field provided.
2400Sstevel@tonic-gate 		 *
2411169Srie 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
2420Sstevel@tonic-gate 		 */
2430Sstevel@tonic-gate 		if (rtype == R_AMD64_32) {
2440Sstevel@tonic-gate 			/*
2451169Srie 			 * Verify that this value will 'zero-extend', this
2461169Srie 			 * requires that the upper 33bits all be 'zero'.
2470Sstevel@tonic-gate 			 */
2480Sstevel@tonic-gate 			if ((*value & HIBITS) != 0) {
2490Sstevel@tonic-gate 				/*
2501169Srie 				 * To keep chkmsg() happy:
2510Sstevel@tonic-gate 				 *  MSG_INTL(MSG_REL_NOFIT)
2520Sstevel@tonic-gate 				 */
2531618Srie 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
2540Sstevel@tonic-gate 				return (0);
2550Sstevel@tonic-gate 			}
2561169Srie 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
2571169Srie 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
2580Sstevel@tonic-gate 			/*
2591169Srie 			 * Verify that this value will properly sign extend.
2601169Srie 			 * This is true of the upper 33bits are all either
2611169Srie 			 * 'zero' or all 'one'.
2620Sstevel@tonic-gate 			 */
2630Sstevel@tonic-gate 			if (((*value & HIBITS) != HIBITS) &&
2640Sstevel@tonic-gate 			    ((*value & HIBITS) != 0)) {
2650Sstevel@tonic-gate 				/*
2661169Srie 				 * To keep chkmsg() happy:
2670Sstevel@tonic-gate 				 *  MSG_INTL(MSG_REL_NOFIT)
2680Sstevel@tonic-gate 				 */
2691618Srie 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
2700Sstevel@tonic-gate 				return (0);
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		}
273*6206Sab196087 
274*6206Sab196087 #if defined(DORELOC_NATIVE)
2750Sstevel@tonic-gate 		/* LINTED */
2760Sstevel@tonic-gate 		*((Word *)off) += *value;
277*6206Sab196087 #else
278*6206Sab196087 		{
279*6206Sab196087 			Word	v;
280*6206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
281*6206Sab196087 
282*6206Sab196087 			if (bswap) {
283*6206Sab196087 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
284*6206Sab196087 				v += *value;
285*6206Sab196087 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
286*6206Sab196087 			} else {
287*6206Sab196087 				UL_ASSIGN_WORD(v_bytes, off);
288*6206Sab196087 				v += *value;
289*6206Sab196087 				UL_ASSIGN_WORD(off, v_bytes);
290*6206Sab196087 			}
291*6206Sab196087 		}
292*6206Sab196087 #endif
2931169Srie 		break;
294*6206Sab196087 
2951169Srie 	case 8:
296*6206Sab196087 #if defined(DORELOC_NATIVE)
2971169Srie 		/* LINTED */
2980Sstevel@tonic-gate 		*((Xword *)off) += *value;
299*6206Sab196087 #else
300*6206Sab196087 		{
301*6206Sab196087 			Xword	v;
302*6206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
303*6206Sab196087 
304*6206Sab196087 			if (bswap) {
305*6206Sab196087 				UL_ASSIGN_BSWAP_XWORD(v_bytes, off);
306*6206Sab196087 				v += *value;
307*6206Sab196087 				UL_ASSIGN_BSWAP_XWORD(off, v_bytes);
308*6206Sab196087 			} else {
309*6206Sab196087 				UL_ASSIGN_XWORD(v_bytes, off);
310*6206Sab196087 				v += *value;
311*6206Sab196087 				UL_ASSIGN_XWORD(off, v_bytes);
312*6206Sab196087 			}
313*6206Sab196087 		}
314*6206Sab196087 #endif
3151169Srie 		break;
3161169Srie 	default:
3171169Srie 		/*
3181169Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
3191169Srie 		 */
3201618Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
3210Sstevel@tonic-gate 		return (0);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	return (1);
3240Sstevel@tonic-gate }
325