xref: /titanic_50/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 015a6ef6781cc3ceba8ad3bfbae98449b6002a1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #if	defined(_KERNEL)
30 #include	<sys/types.h>
31 #include	"reloc.h"
32 #else
33 #define	ELF_TARGET_386
34 #if defined(DO_RELOC_LIBLD)
35 #undef DO_RELOC_LIBLD
36 #define	DO_RELOC_LIBLD_X86
37 #endif
38 #include	<stdio.h>
39 #include	"sgs.h"
40 #include	"machdep.h"
41 #include	"libld.h"
42 #include	"reloc.h"
43 #include	"conv.h"
44 #include	"msg.h"
45 #endif
46 
47 /*
48  * We need to build this code differently when it is used for
49  * cross linking:
50  *	- Data alignment requirements can differ from those
51  *		of the running system, so we can't access data
52  *		in units larger than a byte
53  *	- We have to include code to do byte swapping when the
54  *		target and linker host use different byte ordering,
55  *		but such code is a waste when running natively.
56  */
57 #if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
58 #define	DORELOC_NATIVE
59 #endif
60 
61 /*
62  * This table represents the current relocations that do_reloc() is able to
63  * process.  The relocations below that are marked SPECIAL are relocations that
64  * take special processing and shouldn't actually ever be passed to do_reloc().
65  */
66 const Rel_entry	reloc_table[R_386_NUM] = {
67 /* R_386_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
68 /* R_386_32 */		{0, FLG_RE_NOTREL, 4, 0, 0},
69 /* R_386_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
70 /* R_386_GOT32 */	{0, FLG_RE_GOTADD, 4, 0, 0},
71 /* R_386_PLT32 */	{0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
72 /* R_386_COPY */	{0, FLG_RE_NOTREL, 0, 0, 0},		/* SPECIAL */
73 /* R_386_GLOB_DAT */	{0, FLG_RE_NOTREL, 4, 0, 0},
74 /* R_386_JMP_SLOT */	{0, FLG_RE_NOTREL, 4, 0, 0},		/* SPECIAL */
75 /* R_386_RELATIVE */	{0, FLG_RE_NOTREL, 4, 0, 0},
76 /* R_386_GOTOFF */	{0, FLG_RE_GOTREL, 4, 0, 0},
77 /* R_386_GOTPC */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4,
78 			    0, 0},
79 /* R_386_32PLT */	{0, FLG_RE_PLTREL, 4, 0, 0},
80 /* R_386_TLS_GD_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4,
81 			    0, 0},
82 /* R_386_TLS_LDM_PLT */	{0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4,
83 			    0, 0},
84 /* R_386_TLS_TPOFF */	{0, FLG_RE_NOTREL, 4, 0, 0},
85 /* R_386_TLS_IE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
86 /* R_386_TLS_GOTIE */	{0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
87 /* R_386_TLS_LE */	{0, FLG_RE_TLSLE, 4, 0, 0},
88 /* R_386_TLS_GD */	{0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
89 /* R_386_TLS_LDM */	{0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
90 /* R_386_16 */		{0, FLG_RE_NOTREL, 2, 0, 0},
91 /* R_386_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
92 /* R_386_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
93 /* R_386_PC8 */		{0, FLG_RE_PCREL, 1, 0, 0},
94 /* R_386_UNKNOWN24 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
95 /* R_386_UNKNOWN25 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
96 /* R_386_UNKNOWN26 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
97 /* R_386_UNKNOWN27 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
98 /* R_386_UNKNOWN28 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
99 /* R_386_UNKNOWN29 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
100 /* R_386_UNKNOWN30 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
101 /* R_386_UNKNOWN31 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102 /* R_386_TLS_LDO_32 */	{0, FLG_RE_TLSLD, 4, 0, 0},
103 /* R_386_UNKNOWN33 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
104 /* R_386_UNKNOWN34 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
105 /* R_386_TLS_DTPMOD32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
106 /* R_386_TLS_DTPOFF32 */ {0, FLG_RE_NOTREL, 4, 0, 0},
107 /* R_386_UNKONWN37 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
108 /* R_386_SIZE32 */	{0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0}
109 };
110 
111 /*
112  * Write a single relocated value to its reference location.
113  * We assume we wish to add the relocation amount, value, to the
114  * value of the address already present at the offset.
115  *
116  * NAME			VALUE	FIELD		CALCULATION
117  *
118  * R_386_NONE		 0	none		none
119  * R_386_32		 1	word32		S + A
120  * R_386_PC32		 2	word32		S + A - P
121  * R_386_GOT32		 3	word32		G + A - P
122  * R_386_PLT32		 4	word32		L + A - P
123  * R_386_COPY		 5	none		none
124  * R_386_GLOB_DAT	 6	word32		S
125  * R_386_JMP_SLOT	 7	word32		S
126  * R_386_RELATIVE	 8	word32		B + A
127  * R_386_GOTOFF		 9	word32		S + A - GOT
128  * R_386_GOTPC		10	word32		GOT + A - P
129  * R_386_32PLT		11	word32		L + A
130  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
131  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
132  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
133  * R_386_TLS_IE		15	word32		@indntpoff(S)
134  * R_386_TLS_GD		18	word32		@tlsgd(S)
135  * R_386_TLS_LDM	19	word32		@tlsldm(S)
136  * R_386_16		20	word16		S + A
137  * R_386_PC16		21	word16		S + A - P
138  * R_386_8		22	word8		S + A
139  * R_386_PC8		23	word8		S + A - P
140  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
141  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
142  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
143  * R_386_SIZE32		38	word32		Z + A
144  *
145  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
146  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
147  * and is in the process of being registered with intel ABI (1/13/94).
148  *
149  * Relocations R_386_TLS_* are added to support Thread-Local storage
150  *	as recorded in PSARC/2001/509
151  *
152  * Relocation calculations:
153  *
154  * CALCULATION uses the following notation:
155  *	A	the addend used
156  *	B	the base address of the shared object in memory
157  *	G	the offset into the global offset table
158  *	GOT	the address of teh global offset table
159  *	L	the procedure linkage entry
160  *	P	the place of the storage unit being relocated
161  *	S	the value of the symbol
162  *	Z	the size of the symbol whose index resides in the relocation
163  *		entry
164  *
165  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
166  *	   a Tls_index structure (for passing to __tls_get_addr()). The
167  *	   instructions referencing this entry will be bound to the first
168  *	   of the two GOT entries.
169  *
170  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
171  *	   a Tls_index structure (for passing to __tls_get_addr()). The
172  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
173  *	   ti_module will be filled in at run-time. The call to
174  *	   __tls_get_addr() will return the starting offset of the dynamic
175  *	   TLS block.
176  *
177  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
178  *
179  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
180  *
181  *	@dtpmod(x): calculate the module id of the object containing symbol x.
182  *
183  * The calculations in the CALCULATION column are assumed to have
184  * been performed before calling this function except for the addition of
185  * the addresses in the instructions.
186  */
187 #if defined(_KERNEL)
188 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
189 int
190 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
191     const char *file)
192 #elif defined(DO_RELOC_LIBLD)
193 /*ARGSUSED5*/
194 int
195 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
196     const char *file, int bswap, void *lml)
197 #else
198 int
199 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
200     const char *file, void *lml)
201 #endif
202 {
203 	const Rel_entry	*rep;
204 
205 	rep = &reloc_table[rtype];
206 
207 	switch (rep->re_fsize) {
208 	case 1:
209 		/* LINTED */
210 		*((uchar_t *)off) += (uchar_t)(*value);
211 		break;
212 
213 	case 2:
214 #if defined(DORELOC_NATIVE)
215 		/* LINTED */
216 		*((Half *)off) += (Half)(*value);
217 #else
218 		{
219 			Half	v;
220 			uchar_t	*v_bytes = (uchar_t *)&v;
221 
222 			if (bswap) {
223 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
224 				v += *value;
225 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
226 			} else {
227 				UL_ASSIGN_HALF(v_bytes, off);
228 				v += *value;
229 				UL_ASSIGN_HALF(off, v_bytes);
230 			}
231 		}
232 #endif
233 		break;
234 
235 	case 4:
236 #if defined(DORELOC_NATIVE)
237 		/* LINTED */
238 		*((Xword *)off) += *value;
239 #else
240 		{
241 			Word	v;
242 			uchar_t	*v_bytes = (uchar_t *)&v;
243 
244 			if (bswap) {
245 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
246 				v += *value;
247 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
248 			} else {
249 				UL_ASSIGN_WORD(v_bytes, off);
250 				v += *value;
251 				UL_ASSIGN_WORD(off, v_bytes);
252 			}
253 		}
254 #endif
255 		break;
256 	default:
257 		/*
258 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
259 		 */
260 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
261 		return (0);
262 	}
263 	return (1);
264 }
265