xref: /titanic_41/usr/src/uts/intel/ia32/krtld/doreloc.c (revision bf994817a71d4ac680198e25fe79d13c247306e0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21552ff457Srie 
227c478bd9Sstevel@tonic-gate /*
23*bf994817SAli Bahrami  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
25552ff457Srie 
267c478bd9Sstevel@tonic-gate #if	defined(_KERNEL)
277c478bd9Sstevel@tonic-gate #include	<sys/types.h>
287c478bd9Sstevel@tonic-gate #include	"reloc.h"
297c478bd9Sstevel@tonic-gate #else
30ba2be530Sab196087 #define	ELF_TARGET_386
31ba2be530Sab196087 #if defined(DO_RELOC_LIBLD)
32ba2be530Sab196087 #undef DO_RELOC_LIBLD
33ba2be530Sab196087 #define	DO_RELOC_LIBLD_X86
34ba2be530Sab196087 #endif
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	"sgs.h"
377c478bd9Sstevel@tonic-gate #include	"machdep.h"
387c478bd9Sstevel@tonic-gate #include	"libld.h"
397c478bd9Sstevel@tonic-gate #include	"reloc.h"
407c478bd9Sstevel@tonic-gate #include	"conv.h"
417c478bd9Sstevel@tonic-gate #include	"msg.h"
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
45ba2be530Sab196087  * We need to build this code differently when it is used for
46ba2be530Sab196087  * cross linking:
47ba2be530Sab196087  *	- Data alignment requirements can differ from those
48ba2be530Sab196087  *		of the running system, so we can't access data
49ba2be530Sab196087  *		in units larger than a byte
50ba2be530Sab196087  *	- We have to include code to do byte swapping when the
51ba2be530Sab196087  *		target and linker host use different byte ordering,
52ba2be530Sab196087  *		but such code is a waste when running natively.
53ba2be530Sab196087  */
54ba2be530Sab196087 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
55ba2be530Sab196087 #define	DORELOC_NATIVE
56ba2be530Sab196087 #endif
57ba2be530Sab196087 
58ba2be530Sab196087 /*
59552ff457Srie  * This table represents the current relocations that do_reloc() is able to
60552ff457Srie  * process.  The relocations below that are marked SPECIAL are relocations that
61552ff457Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
64ba2be530Sab196087 /* R_386_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
65ba2be530Sab196087 /* R_386_32 */		{0, FLG_RE_NOTREL, 4, 0, 0},
66ba2be530Sab196087 /* R_386_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
67ba2be530Sab196087 /* R_386_GOT32 */	{0, FLG_RE_GOTADD, 4, 0, 0},
68ba2be530Sab196087 /* R_386_PLT32 */	{0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
69ba2be530Sab196087 /* R_386_COPY */	{0, FLG_RE_NOTREL, 0, 0, 0},		/* SPECIAL */
70ba2be530Sab196087 /* R_386_GLOB_DAT */	{0, FLG_RE_NOTREL, 4, 0, 0},
71ba2be530Sab196087 /* R_386_JMP_SLOT */	{0, FLG_RE_NOTREL, 4, 0, 0},		/* SPECIAL */
72ba2be530Sab196087 /* R_386_RELATIVE */	{0, FLG_RE_NOTREL, 4, 0, 0},
73ba2be530Sab196087 /* R_386_GOTOFF */	{0, FLG_RE_GOTREL, 4, 0, 0},
74ba2be530Sab196087 /* R_386_GOTPC */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4,
75ba2be530Sab196087 			    0, 0},
76ba2be530Sab196087 /* R_386_32PLT */	{0, FLG_RE_PLTREL, 4, 0, 0},
77ba2be530Sab196087 /* R_386_TLS_GD_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4,
78ba2be530Sab196087 			    0, 0},
79ba2be530Sab196087 /* R_386_TLS_LDM_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4,
80ba2be530Sab196087 			    0, 0},
81ba2be530Sab196087 /* R_386_TLS_TPOFF */	{0, FLG_RE_NOTREL, 4, 0, 0},
82ba2be530Sab196087 /* R_386_TLS_IE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
83ba2be530Sab196087 /* R_386_TLS_GOTIE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
84ba2be530Sab196087 /* R_386_TLS_LE */	{0, FLG_RE_TLSLE, 4, 0, 0},
85ba2be530Sab196087 /* R_386_TLS_GD */	{0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
86ba2be530Sab196087 /* R_386_TLS_LDM */	{0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
87ba2be530Sab196087 /* R_386_16 */		{0, FLG_RE_NOTREL, 2, 0, 0},
88ba2be530Sab196087 /* R_386_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
89ba2be530Sab196087 /* R_386_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
90ba2be530Sab196087 /* R_386_PC8 */		{0, FLG_RE_PCREL, 1, 0, 0},
91ba2be530Sab196087 /* R_386_UNKNOWN24 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
92ba2be530Sab196087 /* R_386_UNKNOWN25 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
93ba2be530Sab196087 /* R_386_UNKNOWN26 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
94ba2be530Sab196087 /* R_386_UNKNOWN27 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
95ba2be530Sab196087 /* R_386_UNKNOWN28 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
96ba2be530Sab196087 /* R_386_UNKNOWN29 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
97ba2be530Sab196087 /* R_386_UNKNOWN30 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
98ba2be530Sab196087 /* R_386_UNKNOWN31 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
99ba2be530Sab196087 /* R_386_TLS_LDO_32 */	{0, FLG_RE_TLSLD, 4, 0, 0},
100ba2be530Sab196087 /* R_386_UNKNOWN33 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
101ba2be530Sab196087 /* R_386_UNKNOWN34 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102ba2be530Sab196087 /* R_386_TLS_DTPMOD32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
103ba2be530Sab196087 /* R_386_TLS_DTPOFF32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
104ba2be530Sab196087 /* R_386_UNKONWN37 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
105ba2be530Sab196087 /* R_386_SIZE32 */	{0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0}
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
110552ff457Srie  * We assume we wish to add the relocation amount, value, to the
1117c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
1167c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1177c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
1187c478bd9Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1197c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1207c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1217c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1227c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1237c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1247c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1257c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1267c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1277c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1287c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1297c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1307c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1317c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1327c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
133552ff457Srie  * R_386_16		20	word16		S + A
134552ff457Srie  * R_386_PC16		21	word16		S + A - P
135552ff457Srie  * R_386_8		22	word8		S + A
136552ff457Srie  * R_386_PC8		23	word8		S + A - P
1377c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1387c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1397c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1402926dd2eSrie  * R_386_SIZE32		38	word32		Z + A
1417c478bd9Sstevel@tonic-gate  *
142552ff457Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1437c478bd9Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1447c478bd9Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1477c478bd9Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * Relocation calculations:
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
1527c478bd9Sstevel@tonic-gate  *	A	the addend used
1537c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1547c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
1557c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1567c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
1577c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1587c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
1592926dd2eSrie  *	Z	the size of the symbol whose index resides in the relocation
1602926dd2eSrie  *		entry
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1637c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1647c478bd9Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1657c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1687c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1697c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1707c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1717c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1727c478bd9Sstevel@tonic-gate  *	   TLS block.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1817c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1827c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
1837c478bd9Sstevel@tonic-gate  */
184f3324781Sab196087 #if defined(_KERNEL)
185f3324781Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
1867c478bd9Sstevel@tonic-gate int
do_reloc_krtld(uchar_t rtype,uchar_t * off,Xword * value,const char * sym,const char * file)187f3324781Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
188f3324781Sab196087     const char *file)
189f3324781Sab196087 #elif defined(DO_RELOC_LIBLD)
190ba2be530Sab196087 /*ARGSUSED5*/
191f3324781Sab196087 int
192*bf994817SAli Bahrami do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
193*bf994817SAli Bahrami     rel_desc_sname_func_t rel_desc_sname_func,
194f3324781Sab196087     const char *file, int bswap, void *lml)
195f3324781Sab196087 #else
196f3324781Sab196087 int
197f3324781Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
1985aefb655Srie     const char *file, void *lml)
199f3324781Sab196087 #endif
2007c478bd9Sstevel@tonic-gate {
201*bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
202*bf994817SAli Bahrami #define	sym (* rel_desc_sname_func)(rdesc)
203*bf994817SAli Bahrami 	uchar_t	rtype = rdesc->rel_rtype;
204*bf994817SAli Bahrami #endif
2057c478bd9Sstevel@tonic-gate 	const Rel_entry	*rep;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	rep = &reloc_table[rtype];
208552ff457Srie 
209552ff457Srie 	switch (rep->re_fsize) {
210552ff457Srie 	case 1:
211552ff457Srie 		/* LINTED */
212552ff457Srie 		*((uchar_t *)off) += (uchar_t)(*value);
213552ff457Srie 		break;
214ba2be530Sab196087 
215552ff457Srie 	case 2:
216ba2be530Sab196087 #if defined(DORELOC_NATIVE)
217552ff457Srie 		/* LINTED */
218552ff457Srie 		*((Half *)off) += (Half)(*value);
219ba2be530Sab196087 #else
220ba2be530Sab196087 		{
221ba2be530Sab196087 			Half	v;
222ba2be530Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
223ba2be530Sab196087 
224ba2be530Sab196087 			if (bswap) {
225ba2be530Sab196087 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
226ba2be530Sab196087 				v += *value;
227ba2be530Sab196087 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
228ba2be530Sab196087 			} else {
229ba2be530Sab196087 				UL_ASSIGN_HALF(v_bytes, off);
230ba2be530Sab196087 				v += *value;
231ba2be530Sab196087 				UL_ASSIGN_HALF(off, v_bytes);
232ba2be530Sab196087 			}
233ba2be530Sab196087 		}
234ba2be530Sab196087 #endif
235552ff457Srie 		break;
236ba2be530Sab196087 
237552ff457Srie 	case 4:
238ba2be530Sab196087 #if defined(DORELOC_NATIVE)
2397c478bd9Sstevel@tonic-gate 		/* LINTED */
2407c478bd9Sstevel@tonic-gate 		*((Xword *)off) += *value;
241ba2be530Sab196087 #else
242ba2be530Sab196087 		{
243ba2be530Sab196087 			Word	v;
244ba2be530Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
245ba2be530Sab196087 
246ba2be530Sab196087 			if (bswap) {
247ba2be530Sab196087 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
248ba2be530Sab196087 				v += *value;
249ba2be530Sab196087 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
250ba2be530Sab196087 			} else {
251ba2be530Sab196087 				UL_ASSIGN_WORD(v_bytes, off);
252ba2be530Sab196087 				v += *value;
253ba2be530Sab196087 				UL_ASSIGN_WORD(off, v_bytes);
254ba2be530Sab196087 			}
255ba2be530Sab196087 		}
256ba2be530Sab196087 #endif
257552ff457Srie 		break;
258552ff457Srie 	default:
259552ff457Srie 		/*
260552ff457Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
261552ff457Srie 		 */
2625aefb655Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
263552ff457Srie 		return (0);
264552ff457Srie 	}
2657c478bd9Sstevel@tonic-gate 	return (1);
266*bf994817SAli Bahrami 
267*bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
268*bf994817SAli Bahrami #undef sym
269*bf994817SAli Bahrami #endif
2707c478bd9Sstevel@tonic-gate }
271