xref: /titanic_51/usr/src/uts/sparc/krtld/doreloc.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #if	defined(_KERNEL)
29 #include	<sys/types.h>
30 #include	"reloc.h"
31 #else
32 #include	<stdio.h>
33 #include	"sgs.h"
34 #include	"machdep.h"
35 #include	"libld.h"
36 #include	"reloc.h"
37 #include	"conv.h"
38 #include	"msg.h"
39 #endif
40 
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_SPARC_NUM] = {
48 /* R_SPARC_NONE */	{0x0, 0, 0, 0, 0},
49 /* R_SPARC_8 */		{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 1, 0, 0},
50 /* R_SPARC_16 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 2, 0, 0},
51 /* R_SPARC_32 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 4, 0, 0},
52 /* R_SPARC_DISP8 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
53 				1, 0, 0},
54 /* R_SPARC_DISP16 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
55 				2, 0, 0},
56 /* R_SPARC_DISP32 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
57 				4, 0, 0},
58 /* R_SPARC_WDISP30 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
59 				4, 2, 30},
60 /* R_SPARC_WDISP22 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
61 				4, 2, 22},
62 #if	defined(_ELF64)
63 /* R_SPARC_HI22 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 4, 10, 22},
64 #else
65 /* R_SPARC_HI22 */	{0x0, FLG_RE_NOTREL, 4, 10, 22},
66 #endif
67 /* R_SPARC_22 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 4, 0, 22},
68 /* R_SPARC_13 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY | FLG_RE_SIGN,
69 				4, 0, 13},
70 /* R_SPARC_LO10 */	{0x3ff, FLG_RE_NOTREL | FLG_RE_SIGN, 4, 0, 13},
71 /* R_SPARC_GOT10 */	{0x3ff, FLG_RE_GOTADD | FLG_RE_SIGN, 4, 0, 13},
72 /* R_SPARC_GOT13 */	{0x0, FLG_RE_GOTADD | FLG_RE_VERIFY | FLG_RE_SIGN,
73 				4, 0, 13},
74 /* R_SPARC_GOT22 */	{0x0, FLG_RE_GOTADD, 4, 10, 22},
75 /* R_SPARC_PC10 */	{0x3ff, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_SIGN |
76 				FLG_RE_LOCLBND, 4, 0, 13},
77 /* R_SPARC_PC22 */	{0x0, FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_SIGN |
78 				FLG_RE_VERIFY | FLG_RE_LOCLBND,
79 				4, 10, 22},
80 /* R_SPARC_WPLT30 */	{0x0, FLG_RE_PCREL | FLG_RE_PLTREL |
81 				FLG_RE_VERIFY | FLG_RE_SIGN,
82 				4, 2, 30},
83 /* R_SPARC_COPY */	{0x0, 0, 0, 0, 0},		/* SPECIAL */
84 #if	defined(_ELF64)
85 /* R_SPARC_GLOB_DAT */	{0x0, FLG_RE_NOTREL, 8, 0, 0},
86 #else
87 /* R_SPARC_GLOB_DAT */	{0x0, FLG_RE_NOTREL, 4, 0, 0},
88 #endif
89 /* R_SPARC_JMP_SLOT */	{0x0, 0, 0, 0, 0},		/* SPECIAL */
90 #if	defined(_ELF64)
91 /* R_SPARC_RELATIVE */	{0x0, FLG_RE_NOTREL, 8, 0, 0},
92 #else
93 /* R_SPARC_RELATIVE */	{0x0, FLG_RE_NOTREL, 4, 0, 0},
94 #endif
95 /* R_SPARC_UA32 */	{0x0, FLG_RE_NOTREL | FLG_RE_UNALIGN, 4, 0, 0},
96 /* R_SPARC_PLT32 */	{0x0, FLG_RE_PLTREL | FLG_RE_VERIFY |
97 				FLG_RE_ADDRELATIVE, 4, 0, 0},
98 /* R_SPARC_HIPLT22 */	{0x0, FLG_RE_PLTREL, 4, 10, 22},
99 /* R_SPARC_LOPLT10 */	{0x3ff, FLG_RE_PLTREL, 4, 0, 13},
100 /* R_SPARC_PCPLT32 */	{0x0, FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_VERIFY,
101 				4, 0, 0},
102 /* R_SPARC_PCPLT22 */	{0x0, FLG_RE_PLTREL | FLG_RE_PCREL |
103 				FLG_RE_VERIFY,
104 				4, 10, 22},
105 /* R_SPARC_PCPLT10 */	{0x3ff, FLG_RE_PLTREL | FLG_RE_PCREL |
106 				FLG_RE_VERIFY,
107 				4, 0, 13},
108 /* R_SPARC_10 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY | FLG_RE_SIGN,
109 				4, 0, 10},
110 /* R_SPARC_11 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY | FLG_RE_SIGN,
111 				4, 0, 11},
112 /* R_SPARC_64 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY,
113 				8, 0, 0},	/* V9 */
114 /* R_SPARC_OLO10 */	{0x3ff, FLG_RE_NOTREL | FLG_RE_EXTOFFSET |
115 				FLG_RE_SIGN, 4, 0, 13},	/* V9 */
116 /* R_SPARC_HH22 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY,
117 				4, 42, 22}, /* V9 */
118 /* R_SPARC_HM10 */	{0x3ff, FLG_RE_NOTREL | FLG_RE_SIGN,
119 				4, 32, 13},	/* V9 */
120 /* R_SPARC_LM22 */	{0x0, FLG_RE_NOTREL,
121 				4, 10, 22},	/* V9 */
122 /* R_SPARC_PC_HH22 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY |
123 				FLG_RE_GOTPC, 4, 42, 22},	/* V9 */
124 /* R_SPARC_PC_HM10 */	{0x3ff, FLG_RE_PCREL | FLG_RE_SIGN |
125 				FLG_RE_GOTPC, 4, 32, 13},	/* V9 */
126 /* R_SPARC_PC_LM22 */	{0x0, FLG_RE_PCREL | FLG_RE_GOTPC,
127 				4, 10, 22},	/* V9 */
128 /* R_SPARC_WDISP16 */	{0x0, FLG_RE_PCREL | FLG_RE_WDISP16 |
129 				FLG_RE_VERIFY | FLG_RE_SIGN,
130 				4, 2, 16},
131 /* R_SPARC_WDISP19 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
132 				4, 2, 19},
133 /* R_SPARC_GLOB_JMP */	{0x0, 0, 0, 0, 0},	/* V9 - not implemented */
134 						/* removed from v9 ABI */
135 /* R_SPARC_7 */		{0x0, FLG_RE_NOTREL, 4, 0, 7},
136 /* R_SPARC_5 */		{0x0, FLG_RE_NOTREL, 4, 0, 5},
137 /* R_SPARC_6 */		{0x0, FLG_RE_NOTREL, 4, 0, 6},
138 /* R_SPARC_DISP64 */	{0x0, FLG_RE_PCREL | FLG_RE_VERIFY | FLG_RE_SIGN,
139 				8, 0, 0},
140 /* R_SPARC_PLT64 */	{0x0, FLG_RE_PLTREL | FLG_RE_VERIFY |
141 				FLG_RE_ADDRELATIVE, 8, 0, 0},
142 /* R_SPARC_HIX22 */	{0xffffffffffffffff, FLG_RE_NOTREL | FLG_RE_VERIFY,
143 				4, 10, 22},	/* V9 - HaL */
144 /* R_SPARC_LOX10 */	{0x3ff, FLG_RE_NOTREL | FLG_RE_SIGN,
145 				4, 0, 13},	/* V9 - HaL */
146 /* R_SPARC_H44 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 4,
147 				22, 22}, /* V9 */
148 /* R_SPARC_M44 */	{0x3ff, FLG_RE_NOTREL, 4, 12, 10},	/* V9 */
149 /* R_SPARC_L44 */	{0xfff, FLG_RE_NOTREL, 4, 0, 13},	/* V9 */
150 /* R_SPARC_REGISTER */	{0x0, FLG_RE_REGISTER,
151 				0, 0, 0},	/* V9 - special */
152 /* R_SPARC_UA64 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY | FLG_RE_UNALIGN,
153 				8, 0, 0},	/* V9 */
154 /* R_SPARC_UA16 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY | FLG_RE_UNALIGN,
155 				2, 0, 0},
156 /* R_SPARC_TLS_GD_HI22 */   {0x0, FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSGD,
157 				4, 10, 22},
158 /* R_SPARC_TLS_GD_LO10 */   {0x3ff, FLG_RE_GOTADD | FLG_RE_TLSINS |
159 				FLG_RE_TLSGD | FLG_RE_SIGN, 4, 0, 13},
160 /* R_SPARC_TLS_GD_ADD */    {0x0, FLG_RE_TLSINS | FLG_RE_TLSGD, 0, 0, 0},
161 /* R_SPARC_TLS_GD_CALL */   {0x0, FLG_RE_TLSINS | FLG_RE_TLSGD, 0, 0, 0},
162 /* R_SPARC_TLS_LDM_HI22 */  {0x0, FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSLD,
163 				4, 10, 22},
164 /* R_SPARC_TLS_LDM_LO10 */  {0x3ff, FLG_RE_GOTADD | FLG_RE_TLSINS |
165 				FLG_RE_TLSLD | FLG_RE_SIGN, 4, 0, 13},
166 /* R_SPARC_TLS_LDM_ADD */   {0x0, FLG_RE_TLSINS | FLG_RE_TLSLD, 0, 0, 0},
167 /* R_SPARC_TLS_LDM_CALL */  {0x0, FLG_RE_TLSINS | FLG_RE_TLSLD, 0, 0, 0},
168 /* R_SPARC_TLS_LDO_HIX22 */ {0x0, FLG_RE_VERIFY |
169 				FLG_RE_TLSINS | FLG_RE_NOTREL | FLG_RE_TLSLD,
170 				4, 10, 22},
171 /* R_SPARC_TLS_LDO_LOX10 */ {0x3ff, FLG_RE_TLSINS | FLG_RE_SIGN |
172 				FLG_RE_TLSLD |FLG_RE_NOTREL,
173 				4, 0, 13},
174 /* R_SPARC_TLS_LDO_ADD */   {0x0, FLG_RE_TLSINS | FLG_RE_TLSLD, 0, 0, 0},
175 /* R_SPARC_TLS_IE_HI22 */   {0x0, FLG_RE_TLSINS | FLG_RE_GOTADD | FLG_RE_TLSIE,
176 				4, 10, 22},
177 /* R_SPARC_TLS_IE_LO10 */   {0x3ff, FLG_RE_TLSINS | FLG_RE_GOTADD |
178 				FLG_RE_TLSIE | FLG_RE_SIGN, 4, 0, 13},
179 /* R_SPARC_TLS_IE_LD */	    {0x0, FLG_RE_TLSINS | FLG_RE_TLSIE, 0, 0, 0},
180 /* R_SPARC_TLS_IE_LDX */    {0x0, FLG_RE_TLSINS | FLG_RE_TLSIE, 0, 0, 0},
181 /* R_SPARC_TLS_IE_ADD */    {0x0, FLG_RE_TLSINS | FLG_RE_TLSIE, 0, 0, 0},
182 /* R_SPARC_TLS_LE_HIX22 */  {(unsigned long)((long)-1), FLG_RE_VERIFY |
183 				FLG_RE_TLSINS | FLG_RE_NOTREL | FLG_RE_TLSLE,
184 				4, 10, 22},
185 /* R_SPARC_TLS_LE_LOX10 */  {0x3ff, FLG_RE_TLSINS | FLG_RE_SIGN |
186 				FLG_RE_TLSLE |FLG_RE_NOTREL,
187 				4, 0, 13},
188 /* R_SPARC_TLS_DTPMOD32 */  {0x0, FLG_RE_NOTREL, 4, 0, 0},
189 /* R_SPARC_TLS_DTPMOD64 */  {0x0, FLG_RE_NOTREL, 8, 0, 0},
190 /* R_SPARC_TLS_DTPOFF32 */  {0x0, FLG_RE_NOTREL, 4, 0, 0},
191 /* R_SPARC_TLS_DTPOFF64 */  {0x0, FLG_RE_NOTREL, 8, 0, 0},
192 /* R_SPARC_TLS_TPOFF32 */   {0x0, FLG_RE_NOTREL, 4, 0, 0},
193 /* R_SPARC_TLS_TPOFF64 */   {0x0, FLG_RE_NOTREL, 8, 0, 0},
194 /* R_SPARC_GOTDATA_HIX22 */	{0, FLG_RE_GOTREL, 4, 10, 22},
195 /* R_SPARC_GOTDATA_LOX10 */	{ 0x3ff, FLG_RE_GOTREL | FLG_RE_SIGN, 4, 0, 13},
196 /* R_SPARC_GOTDATA_OP_HIX22 */	{ 0x0, FLG_RE_GOTOPINS | FLG_RE_GOTADD,
197 					4, 10, 22},
198 /* R_SPARC_GOTDATA_OP_LOX10 */	{ 0x3ff, FLG_RE_SIGN | FLG_RE_GOTOPINS |
199 					FLG_RE_GOTADD, 4, 0, 13},
200 /* R_SPARC_GOTDATA_OP */	{ 0x0, FLG_RE_GOTOPINS, 0, 0, 0},
201 /* R_SPARC_H34 */	{0x0, FLG_RE_NOTREL | FLG_RE_VERIFY, 4,
202 				12, 22} /* V9 */
203 };
204 
205 
206 /*
207  * Write a single relocated value to its reference location.
208  * We assume we wish to add the relocation amount, value, to the
209  * the value of the address already present in the instruction.
210  *
211  * NAME			VALUE	FIELD		CALCULATION
212  *
213  * R_SPARC_NONE		0	none		none
214  * R_SPARC_8		1	V-byte8		S + A
215  * R_SPARC_16		2	V-half16	S + A
216  * R_SPARC_32		3	V-word32	S + A
217  * R_SPARC_DISP8	4	V-byte8		S + A - P
218  * R_SPARC_DISP16	5	V-half16	S + A - P
219  * R_SPARC_DISP32	6	V-word32	S + A - P
220  * R_SPARC_WDISP30	7	V-disp30	(S + A - P) >> 2
221  * R_SPARC_WDISP22	8	V-disp22	(S + A - P) >> 2
222  * R_SPARC_HI22		9	T-imm22		(S + A) >> 10
223  * R_SPARC_22		10	V-imm22		S + A
224  * R_SPARC_13		11	V-simm13	S + A
225  * R_SPARC_LO10		12	T-simm13	(S + A) & 0x3ff
226  * R_SPARC_GOT10	13	T-simm13	G & 0x3ff
227  * R_SPARC_GOT13	14	V-simm13	G
228  * R_SPARC_GOT22	15	T-imm22		G >> 10
229  * R_SPARC_PC10		16	T-simm13	(S + A - P) & 0x3ff
230  * R_SPARC_PC22		17	V-disp22	(S + A - P) >> 10
231  * R_SPARC_WPLT30	18	V-disp30	(L + A - P) >> 2
232  * R_SPARC_COPY		19	none		none
233  * R_SPARC_GLOB_DAT	20	V-word32	S + A
234  * R_SPARC_JMP_SLOT	21	V-plt22		S + A
235  * R_SPARC_RELATIVE	22	V-word32	S + A
236  * R_SPARC_UA32		23	V-word32	S + A
237  * R_SPARC_PLT32	24	V-word32        L + A
238  * R_SPARC_HIPLT22	25	T-imm22         (L + A) >> 10
239  * R_SPARC_LOPLT10	26	T-simm13        (L + A) & 0x3ff
240  * R_SPARC_PCPLT32	27	V-word32        L + A - P
241  * R_SPARC_PCPLT22	28	V-disp22        (L + A - P) >> 10
242  * R_SPARC_PCPLT10	29	V-simm13        (L + A - P) & 0x3ff
243  * R_SPARC_10		30	V-simm10	S + A
244  * R_SPARC_11		31	V-simm11	S + A
245  * R_SPARC_64		32	V-xword64	S + A
246  * R_SPARC_OLO10	33	V-simm13	((S + A) & 0x3ff) + O
247  * R_SPARC_HH22		34	V-imm22		(S + A) >> 42
248  * R_SPARC_HM10		35	T-simm13	((S + A) >>32) & 0x3ff
249  * R_SPARC_LM22		36	T-imm22		(S + A) >> 10
250  * R_SPARC_PC_HH22	37	V-imm22		(S + A - P) >> 42
251  * R_SPARC_PC_HM10	38	T-simm13	((S + A - P) >> 32) & 0x3ff
252  * R_SPARC_PC_LM22	39	T-imm22		(S + A - P) >> 10
253  * R_SPARC_WDISP16	40	V-d2/disp14	(S + A - P) >> 2
254  * R_SPARC_WDISP19	41	V-disp19	(S + A - P) >> 2
255  * R_SPARC_GLOB_JMP	42	V-xword64	S + A
256  * R_SPARC_7		43	V-imm7		S + A
257  * R_SPARC_5		44	V-imm5		S + A
258  * R_SPARC_6		45	V-imm6		S + A
259  * R_SPARC_DISP64	46	V-xword64	S + A - P
260  * R_SPARC_PLT64	47	V-xword64	L + A
261  * R_SPARC_HIX22	48	V-imm22	((S + A)^0xffffffffffffffff) >> 10
262  * R_SPARC_LOX10	49	T-simm13	((S + A) & 0x3ff) | 0x1c00
263  * R_SPARC_H44		50	V-imm22		(S + A) >> 22
264  * R_SPARC_M44		51	T-imm10		((S + A) >> 12) & 0x3ff
265  * R_SPARC_L44		52	T-imm13		(S + A) & 0xfff
266  * R_SPARC_REGISTER	53	V-xword64	S + A
267  * R_SPARC_UA64		54	V-xword64	S + A
268  * R_SPARC_UA16		55	V-half16	S + A
269  * R_SPARC_TLS_GD_HI22	56	T-simm22	@dtlndx(S+A) >> 10
270  * R_SPARC_TLS_GD_LO10	57	T-simm13	@dtlndx(S+A) & 0x3ff
271  * R_SPARC_TLS_GD_ADD	58	none		special
272  * R_SPARC_TLS_GD_CALL	59	V-disp30	special
273  * R_SPARC_TLS_LDM_HI22	60	T-simm22	@tmndx(S+A) >> 10
274  * R_SPARC_TLS_LDM_LO10	61	T-simm13	@tmndx(S+A) & 0x3ff
275  * R_SPARC_TLS_LDM_ADD	62	none		special
276  * R_SPARC_TLS_LDM_CALL	63	V-disp30	special
277  * R_SPARC_TLS_LDO_HIX22 64	V-simm22	@dtpoff(S+A) >> 10
278  * R_SPARC_TLS_LDO_LOX10 65	T-simm13	@dtpoff(S+A) & 0x3ff
279  * R_SPARC_TLS_LDO_ADD	66	none		special
280  * R_SPARC_TLS_IE_HI22	67	T-simm22	@got(@tpoff(S+A)) >> 10
281  * R_SPARC_TLS_IE_LO10	68	T-simm13	@got(@tpoff(S+A)) & 0x3ff
282  * R_SPARC_TLS_IE_LD	69	none		special
283  * R_SPARC_TLS_IE_LDX	70	none		special
284  * R_SPARC_TLS_IE_ADD	71	none		special
285  * R_SPARC_TLS_LE_HIX22	72	V-simm22	(@tpoff(S+A)^0xffffffff) >> 10
286  * R_SPARC_TLS_LE_LOX10	73	T-simm13	(@tpoff(S+A) & 0x3ff) | 0x1c00
287  * R_SPARC_TLS_DTPMOD32	74	V-word32	@dtmod(S+A)
288  * R_SPARC_TLS_DTPMOD64	75	V-word64	@dtmod(S+A)
289  * R_SPARC_TLS_DTPOFF32	76	V-word32	@dtpoff(S+A)
290  * R_SPARC_TLS_DTPOFF64	77	V-word64	@dtpoff(S+A)
291  * R_SPARC_TLS_TPOFF32	78	V-word32	@tpoff(S+A)
292  * R_SPARC_TLS_TPOFF64	79	V-word64	@tpoff(S+A)
293  * R_SPARC_GOTDATA_HIX22 80	T-imm22		((S + A - G) >> 10) ^
294  *						  ((S + A- G) >> 42)
295  * R_SPARC_GOTDATA_LOX10 81	T-simm13	((S + A - G) & 0x3ff) |
296  *						  (((S + A - G) >> 31) & 0x1c00)
297  * R_SPARC_GOTDATA_OP_HIX22 82	T-imm22		(G >> 10) & (G >> 42)
298  * R_SPARC_GOTDATA_OP_LOX10	T-simm13	(G & 0x3ff) |
299  *						  ((0x1c00^~((G>>50)&0x1c00)))
300  * R_SPARC_H34			V-imm22		(S + A) >> 12
301  *
302  *	This is Figure 4-20: Relocation Types from the Draft Copy of
303  * the ABI, Printed on 11/29/88.
304  *
305  * NOTE1: relocations 24->45 are newly registered relocations to support
306  *	 C++ ABI & SPARC V8+ and SPARC V9 architectures (1/9/94), and
307  *	 64-bit relocations 46-55 were added for SPARC V9.
308  *
309  * NOTE2: relocations 56->79 are added to support Thread-Local storage
310  *	  as recorded in PSARC/2001/509
311  *
312  * NOTE3: The value to be passed for relocations R_SPARC_HIX22 and
313  *	  R_SPARC_TLS_HIX22 are negative values. So the upper 10 or 40 bits
314  *	  are 1. (So when the exclusive OR is applied, the upper bits
315  *	  will be 0.)
316  *
317  * Relocation calculations:
318  *
319  * The FIELD names indicate whether the relocation type checks for overflow.
320  * A calculated relocation value may be larger than the intended field, and
321  * the relocation type may verify (V) that the value fits, or truncate (T)
322  * the result.
323  *
324  * CALCULATION uses the following notation:
325  *      A       the addend used
326  *      B       the base address of the shared object in memory
327  *      G       the offset into the global offset table
328  *      L       the procedure linkage entry
329  *      P       the place of the storage unit being relocated
330  *      S       the value of the symbol
331  *	O	secondary addend (extra offset) in v9 r_info field
332  *
333  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
334  *	   a Tls_index structure (for passing to __tls_get_addr()). The
335  *	   instructions referencing this entry will be bound to the first
336  *	   of the two GOT entries.
337  *
338  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
339  *	   a Tls_index structure (for passing to __tls_get_addr()). The
340  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
341  *	   ti_module will be filled in at run-time. The call to
342  *	   __tls_get_addr() will return the starting offset of the dynamic
343  *	   TLS block.
344  *
345  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
346  *
347  *	@tpoff(x): calculate the negative tlsoffset relative to the static
348  *	   TLS block. This value can be added to the thread-pointer to
349  *	   calculate the tls address.
350  *
351  *	@dtpmod(x): calculate the module id of the object containing symbol x.
352  *
353  * The calculations in the CALCULATION column are assumed to have been performed
354  * before calling this function except for the addition of the addresses in the
355  * instructions.
356  *
357  * Upon successful completion of do_reloc() *value will be set to the
358  * 'bit-shifted' value that will be or'ed into memory.
359  */
360 /* ARGSUSED3 */
361 int
362 do_reloc(unsigned char rtype, unsigned char *off, Xword *value,
363 	const char *sym, const char *file)
364 {
365 	Xword			uvalue = 0;
366 	Xword			basevalue, sigbit_mask, sigfit_mask;
367 	Xword			corevalue = *value;
368 	unsigned char		bshift;
369 	int			field_size, re_flags;
370 	const Rel_entry *	rep;
371 
372 	rep = &reloc_table[rtype];
373 	bshift = rep->re_bshift;
374 	field_size = rep->re_fsize;
375 	re_flags = rep->re_flags;
376 	sigbit_mask = S_MASK(rep->re_sigbits);
377 
378 	if ((re_flags & FLG_RE_SIGN) && sigbit_mask) {
379 		/*
380 		 * sigfit_mask takes into account that a value
381 		 * might be signed and discards the signbit for
382 		 * comparison.
383 		 */
384 		sigfit_mask = S_MASK(rep->re_sigbits - 1);
385 	} else
386 		sigfit_mask = sigbit_mask;
387 
388 	if (field_size == 0) {
389 		REL_ERR_UNIMPL(file, sym, rtype);
390 		return (0);
391 	}
392 
393 	if (re_flags & FLG_RE_UNALIGN) {
394 		int		i;
395 		unsigned char *dest = (unsigned char *)&basevalue;
396 
397 		/*
398 		 * Adjust the offset.
399 		 */
400 		/* LINTED */
401 		i = (int)(sizeof (Xword) - field_size);
402 		if (i > 0)
403 			dest += i;
404 
405 		basevalue = 0;
406 		for (i = field_size - 1; i >= 0; i--)
407 			dest[i] = off[i];
408 	} else {
409 		if (((field_size == 2) && ((long long)off & 0x1)) ||
410 		    ((field_size == 4) && ((long long)off & 0x3)) ||
411 		    ((field_size == 8) && ((long long)off & 0x7))) {
412 			REL_ERR_NONALIGN(file, sym, rtype, off);
413 			return (0);
414 		}
415 		switch (field_size) {
416 		case 1:
417 			basevalue = (Xword)*((unsigned char *)off);
418 			break;
419 		case 2:
420 			/* LINTED */
421 			basevalue = (Xword)*((Half *)off);
422 			break;
423 		case 4:
424 			/* LINTED */
425 			basevalue = (Xword)*((Word *)off);
426 			break;
427 		case 8:
428 			/* LINTED */
429 			basevalue = (Xword)*((Xword *)off);
430 			break;
431 		default:
432 			REL_ERR_UNNOBITS(file, sym, rtype, rep->re_fsize * 8);
433 			return (0);
434 		}
435 	}
436 
437 	if (sigbit_mask) {
438 		/*
439 		 * The WDISP16 relocation is an unusual one in that it's bits
440 		 * are not all contiguous.  We have to selectivly pull them out.
441 		 */
442 		if (re_flags & FLG_RE_WDISP16) {
443 			uvalue = ((basevalue & 0x300000) >> 6) |
444 				(basevalue & 0x3fff);
445 			basevalue &= ~0x303fff;
446 		} else {
447 			uvalue = sigbit_mask & basevalue;
448 			basevalue &= ~sigbit_mask;
449 		}
450 		/*
451 		 * If value is signed make sure that we signextend the uvalue.
452 		 */
453 		if (re_flags & FLG_RE_SIGN) {
454 			if (uvalue & (~sigbit_mask & sigfit_mask))
455 				uvalue |= ~sigbit_mask;
456 		}
457 	} else
458 		uvalue = basevalue;
459 
460 	if (bshift)
461 		uvalue <<= bshift;
462 
463 	uvalue += *value;
464 
465 	if (rep->re_mask &&
466 	    ((rtype == R_SPARC_HIX22) || (rtype == R_SPARC_TLS_LE_HIX22)))
467 		uvalue ^= rep->re_mask;
468 
469 	if (bshift) {
470 		/*
471 		 * This is to check that we are not attempting to
472 		 * jump to a non-4 byte aligned address.
473 		 */
474 		if ((bshift == 2) && (uvalue & 0x3)) {
475 			REL_ERR_LOSEBITS(file, sym, rtype, uvalue, 2, off);
476 			return (0);
477 		}
478 
479 		if (re_flags & FLG_RE_SIGN) {
480 			uvalue = (Sxword)uvalue >> bshift;
481 		} else {
482 			uvalue >>= bshift;
483 		}
484 		corevalue >>= bshift;
485 	}
486 
487 	if ((rtype == R_SPARC_GOTDATA_HIX22) ||
488 	    (rtype == R_SPARC_GOTDATA_OP_HIX22)) {
489 		uvalue ^= ((Sxword)(*value) >> 31);
490 		corevalue ^= ((Sxword)(*value) >> 31);
491 	}
492 
493 	if (rep->re_mask &&
494 	    (rtype != R_SPARC_HIX22) && (rtype != R_SPARC_TLS_LE_HIX22) &&
495 	    (rtype != R_SPARC_GOTDATA_HIX22))
496 		uvalue &= rep->re_mask;
497 
498 	if ((rtype == R_SPARC_LOX10) || (rtype == R_SPARC_TLS_LE_LOX10)) {
499 		uvalue |= 0x1c00;
500 		corevalue |= 0x1c00;
501 	}
502 
503 	if ((rtype == R_SPARC_GOTDATA_LOX10) ||
504 	    (rtype == R_SPARC_GOTDATA_OP_LOX10)) {
505 		uvalue |= ((Sxword)(*value) >> 31) & 0x1c00;
506 		corevalue |= ((Sxword)(*value) >> 31) & 0x1c00;
507 	}
508 
509 
510 	if ((re_flags & FLG_RE_VERIFY) && sigbit_mask) {
511 		if (((re_flags & FLG_RE_SIGN) &&
512 		    (S_INRANGE((Sxword)uvalue, rep->re_sigbits - 1) == 0)) ||
513 		    (!(re_flags & FLG_RE_SIGN) &&
514 		    ((sigbit_mask & uvalue) != uvalue))) {
515 			REL_ERR_NOFIT(file, sym, rtype, uvalue);
516 			return (0);
517 		}
518 	}
519 
520 	if (sigbit_mask) {
521 		/*
522 		 * Again the R_SPARC_WDISP16 relocation takes special
523 		 * processing because of its non-continguous bits.
524 		 */
525 		if (re_flags & FLG_RE_WDISP16)
526 			uvalue = ((uvalue & 0xc000) << 6) |
527 				(uvalue & 0x3fff);
528 		else
529 			uvalue &= sigbit_mask;
530 		/*
531 		 * Combine value back with original word
532 		 */
533 		uvalue |= basevalue;
534 	}
535 	*value = corevalue;
536 
537 	if (re_flags & FLG_RE_UNALIGN) {
538 		int		i;
539 		unsigned char *src = (unsigned char *)&uvalue;
540 
541 		/*
542 		 * Adjust the offset.
543 		 */
544 		/* LINTED */
545 		i = (int)(sizeof (Xword) - field_size);
546 		if (i > 0)
547 			src += i;
548 
549 		for (i = field_size - 1; i >= 0; i--)
550 			off[i] = src[i];
551 	} else {
552 		switch (rep->re_fsize) {
553 		case 1:
554 			*((unsigned char *)off) = (unsigned char)uvalue;
555 			break;
556 		case 2:
557 			/* LINTED */
558 			*((Half *)off) = (Half)uvalue;
559 			break;
560 		case 4:
561 			/* LINTED */
562 			*((Word *)off) = uvalue;
563 			break;
564 		case 8:
565 			/* LINTED */
566 			*((Xword *)off) = uvalue;
567 			break;
568 		}
569 	}
570 	return (1);
571 }
572