xref: /titanic_50/usr/src/uts/intel/amd64/krtld/doreloc.c (revision 35a5a3587fd94b666239c157d3722745250ccbd7)
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_AMD64
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_AMD64_NUM] = {
67 /* R_AMD64_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
68 /* R_AMD64_64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
69 /* R_AMD64_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
70 /* R_AMD64_GOT32 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
71 /* R_AMD64_PLT32 */	{0, FLG_RE_PCREL | FLG_RE_PLTREL |
72 			    FLG_RE_VERIFY | FLG_RE_SIGN, 4, 0, 0},
73 /* R_AMD64_COPY */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
74 /* R_AMD64_GLOB_DAT */	{0, FLG_RE_NOTREL, 8, 0, 0},
75 /* R_AMD64_JUMP_SLOT */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
76 /* R_AMD64_RELATIVE */	{0, FLG_RE_NOTREL, 8, 0, 0},
77 /* R_AMD64_GOTPCREL */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD, 4, 0, 0},
78 /* R_AMD64_32 */	{0, FLG_RE_NOTREL, 4, 0, 0},
79 /* R_AMD64_32S */	{0, FLG_RE_NOTREL, 4, 0, 0},
80 /* R_AMD64_16 */	{0, FLG_RE_NOTREL, 2, 0, 0},
81 /* R_AMD64_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
82 /* R_AMD64_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
83 /* R_AMD64_PC8 */	{0, FLG_RE_PCREL, 1, 0, 0},
84 /* R_AMD64_DTPMOD64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
85 /* R_AMD64_DTPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
86 /* R_AMD64_TPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
87 /* R_AMD64_TLSGD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSGD,
88 			    4, 0, 0},
89 /* R_AMD64_TLSLD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSLD,
90 			    4, 0, 0},
91 /* R_AMD64_DTPOFF32 */	{0, FLG_RE_TLSLD, 4},
92 /* R_AMD64_GOTTPOFF */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSIE,
93 			    4, 0, 0},
94 /* R_AMD64_TPOFF32 */	{0, FLG_RE_TLSLE, 4, 0, 0},
95 /* R_AMD64_PC64 */	{0, FLG_RE_PCREL, 8, 0, 0},
96 /* R_AMD64_GOTOFF64 */	{0, FLG_RE_GOTREL, 8, 0, 0},
97 /* R_AMD64_GOTPC32 */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND,
98 			    4, 0, 0},
99 /* R_AMD64_GOT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
100 /* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0, 0, 0},
101 /* R_AMD64_GOTPC6 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
102 /* R_AMD64_GOTPLT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
103 /* R_AMD64_PLTOFF64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
104 /* R_AMD64_SIZE32 */	{0, FLG_RE_SIZE, 4, 0, 0},
105 /* R_AMD64_SIZE64 */	{0, FLG_RE_SIZE, 8, 0, 0}
106 };
107 #if	(R_AMD64_NUM != (R_AMD64_SIZE64 + 1))
108 #error	"R_AMD64_NUM has grown"
109 #endif
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_AMD64_NONE		 0	none		none
119  * R_AMD64_64		 1	word64		S + A
120  * R_AMD64_PC32		 2	word64		S + A
121  * R_AMD64_GOT32	 3	word32		G + A
122  * R_AMD64_PLT32	 4	word32		L + A - P
123  * R_AMD64_COPY		 5	none		none
124  * R_AMD64_GLOB_DAT	 6	word64		S
125  * R_AMD64_JUMP_SLOT	 7	word64		S
126  * R_AMD64_RELATIVE	 8	word64		B + A
127  * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
128  * R_AMD64_32		10	word32		S + A
129  * R_AMD64_32S		11	word32		S + A
130  * R_AMD64_16		12	word16		S + A
131  * R_AMD64_PC16		13	word16		S + A - P
132  * R_AMD64_8		14	word8		S + A
133  * R_AMD64_PC8		15	word8		S + A - P
134  * R_AMD64_DTPMOD64	16	word64
135  * R_AMD64_DTPOFF64	17	word64
136  * R_AMD64_TPOFF64	18	word64
137  * R_AMD64_TLSGD	19	word32
138  * R_AMD64_TLSLD	20	word32
139  * R_AMD64_DTPOFF32	21	word32
140  * R_AMD64_GOTTPOFF	22	word32
141  * R_AMD64_TPOFF32	23	word32
142  * R_AMD64_PC64		24	word32		S + A - P
143  * R_AMD64_GOTOFF64	25	word32		S + A - GOT
144  * R_AMD64_GOTPC32	26	word32		GOT + A - P
145  * R_AMD64_GOT64	27			reserved for future expansion
146  * R_AMD64_GOTPCREL64	28			reserved for future expansion
147  * R_AMD64_GOTPC64	29			reserved for future expansion
148  * R_AMD64_GOTPLT64	30			reserved for future expansion
149  * R_AMD64_PLTOFF64	31			reserved for future expansion
150  * R_AMD64_SIZE32	32	word32		Z + A
151  * R_AMD64_SIZE64	33	word64		Z + A
152  *
153  * Relocation calculations:
154  *	A	Represents the addend used to compute the value of the
155  *		relocatable field.
156  *
157  *	B	Represents the base address at which a shared objects has
158  *		been loaded into memory during executaion.  Generally, a
159  *		shared objects is built with a 0 base virtual address,
160  *		but the execution address will be different.
161  *
162  *	G	Represents the offset into the global offset table
163  *		at which the relocation entry's symbol will reside
164  *		during execution.
165  *
166  *	GOT	Rrepresents the address of the global offset table.
167  *
168  *	L	Represents the place (section offset or address) of
169  *		the Procedure Linkage Table entry for a symbol.
170  *
171  *	P	Represents the place (section offset or address) of the
172  *		storage unit being relocated (computed using r_offset).
173  *
174  *	S	Represents the value of the symbol whose index resides
175  *		in the relocation entry.
176  *
177  *	Z	the size of the symbol whose index resides in the relocation
178  *		entry
179  */
180 
181 #define	HIBITS	0xffffffff80000000ULL
182 
183 #if defined(_KERNEL)
184 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
185 int
186 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
187     const char *file)
188 #elif defined(DO_RELOC_LIBLD)
189 /*ARGSUSED5*/
190 int
191 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
192     const char *file, int bswap, void *lml)
193 #else
194 int
195 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
196     const char *file, void *lml)
197 #endif
198 {
199 	const Rel_entry	*rep;
200 
201 	rep = &reloc_table[rtype];
202 
203 	switch (rep->re_fsize) {
204 	case 1:
205 		/* LINTED */
206 		*((uchar_t *)off) = (uchar_t)(*value);
207 		break;
208 
209 	case 2:
210 #if defined(DORELOC_NATIVE)
211 		/* LINTED */
212 		*((Half *)off) = (Half)(*value);
213 #else
214 		{
215 			Half	v = (Half)(*value);
216 			uchar_t	*v_bytes = (uchar_t *)&v;
217 
218 			if (bswap) {
219 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
220 			} else {
221 				UL_ASSIGN_HALF(off, v_bytes);
222 			}
223 		}
224 #endif
225 		break;
226 
227 	case 4:
228 		/*
229 		 * The amd64 psABI requires that we perform the following
230 		 * verifications:
231 		 *
232 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
233 		 *    computed value to 32bits.  Verify that the generated value
234 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
235 		 *    extends) to the original 64-bit value.
236 		 *
237 		 * Also, the following relocations are all 32 bit PC relative
238 		 * references.  Validate that the value being written will fit
239 		 * in the field provided.
240 		 *
241 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
242 		 */
243 		if (rtype == R_AMD64_32) {
244 			/*
245 			 * Verify that this value will 'zero-extend', this
246 			 * requires that the upper 33bits all be 'zero'.
247 			 */
248 			if ((*value & HIBITS) != 0) {
249 				/*
250 				 * To keep chkmsg() happy:
251 				 *  MSG_INTL(MSG_REL_NOFIT)
252 				 */
253 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
254 				return (0);
255 			}
256 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
257 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
258 			/*
259 			 * Verify that this value will properly sign extend.
260 			 * This is true of the upper 33bits are all either
261 			 * 'zero' or all 'one'.
262 			 */
263 			if (((*value & HIBITS) != HIBITS) &&
264 			    ((*value & HIBITS) != 0)) {
265 				/*
266 				 * To keep chkmsg() happy:
267 				 *  MSG_INTL(MSG_REL_NOFIT)
268 				 */
269 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
270 				return (0);
271 			}
272 		}
273 
274 #if defined(DORELOC_NATIVE)
275 		/* LINTED */
276 		*((Word *)off) += *value;
277 #else
278 		{
279 			Word	v;
280 			uchar_t	*v_bytes = (uchar_t *)&v;
281 
282 			if (bswap) {
283 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
284 				v += *value;
285 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
286 			} else {
287 				UL_ASSIGN_WORD(v_bytes, off);
288 				v += *value;
289 				UL_ASSIGN_WORD(off, v_bytes);
290 			}
291 		}
292 #endif
293 		break;
294 
295 	case 8:
296 #if defined(DORELOC_NATIVE)
297 		/* LINTED */
298 		*((Xword *)off) += *value;
299 #else
300 		{
301 			Xword	v;
302 			uchar_t	*v_bytes = (uchar_t *)&v;
303 
304 			if (bswap) {
305 				UL_ASSIGN_BSWAP_XWORD(v_bytes, off);
306 				v += *value;
307 				UL_ASSIGN_BSWAP_XWORD(off, v_bytes);
308 			} else {
309 				UL_ASSIGN_XWORD(v_bytes, off);
310 				v += *value;
311 				UL_ASSIGN_XWORD(off, v_bytes);
312 			}
313 		}
314 #endif
315 		break;
316 	default:
317 		/*
318 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
319 		 */
320 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
321 		return (0);
322 	}
323 	return (1);
324 }
325