xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision ba2be530)
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*ba2be530Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26552ff457Srie 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if	defined(_KERNEL)
307c478bd9Sstevel@tonic-gate #include	<sys/types.h>
317c478bd9Sstevel@tonic-gate #include	"reloc.h"
327c478bd9Sstevel@tonic-gate #else
33*ba2be530Sab196087 #define	ELF_TARGET_386
34*ba2be530Sab196087 #if defined(DO_RELOC_LIBLD)
35*ba2be530Sab196087 #undef DO_RELOC_LIBLD
36*ba2be530Sab196087 #define	DO_RELOC_LIBLD_X86
37*ba2be530Sab196087 #endif
387c478bd9Sstevel@tonic-gate #include	<stdio.h>
397c478bd9Sstevel@tonic-gate #include	"sgs.h"
407c478bd9Sstevel@tonic-gate #include	"machdep.h"
417c478bd9Sstevel@tonic-gate #include	"libld.h"
427c478bd9Sstevel@tonic-gate #include	"reloc.h"
437c478bd9Sstevel@tonic-gate #include	"conv.h"
447c478bd9Sstevel@tonic-gate #include	"msg.h"
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
48*ba2be530Sab196087  * We need to build this code differently when it is used for
49*ba2be530Sab196087  * cross linking:
50*ba2be530Sab196087  *	- Data alignment requirements can differ from those
51*ba2be530Sab196087  *		of the running system, so we can't access data
52*ba2be530Sab196087  *		in units larger than a byte
53*ba2be530Sab196087  *	- We have to include code to do byte swapping when the
54*ba2be530Sab196087  *		target and linker host use different byte ordering,
55*ba2be530Sab196087  *		but such code is a waste when running natively.
56*ba2be530Sab196087  */
57*ba2be530Sab196087 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
58*ba2be530Sab196087 #define	DORELOC_NATIVE
59*ba2be530Sab196087 #endif
60*ba2be530Sab196087 
61*ba2be530Sab196087 /*
62552ff457Srie  * This table represents the current relocations that do_reloc() is able to
63552ff457Srie  * process.  The relocations below that are marked SPECIAL are relocations that
64552ff457Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
67*ba2be530Sab196087 /* R_386_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
68*ba2be530Sab196087 /* R_386_32 */		{0, FLG_RE_NOTREL, 4, 0, 0},
69*ba2be530Sab196087 /* R_386_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
70*ba2be530Sab196087 /* R_386_GOT32 */	{0, FLG_RE_GOTADD, 4, 0, 0},
71*ba2be530Sab196087 /* R_386_PLT32 */	{0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
72*ba2be530Sab196087 /* R_386_COPY */	{0, FLG_RE_NOTREL, 0, 0, 0},		/* SPECIAL */
73*ba2be530Sab196087 /* R_386_GLOB_DAT */	{0, FLG_RE_NOTREL, 4, 0, 0},
74*ba2be530Sab196087 /* R_386_JMP_SLOT */	{0, FLG_RE_NOTREL, 4, 0, 0},		/* SPECIAL */
75*ba2be530Sab196087 /* R_386_RELATIVE */	{0, FLG_RE_NOTREL, 4, 0, 0},
76*ba2be530Sab196087 /* R_386_GOTOFF */	{0, FLG_RE_GOTREL, 4, 0, 0},
77*ba2be530Sab196087 /* R_386_GOTPC */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4,
78*ba2be530Sab196087 			    0, 0},
79*ba2be530Sab196087 /* R_386_32PLT */	{0, FLG_RE_PLTREL, 4, 0, 0},
80*ba2be530Sab196087 /* R_386_TLS_GD_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4,
81*ba2be530Sab196087 			    0, 0},
82*ba2be530Sab196087 /* R_386_TLS_LDM_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4,
83*ba2be530Sab196087 			    0, 0},
84*ba2be530Sab196087 /* R_386_TLS_TPOFF */	{0, FLG_RE_NOTREL, 4, 0, 0},
85*ba2be530Sab196087 /* R_386_TLS_IE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
86*ba2be530Sab196087 /* R_386_TLS_GOTIE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
87*ba2be530Sab196087 /* R_386_TLS_LE */	{0, FLG_RE_TLSLE, 4, 0, 0},
88*ba2be530Sab196087 /* R_386_TLS_GD */	{0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
89*ba2be530Sab196087 /* R_386_TLS_LDM */	{0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
90*ba2be530Sab196087 /* R_386_16 */		{0, FLG_RE_NOTREL, 2, 0, 0},
91*ba2be530Sab196087 /* R_386_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
92*ba2be530Sab196087 /* R_386_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
93*ba2be530Sab196087 /* R_386_PC8 */		{0, FLG_RE_PCREL, 1, 0, 0},
94*ba2be530Sab196087 /* R_386_UNKNOWN24 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
95*ba2be530Sab196087 /* R_386_UNKNOWN25 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
96*ba2be530Sab196087 /* R_386_UNKNOWN26 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
97*ba2be530Sab196087 /* R_386_UNKNOWN27 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
98*ba2be530Sab196087 /* R_386_UNKNOWN28 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
99*ba2be530Sab196087 /* R_386_UNKNOWN29 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
100*ba2be530Sab196087 /* R_386_UNKNOWN30 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
101*ba2be530Sab196087 /* R_386_UNKNOWN31 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102*ba2be530Sab196087 /* R_386_TLS_LDO_32 */	{0, FLG_RE_TLSLD, 4, 0, 0},
103*ba2be530Sab196087 /* R_386_UNKNOWN33 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
104*ba2be530Sab196087 /* R_386_UNKNOWN34 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
105*ba2be530Sab196087 /* R_386_TLS_DTPMOD32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
106*ba2be530Sab196087 /* R_386_TLS_DTPOFF32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
107*ba2be530Sab196087 /* R_386_UNKONWN37 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
108*ba2be530Sab196087 /* R_386_SIZE32 */	{0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0}
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
113552ff457Srie  * We assume we wish to add the relocation amount, value, to the
1147c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
1197c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1207c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
1217c478bd9Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1227c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1237c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1247c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1257c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1267c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1277c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1287c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1297c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1307c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1317c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1327c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1337c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1347c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1357c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
136552ff457Srie  * R_386_16		20	word16		S + A
137552ff457Srie  * R_386_PC16		21	word16		S + A - P
138552ff457Srie  * R_386_8		22	word8		S + A
139552ff457Srie  * R_386_PC8		23	word8		S + A - P
1407c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1417c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1427c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1432926dd2eSrie  * R_386_SIZE32		38	word32		Z + A
1447c478bd9Sstevel@tonic-gate  *
145552ff457Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1467c478bd9Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1477c478bd9Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1507c478bd9Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * Relocation calculations:
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
1557c478bd9Sstevel@tonic-gate  *	A	the addend used
1567c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1577c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
1587c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1597c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
1607c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1617c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
1622926dd2eSrie  *	Z	the size of the symbol whose index resides in the relocation
1632926dd2eSrie  *		entry
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1667c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1677c478bd9Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1687c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1717c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1727c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1737c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1747c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1757c478bd9Sstevel@tonic-gate  *	   TLS block.
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1847c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1857c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
1867c478bd9Sstevel@tonic-gate  */
187f3324781Sab196087 #if defined(_KERNEL)
188f3324781Sab196087 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
1897c478bd9Sstevel@tonic-gate int
190f3324781Sab196087 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
191f3324781Sab196087     const char *file)
192f3324781Sab196087 #elif defined(DO_RELOC_LIBLD)
193*ba2be530Sab196087 /*ARGSUSED5*/
194f3324781Sab196087 int
195f3324781Sab196087 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
196f3324781Sab196087     const char *file, int bswap, void *lml)
197f3324781Sab196087 #else
198f3324781Sab196087 int
199f3324781Sab196087 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
2005aefb655Srie     const char *file, void *lml)
201f3324781Sab196087 #endif
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	const Rel_entry	*rep;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	rep = &reloc_table[rtype];
206552ff457Srie 
207552ff457Srie 	switch (rep->re_fsize) {
208552ff457Srie 	case 1:
209552ff457Srie 		/* LINTED */
210552ff457Srie 		*((uchar_t *)off) += (uchar_t)(*value);
211552ff457Srie 		break;
212*ba2be530Sab196087 
213552ff457Srie 	case 2:
214*ba2be530Sab196087 #if defined(DORELOC_NATIVE)
215552ff457Srie 		/* LINTED */
216552ff457Srie 		*((Half *)off) += (Half)(*value);
217*ba2be530Sab196087 #else
218*ba2be530Sab196087 		{
219*ba2be530Sab196087 			Half	v;
220*ba2be530Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
221*ba2be530Sab196087 
222*ba2be530Sab196087 			if (bswap) {
223*ba2be530Sab196087 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
224*ba2be530Sab196087 				v += *value;
225*ba2be530Sab196087 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
226*ba2be530Sab196087 			} else {
227*ba2be530Sab196087 				UL_ASSIGN_HALF(v_bytes, off);
228*ba2be530Sab196087 				v += *value;
229*ba2be530Sab196087 				UL_ASSIGN_HALF(off, v_bytes);
230*ba2be530Sab196087 			}
231*ba2be530Sab196087 		}
232*ba2be530Sab196087 #endif
233552ff457Srie 		break;
234*ba2be530Sab196087 
235552ff457Srie 	case 4:
236*ba2be530Sab196087 #if defined(DORELOC_NATIVE)
2377c478bd9Sstevel@tonic-gate 		/* LINTED */
2387c478bd9Sstevel@tonic-gate 		*((Xword *)off) += *value;
239*ba2be530Sab196087 #else
240*ba2be530Sab196087 		{
241*ba2be530Sab196087 			Word	v;
242*ba2be530Sab196087 			uchar_t	*v_bytes = (uchar_t *)&v;
243*ba2be530Sab196087 
244*ba2be530Sab196087 			if (bswap) {
245*ba2be530Sab196087 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
246*ba2be530Sab196087 				v += *value;
247*ba2be530Sab196087 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
248*ba2be530Sab196087 			} else {
249*ba2be530Sab196087 				UL_ASSIGN_WORD(v_bytes, off);
250*ba2be530Sab196087 				v += *value;
251*ba2be530Sab196087 				UL_ASSIGN_WORD(off, v_bytes);
252*ba2be530Sab196087 			}
253*ba2be530Sab196087 		}
254*ba2be530Sab196087 #endif
255552ff457Srie 		break;
256552ff457Srie 	default:
257552ff457Srie 		/*
258552ff457Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
259552ff457Srie 		 */
2605aefb655Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
261552ff457Srie 		return (0);
262552ff457Srie 	}
2637c478bd9Sstevel@tonic-gate 	return (1);
2647c478bd9Sstevel@tonic-gate }
265