xref: /titanic_52/usr/src/uts/intel/ia32/krtld/doreloc.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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_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 };
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_386_NONE		 0	none		none
96  * R_386_32		 1	word32		S + A
97  * R_386_PC32		 2	word32		S + A - P
98  * R_386_GOT32		 3	word32		G + A - P
99  * R_386_PLT32		 4	word32		L + A - P
100  * R_386_COPY		 5	none		none
101  * R_386_GLOB_DAT	 6	word32		S
102  * R_386_JMP_SLOT	 7	word32		S
103  * R_386_RELATIVE	 8	word32		B + A
104  * R_386_GOTOFF		 9	word32		S + A - GOT
105  * R_386_GOTPC		10	word32		GOT + A - P
106  * R_386_32PLT		11	word32		L + A
107  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
108  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
109  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
110  * R_386_TLS_IE		15	word32		@indntpoff(S)
111  * R_386_TLS_GD		18	word32		@tlsgd(S)
112  * R_386_TLS_LDM	19	word32		@tlsldm(S)
113  * R_386_16		20	word16		S + A
114  * R_386_PC16		21	word16		S + A - P
115  * R_386_8		22	word8		S + A
116  * R_386_PC8		23	word8		S + A - P
117  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
118  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
119  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
120  *
121  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
122  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
123  * and is in the process of being registered with intel ABI (1/13/94).
124  *
125  * Relocations R_386_TLS_* are added to support Thread-Local storage
126  *	as recorded in PSARC/2001/509
127  *
128  * Relocation calculations:
129  *
130  * CALCULATION uses the following notation:
131  *	A	the addend used
132  *	B	the base address of the shared object in memory
133  *	G	the offset into the global offset table
134  *	GOT	the address of teh global offset table
135  *	L	the procedure linkage entry
136  *	P	the place of the storage unit being relocated
137  *	S	the value of the symbol
138  *
139  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
140  *	   a Tls_index structure (for passing to __tls_get_addr()). The
141  *	   instructions referencing this entry will be bound to the first
142  *	   of the two GOT entries.
143  *
144  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
145  *	   a Tls_index structure (for passing to __tls_get_addr()). The
146  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
147  *	   ti_module will be filled in at run-time. The call to
148  *	   __tls_get_addr() will return the starting offset of the dynamic
149  *	   TLS block.
150  *
151  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
152  *
153  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
154  *
155  *	@dtpmod(x): calculate the module id of the object containing symbol x.
156  *
157  * The calculations in the CALCULATION column are assumed to have
158  * been performed before calling this function except for the addition of
159  * the addresses in the instructions.
160  */
161 /* ARGSUSED5 */
162 int
163 do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
164     const char *file, void *lml)
165 {
166 	const Rel_entry	*rep;
167 
168 	rep = &reloc_table[rtype];
169 
170 	switch (rep->re_fsize) {
171 	case 1:
172 		/* LINTED */
173 		*((uchar_t *)off) += (uchar_t)(*value);
174 		break;
175 	case 2:
176 		/* LINTED */
177 		*((Half *)off) += (Half)(*value);
178 		break;
179 	case 4:
180 		/* LINTED */
181 		*((Xword *)off) += *value;
182 		break;
183 	default:
184 		/*
185 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
186 		 */
187 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
188 		return (0);
189 	}
190 	return (1);
191 }
192