xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 0250bb16)
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 
26*0250bb16SJohn Levon /*
27*0250bb16SJohn Levon  * Copyright 2019 Joyent, Inc.
28*0250bb16SJohn Levon  */
29*0250bb16SJohn Levon 
30*0250bb16SJohn Levon /*
31*0250bb16SJohn Levon  * While this is no longer relevant to the kernel, we keep it in its
32*0250bb16SJohn Levon  * traditional location to match the other variants, used from the ld
33*0250bb16SJohn Levon  * code.
34*0250bb16SJohn Levon  */
35*0250bb16SJohn Levon 
36ba2be530Sab196087 #define	ELF_TARGET_386
37ba2be530Sab196087 #if defined(DO_RELOC_LIBLD)
38ba2be530Sab196087 #undef DO_RELOC_LIBLD
39ba2be530Sab196087 #define	DO_RELOC_LIBLD_X86
40ba2be530Sab196087 #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 
497c478bd9Sstevel@tonic-gate /*
50ba2be530Sab196087  * We need to build this code differently when it is used for
51ba2be530Sab196087  * cross linking:
52ba2be530Sab196087  *	- Data alignment requirements can differ from those
53ba2be530Sab196087  *		of the running system, so we can't access data
54ba2be530Sab196087  *		in units larger than a byte
55ba2be530Sab196087  *	- We have to include code to do byte swapping when the
56ba2be530Sab196087  *		target and linker host use different byte ordering,
57ba2be530Sab196087  *		but such code is a waste when running natively.
58ba2be530Sab196087  */
59ba2be530Sab196087 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
60ba2be530Sab196087 #define	DORELOC_NATIVE
61ba2be530Sab196087 #endif
62ba2be530Sab196087 
63ba2be530Sab196087 /*
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] = {
69ba2be530Sab196087 /* R_386_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
70ba2be530Sab196087 /* R_386_32 */		{0, FLG_RE_NOTREL, 4, 0, 0},
71ba2be530Sab196087 /* R_386_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
72ba2be530Sab196087 /* R_386_GOT32 */	{0, FLG_RE_GOTADD, 4, 0, 0},
73ba2be530Sab196087 /* R_386_PLT32 */	{0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
74ba2be530Sab196087 /* R_386_COPY */	{0, FLG_RE_NOTREL, 0, 0, 0},		/* SPECIAL */
75ba2be530Sab196087 /* R_386_GLOB_DAT */	{0, FLG_RE_NOTREL, 4, 0, 0},
76ba2be530Sab196087 /* R_386_JMP_SLOT */	{0, FLG_RE_NOTREL, 4, 0, 0},		/* SPECIAL */
77ba2be530Sab196087 /* R_386_RELATIVE */	{0, FLG_RE_NOTREL, 4, 0, 0},
78ba2be530Sab196087 /* R_386_GOTOFF */	{0, FLG_RE_GOTREL, 4, 0, 0},
79ba2be530Sab196087 /* R_386_GOTPC */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4,
80ba2be530Sab196087 			    0, 0},
81ba2be530Sab196087 /* R_386_32PLT */	{0, FLG_RE_PLTREL, 4, 0, 0},
82ba2be530Sab196087 /* R_386_TLS_GD_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4,
83ba2be530Sab196087 			    0, 0},
84ba2be530Sab196087 /* R_386_TLS_LDM_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4,
85ba2be530Sab196087 			    0, 0},
86ba2be530Sab196087 /* R_386_TLS_TPOFF */	{0, FLG_RE_NOTREL, 4, 0, 0},
87ba2be530Sab196087 /* R_386_TLS_IE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
88ba2be530Sab196087 /* R_386_TLS_GOTIE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
89ba2be530Sab196087 /* R_386_TLS_LE */	{0, FLG_RE_TLSLE, 4, 0, 0},
90ba2be530Sab196087 /* R_386_TLS_GD */	{0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
91ba2be530Sab196087 /* R_386_TLS_LDM */	{0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
92ba2be530Sab196087 /* R_386_16 */		{0, FLG_RE_NOTREL, 2, 0, 0},
93ba2be530Sab196087 /* R_386_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
94ba2be530Sab196087 /* R_386_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
95ba2be530Sab196087 /* R_386_PC8 */		{0, FLG_RE_PCREL, 1, 0, 0},
96ba2be530Sab196087 /* R_386_UNKNOWN24 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
97ba2be530Sab196087 /* R_386_UNKNOWN25 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
98ba2be530Sab196087 /* R_386_UNKNOWN26 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
99ba2be530Sab196087 /* R_386_UNKNOWN27 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
100ba2be530Sab196087 /* R_386_UNKNOWN28 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
101ba2be530Sab196087 /* R_386_UNKNOWN29 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102ba2be530Sab196087 /* R_386_UNKNOWN30 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
103ba2be530Sab196087 /* R_386_UNKNOWN31 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
104ba2be530Sab196087 /* R_386_TLS_LDO_32 */	{0, FLG_RE_TLSLD, 4, 0, 0},
105ba2be530Sab196087 /* R_386_UNKNOWN33 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
106ba2be530Sab196087 /* R_386_UNKNOWN34 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
107ba2be530Sab196087 /* R_386_TLS_DTPMOD32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
108ba2be530Sab196087 /* R_386_TLS_DTPOFF32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
109ba2be530Sab196087 /* R_386_UNKONWN37 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
110ba2be530Sab196087 /* R_386_SIZE32 */	{0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0}
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
115552ff457Srie  * We assume we wish to add the relocation amount, value, to the
1167c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
1217c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1227c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
1237c478bd9Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1247c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1257c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1267c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1277c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1287c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1297c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1307c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1317c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1327c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1337c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1347c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1357c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1367c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1377c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
138552ff457Srie  * R_386_16		20	word16		S + A
139552ff457Srie  * R_386_PC16		21	word16		S + A - P
140552ff457Srie  * R_386_8		22	word8		S + A
141552ff457Srie  * R_386_PC8		23	word8		S + A - P
1427c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1437c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1447c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1452926dd2eSrie  * R_386_SIZE32		38	word32		Z + A
1467c478bd9Sstevel@tonic-gate  *
147552ff457Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1487c478bd9Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1497c478bd9Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1527c478bd9Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * Relocation calculations:
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
1577c478bd9Sstevel@tonic-gate  *	A	the addend used
1587c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1597c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
1607c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1617c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
1627c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1637c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
1642926dd2eSrie  *	Z	the size of the symbol whose index resides in the relocation
1652926dd2eSrie  *		entry
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  *	@dtlndx(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  *	   instructions referencing this entry will be bound to the first
1707c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1737c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1747c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1757c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1767c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1777c478bd9Sstevel@tonic-gate  *	   TLS block.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1867c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1877c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
1887c478bd9Sstevel@tonic-gate  */
189*0250bb16SJohn Levon #if defined(DO_RELOC_LIBLD)
190ba2be530Sab196087 /*ARGSUSED5*/
191f3324781Sab196087 int
192bf994817SAli Bahrami do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
193bf994817SAli 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 {
201bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
202bf994817SAli Bahrami #define	sym (* rel_desc_sname_func)(rdesc)
203bf994817SAli Bahrami 	uchar_t	rtype = rdesc->rel_rtype;
204bf994817SAli 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);
266bf994817SAli Bahrami 
267bf994817SAli Bahrami #ifdef DO_RELOC_LIBLD
268bf994817SAli Bahrami #undef sym
269bf994817SAli Bahrami #endif
2707c478bd9Sstevel@tonic-gate }
271