xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 2c4055eb)
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 /*
23bf994817SAli Bahrami  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
25552ff457Srie 
260250bb16SJohn Levon /*
270250bb16SJohn Levon  * Copyright 2019 Joyent, Inc.
280250bb16SJohn Levon  */
290250bb16SJohn Levon 
300250bb16SJohn Levon /*
310250bb16SJohn Levon  * While this is no longer relevant to the kernel, we keep it in its
320250bb16SJohn Levon  * traditional location to match the other variants, used from the ld
330250bb16SJohn Levon  * code.
340250bb16SJohn Levon  */
350250bb16SJohn Levon 
36ba2be530Sab #define	ELF_TARGET_386
37ba2be530Sab #if defined(DO_RELOC_LIBLD)
38ba2be530Sab #undef DO_RELOC_LIBLD
39ba2be530Sab #define	DO_RELOC_LIBLD_X86
40ba2be530Sab #endif
417c478bd9Sstevel@tonic-gate #include	<stdio.h>
427c478bd9Sstevel@tonic-gate #include	"sgs.h"
437c478bd9Sstevel@tonic-gate #include	"machdep.h"
447c478bd9Sstevel@tonic-gate #include	"libld.h"
457c478bd9Sstevel@tonic-gate #include	"reloc.h"
467c478bd9Sstevel@tonic-gate #include	"conv.h"
477c478bd9Sstevel@tonic-gate #include	"msg.h"
487c478bd9Sstevel@tonic-gate 
49ba2be530Sab /*
50ba2be530Sab  * We need to build this code differently when it is used for
51ba2be530Sab  * cross linking:
52ba2be530Sab  *	- Data alignment requirements can differ from those
53ba2be530Sab  *		of the running system, so we can't access data
54ba2be530Sab  *		in units larger than a byte
55ba2be530Sab  *	- We have to include code to do byte swapping when the
56ba2be530Sab  *		target and linker host use different byte ordering,
57ba2be530Sab  *		but such code is a waste when running natively.
58ba2be530Sab  */
5986ef0a63SRichard Lowe #if !defined(DO_RELOC_LIBLD) || defined(__x86)
60ba2be530Sab #define	DORELOC_NATIVE
61ba2be530Sab #endif
62ba2be530Sab 
637c478bd9Sstevel@tonic-gate /*
64552ff457Srie  * This table represents the current relocations that do_reloc() is able to
65552ff457Srie  * process.  The relocations below that are marked SPECIAL are relocations that
66552ff457Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
69a530dbfeSRichard Lowe 	[R_386_NONE]		= {0, FLG_RE_NOTREL, 0, 0, 0},
70a530dbfeSRichard Lowe 	[R_386_32]		= {0, FLG_RE_NOTREL, 4, 0, 0},
71a530dbfeSRichard Lowe 	[R_386_PC32]		= {0, FLG_RE_PCREL, 4, 0, 0},
72a530dbfeSRichard Lowe 	[R_386_GOT32]		= {0, FLG_RE_GOTADD, 4, 0, 0},
73a530dbfeSRichard Lowe 	[R_386_PLT32]		= {0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
74a530dbfeSRichard Lowe 	[R_386_COPY]		= {0, FLG_RE_NOTREL, 0, 0, 0},	/* SPECIAL */
75a530dbfeSRichard Lowe 	[R_386_GLOB_DAT]	= {0, FLG_RE_NOTREL, 4, 0, 0},
76a530dbfeSRichard Lowe 	[R_386_JMP_SLOT]	= {0, FLG_RE_NOTREL, 4, 0, 0},	/* SPECIAL */
77a530dbfeSRichard Lowe 	[R_386_RELATIVE]	= {0, FLG_RE_NOTREL, 4, 0, 0},
78a530dbfeSRichard Lowe 	[R_386_GOTOFF]		= {0, FLG_RE_GOTREL, 4, 0, 0},
79a530dbfeSRichard Lowe 	[R_386_GOTPC]		= {0, FLG_RE_PCREL | FLG_RE_GOTPC |
80a530dbfeSRichard Lowe 	    FLG_RE_LOCLBND, 4, 0, 0},
81a530dbfeSRichard Lowe 	[R_386_32PLT]		= {0, FLG_RE_PLTREL, 4, 0, 0},
82a530dbfeSRichard Lowe 	[R_386_TLS_GD_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
83a530dbfeSRichard Lowe 	    FLG_RE_TLSGD, 4, 0, 0},
84a530dbfeSRichard Lowe 	[R_386_TLS_LDM_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
85a530dbfeSRichard Lowe 	    FLG_RE_TLSLD, 4, 0, 0},
86a530dbfeSRichard Lowe 	[R_386_TLS_TPOFF]	= {0, FLG_RE_NOTREL, 4, 0, 0},
87a530dbfeSRichard Lowe 	[R_386_TLS_IE]		= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
88a530dbfeSRichard Lowe 	[R_386_TLS_GOTIE]	= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
89a530dbfeSRichard Lowe 	[R_386_TLS_LE]		= {0, FLG_RE_TLSLE, 4, 0, 0},
90a530dbfeSRichard Lowe 	[R_386_TLS_GD]		= {0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
91a530dbfeSRichard Lowe 	[R_386_TLS_LDM]		= {0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
92a530dbfeSRichard Lowe 	[R_386_16]		= {0, FLG_RE_NOTREL, 2, 0, 0},
93a530dbfeSRichard Lowe 	[R_386_PC16]		= {0, FLG_RE_PCREL, 2, 0, 0},
94a530dbfeSRichard Lowe 	[R_386_8]		= {0, FLG_RE_NOTREL, 1, 0, 0},
95a530dbfeSRichard Lowe 	[R_386_PC8]		= {0, FLG_RE_PCREL, 1, 0, 0},
96a530dbfeSRichard Lowe 	[R_386_TLS_GD_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
97a530dbfeSRichard Lowe 	[R_386_TLS_GD_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
98a530dbfeSRichard Lowe 	[R_386_TLS_GD_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
99a530dbfeSRichard Lowe 	[R_386_TLS_GD_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
100a530dbfeSRichard Lowe 	[R_386_TLS_LDM_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
101a530dbfeSRichard Lowe 	[R_386_TLS_LDM_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
102a530dbfeSRichard Lowe 	[R_386_TLS_LDM_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
103a530dbfeSRichard Lowe 	[R_386_TLS_LDM_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
104a530dbfeSRichard Lowe 	[R_386_TLS_LDO_32]	= {0, FLG_RE_TLSLD, 4, 0, 0},
105a530dbfeSRichard Lowe 	[R_386_TLS_IE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
106a530dbfeSRichard Lowe 	[R_386_TLS_LE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
107a530dbfeSRichard Lowe 	[R_386_TLS_DTPMOD32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
108a530dbfeSRichard Lowe 	[R_386_TLS_DTPOFF32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
109a530dbfeSRichard Lowe 	[R_386_TLS_TPOFF32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
110a530dbfeSRichard Lowe 	[R_386_SIZE32]		= {0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0},
111a530dbfeSRichard Lowe 	[R_386_TLS_GOTDESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
112a530dbfeSRichard Lowe 	[R_386_TLS_DESC_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
113a530dbfeSRichard Lowe 	[R_386_TLS_DESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
114a530dbfeSRichard Lowe 	[R_386_IRELATIVE]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
115*2c4055ebSRichard Lowe 	[R_386_GOT32X]		= {0, FLG_RE_GOTADD, 4, 0, 0},
1167c478bd9Sstevel@tonic-gate };
117a530dbfeSRichard Lowe #if	(R_386_NUM != (R_386_GOT32X + 1))
118a530dbfeSRichard Lowe #error	"R_386_NUM has grown"
119a530dbfeSRichard Lowe #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
123552ff457Srie  * We assume we wish to add the relocation amount, value, to the
1247c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
1297c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1307c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
131a530dbfeSRichard Lowe  * R_386_GOT32		 3	word32		G + A - GOT / G + A
1327c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1337c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1347c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1357c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1367c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1377c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1387c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1397c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1407c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1417c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1427c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1437c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
144a530dbfeSRichard Lowe  * R_386_TLS_GOTIE	16	word32		@gotntpoff(S)
145a530dbfeSRichard Lowe  * R_386_TLS_LE		17	word32		@ntpoff(S)
1467c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1477c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
148552ff457Srie  * R_386_16		20	word16		S + A
149552ff457Srie  * R_386_PC16		21	word16		S + A - P
150552ff457Srie  * R_386_8		22	word8		S + A
151552ff457Srie  * R_386_PC8		23	word8		S + A - P
152a530dbfeSRichard Lowe  * R_386_TLS_GD_32	24	word32
153a530dbfeSRichard Lowe  * R_386_TLS_GD_PUSH	25	word32
154a530dbfeSRichard Lowe  * R_386_TLS_GD_CALL	26	word32
155a530dbfeSRichard Lowe  * R_386_TLS_GD_POP	27	word32
156a530dbfeSRichard Lowe  * R_386_TLS_LDM_32	28	word32
157a530dbfeSRichard Lowe  * R_386_TLS_LDM_PUSH	29	word32
158a530dbfeSRichard Lowe  * R_386_TLS_LDM_CALL	30	word32
159a530dbfeSRichard Lowe  * R_386_TLS_LDM_POP	31	word32
1607c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
161a530dbfeSRichard Lowe  * R_386_TLS_IE_32	33	word32		@gotpoff(S)
162a530dbfeSRichard Lowe  * R_386_TLS_LE_32	34	word32		@tpoff(S)
1637c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1647c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
165a530dbfeSRichard Lowe  * R_386_TLS_TPOFF32	37	word32
1662926dd2eSrie  * R_386_SIZE32		38	word32		Z + A
167a530dbfeSRichard Lowe  * R_386_TLS_GOTDESC	39	word32
168a530dbfeSRichard Lowe  * R_386_TLS_DESC_CALL	40	none		none
169a530dbfeSRichard Lowe  * R_386_TLS_DESC	41	word32
170a530dbfeSRichard Lowe  * R_386_IRELATIVE	42	word32		indirect (B + A)
171a530dbfeSRichard Lowe  * R_386_GOT32X		43	word32		G + A - GOT / G + A
1727c478bd9Sstevel@tonic-gate  *
173a530dbfeSRichard Lowe  * Relocations are from Table 3.6: Relocation Types from the 386 psABI.
174a530dbfeSRichard Lowe  * (with reference also to the thread-local storage ABIs)
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * Relocation calculations:
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
1797c478bd9Sstevel@tonic-gate  *	A	the addend used
1807c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1817c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
1827c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1837c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
1847c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1857c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
1862926dd2eSrie  *	Z	the size of the symbol whose index resides in the relocation
1872926dd2eSrie  *		entry
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1907c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1917c478bd9Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1927c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1957c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1967c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1977c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1987c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1997c478bd9Sstevel@tonic-gate  *	   TLS block.
2007c478bd9Sstevel@tonic-gate  *
2017c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
2027c478bd9Sstevel@tonic-gate  *
2037c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
2067c478bd9Sstevel@tonic-gate  *
2077c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
2087c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
2097c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
2107c478bd9Sstevel@tonic-gate  */
21186ef0a63SRichard Lowe /* BEGIN CSTYLED */
2120250bb16SJohn Levon #if defined(DO_RELOC_LIBLD)
213ba2be530Sab /*ARGSUSED5*/
214f3324781Sab int
do_reloc_ld(Rel_desc * rdesc,uchar_t * off,Xword * value,rel_desc_sname_func_t rel_desc_sname_func,const char * file,int bswap,void * lml)215bf994817SAli Bahrami do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
216bf994817SAli Bahrami     rel_desc_sname_func_t rel_desc_sname_func,
217f3324781Sab     const char *file, int bswap, void *lml)
218f3324781Sab #else
219f3324781Sab int
220f3324781Sab do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
2215aefb655Srie     const char *file, void *lml)
222f3324781Sab #endif
2237c478bd9Sstevel@tonic-gate {
22486ef0a63SRichard Lowe /* END CSTYLED */
225bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
226bf994817SAli Bahrami #define	sym (* rel_desc_sname_func)(rdesc)
227bf994817SAli Bahrami 	uchar_t	rtype = rdesc->rel_rtype;
228bf994817SAli Bahrami #endif
229552ff457Srie 	const Rel_entry	*rep;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	rep = &reloc_table[rtype];
232552ff457Srie 
233552ff457Srie 	switch (rep->re_fsize) {
234552ff457Srie 	case 1:
235552ff457Srie 		/* LINTED */
236552ff457Srie 		*((uchar_t *)off) += (uchar_t)(*value);
237552ff457Srie 		break;
238ba2be530Sab 
239552ff457Srie 	case 2:
240ba2be530Sab #if defined(DORELOC_NATIVE)
241552ff457Srie 		/* LINTED */
242552ff457Srie 		*((Half *)off) += (Half)(*value);
243ba2be530Sab #else
244ba2be530Sab 		{
245ba2be530Sab 			Half	v;
246ba2be530Sab 			uchar_t	*v_bytes = (uchar_t *)&v;
247ba2be530Sab 
248ba2be530Sab 			if (bswap) {
249ba2be530Sab 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
250ba2be530Sab 				v += *value;
251ba2be530Sab 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
252ba2be530Sab 			} else {
253ba2be530Sab 				UL_ASSIGN_HALF(v_bytes, off);
254ba2be530Sab 				v += *value;
255ba2be530Sab 				UL_ASSIGN_HALF(off, v_bytes);
256ba2be530Sab 			}
257ba2be530Sab 		}
258ba2be530Sab #endif
259552ff457Srie 		break;
260ba2be530Sab 
261552ff457Srie 	case 4:
262ba2be530Sab #if defined(DORELOC_NATIVE)
263552ff457Srie 		/* LINTED */
264552ff457Srie 		*((Xword *)off) += *value;
265ba2be530Sab #else
266ba2be530Sab 		{
267ba2be530Sab 			Word	v;
268ba2be530Sab 			uchar_t	*v_bytes = (uchar_t *)&v;
269ba2be530Sab 
270ba2be530Sab 			if (bswap) {
271ba2be530Sab 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
272ba2be530Sab 				v += *value;
273ba2be530Sab 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
274ba2be530Sab 			} else {
275ba2be530Sab 				UL_ASSIGN_WORD(v_bytes, off);
276ba2be530Sab 				v += *value;
277ba2be530Sab 				UL_ASSIGN_WORD(off, v_bytes);
278ba2be530Sab 			}
279ba2be530Sab 		}
280ba2be530Sab #endif
281552ff457Srie 		break;
282552ff457Srie 	default:
283552ff457Srie 		/*
284552ff457Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
285552ff457Srie 		 */
2865aefb655Srie 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
2877c478bd9Sstevel@tonic-gate 		return (0);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	return (1);
290bf994817SAli Bahrami 
291bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
292bf994817SAli Bahrami #undef sym
293bf994817SAli Bahrami #endif
2947c478bd9Sstevel@tonic-gate }
295