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