xref: /titanic_44/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 587032cf0967234b39ccb50adca936a367841063)
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_TLSINS | FLG_RE_PLTREL |
61 				FLG_RE_PCREL | FLG_RE_TLSGD, 4},
62 /* R_386_TLS_LDM_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
63 				FLG_RE_PCREL | FLG_RE_TLSLD, 4},
64 /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
65 /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
66 /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
67 /* R_386_TLS_LE */	{FLG_RE_TLSINS | FLG_RE_TLSLE, 4},
68 /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSGD, 4},
69 /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD | FLG_RE_TLSINS, 4},
70 /* R_386_16 */		{FLG_RE_NOTREL, 2},
71 /* R_386_PC16 */	{FLG_RE_PCREL, 2},
72 /* R_386_8 */		{FLG_RE_NOTREL, 1},
73 /* R_386_PC8 */		{FLG_RE_PCREL, 1},
74 /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
75 /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
76 /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
77 /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
78 /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
79 /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
80 /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
81 /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
82 /* R_386_TLS_LDO_32 */	{FLG_RE_TLSINS | FLG_RE_TLSLD, 4},
83 /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
84 /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
85 /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
86 /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
87 /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0}
88 };
89 
90 /*
91  * Write a single relocated value to its reference location.
92  * We assume we wish to add the relocation amount, value, to the
93  * value of the address already present at the offset.
94  *
95  * NAME			VALUE	FIELD		CALCULATION
96  *
97  * R_386_NONE		 0	none		none
98  * R_386_32		 1	word32		S + A
99  * R_386_PC32		 2	word32		S + A - P
100  * R_386_GOT32		 3	word32		G + A - P
101  * R_386_PLT32		 4	word32		L + A - P
102  * R_386_COPY		 5	none		none
103  * R_386_GLOB_DAT	 6	word32		S
104  * R_386_JMP_SLOT	 7	word32		S
105  * R_386_RELATIVE	 8	word32		B + A
106  * R_386_GOTOFF		 9	word32		S + A - GOT
107  * R_386_GOTPC		10	word32		GOT + A - P
108  * R_386_32PLT		11	word32		L + A
109  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
110  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
111  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
112  * R_386_TLS_IE		15	word32		@indntpoff(S)
113  * R_386_TLS_GD		18	word32		@tlsgd(S)
114  * R_386_TLS_LDM	19	word32		@tlsldm(S)
115  * R_386_16		20	word16		S + A
116  * R_386_PC16		21	word16		S + A - P
117  * R_386_8		22	word8		S + A
118  * R_386_PC8		23	word8		S + A - P
119  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
120  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
121  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
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  *
141  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
142  *	   a Tls_index structure (for passing to __tls_get_addr()). The
143  *	   instructions referencing this entry will be bound to the first
144  *	   of the two GOT entries.
145  *
146  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
147  *	   a Tls_index structure (for passing to __tls_get_addr()). The
148  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
149  *	   ti_module will be filled in at run-time. The call to
150  *	   __tls_get_addr() will return the starting offset of the dynamic
151  *	   TLS block.
152  *
153  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
154  *
155  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
156  *
157  *	@dtpmod(x): calculate the module id of the object containing symbol x.
158  *
159  * The calculations in the CALCULATION column are assumed to have
160  * been performed before calling this function except for the addition of
161  * the addresses in the instructions.
162  */
163 /* ARGSUSED5 */
164 int
165 do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
166     const char *file, void *lml)
167 {
168 	const Rel_entry	*rep;
169 
170 	rep = &reloc_table[rtype];
171 
172 	switch (rep->re_fsize) {
173 	case 1:
174 		/* LINTED */
175 		*((uchar_t *)off) += (uchar_t)(*value);
176 		break;
177 	case 2:
178 		/* LINTED */
179 		*((Half *)off) += (Half)(*value);
180 		break;
181 	case 4:
182 		/* LINTED */
183 		*((Xword *)off) += *value;
184 		break;
185 	default:
186 		/*
187 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
188 		 */
189 		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
190 		return (0);
191 	}
192 	return (1);
193 }
194