xref: /titanic_52/usr/src/uts/intel/ia32/krtld/doreloc.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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  * This table represents the current relocations that do_reloc()
43  * is able to process.  The relocations below that are marked
44  * 'SPECIAL' in the comments are relocations that take special
45  * 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 */	{0, 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 */	{0, 0},					/* SPECIAL */
54 /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
55 /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},
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_NOTREL | 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_NOTSUP, 0},
71 /* R_386_PC16 */	{FLG_RE_NOTSUP, 0},
72 /* R_386_8 */		{FLG_RE_NOTSUP, 0},
73 /* R_386_PC8 */		{FLG_RE_NOTSUP, 0},
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 | FLG_RE_NOTREL, 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 /*
92  * Write a single relocated value to its reference location.
93  * We assume we wish to add the relocatoin amount, value, to the
94  * value of the address already present at the offset.
95  *
96  * NAME			VALUE	FIELD		CALCULATION
97  *
98  * R_386_NONE		 0	none		none
99  * R_386_32		 1	word32		S + A
100  * R_386_PC32		 2	word32		S + A - P
101  * R_386_GOT32		 3	word32		G + A - P
102  * R_386_PLT32		 4	word32		L + A - P
103  * R_386_COPY		 5	none		none
104  * R_386_GLOB_DAT	 6	word32		S
105  * R_386_JMP_SLOT	 7	word32		S
106  * R_386_RELATIVE	 8	word32		B + A
107  * R_386_GOTOFF		 9	word32		S + A - GOT
108  * R_386_GOTPC		10	word32		GOT + A - P
109  * R_386_32PLT		11	word32		L + A
110  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
111  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
112  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
113  * R_386_TLS_IE		15	word32		@indntpoff(S)
114  * R_386_TLS_GD		18	word32		@tlsgd(S)
115  * R_386_TLS_LDM	19	word32		@tlsldm(S)
116  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
117  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
118  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
119  *
120  * Relocatoins 0-10 are from Figure 4-4: Relocations Types from the
121  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
122  * and is in the process of being registered with intel ABI (1/13/94).
123  *
124  * Relocations R_386_TLS_* are added to support Thread-Local storage
125  *	as recorded in PSARC/2001/509
126  *
127  * Relocation calculations:
128  *
129  * CALCULATION uses the following notation:
130  *	A	the addend used
131  *	B	the base address of the shared object in memory
132  *	G	the offset into the global offset table
133  *	GOT	the address of teh global offset table
134  *	L	the procedure linkage entry
135  *	P	the place of the storage unit being relocated
136  *	S	the value of the symbol
137  *
138  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
139  *	   a Tls_index structure (for passing to __tls_get_addr()). The
140  *	   instructions referencing this entry will be bound to the first
141  *	   of the two GOT entries.
142  *
143  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
144  *	   a Tls_index structure (for passing to __tls_get_addr()). The
145  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
146  *	   ti_module will be filled in at run-time. The call to
147  *	   __tls_get_addr() will return the starting offset of the dynamic
148  *	   TLS block.
149  *
150  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
151  *
152  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
153  *
154  *	@dtpmod(x): calculate the module id of the object containing symbol x.
155  *
156  * The calculations in the CALCULATION column are assumed to have
157  * been performed before calling this function except for the addition of
158  * the addresses in the instructions.
159  */
160 int
161 do_reloc(unsigned char rtype, unsigned char *off, Xword *value,
162 	const char *sym, const char *file)
163 {
164 	const Rel_entry *rep;
165 
166 	rep = &reloc_table[rtype];
167 	/*
168 	 * Currenty 386 *only* relocates against full words
169 	 */
170 	if (rep->re_fsize != 4) {
171 		eprintf(ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ),
172 		    conv_reloc_386_type_str(rtype), file,
173 		    (sym ? sym : MSG_INTL(MSG_STR_UNKNOWN)),
174 		    EC_WORD(rep->re_fsize));
175 		return (0);
176 	}
177 	/* LINTED */
178 	*((Xword *)off) += *value;
179 	return (1);
180 }
181