xref: /titanic_50/usr/src/uts/intel/amd64/krtld/doreloc.c (revision 82d33c01b078ed404a986a369750cdb4743773fb)
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 |
69 			    FLG_RE_TLSINS | FLG_RE_TLSGD, 4},
70 /* R_AMD64_TLSLD */	{FLG_RE_GOTPC | FLG_RE_GOTADD |
71 			    FLG_RE_TLSINS | FLG_RE_TLSLD, 4},
72 /* R_AMD64_DTPOFF32 */	{FLG_RE_TLSINS | FLG_RE_TLSLD, 4},
73 /* R_AMD64_GOTTPOFF */	{FLG_RE_GOTPC | FLG_RE_GOTADD |
74 			    FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
75 /* R_AMD64_TPOFF32 */	{FLG_RE_TLSINS | FLG_RE_TLSLE, 4},
76 /* R_AMD64_PC64 */	{FLG_RE_PCREL, 8},
77 /* R_AMD64_GOTOFF64 */	{FLG_RE_GOTREL, 8},
78 /* R_AMD64_GOTPC32 */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
79 /* R_AMD64_GOT64 */	{FLG_RE_NOTSUP, 0},
80 /* R_AMD64_GOTPCREL64 */	{FLG_RE_NOTSUP, 0},
81 /* R_AMD64_GOTPC6 */	{FLG_RE_NOTSUP, 0},
82 /* R_AMD64_GOTPLT64 */	{FLG_RE_NOTSUP, 0},
83 /* R_AMD64_PLTOFF64 */	{FLG_RE_NOTSUP, 0}
84 
85 };
86 #if	(R_AMD64_NUM != (R_AMD64_PLTOFF64 + 1))
87 #error	"R_AMD64_NUM has grown"
88 #endif
89 
90 /*
91  * Write a single relocated value to its reference location.
92  * We assume we wish to add the relocation amount, value, to the
93  * value of the address already present at the offset.
94  *
95  * NAME			VALUE	FIELD		CALCULATION
96  *
97  * R_AMD64_NONE		 0	none		none
98  * R_AMD64_64		 1	word64		S + A
99  * R_AMD64_PC32		 2	word64		S + A
100  * R_AMD64_GOT32	 3	word32		G + A
101  * R_AMD64_PLT32	 4	word32		L + A - P
102  * R_AMD64_COPY		 5	none		none
103  * R_AMD64_GLOB_DAT	 6	word64		S
104  * R_AMD64_JUMP_SLOT	 7	word64		S
105  * R_AMD64_RELATIVE	 8	word64		B + A
106  * R_AMD64_GOTPCREL	 9	word32		G + GOT + A - P
107  * R_AMD64_32		10	word32		S + A
108  * R_AMD64_32S		11	word32		S + A
109  * R_AMD64_16		12	word16		S + A
110  * R_AMD64_PC16		13	word16		S + A - P
111  * R_AMD64_8		14	word8		S + A
112  * R_AMD64_PC8		15	word8		S + A - P
113  * R_AMD64_DTPMOD64	16	word64
114  * R_AMD64_DTPOFF64	17	word64
115  * R_AMD64_TPOFF64	18	word64
116  * R_AMD64_TLSGD	19	word32
117  * R_AMD64_TLSLD	20	word32
118  * R_AMD64_DTPOFF32	21	word32
119  * R_AMD64_GOTTPOFF	22	word32
120  * R_AMD64_TPOFF32	23	word32
121  * R_AMD64_PC64		24	word32		S + A - P
122  * R_AMD64_GOTOFF64	25	word32		S + A - GOT
123  * R_AMD64_GOTPC32	26	word32		GOT + A - P
124  * R_AMD64_GOT64	27			reserved for future expansion
125  * R_AMD64_GOTPCREL64	28			reserved for future expansion
126  * R_AMD64_GOTPC64	29			reserved for future expansion
127  * R_AMD64_GOTPLT64	30			reserved for future expansion
128  * R_AMD64_PLTOFF64	31			reserved for future expansion
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 
155 #define	HIBITS	0xffffffff80000000ULL
156 
157 /* ARGSUSED5 */
158 int
159 do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
160     const char *file, void *lml)
161 {
162 	const Rel_entry	*rep;
163 
164 	rep = &reloc_table[rtype];
165 
166 	switch (rep->re_fsize) {
167 	case 1:
168 		/* LINTED */
169 		*((uchar_t *)off) = (uchar_t)(*value);
170 		break;
171 	case 2:
172 		/* LINTED */
173 		*((Half *)off) = (Half)(*value);
174 		break;
175 	case 4:
176 		/*
177 		 * The amd64 psABI requires that we perform the following
178 		 * verifications:
179 		 *
180 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
181 		 *    computed value to 32bits.  Verify that the generated value
182 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
183 		 *    extends) to the original 64-bit value.
184 		 *
185 		 * Also, the following relocations are all 32 bit PC relative
186 		 * references.  Validate that the value being written will fit
187 		 * in the field provided.
188 		 *
189 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
190 		 */
191 		if (rtype == R_AMD64_32) {
192 			/*
193 			 * Verify that this value will 'zero-extend', this
194 			 * requires that the upper 33bits all be 'zero'.
195 			 */
196 			if ((*value & HIBITS) != 0) {
197 				/*
198 				 * To keep chkmsg() happy:
199 				 *  MSG_INTL(MSG_REL_NOFIT)
200 				 */
201 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
202 				return (0);
203 			}
204 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
205 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
206 			/*
207 			 * Verify that this value will properly sign extend.
208 			 * This is true of the upper 33bits are all either
209 			 * 'zero' or all 'one'.
210 			 */
211 			if (((*value & HIBITS) != HIBITS) &&
212 			    ((*value & HIBITS) != 0)) {
213 				/*
214 				 * To keep chkmsg() happy:
215 				 *  MSG_INTL(MSG_REL_NOFIT)
216 				 */
217 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
218 				return (0);
219 			}
220 		}
221 		/* LINTED */
222 		*((Word *)off) += *value;
223 		break;
224 	case 8:
225 		/* LINTED */
226 		*((Xword *)off) += *value;
227 		break;
228 	default:
229 		/*
230 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
231 		 */
232 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
233 		return (0);
234 	}
235 	return (1);
236 }
237