xref: /titanic_50/usr/src/uts/intel/ia32/krtld/doreloc.c (revision ab4a9beb2e4d596be0b3288c7d92919e27781b57)
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_386_NUM] = {
48 /* R_386_NONE */	{FLG_RE_NOTREL, 0},
49 /* R_386_32 */		{FLG_RE_NOTREL, 4},
50 /* R_386_PC32 */	{FLG_RE_PCREL, 4},
51 /* R_386_GOT32 */	{FLG_RE_GOTADD, 4},
52 /* R_386_PLT32 */	{FLG_RE_PLTREL | FLG_RE_PCREL, 4},
53 /* R_386_COPY */	{FLG_RE_NOTREL, 0},			/* SPECIAL */
54 /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
55 /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},			/* SPECIAL */
56 /* R_386_RELATIVE */	{FLG_RE_NOTREL, 4},
57 /* R_386_GOTOFF */	{FLG_RE_GOTREL, 4},
58 /* R_386_GOTPC */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
59 /* R_386_32PLT */	{FLG_RE_PLTREL, 4},
60 /* R_386_TLS_GD_PLT */	{FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSGD, 4},
61 /* R_386_TLS_LDM_PLT */	{FLG_RE_PLTREL | FLG_RE_PCREL | FLG_RE_TLSLD, 4},
62 /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
63 /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSIE, 4},
64 /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSIE, 4},
65 /* R_386_TLS_LE */	{FLG_RE_TLSLE, 4},
66 /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSGD, 4},
67 /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD, 4},
68 /* R_386_16 */		{FLG_RE_NOTREL, 2},
69 /* R_386_PC16 */	{FLG_RE_PCREL, 2},
70 /* R_386_8 */		{FLG_RE_NOTREL, 1},
71 /* R_386_PC8 */		{FLG_RE_PCREL, 1},
72 /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
73 /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
74 /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
75 /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
76 /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
77 /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
78 /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
79 /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
80 /* R_386_TLS_LDO_32 */	{FLG_RE_TLSLD, 4},
81 /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
82 /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
83 /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
84 /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
85 /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0},
86 /* R_386_SIZE32 */	{FLG_RE_SIZE | FLG_RE_VERIFY, 4}
87 };
88 
89 /*
90  * Write a single relocated value to its reference location.
91  * We assume we wish to add the relocation amount, value, to the
92  * value of the address already present at the offset.
93  *
94  * NAME			VALUE	FIELD		CALCULATION
95  *
96  * R_386_NONE		 0	none		none
97  * R_386_32		 1	word32		S + A
98  * R_386_PC32		 2	word32		S + A - P
99  * R_386_GOT32		 3	word32		G + A - P
100  * R_386_PLT32		 4	word32		L + A - P
101  * R_386_COPY		 5	none		none
102  * R_386_GLOB_DAT	 6	word32		S
103  * R_386_JMP_SLOT	 7	word32		S
104  * R_386_RELATIVE	 8	word32		B + A
105  * R_386_GOTOFF		 9	word32		S + A - GOT
106  * R_386_GOTPC		10	word32		GOT + A - P
107  * R_386_32PLT		11	word32		L + A
108  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
109  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
110  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
111  * R_386_TLS_IE		15	word32		@indntpoff(S)
112  * R_386_TLS_GD		18	word32		@tlsgd(S)
113  * R_386_TLS_LDM	19	word32		@tlsldm(S)
114  * R_386_16		20	word16		S + A
115  * R_386_PC16		21	word16		S + A - P
116  * R_386_8		22	word8		S + A
117  * R_386_PC8		23	word8		S + A - P
118  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
119  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
120  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
121  * R_386_SIZE32		38	word32		Z + A
122  *
123  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
124  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
125  * and is in the process of being registered with intel ABI (1/13/94).
126  *
127  * Relocations R_386_TLS_* are added to support Thread-Local storage
128  *	as recorded in PSARC/2001/509
129  *
130  * Relocation calculations:
131  *
132  * CALCULATION uses the following notation:
133  *	A	the addend used
134  *	B	the base address of the shared object in memory
135  *	G	the offset into the global offset table
136  *	GOT	the address of teh global offset table
137  *	L	the procedure linkage entry
138  *	P	the place of the storage unit being relocated
139  *	S	the value of the symbol
140  *	Z	the size of the symbol whose index resides in the relocation
141  *		entry
142  *
143  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
144  *	   a Tls_index structure (for passing to __tls_get_addr()). The
145  *	   instructions referencing this entry will be bound to the first
146  *	   of the two GOT entries.
147  *
148  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
149  *	   a Tls_index structure (for passing to __tls_get_addr()). The
150  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
151  *	   ti_module will be filled in at run-time. The call to
152  *	   __tls_get_addr() will return the starting offset of the dynamic
153  *	   TLS block.
154  *
155  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
156  *
157  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
158  *
159  *	@dtpmod(x): calculate the module id of the object containing symbol x.
160  *
161  * The calculations in the CALCULATION column are assumed to have
162  * been performed before calling this function except for the addition of
163  * the addresses in the instructions.
164  */
165 #if defined(_KERNEL)
166 #define	lml	0		/* Needed by arglist of REL_ERR_* macros */
167 int
168 do_reloc_krtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
169     const char *file)
170 #elif defined(DO_RELOC_LIBLD)
171 int
172 do_reloc_ld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
173     const char *file, int bswap, void *lml)
174 #else
175 int
176 do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
177     const char *file, void *lml)
178 #endif
179 {
180 	const Rel_entry	*rep;
181 
182 #if defined(DO_RELOC_LIBLD)
183 	/*
184 	 * We do not support building the X86 linker as a cross linker
185 	 * at this time.
186 	 */
187 	if (bswap) {
188 		REL_ERR_NOSWAP(lml, file, sym, rtype);
189 		return (0);
190 	}
191 #endif
192 
193 	rep = &reloc_table[rtype];
194 
195 	switch (rep->re_fsize) {
196 	case 1:
197 		/* LINTED */
198 		*((uchar_t *)off) += (uchar_t)(*value);
199 		break;
200 	case 2:
201 		/* LINTED */
202 		*((Half *)off) += (Half)(*value);
203 		break;
204 	case 4:
205 		/* LINTED */
206 		*((Xword *)off) += *value;
207 		break;
208 	default:
209 		/*
210 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
211 		 */
212 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
213 		return (0);
214 	}
215 	return (1);
216 }
217