summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/amd64/krtld/doreloc.c
blob: aac6f6dcc7fbd2fd54a4789df4c04d0e9edea3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#if	defined(_KERNEL)
#include	<sys/types.h>
#include	"reloc.h"
#else
#define	ELF_TARGET_AMD64
#if defined(DO_RELOC_LIBLD)
#undef DO_RELOC_LIBLD
#define	DO_RELOC_LIBLD_X86
#endif
#include	<stdio.h>
#include	"sgs.h"
#include	"machdep.h"
#include	"libld.h"
#include	"reloc.h"
#include	"conv.h"
#include	"msg.h"
#endif

/*
 * We need to build this code differently when it is used for
 * cross linking:
 *	- Data alignment requirements can differ from those
 *		of the running system, so we can't access data
 *		in units larger than a byte
 *	- We have to include code to do byte swapping when the
 *		target and linker host use different byte ordering,
 *		but such code is a waste when running natively.
 */
#if !defined(DO_RELOC_LIBLD) || defined(__i386) || defined(__amd64)
#define	DORELOC_NATIVE
#endif

/*
 * This table represents the current relocations that do_reloc() is able to
 * process.  The relocations below that are marked SPECIAL are relocations that
 * take special processing and shouldn't actually ever be passed to do_reloc().
 */
const Rel_entry	reloc_table[R_AMD64_NUM] = {
/* R_AMD64_NONE */	{0, FLG_RE_NOTREL, 0, 0, 0},
/* R_AMD64_64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_PC32 */	{0, FLG_RE_PCREL, 4, 0, 0},
/* R_AMD64_GOT32 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_PLT32 */	{0, FLG_RE_PCREL | FLG_RE_PLTREL |
			    FLG_RE_VERIFY | FLG_RE_SIGN, 4, 0, 0},
/* R_AMD64_COPY */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
/* R_AMD64_GLOB_DAT */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_JUMP_SLOT */	{0, FLG_RE_NOTSUP, 0, 0, 0},	/* SPECIAL */
/* R_AMD64_RELATIVE */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_GOTPCREL */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD, 4, 0, 0},
/* R_AMD64_32 */	{0, FLG_RE_NOTREL, 4, 0, 0},
/* R_AMD64_32S */	{0, FLG_RE_NOTREL, 4, 0, 0},
/* R_AMD64_16 */	{0, FLG_RE_NOTREL, 2, 0, 0},
/* R_AMD64_PC16 */	{0, FLG_RE_PCREL, 2, 0, 0},
/* R_AMD64_8 */		{0, FLG_RE_NOTREL, 1, 0, 0},
/* R_AMD64_PC8 */	{0, FLG_RE_PCREL, 1, 0, 0},
/* R_AMD64_DTPMOD64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_DTPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_TPOFF64 */	{0, FLG_RE_NOTREL, 8, 0, 0},
/* R_AMD64_TLSGD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSGD,
			    4, 0, 0},
/* R_AMD64_TLSLD */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSLD,
			    4, 0, 0},
/* R_AMD64_DTPOFF32 */	{0, FLG_RE_TLSLD, 4},
/* R_AMD64_GOTTPOFF */	{0, FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSIE,
			    4, 0, 0},
/* R_AMD64_TPOFF32 */	{0, FLG_RE_TLSLE, 4, 0, 0},
/* R_AMD64_PC64 */	{0, FLG_RE_PCREL, 8, 0, 0},
/* R_AMD64_GOTOFF64 */	{0, FLG_RE_GOTREL, 8, 0, 0},
/* R_AMD64_GOTPC32 */	{0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND,
			    4, 0, 0},
/* R_AMD64_GOT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_GOTPC6 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_GOTPLT64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_PLTOFF64 */	{0, FLG_RE_NOTSUP, 0, 0, 0},
/* R_AMD64_SIZE32 */	{0, FLG_RE_SIZE, 4, 0, 0},
/* R_AMD64_SIZE64 */	{0, FLG_RE_SIZE, 8, 0, 0}
};
#if	(R_AMD64_NUM != (R_AMD64_SIZE64 + 1))
#error	"R_AMD64_NUM has grown"
#endif

/*
 * Write a single relocated value to its reference location.
 * We assume we wish to add the relocation amount, value, to the
 * value of the address already present at the offset.
 *
 * NAME			VALUE	FIELD		CALCULATION
 *
 * R_AMD64_NONE		 0	none		none
 * R_AMD64_64		 1	word64		S + A
 * R_AMD64_PC32		 2	word64		S + A
 * R_AMD64_GOT32	 3	word32		G + A
 * R_AMD64_PLT32	 4	word32		L + A - P
 * R_AMD64_COPY		 5	none		none
 * R_AMD64_GLOB_DAT	 6	word64		S
 * R_AMD64_JUMP_SLOT	 7	word64		S
 * R_AMD64_RELATIVE	 8	word64		B + A
 * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
 * R_AMD64_32		10	word32		S + A
 * R_AMD64_32S		11	word32		S + A
 * R_AMD64_16		12	word16		S + A
 * R_AMD64_PC16		13	word16		S + A - P
 * R_AMD64_8		14	word8		S + A
 * R_AMD64_PC8		15	word8		S + A - P
 * R_AMD64_DTPMOD64	16	word64
 * R_AMD64_DTPOFF64	17	word64
 * R_AMD64_TPOFF64	18	word64
 * R_AMD64_TLSGD	19	word32
 * R_AMD64_TLSLD	20	word32
 * R_AMD64_DTPOFF32	21	word32
 * R_AMD64_GOTTPOFF	22	word32
 * R_AMD64_TPOFF32	23	word32
 * R_AMD64_PC64		24	word32		S + A - P
 * R_AMD64_GOTOFF64	25	word32		S + A - GOT
 * R_AMD64_GOTPC32	26	word32		GOT + A - P
 * R_AMD64_GOT64	27			reserved for future expansion
 * R_AMD64_GOTPCREL64	28			reserved for future expansion
 * R_AMD64_GOTPC64	29			reserved for future expansion
 * R_AMD64_GOTPLT64	30			reserved for future expansion
 * R_AMD64_PLTOFF64	31			reserved for future expansion
 * R_AMD64_SIZE32	32	word32		Z + A
 * R_AMD64_SIZE64	33	word64		Z + A
 *
 * Relocation calculations:
 *	A	Represents the addend used to compute the value of the
 *		relocatable field.
 *
 *	B	Represents the base address at which a shared objects has
 *		been loaded into memory during executaion.  Generally, a
 *		shared objects is built with a 0 base virtual address,
 *		but the execution address will be different.
 *
 *	G	Represents the offset into the global offset table
 *		at which the relocation entry's symbol will reside
 *		during execution.
 *
 *	GOT	Rrepresents the address of the global offset table.
 *
 *	L	Represents the place (section offset or address) of
 *		the Procedure Linkage Table entry for a symbol.
 *
 *	P	Represents the place (section offset or address) of the
 *		storage unit being relocated (computed using r_offset).
 *
 *	S	Represents the value of the symbol whose index resides
 *		in the relocation entry.
 *
 *	Z	the size of the symbol whose index resides in the relocation
 *		entry
 */


/*
 * Bits that must be cleared or identical for a value to act as if extended in
 * the given way.
 */
#define	ZEROEXBITS	0xffffffff00000000ULL
#define	SIGNEXBITS	0xffffffff80000000ULL

#if defined(_KERNEL)
#define	lml	0		/* Needed by arglist of REL_ERR_* macros */
int
do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
    const char *file)
#elif defined(DO_RELOC_LIBLD)
/*ARGSUSED5*/
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)
#else
int
do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
    const char *file, void *lml)
#endif
{
#ifdef DO_RELOC_LIBLD
#define	sym (* rel_desc_sname_func)(rdesc)
	uchar_t	rtype = rdesc->rel_rtype;
#endif
	const Rel_entry	*rep;

	rep = &reloc_table[rtype];

	switch (rep->re_fsize) {
	case 1:
		/* LINTED */
		*((uchar_t *)off) = (uchar_t)(*value);
		break;

	case 2:
#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Half *)off) = (Half)(*value);
#else
		{
			Half	v = (Half)(*value);
			uchar_t	*v_bytes = (uchar_t *)&v;

			if (bswap) {
				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
			} else {
				UL_ASSIGN_HALF(off, v_bytes);
			}
		}
#endif
		break;

	case 4:
		/*
		 * The amd64 psABI requires that we perform the following
		 * verifications:
		 *
		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
		 *    computed value to 32bits.  Verify that the generated value
		 *    for the R_AMD64_32/32S relocation zero-extends (sign
		 *    extends) to the original 64-bit value.
		 *
		 * Also, the following relocations are all 32 bit PC relative
		 * references.  Validate that the value being written will fit
		 * in the field provided.
		 *
		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
		 */
		if (rtype == R_AMD64_32) {
			/*
			 * Verify that this value will act as a zero-extended
			 * unsigned 32 bit value.  That is, that the upper
			 * 32 bits are zero.
			 */
			if ((*value & ZEROEXBITS) != 0) {
				/*
				 * To keep chkmsg() happy:
				 *  MSG_INTL(MSG_REL_NOFIT)
				 */
				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
				return (0);
			}
		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
			/*
			 * Verify that this value will act as a sign-extended
			 * signed 32 bit value, that is that the upper 33 bits
			 * are either all zero or all one.
			 */
			if (((*value & SIGNEXBITS) != SIGNEXBITS) &&
			    ((*value & SIGNEXBITS) != 0)) {
				/*
				 * To keep chkmsg() happy:
				 *  MSG_INTL(MSG_REL_NOFIT)
				 */
				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
				return (0);
			}
		}

#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Word *)off) += *value;
#else
		{
			Word	v;
			uchar_t	*v_bytes = (uchar_t *)&v;

			if (bswap) {
				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
			} else {
				UL_ASSIGN_WORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_WORD(off, v_bytes);
			}
		}
#endif
		break;

	case 8:
#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Xword *)off) += *value;
#else
		{
			Xword	v;
			uchar_t	*v_bytes = (uchar_t *)&v;

			if (bswap) {
				UL_ASSIGN_BSWAP_XWORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_BSWAP_XWORD(off, v_bytes);
			} else {
				UL_ASSIGN_XWORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_XWORD(off, v_bytes);
			}
		}
#endif
		break;
	default:
		/*
		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
		 */
		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
		return (0);
	}
	return (1);

#ifdef DO_RELOC_LIBLD
#undef sym
#endif
}