summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/ia32/krtld/doreloc.c
blob: 019a7602b192f9ecac5a0a301136e7bbbfb4031b (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
/*
 * 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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright 2019 Joyent, Inc.
 */

/*
 * While this is no longer relevant to the kernel, we keep it in its
 * traditional location to match the other variants, used from the ld
 * code.
 */

#define	ELF_TARGET_386
#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"

/*
 * 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(__x86)
#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_386_NUM] = {
	[R_386_NONE]		= {0, FLG_RE_NOTREL, 0, 0, 0},
	[R_386_32]		= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_PC32]		= {0, FLG_RE_PCREL, 4, 0, 0},
	[R_386_GOT32]		= {0, FLG_RE_GOTADD, 4, 0, 0},
	[R_386_PLT32]		= {0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
	[R_386_COPY]		= {0, FLG_RE_NOTREL, 0, 0, 0},	/* SPECIAL */
	[R_386_GLOB_DAT]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_JMP_SLOT]	= {0, FLG_RE_NOTREL, 4, 0, 0},	/* SPECIAL */
	[R_386_RELATIVE]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_GOTOFF]		= {0, FLG_RE_GOTREL, 4, 0, 0},
	[R_386_GOTPC]		= {0, FLG_RE_PCREL | FLG_RE_GOTPC |
	    FLG_RE_LOCLBND, 4, 0, 0},
	[R_386_32PLT]		= {0, FLG_RE_PLTREL, 4, 0, 0},
	[R_386_TLS_GD_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
	    FLG_RE_TLSGD, 4, 0, 0},
	[R_386_TLS_LDM_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
	    FLG_RE_TLSLD, 4, 0, 0},
	[R_386_TLS_TPOFF]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_IE]		= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
	[R_386_TLS_GOTIE]	= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
	[R_386_TLS_LE]		= {0, FLG_RE_TLSLE, 4, 0, 0},
	[R_386_TLS_GD]		= {0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
	[R_386_TLS_LDM]		= {0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
	[R_386_16]		= {0, FLG_RE_NOTREL, 2, 0, 0},
	[R_386_PC16]		= {0, FLG_RE_PCREL, 2, 0, 0},
	[R_386_8]		= {0, FLG_RE_NOTREL, 1, 0, 0},
	[R_386_PC8]		= {0, FLG_RE_PCREL, 1, 0, 0},
	[R_386_TLS_GD_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDO_32]	= {0, FLG_RE_TLSLD, 4, 0, 0},
	[R_386_TLS_IE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DTPMOD32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_DTPOFF32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_TPOFF32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_SIZE32]		= {0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0},
	[R_386_TLS_GOTDESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DESC_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_IRELATIVE]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_GOT32X]		= {0, FLG_RE_GOTADD, 4, 0, 0},
};
#if	(R_386_NUM != (R_386_GOT32X + 1))
#error	"R_386_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_386_NONE		 0	none		none
 * R_386_32		 1	word32		S + A
 * R_386_PC32		 2	word32		S + A - P
 * R_386_GOT32		 3	word32		G + A - GOT / G + A
 * R_386_PLT32		 4	word32		L + A - P
 * R_386_COPY		 5	none		none
 * R_386_GLOB_DAT	 6	word32		S
 * R_386_JMP_SLOT	 7	word32		S
 * R_386_RELATIVE	 8	word32		B + A
 * R_386_GOTOFF		 9	word32		S + A - GOT
 * R_386_GOTPC		10	word32		GOT + A - P
 * R_386_32PLT		11	word32		L + A
 * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
 * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
 * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
 * R_386_TLS_IE		15	word32		@indntpoff(S)
 * R_386_TLS_GOTIE	16	word32		@gotntpoff(S)
 * R_386_TLS_LE		17	word32		@ntpoff(S)
 * R_386_TLS_GD		18	word32		@tlsgd(S)
 * R_386_TLS_LDM	19	word32		@tlsldm(S)
 * R_386_16		20	word16		S + A
 * R_386_PC16		21	word16		S + A - P
 * R_386_8		22	word8		S + A
 * R_386_PC8		23	word8		S + A - P
 * R_386_TLS_GD_32	24	word32
 * R_386_TLS_GD_PUSH	25	word32
 * R_386_TLS_GD_CALL	26	word32
 * R_386_TLS_GD_POP	27	word32
 * R_386_TLS_LDM_32	28	word32
 * R_386_TLS_LDM_PUSH	29	word32
 * R_386_TLS_LDM_CALL	30	word32
 * R_386_TLS_LDM_POP	31	word32
 * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
 * R_386_TLS_IE_32	33	word32		@gotpoff(S)
 * R_386_TLS_LE_32	34	word32		@tpoff(S)
 * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
 * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
 * R_386_TLS_TPOFF32	37	word32
 * R_386_SIZE32		38	word32		Z + A
 * R_386_TLS_GOTDESC	39	word32
 * R_386_TLS_DESC_CALL	40	none		none
 * R_386_TLS_DESC	41	word32
 * R_386_IRELATIVE	42	word32		indirect (B + A)
 * R_386_GOT32X		43	word32		G + A - GOT / G + A
 *
 * Relocations are from Table 3.6: Relocation Types from the 386 psABI.
 * (with reference also to the thread-local storage ABIs)
 *
 * Relocation calculations:
 *
 * CALCULATION uses the following notation:
 *	A	the addend used
 *	B	the base address of the shared object in memory
 *	G	the offset into the global offset table
 *	GOT	the address of teh global offset table
 *	L	the procedure linkage entry
 *	P	the place of the storage unit being relocated
 *	S	the value of the symbol
 *	Z	the size of the symbol whose index resides in the relocation
 *		entry
 *
 *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
 *	   a Tls_index structure (for passing to __tls_get_addr()). The
 *	   instructions referencing this entry will be bound to the first
 *	   of the two GOT entries.
 *
 *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
 *	   a Tls_index structure (for passing to __tls_get_addr()). The
 *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
 *	   ti_module will be filled in at run-time. The call to
 *	   __tls_get_addr() will return the starting offset of the dynamic
 *	   TLS block.
 *
 *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
 *
 *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
 *
 *	@dtpmod(x): calculate the module id of the object containing symbol x.
 *
 * The calculations in the CALCULATION column are assumed to have
 * been performed before calling this function except for the addition of
 * the addresses in the instructions.
 */
/* BEGIN CSTYLED */
#if 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
{
/* END CSTYLED */
#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;
			uchar_t	*v_bytes = (uchar_t *)&v;

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

	case 4:
#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Xword *)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;
	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
}