xref: /titanic_44/usr/src/uts/intel/ia32/krtld/doreloc.c (revision ee519a1f9541a20bb76ef306dfc8e5616f8a5e26)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #if	defined(_KERNEL)
31 #include	<sys/types.h>
32 #include	"reloc.h"
33 #else
34 #include	<stdio.h>
35 #include	"sgs.h"
36 #include	"machdep.h"
37 #include	"libld.h"
38 #include	"reloc.h"
39 #include	"conv.h"
40 #include	"msg.h"
41 #endif
42 
43 /*
44  * This table represents the current relocations that do_reloc() is able to
45  * process.  The relocations below that are marked SPECIAL are relocations that
46  * take special processing and shouldn't actually ever be passed to do_reloc().
47  */
48 const Rel_entry	reloc_table[R_386_NUM] = {
49 /* R_386_NONE */	{FLG_RE_NOTREL, 0},
50 /* R_386_32 */		{FLG_RE_NOTREL, 4},
51 /* R_386_PC32 */	{FLG_RE_PCREL, 4},
52 /* R_386_GOT32 */	{FLG_RE_GOTADD, 4},
53 /* R_386_PLT32 */	{FLG_RE_PLTREL | FLG_RE_PCREL, 4},
54 /* R_386_COPY */	{FLG_RE_NOTREL, 0},			/* SPECIAL */
55 /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
56 /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},			/* SPECIAL */
57 /* R_386_RELATIVE */	{FLG_RE_NOTREL, 4},
58 /* R_386_GOTOFF */	{FLG_RE_GOTREL, 4},
59 /* R_386_GOTPC */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
60 /* R_386_32PLT */	{FLG_RE_PLTREL, 4},
61 /* R_386_TLS_GD_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
62 				FLG_RE_PCREL | FLG_RE_TLSGD, 4},
63 /* R_386_TLS_LDM_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
64 				FLG_RE_PCREL | FLG_RE_TLSLD, 4},
65 /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
66 /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
67 /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
68 /* R_386_TLS_LE */	{FLG_RE_TLSINS | FLG_RE_TLSLE, 4},
69 /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSGD, 4},
70 /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD | FLG_RE_TLSINS, 4},
71 /* R_386_16 */		{FLG_RE_NOTREL, 2},
72 /* R_386_PC16 */	{FLG_RE_PCREL, 2},
73 /* R_386_8 */		{FLG_RE_NOTREL, 1},
74 /* R_386_PC8 */		{FLG_RE_PCREL, 1},
75 /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
76 /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
77 /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
78 /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
79 /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
80 /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
81 /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
82 /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
83 /* R_386_TLS_LDO_32 */	{FLG_RE_TLSINS | FLG_RE_TLSLD, 4},
84 /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
85 /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
86 /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
87 /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
88 /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0}
89 };
90 
91 /*
92  * Write a single relocated value to its reference location.
93  * We assume we wish to add the relocation 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_16		20	word16		S + A
117  * R_386_PC16		21	word16		S + A - P
118  * R_386_8		22	word8		S + A
119  * R_386_PC8		23	word8		S + A - P
120  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
121  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
122  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
123  *
124  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
125  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
126  * and is in the process of being registered with intel ABI (1/13/94).
127  *
128  * Relocations R_386_TLS_* are added to support Thread-Local storage
129  *	as recorded in PSARC/2001/509
130  *
131  * Relocation calculations:
132  *
133  * CALCULATION uses the following notation:
134  *	A	the addend used
135  *	B	the base address of the shared object in memory
136  *	G	the offset into the global offset table
137  *	GOT	the address of teh global offset table
138  *	L	the procedure linkage entry
139  *	P	the place of the storage unit being relocated
140  *	S	the value of the symbol
141  *
142  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
143  *	   a Tls_index structure (for passing to __tls_get_addr()). The
144  *	   instructions referencing this entry will be bound to the first
145  *	   of the two GOT entries.
146  *
147  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
148  *	   a Tls_index structure (for passing to __tls_get_addr()). The
149  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
150  *	   ti_module will be filled in at run-time. The call to
151  *	   __tls_get_addr() will return the starting offset of the dynamic
152  *	   TLS block.
153  *
154  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
155  *
156  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
157  *
158  *	@dtpmod(x): calculate the module id of the object containing symbol x.
159  *
160  * The calculations in the CALCULATION column are assumed to have
161  * been performed before calling this function except for the addition of
162  * the addresses in the instructions.
163  */
164 int
165 do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
166     const char *file)
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(file, sym, rtype, rep->re_fsize);
190 		return (0);
191 	}
192 	return (1);
193 }
194