xref: /onnv-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 5189:66a4f4f8a159)
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*5189Sab196087  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
261169Srie 
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
330Sstevel@tonic-gate #include	<stdio.h>
340Sstevel@tonic-gate #include	"sgs.h"
350Sstevel@tonic-gate #include	"machdep.h"
360Sstevel@tonic-gate #include	"libld.h"
370Sstevel@tonic-gate #include	"reloc.h"
380Sstevel@tonic-gate #include	"conv.h"
390Sstevel@tonic-gate #include	"msg.h"
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
431169Srie  * This table represents the current relocations that do_reloc() is able to
441169Srie  * process.  The relocations below that are marked SPECIAL are relocations that
451169Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
481169Srie /* R_386_NONE */	{FLG_RE_NOTREL, 0},
490Sstevel@tonic-gate /* R_386_32 */		{FLG_RE_NOTREL, 4},
500Sstevel@tonic-gate /* R_386_PC32 */	{FLG_RE_PCREL, 4},
510Sstevel@tonic-gate /* R_386_GOT32 */	{FLG_RE_GOTADD, 4},
520Sstevel@tonic-gate /* R_386_PLT32 */	{FLG_RE_PLTREL | FLG_RE_PCREL, 4},
531169Srie /* R_386_COPY */	{FLG_RE_NOTREL, 0},			/* SPECIAL */
540Sstevel@tonic-gate /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
551169Srie /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},			/* SPECIAL */
560Sstevel@tonic-gate /* R_386_RELATIVE */	{FLG_RE_NOTREL, 4},
570Sstevel@tonic-gate /* R_386_GOTOFF */	{FLG_RE_GOTREL, 4},
580Sstevel@tonic-gate /* R_386_GOTPC */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
590Sstevel@tonic-gate /* R_386_32PLT */	{FLG_RE_PLTREL, 4},
602145Srie /* R_386_TLS_GD_PLT */	{FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4},
612145Srie /* R_386_TLS_LDM_PLT */	{FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4},
620Sstevel@tonic-gate /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
632145Srie /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSIE, 4},
642145Srie /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSIE, 4},
652145Srie /* R_386_TLS_LE */	{FLG_RE_TLSLE, 4},
662145Srie /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSGD, 4},
672145Srie /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD, 4},
681169Srie /* R_386_16 */		{FLG_RE_NOTREL, 2},
691169Srie /* R_386_PC16 */	{FLG_RE_PCREL, 2},
701169Srie /* R_386_8 */		{FLG_RE_NOTREL, 1},
711169Srie /* R_386_PC8 */		{FLG_RE_PCREL, 1},
720Sstevel@tonic-gate /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
730Sstevel@tonic-gate /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
740Sstevel@tonic-gate /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
750Sstevel@tonic-gate /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
760Sstevel@tonic-gate /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
770Sstevel@tonic-gate /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
780Sstevel@tonic-gate /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
790Sstevel@tonic-gate /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
802145Srie /* R_386_TLS_LDO_32 */	{FLG_RE_TLSLD, 4},
810Sstevel@tonic-gate /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
820Sstevel@tonic-gate /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
830Sstevel@tonic-gate /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
840Sstevel@tonic-gate /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
852850Srie /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0},
862850Srie /* R_386_SIZE32 */	{FLG_RE_SIZE | FLG_RE_VERIFY, 4}
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Write a single relocated value to its reference location.
911169Srie  * We assume we wish to add the relocation amount, value, to the
920Sstevel@tonic-gate  * value of the address already present at the offset.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * R_386_NONE		 0	none		none
970Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
980Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
990Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1000Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1010Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1020Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1030Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1040Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1050Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1060Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1070Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1080Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1090Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1100Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1110Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1120Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1130Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
1141169Srie  * R_386_16		20	word16		S + A
1151169Srie  * R_386_PC16		21	word16		S + A - P
1161169Srie  * R_386_8		22	word8		S + A
1171169Srie  * R_386_PC8		23	word8		S + A - P
1180Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1190Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1200Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1212850Srie  * R_386_SIZE32		38	word32		Z + A
1220Sstevel@tonic-gate  *
1231169Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1240Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1250Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1280Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Relocation calculations:
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  * CALCULATION uses the following notation:
1330Sstevel@tonic-gate  *	A	the addend used
1340Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1350Sstevel@tonic-gate  *	G	the offset into the global offset table
1360Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1370Sstevel@tonic-gate  *	L	the procedure linkage entry
1380Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1390Sstevel@tonic-gate  *	S	the value of the symbol
1402850Srie  *	Z	the size of the symbol whose index resides in the relocation
1412850Srie  *		entry
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1440Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1450Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1460Sstevel@tonic-gate  *	   of the two GOT entries.
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1490Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1500Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1510Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1520Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1530Sstevel@tonic-gate  *	   TLS block.
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1620Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1630Sstevel@tonic-gate  * the addresses in the instructions.
1640Sstevel@tonic-gate  */
165*5189Sab196087 #if defined(_KERNEL)
166*5189Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
167*5189Sab196087 int
168*5189Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
169*5189Sab196087     const char *file)
170*5189Sab196087 #elif defined(DO_RELOC_LIBLD)
1710Sstevel@tonic-gate int
172*5189Sab196087 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
173*5189Sab196087     const char *file, int bswap, void *lml)
174*5189Sab196087 #else
175*5189Sab196087 int
176*5189Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1771618Srie     const char *file, void *lml)
178*5189Sab196087 #endif
1790Sstevel@tonic-gate {
1801169Srie 	const Rel_entry	*rep;
1810Sstevel@tonic-gate 
182*5189Sab196087 #if defined(DO_RELOC_LIBLD)
183*5189Sab196087 	/*
184*5189Sab196087 	 * We do not support building the X86 linker as a cross linker
185*5189Sab196087 	 * at this time.
186*5189Sab196087 	 */
187*5189Sab196087 	if (bswap) {
188*5189Sab196087 		REL_ERR_NOSWAP(lml, file, sym, rtype);
189*5189Sab196087 		return (0);
190*5189Sab196087 	}
191*5189Sab196087 #endif
192*5189Sab196087 
1930Sstevel@tonic-gate 	rep = &reloc_table[rtype];
1941169Srie 
1951169Srie 	switch (rep->re_fsize) {
1961169Srie 	case 1:
1971169Srie 		/* LINTED */
1981169Srie 		*((uchar_t *)off) += (uchar_t)(*value);
1991169Srie 		break;
2001169Srie 	case 2:
2011169Srie 		/* LINTED */
2021169Srie 		*((Half *)off) += (Half)(*value);
2031169Srie 		break;
2041169Srie 	case 4:
2051169Srie 		/* LINTED */
2061169Srie 		*((Xword *)off) += *value;
2071169Srie 		break;
2081169Srie 	default:
2091169Srie 		/*
2101169Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
2111169Srie 		 */
2121618Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
2130Sstevel@tonic-gate 		return (0);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	return (1);
2160Sstevel@tonic-gate }
217