xref: /onnv-gate/usr/src/uts/intel/ia32/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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
251169Srie 
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_386
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_386_NUM] = {
646206Sab196087 /* R_386_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
656206Sab196087 /* R_386_32 */		{0, FLG_RE_NOTREL, 4, 0, 0},
666206Sab196087 /* R_386_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
676206Sab196087 /* R_386_GOT32 */	{0, FLG_RE_GOTADD, 4, 0, 0},
686206Sab196087 /* R_386_PLT32 */	{0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
696206Sab196087 /* R_386_COPY */	{0, FLG_RE_NOTREL, 0, 0, 0},		/* SPECIAL */
706206Sab196087 /* R_386_GLOB_DAT */	{0, FLG_RE_NOTREL, 4, 0, 0},
716206Sab196087 /* R_386_JMP_SLOT */	{0, FLG_RE_NOTREL, 4, 0, 0},		/* SPECIAL */
726206Sab196087 /* R_386_RELATIVE */	{0, FLG_RE_NOTREL, 4, 0, 0},
736206Sab196087 /* R_386_GOTOFF */	{0, FLG_RE_GOTREL, 4, 0, 0},
746206Sab196087 /* R_386_GOTPC */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4,
756206Sab196087 			    0, 0},
766206Sab196087 /* R_386_32PLT */	{0, FLG_RE_PLTREL, 4, 0, 0},
776206Sab196087 /* R_386_TLS_GD_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4,
786206Sab196087 			    0, 0},
796206Sab196087 /* R_386_TLS_LDM_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4,
806206Sab196087 			    0, 0},
816206Sab196087 /* R_386_TLS_TPOFF */	{0, FLG_RE_NOTREL, 4, 0, 0},
826206Sab196087 /* R_386_TLS_IE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
836206Sab196087 /* R_386_TLS_GOTIE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
846206Sab196087 /* R_386_TLS_LE */	{0, FLG_RE_TLSLE, 4, 0, 0},
856206Sab196087 /* R_386_TLS_GD */	{0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
866206Sab196087 /* R_386_TLS_LDM */	{0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
876206Sab196087 /* R_386_16 */		{0, FLG_RE_NOTREL, 2, 0, 0},
886206Sab196087 /* R_386_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
896206Sab196087 /* R_386_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
906206Sab196087 /* R_386_PC8 */		{0, FLG_RE_PCREL, 1, 0, 0},
916206Sab196087 /* R_386_UNKNOWN24 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
926206Sab196087 /* R_386_UNKNOWN25 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
936206Sab196087 /* R_386_UNKNOWN26 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
946206Sab196087 /* R_386_UNKNOWN27 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
956206Sab196087 /* R_386_UNKNOWN28 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
966206Sab196087 /* R_386_UNKNOWN29 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
976206Sab196087 /* R_386_UNKNOWN30 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
986206Sab196087 /* R_386_UNKNOWN31 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
996206Sab196087 /* R_386_TLS_LDO_32 */	{0, FLG_RE_TLSLD, 4, 0, 0},
1006206Sab196087 /* R_386_UNKNOWN33 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
1016206Sab196087 /* R_386_UNKNOWN34 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
1026206Sab196087 /* R_386_TLS_DTPMOD32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
1036206Sab196087 /* R_386_TLS_DTPOFF32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
1046206Sab196087 /* R_386_UNKONWN37 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
1056206Sab196087 /* R_386_SIZE32 */	{0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0}
1060Sstevel@tonic-gate };
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_386_NONE		 0	none		none
1160Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1170Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
1180Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1190Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1200Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1210Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1220Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1230Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1240Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1250Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1260Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1270Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1280Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1290Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1300Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1310Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1320Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
1331169Srie  * R_386_16		20	word16		S + A
1341169Srie  * R_386_PC16		21	word16		S + A - P
1351169Srie  * R_386_8		22	word8		S + A
1361169Srie  * R_386_PC8		23	word8		S + A - P
1370Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1380Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1390Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1402850Srie  * R_386_SIZE32		38	word32		Z + A
1410Sstevel@tonic-gate  *
1421169Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1430Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1440Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1470Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  * Relocation calculations:
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * CALCULATION uses the following notation:
1520Sstevel@tonic-gate  *	A	the addend used
1530Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1540Sstevel@tonic-gate  *	G	the offset into the global offset table
1550Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1560Sstevel@tonic-gate  *	L	the procedure linkage entry
1570Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1580Sstevel@tonic-gate  *	S	the value of the symbol
1592850Srie  *	Z	the size of the symbol whose index resides in the relocation
1602850Srie  *		entry
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1630Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1640Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1650Sstevel@tonic-gate  *	   of the two GOT entries.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1680Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1690Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1700Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1710Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1720Sstevel@tonic-gate  *	   TLS block.
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1790Sstevel@tonic-gate  *
1800Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1810Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1820Sstevel@tonic-gate  * the addresses in the instructions.
1830Sstevel@tonic-gate  */
1845189Sab196087 #if defined(_KERNEL)
1855189Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
1865189Sab196087 int
do_reloc_krtld(uchar_t rtype,uchar_t * off,Xword * value,const char * sym,const char * file)1875189Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1885189Sab196087     const char *file)
1895189Sab196087 #elif defined(DO_RELOC_LIBLD)
1906206Sab196087 /*ARGSUSED5*/
1910Sstevel@tonic-gate int
192*12155SAli.Bahrami@Sun.COM do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
193*12155SAli.Bahrami@Sun.COM     rel_desc_sname_func_t rel_desc_sname_func,
1945189Sab196087     const char *file, int bswap, void *lml)
1955189Sab196087 #else
1965189Sab196087 int
1975189Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1981618Srie     const char *file, void *lml)
1995189Sab196087 #endif
2000Sstevel@tonic-gate {
201*12155SAli.Bahrami@Sun.COM #ifdef DO_RELOC_LIBLD
202*12155SAli.Bahrami@Sun.COM #define	sym (* rel_desc_sname_func)(rdesc)
203*12155SAli.Bahrami@Sun.COM 	uchar_t	rtype = rdesc->rel_rtype;
204*12155SAli.Bahrami@Sun.COM #endif
2051169Srie 	const Rel_entry	*rep;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	rep = &reloc_table[rtype];
2081169Srie 
2091169Srie 	switch (rep->re_fsize) {
2101169Srie 	case 1:
2111169Srie 		/* LINTED */
2121169Srie 		*((uchar_t *)off) += (uchar_t)(*value);
2131169Srie 		break;
2146206Sab196087 
2151169Srie 	case 2:
2166206Sab196087 #if defined(DORELOC_NATIVE)
2171169Srie 		/* LINTED */
2181169Srie 		*((Half *)off) += (Half)(*value);
2196206Sab196087 #else
2206206Sab196087 		{
2216206Sab196087 			Half	v;
2226206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
2236206Sab196087 
2246206Sab196087 			if (bswap) {
2256206Sab196087 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
2266206Sab196087 				v += *value;
2276206Sab196087 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
2286206Sab196087 			} else {
2296206Sab196087 				UL_ASSIGN_HALF(v_bytes, off);
2306206Sab196087 				v += *value;
2316206Sab196087 				UL_ASSIGN_HALF(off, v_bytes);
2326206Sab196087 			}
2336206Sab196087 		}
2346206Sab196087 #endif
2351169Srie 		break;
2366206Sab196087 
2371169Srie 	case 4:
2386206Sab196087 #if defined(DORELOC_NATIVE)
2391169Srie 		/* LINTED */
2401169Srie 		*((Xword *)off) += *value;
2416206Sab196087 #else
2426206Sab196087 		{
2436206Sab196087 			Word	v;
2446206Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
2456206Sab196087 
2466206Sab196087 			if (bswap) {
2476206Sab196087 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
2486206Sab196087 				v += *value;
2496206Sab196087 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
2506206Sab196087 			} else {
2516206Sab196087 				UL_ASSIGN_WORD(v_bytes, off);
2526206Sab196087 				v += *value;
2536206Sab196087 				UL_ASSIGN_WORD(off, v_bytes);
2546206Sab196087 			}
2556206Sab196087 		}
2566206Sab196087 #endif
2571169Srie 		break;
2581169Srie 	default:
2591169Srie 		/*
2601169Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
2611169Srie 		 */
2621618Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
2630Sstevel@tonic-gate 		return (0);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 	return (1);
266*12155SAli.Bahrami@Sun.COM 
267*12155SAli.Bahrami@Sun.COM #ifdef DO_RELOC_LIBLD
268*12155SAli.Bahrami@Sun.COM #undef sym
269*12155SAli.Bahrami@Sun.COM #endif
2700Sstevel@tonic-gate }
271