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 2007 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 #if defined(_KERNEL)
161 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
162 int
163 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
164     const char *file)
165 #elif defined(DO_RELOC_LIBLD)
166 int
167 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
168     const char *file, int bswap, void *lml)
169 #else
170 int
171 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
172     const char *file, void *lml)
173 #endif
174 {
175 	const Rel_entry	*rep;
176 
177 #if defined(DO_RELOC_LIBLD)
178 	/*
179 	 * We do not support building the amd64 linker as a cross linker
180 	 * at this time.
181 	 */
182 	if (bswap) {
183 		REL_ERR_NOSWAP(lml, file, sym, rtype);
184 		return (0);
185 	}
186 #endif
187 
188 	rep = &reloc_table[rtype];
189 
190 	switch (rep->re_fsize) {
191 	case 1:
192 		/* LINTED */
193 		*((uchar_t *)off) = (uchar_t)(*value);
194 		break;
195 	case 2:
196 		/* LINTED */
197 		*((Half *)off) = (Half)(*value);
198 		break;
199 	case 4:
200 		/*
201 		 * The amd64 psABI requires that we perform the following
202 		 * verifications:
203 		 *
204 		 *    The R_AMD64_32 and R_AMD64_32S relocations truncate the
205 		 *    computed value to 32bits.  Verify that the generated value
206 		 *    for the R_AMD64_32/32S relocation zero-extends (sign
207 		 *    extends) to the original 64-bit value.
208 		 *
209 		 * Also, the following relocations are all 32 bit PC relative
210 		 * references.  Validate that the value being written will fit
211 		 * in the field provided.
212 		 *
213 		 *    R_AMD64_PC32, R_AMD64_GOTPC32, R_AMD64_GOTPCREL
214 		 */
215 		if (rtype == R_AMD64_32) {
216 			/*
217 			 * Verify that this value will 'zero-extend', this
218 			 * requires that the upper 33bits all be 'zero'.
219 			 */
220 			if ((*value & HIBITS) != 0) {
221 				/*
222 				 * To keep chkmsg() happy:
223 				 *  MSG_INTL(MSG_REL_NOFIT)
224 				 */
225 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
226 				return (0);
227 			}
228 		} else if ((rtype == R_AMD64_32S) || (rtype == R_AMD64_PC32) ||
229 		    (rtype == R_AMD64_GOTPCREL) || (rtype == R_AMD64_GOTPC32)) {
230 			/*
231 			 * Verify that this value will properly sign extend.
232 			 * This is true of the upper 33bits are all either
233 			 * 'zero' or all 'one'.
234 			 */
235 			if (((*value & HIBITS) != HIBITS) &&
236 			    ((*value & HIBITS) != 0)) {
237 				/*
238 				 * To keep chkmsg() happy:
239 				 *  MSG_INTL(MSG_REL_NOFIT)
240 				 */
241 				REL_ERR_NOFIT(lml, file, sym, rtype, *value);
242 				return (0);
243 			}
244 		}
245 		/* LINTED */
246 		*((Word *)off) += *value;
247 		break;
248 	case 8:
249 		/* LINTED */
250 		*((Xword *)off) += *value;
251 		break;
252 	default:
253 		/*
254 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
255 		 */
256 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
257 		return (0);
258 	}
259 	return (1);
260 }
261