xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2019 Joyent, Inc.
28  */
29 
30 /*
31  * While this is no longer relevant to the kernel, we keep it in its
32  * traditional location to match the other variants, used from the ld
33  * code.
34  */
35 
36 #define	ELF_TARGET_386
37 #if defined(DO_RELOC_LIBLD)
38 #undef DO_RELOC_LIBLD
39 #define	DO_RELOC_LIBLD_X86
40 #endif
41 #include	<stdio.h>
42 #include	"sgs.h"
43 #include	"machdep.h"
44 #include	"libld.h"
45 #include	"reloc.h"
46 #include	"conv.h"
47 #include	"msg.h"
48 
49 /*
50  * We need to build this code differently when it is used for
51  * cross linking:
52  *	- Data alignment requirements can differ from those
53  *		of the running system, so we can't access data
54  *		in units larger than a byte
55  *	- We have to include code to do byte swapping when the
56  *		target and linker host use different byte ordering,
57  *		but such code is a waste when running natively.
58  */
59 #if !defined(DO_RELOC_LIBLD) || defined(__x86)
60 #define	DORELOC_NATIVE
61 #endif
62 
63 /*
64  * This table represents the current relocations that do_reloc() is able to
65  * process.  The relocations below that are marked SPECIAL are relocations that
66  * take special processing and shouldn't actually ever be passed to do_reloc().
67  */
68 const Rel_entry	reloc_table[R_386_NUM] = {
69 	[R_386_NONE]		= {0, FLG_RE_NOTREL, 0, 0, 0},
70 	[R_386_32]		= {0, FLG_RE_NOTREL, 4, 0, 0},
71 	[R_386_PC32]		= {0, FLG_RE_PCREL, 4, 0, 0},
72 	[R_386_GOT32]		= {0, FLG_RE_GOTADD, 4, 0, 0},
73 	[R_386_PLT32]		= {0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
74 	[R_386_COPY]		= {0, FLG_RE_NOTREL, 0, 0, 0},	/* SPECIAL */
75 	[R_386_GLOB_DAT]	= {0, FLG_RE_NOTREL, 4, 0, 0},
76 	[R_386_JMP_SLOT]	= {0, FLG_RE_NOTREL, 4, 0, 0},	/* SPECIAL */
77 	[R_386_RELATIVE]	= {0, FLG_RE_NOTREL, 4, 0, 0},
78 	[R_386_GOTOFF]		= {0, FLG_RE_GOTREL, 4, 0, 0},
79 	[R_386_GOTPC]		= {0, FLG_RE_PCREL | FLG_RE_GOTPC |
80 	    FLG_RE_LOCLBND, 4, 0, 0},
81 	[R_386_32PLT]		= {0, FLG_RE_PLTREL, 4, 0, 0},
82 	[R_386_TLS_GD_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
83 	    FLG_RE_TLSGD, 4, 0, 0},
84 	[R_386_TLS_LDM_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
85 	    FLG_RE_TLSLD, 4, 0, 0},
86 	[R_386_TLS_TPOFF]	= {0, FLG_RE_NOTREL, 4, 0, 0},
87 	[R_386_TLS_IE]		= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
88 	[R_386_TLS_GOTIE]	= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
89 	[R_386_TLS_LE]		= {0, FLG_RE_TLSLE, 4, 0, 0},
90 	[R_386_TLS_GD]		= {0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
91 	[R_386_TLS_LDM]		= {0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
92 	[R_386_16]		= {0, FLG_RE_NOTREL, 2, 0, 0},
93 	[R_386_PC16]		= {0, FLG_RE_PCREL, 2, 0, 0},
94 	[R_386_8]		= {0, FLG_RE_NOTREL, 1, 0, 0},
95 	[R_386_PC8]		= {0, FLG_RE_PCREL, 1, 0, 0},
96 	[R_386_TLS_GD_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
97 	[R_386_TLS_GD_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
98 	[R_386_TLS_GD_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
99 	[R_386_TLS_GD_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
100 	[R_386_TLS_LDM_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
101 	[R_386_TLS_LDM_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
102 	[R_386_TLS_LDM_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
103 	[R_386_TLS_LDM_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
104 	[R_386_TLS_LDO_32]	= {0, FLG_RE_TLSLD, 4, 0, 0},
105 	[R_386_TLS_IE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
106 	[R_386_TLS_LE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
107 	[R_386_TLS_DTPMOD32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
108 	[R_386_TLS_DTPOFF32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
109 	[R_386_TLS_TPOFF32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
110 	[R_386_SIZE32]		= {0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0},
111 	[R_386_TLS_GOTDESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
112 	[R_386_TLS_DESC_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
113 	[R_386_TLS_DESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
114 	[R_386_IRELATIVE]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
115 	[R_386_GOT32X]		= {0, FLG_RE_GOTADD, 4, 0, 0},
116 };
117 #if	(R_386_NUM != (R_386_GOT32X + 1))
118 #error	"R_386_NUM has grown"
119 #endif
120 
121 /*
122  * Write a single relocated value to its reference location.
123  * We assume we wish to add the relocation amount, value, to the
124  * value of the address already present at the offset.
125  *
126  * NAME			VALUE	FIELD		CALCULATION
127  *
128  * R_386_NONE		 0	none		none
129  * R_386_32		 1	word32		S + A
130  * R_386_PC32		 2	word32		S + A - P
131  * R_386_GOT32		 3	word32		G + A - GOT / G + A
132  * R_386_PLT32		 4	word32		L + A - P
133  * R_386_COPY		 5	none		none
134  * R_386_GLOB_DAT	 6	word32		S
135  * R_386_JMP_SLOT	 7	word32		S
136  * R_386_RELATIVE	 8	word32		B + A
137  * R_386_GOTOFF		 9	word32		S + A - GOT
138  * R_386_GOTPC		10	word32		GOT + A - P
139  * R_386_32PLT		11	word32		L + A
140  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
141  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
142  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
143  * R_386_TLS_IE		15	word32		@indntpoff(S)
144  * R_386_TLS_GOTIE	16	word32		@gotntpoff(S)
145  * R_386_TLS_LE		17	word32		@ntpoff(S)
146  * R_386_TLS_GD		18	word32		@tlsgd(S)
147  * R_386_TLS_LDM	19	word32		@tlsldm(S)
148  * R_386_16		20	word16		S + A
149  * R_386_PC16		21	word16		S + A - P
150  * R_386_8		22	word8		S + A
151  * R_386_PC8		23	word8		S + A - P
152  * R_386_TLS_GD_32	24	word32
153  * R_386_TLS_GD_PUSH	25	word32
154  * R_386_TLS_GD_CALL	26	word32
155  * R_386_TLS_GD_POP	27	word32
156  * R_386_TLS_LDM_32	28	word32
157  * R_386_TLS_LDM_PUSH	29	word32
158  * R_386_TLS_LDM_CALL	30	word32
159  * R_386_TLS_LDM_POP	31	word32
160  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
161  * R_386_TLS_IE_32	33	word32		@gotpoff(S)
162  * R_386_TLS_LE_32	34	word32		@tpoff(S)
163  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
164  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
165  * R_386_TLS_TPOFF32	37	word32
166  * R_386_SIZE32		38	word32		Z + A
167  * R_386_TLS_GOTDESC	39	word32
168  * R_386_TLS_DESC_CALL	40	none		none
169  * R_386_TLS_DESC	41	word32
170  * R_386_IRELATIVE	42	word32		indirect (B + A)
171  * R_386_GOT32X		43	word32		G + A - GOT / G + A
172  *
173  * Relocations are from Table 3.6: Relocation Types from the 386 psABI.
174  * (with reference also to the thread-local storage ABIs)
175  *
176  * Relocation calculations:
177  *
178  * CALCULATION uses the following notation:
179  *	A	the addend used
180  *	B	the base address of the shared object in memory
181  *	G	the offset into the global offset table
182  *	GOT	the address of teh global offset table
183  *	L	the procedure linkage entry
184  *	P	the place of the storage unit being relocated
185  *	S	the value of the symbol
186  *	Z	the size of the symbol whose index resides in the relocation
187  *		entry
188  *
189  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
190  *	   a Tls_index structure (for passing to __tls_get_addr()). The
191  *	   instructions referencing this entry will be bound to the first
192  *	   of the two GOT entries.
193  *
194  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
195  *	   a Tls_index structure (for passing to __tls_get_addr()). The
196  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
197  *	   ti_module will be filled in at run-time. The call to
198  *	   __tls_get_addr() will return the starting offset of the dynamic
199  *	   TLS block.
200  *
201  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
202  *
203  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
204  *
205  *	@dtpmod(x): calculate the module id of the object containing symbol x.
206  *
207  * The calculations in the CALCULATION column are assumed to have
208  * been performed before calling this function except for the addition of
209  * the addresses in the instructions.
210  */
211 /* BEGIN CSTYLED */
212 #if defined(DO_RELOC_LIBLD)
213 /*ARGSUSED5*/
214 int
215 do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
216     rel_desc_sname_func_t rel_desc_sname_func,
217     const char *file, int bswap, void *lml)
218 #else
219 int
220 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
221     const char *file, void *lml)
222 #endif
223 {
224 /* END CSTYLED */
225 #ifdef DO_RELOC_LIBLD
226 #define	sym (* rel_desc_sname_func)(rdesc)
227 	uchar_t	rtype = rdesc->rel_rtype;
228 #endif
229 	const Rel_entry	*rep;
230 
231 	rep = &reloc_table[rtype];
232 
233 	switch (rep->re_fsize) {
234 	case 1:
235 		/* LINTED */
236 		*((uchar_t *)off) += (uchar_t)(*value);
237 		break;
238 
239 	case 2:
240 #if defined(DORELOC_NATIVE)
241 		/* LINTED */
242 		*((Half *)off) += (Half)(*value);
243 #else
244 		{
245 			Half	v;
246 			uchar_t	*v_bytes = (uchar_t *)&v;
247 
248 			if (bswap) {
249 				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
250 				v += *value;
251 				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
252 			} else {
253 				UL_ASSIGN_HALF(v_bytes, off);
254 				v += *value;
255 				UL_ASSIGN_HALF(off, v_bytes);
256 			}
257 		}
258 #endif
259 		break;
260 
261 	case 4:
262 #if defined(DORELOC_NATIVE)
263 		/* LINTED */
264 		*((Xword *)off) += *value;
265 #else
266 		{
267 			Word	v;
268 			uchar_t	*v_bytes = (uchar_t *)&v;
269 
270 			if (bswap) {
271 				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
272 				v += *value;
273 				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
274 			} else {
275 				UL_ASSIGN_WORD(v_bytes, off);
276 				v += *value;
277 				UL_ASSIGN_WORD(off, v_bytes);
278 			}
279 		}
280 #endif
281 		break;
282 	default:
283 		/*
284 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
285 		 */
286 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
287 		return (0);
288 	}
289 	return (1);
290 
291 #ifdef DO_RELOC_LIBLD
292 #undef sym
293 #endif
294 }
295