xref: /illumos-gate/usr/src/uts/intel/amd64/krtld/doreloc.c (revision 0173c38a73f34277e0c97a19fedfd25d81ba8380)
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 2006 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 #include	<stdio.h>
34 #include	"sgs.h"
35 #include	"machdep.h"
36 #include	"libld.h"
37 #include	"reloc.h"
38 #include	"conv.h"
39 #include	"msg.h"
40 #endif
41 
42 /*
43  * This table represents the current relocations that do_reloc() is able to
44  * process.  The relocations below that are marked SPECIAL are relocations that
45  * take special processing and shouldn't actually ever be passed to do_reloc().
46  */
47 const Rel_entry	reloc_table[R_AMD64_NUM] = {
48 /* R_AMD64_NONE */	{FLG_RE_NOTREL, 0},
49 /* R_AMD64_64 */	{FLG_RE_NOTREL, 8},
50 /* R_AMD64_PC32 */	{FLG_RE_PCREL, 4},
51 /* R_AMD64_GOT32 */	{FLG_RE_NOTSUP, 0},
52 /* R_AMD64_PLT32 */	{FLG_RE_PCREL | FLG_RE_PLTREL |
53 			    FLG_RE_VERIFY | FLG_RE_SIGN, 4},
54 /* R_AMD64_COPY */	{FLG_RE_NOTSUP, 0},		/* SPECIAL */
55 /* R_AMD64_GLOB_DAT */	{FLG_RE_NOTREL, 8},
56 /* R_AMD64_JUMP_SLOT */	{FLG_RE_NOTSUP, 0},		/* SPECIAL */
57 /* R_AMD64_RELATIVE */	{FLG_RE_NOTREL, 8},
58 /* R_AMD64_GOTPCREL */	{FLG_RE_GOTPC | FLG_RE_GOTADD, 4},
59 /* R_AMD64_32 */	{FLG_RE_NOTREL, 4},
60 /* R_AMD64_32S */	{FLG_RE_NOTREL, 4},
61 /* R_AMD64_16 */	{FLG_RE_NOTREL, 2},
62 /* R_AMD64_PC16 */	{FLG_RE_PCREL, 2},
63 /* R_AMD64_8 */		{FLG_RE_NOTREL, 1},
64 /* R_AMD64_PC8 */	{FLG_RE_PCREL, 1},
65 /* R_AMD64_DTPMOD64 */	{FLG_RE_NOTREL, 8},
66 /* R_AMD64_DTPOFF64 */	{FLG_RE_NOTREL, 8},
67 /* R_AMD64_TPOFF64 */	{FLG_RE_NOTREL, 8},
68 /* R_AMD64_TLSGD */	{FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSGD, 4},
69 /* R_AMD64_TLSLD */	{FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSLD, 4},
70 /* R_AMD64_DTPOFF32 */	{FLG_RE_TLSLD, 4},
71 /* R_AMD64_GOTTPOFF */	{FLG_RE_GOTPC | FLG_RE_GOTADD | FLG_RE_TLSIE, 4},
72 /* R_AMD64_TPOFF32 */	{FLG_RE_TLSLE, 4},
73 /* R_AMD64_PC64 */	{FLG_RE_PCREL, 8},
74 /* R_AMD64_GOTOFF64 */	{FLG_RE_GOTREL, 8},
75 /* R_AMD64_GOTPC32 */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
76 /* R_AMD64_GOT64 */	{FLG_RE_NOTSUP, 0},
77 /* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0},
78 /* R_AMD64_GOTPC6 */	{FLG_RE_NOTSUP, 0},
79 /* R_AMD64_GOTPLT64 */	{FLG_RE_NOTSUP, 0},
80 /* R_AMD64_PLTOFF64 */	{FLG_RE_NOTSUP, 0}
81 
82 };
83 #if	(R_AMD64_NUM != (R_AMD64_PLTOFF64 + 1))
84 #error	"R_AMD64_NUM has grown"
85 #endif
86 
87 /*
88  * Write a single relocated value to its reference location.
89  * We assume we wish to add the relocation amount, value, to the
90  * value of the address already present at the offset.
91  *
92  * NAME			VALUE	FIELD		CALCULATION
93  *
94  * R_AMD64_NONE		 0	none		none
95  * R_AMD64_64		 1	word64		S + A
96  * R_AMD64_PC32		 2	word64		S + A
97  * R_AMD64_GOT32	 3	word32		G + A
98  * R_AMD64_PLT32	 4	word32		L + A - P
99  * R_AMD64_COPY		 5	none		none
100  * R_AMD64_GLOB_DAT	 6	word64		S
101  * R_AMD64_JUMP_SLOT	 7	word64		S
102  * R_AMD64_RELATIVE	 8	word64		B + A
103  * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
104  * R_AMD64_32		10	word32		S + A
105  * R_AMD64_32S		11	word32		S + A
106  * R_AMD64_16		12	word16		S + A
107  * R_AMD64_PC16		13	word16		S + A - P
108  * R_AMD64_8		14	word8		S + A
109  * R_AMD64_PC8		15	word8		S + A - P
110  * R_AMD64_DTPMOD64	16	word64
111  * R_AMD64_DTPOFF64	17	word64
112  * R_AMD64_TPOFF64	18	word64
113  * R_AMD64_TLSGD	19	word32
114  * R_AMD64_TLSLD	20	word32
115  * R_AMD64_DTPOFF32	21	word32
116  * R_AMD64_GOTTPOFF	22	word32
117  * R_AMD64_TPOFF32	23	word32
118  * R_AMD64_PC64		24	word32		S + A - P
119  * R_AMD64_GOTOFF64	25	word32		S + A - GOT
120  * R_AMD64_GOTPC32	26	word32		GOT + A - P
121  * R_AMD64_GOT64	27			reserved for future expansion
122  * R_AMD64_GOTPCREL64	28			reserved for future expansion
123  * R_AMD64_GOTPC64	29			reserved for future expansion
124  * R_AMD64_GOTPLT64	30			reserved for future expansion
125  * R_AMD64_PLTOFF64	31			reserved for future expansion
126  *
127  * Relocation calculations:
128  *	A	Represents the addend used to compute the value of the
129  *		relocatable field.
130  *
131  *	B	Represents the base address at which a shared objects has
132  *		been loaded into memory during executaion.  Generally, a
133  *		shared objects is built with a 0 base virtual address,
134  *		but the execution address will be different.
135  *
136  *	G	Represents the offset into the global offset table
137  *		at which the relocation entry's symbol will reside
138  *		during execution.
139  *
140  *	GOT	Rrepresents the address of the global offset table.
141  *
142  *	L	Represents the place (section offset or address) of
143  *		the Procedure Linkage Table entry for a symbol.
144  *
145  *	P	Represents the place (section offset or address) of the
146  *		storage unit being relocated (computed using r_offset).
147  *
148  *	S	Represents the value of the symbol whose index resides
149  *		in the relocation entry.
150  */
151 
152 #define	HIBITS	0xffffffff80000000ULL
153 
154 /* ARGSUSED5 */
155 int
156 do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
157     const char *file, void *lml)
158 {
159 	const Rel_entry	*rep;
160 
161 	rep = &reloc_table[rtype];
162 
163 	switch (rep->re_fsize) {
164 	case 1:
165 		/* LINTED */
166 		*((uchar_t *)off) = (uchar_t)(*value);
167 		break;
168 	case 2:
169 		/* LINTED */
170 		*((Half *)off) = (Half)(*value);
171 		break;
172 	case 4:
173 		/*
174 		 * The amd64 psABI requires that we perform the following
175 		 * verifications:
176 		 *
177 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
178 		 *    computed value to 32bits.  Verify that the generated value
179 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
180 		 *    extends) to the original 64-bit value.
181 		 *
182 		 * Also, the following relocations are all 32 bit PC relative
183 		 * references.  Validate that the value being written will fit
184 		 * in the field provided.
185 		 *
186 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
187 		 */
188 		if (rtype == R_AMD64_32) {
189 			/*
190 			 * Verify that this value will 'zero-extend', this
191 			 * requires that the upper 33bits all be 'zero'.
192 			 */
193 			if ((*value & HIBITS) != 0) {
194 				/*
195 				 * To keep chkmsg() happy:
196 				 *  MSG_INTL(MSG_REL_NOFIT)
197 				 */
198 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
199 				return (0);
200 			}
201 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
202 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
203 			/*
204 			 * Verify that this value will properly sign extend.
205 			 * This is true of the upper 33bits are all either
206 			 * 'zero' or all 'one'.
207 			 */
208 			if (((*value & HIBITS) != HIBITS) &&
209 			    ((*value & HIBITS) != 0)) {
210 				/*
211 				 * To keep chkmsg() happy:
212 				 *  MSG_INTL(MSG_REL_NOFIT)
213 				 */
214 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
215 				return (0);
216 			}
217 		}
218 		/* LINTED */
219 		*((Word *)off) += *value;
220 		break;
221 	case 8:
222 		/* LINTED */
223 		*((Xword *)off) += *value;
224 		break;
225 	default:
226 		/*
227 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
228 		 */
229 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
230 		return (0);
231 	}
232 	return (1);
233 }
234