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