xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #if	defined(_KERNEL)
29*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include	"reloc.h"
31*7c478bd9Sstevel@tonic-gate #else
32*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
33*7c478bd9Sstevel@tonic-gate #include	"sgs.h"
34*7c478bd9Sstevel@tonic-gate #include	"machdep.h"
35*7c478bd9Sstevel@tonic-gate #include	"libld.h"
36*7c478bd9Sstevel@tonic-gate #include	"reloc.h"
37*7c478bd9Sstevel@tonic-gate #include	"conv.h"
38*7c478bd9Sstevel@tonic-gate #include	"msg.h"
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * This table represents the current relocations that do_reloc()
43*7c478bd9Sstevel@tonic-gate  * is able to process.  The relocations below that are marked
44*7c478bd9Sstevel@tonic-gate  * 'SPECIAL' in the comments are relocations that take special
45*7c478bd9Sstevel@tonic-gate  * processing and shouldn't actually ever be passed to do_reloc().
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
48*7c478bd9Sstevel@tonic-gate /* R_386_NONE */	{0, 0},
49*7c478bd9Sstevel@tonic-gate /* R_386_32 */		{FLG_RE_NOTREL, 4},
50*7c478bd9Sstevel@tonic-gate /* R_386_PC32 */	{FLG_RE_PCREL, 4},
51*7c478bd9Sstevel@tonic-gate /* R_386_GOT32 */	{FLG_RE_GOTADD, 4},
52*7c478bd9Sstevel@tonic-gate /* R_386_PLT32 */	{FLG_RE_PLTREL | FLG_RE_PCREL, 4},
53*7c478bd9Sstevel@tonic-gate /* R_386_COPY */	{0, 0},					/* SPECIAL */
54*7c478bd9Sstevel@tonic-gate /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
55*7c478bd9Sstevel@tonic-gate /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},
56*7c478bd9Sstevel@tonic-gate /* R_386_RELATIVE */	{FLG_RE_NOTREL, 4},
57*7c478bd9Sstevel@tonic-gate /* R_386_GOTOFF */	{FLG_RE_GOTREL, 4},
58*7c478bd9Sstevel@tonic-gate /* R_386_GOTPC */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
59*7c478bd9Sstevel@tonic-gate /* R_386_32PLT */	{FLG_RE_PLTREL, 4},
60*7c478bd9Sstevel@tonic-gate /* R_386_TLS_GD_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
61*7c478bd9Sstevel@tonic-gate 				FLG_RE_PCREL | FLG_RE_TLSGD, 4},
62*7c478bd9Sstevel@tonic-gate /* R_386_TLS_LDM_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
63*7c478bd9Sstevel@tonic-gate 				FLG_RE_PCREL | FLG_RE_TLSLD, 4},
64*7c478bd9Sstevel@tonic-gate /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
65*7c478bd9Sstevel@tonic-gate /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
66*7c478bd9Sstevel@tonic-gate /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
67*7c478bd9Sstevel@tonic-gate /* R_386_TLS_LE */	{FLG_RE_TLSINS | FLG_RE_NOTREL | FLG_RE_TLSLE, 4},
68*7c478bd9Sstevel@tonic-gate /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSGD, 4},
69*7c478bd9Sstevel@tonic-gate /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD | FLG_RE_TLSINS, 4},
70*7c478bd9Sstevel@tonic-gate /* R_386_16 */		{FLG_RE_NOTSUP, 0},
71*7c478bd9Sstevel@tonic-gate /* R_386_PC16 */	{FLG_RE_NOTSUP, 0},
72*7c478bd9Sstevel@tonic-gate /* R_386_8 */		{FLG_RE_NOTSUP, 0},
73*7c478bd9Sstevel@tonic-gate /* R_386_PC8 */		{FLG_RE_NOTSUP, 0},
74*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
75*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
76*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
77*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
78*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
79*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
80*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
81*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
82*7c478bd9Sstevel@tonic-gate /* R_386_TLS_LDO_32 */	{FLG_RE_TLSINS | FLG_RE_TLSLD | FLG_RE_NOTREL, 4},
83*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
84*7c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
85*7c478bd9Sstevel@tonic-gate /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
86*7c478bd9Sstevel@tonic-gate /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
87*7c478bd9Sstevel@tonic-gate /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0}
88*7c478bd9Sstevel@tonic-gate };
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
93*7c478bd9Sstevel@tonic-gate  * We assume we wish to add the relocatoin amount, value, to the
94*7c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
99*7c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
100*7c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
101*7c478bd9Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
102*7c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
103*7c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
104*7c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
105*7c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
106*7c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
107*7c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
108*7c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
109*7c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
110*7c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
111*7c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
112*7c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
113*7c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
114*7c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
115*7c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
116*7c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
117*7c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
118*7c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
119*7c478bd9Sstevel@tonic-gate  *
120*7c478bd9Sstevel@tonic-gate  * Relocatoins 0-10 are from Figure 4-4: Relocations Types from the
121*7c478bd9Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
122*7c478bd9Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
123*7c478bd9Sstevel@tonic-gate  *
124*7c478bd9Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
125*7c478bd9Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
126*7c478bd9Sstevel@tonic-gate  *
127*7c478bd9Sstevel@tonic-gate  * Relocation calculations:
128*7c478bd9Sstevel@tonic-gate  *
129*7c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
130*7c478bd9Sstevel@tonic-gate  *	A	the addend used
131*7c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
132*7c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
133*7c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
134*7c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
135*7c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
136*7c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
137*7c478bd9Sstevel@tonic-gate  *
138*7c478bd9Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
139*7c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
140*7c478bd9Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
141*7c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
144*7c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
145*7c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
146*7c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
147*7c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
148*7c478bd9Sstevel@tonic-gate  *	   TLS block.
149*7c478bd9Sstevel@tonic-gate  *
150*7c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
151*7c478bd9Sstevel@tonic-gate  *
152*7c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
153*7c478bd9Sstevel@tonic-gate  *
154*7c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
155*7c478bd9Sstevel@tonic-gate  *
156*7c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
157*7c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
158*7c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
159*7c478bd9Sstevel@tonic-gate  */
160*7c478bd9Sstevel@tonic-gate int
161*7c478bd9Sstevel@tonic-gate do_reloc(unsigned char rtype, unsigned char *off, Xword *value,
162*7c478bd9Sstevel@tonic-gate 	const char *sym, const char *file)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	const Rel_entry *rep;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	rep = &reloc_table[rtype];
167*7c478bd9Sstevel@tonic-gate 	/*
168*7c478bd9Sstevel@tonic-gate 	 * Currenty 386 *only* relocates against full words
169*7c478bd9Sstevel@tonic-gate 	 */
170*7c478bd9Sstevel@tonic-gate 	if (rep->re_fsize != 4) {
171*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ),
172*7c478bd9Sstevel@tonic-gate 		    conv_reloc_386_type_str(rtype), file,
173*7c478bd9Sstevel@tonic-gate 		    (sym ? sym : MSG_INTL(MSG_STR_UNKNOWN)),
174*7c478bd9Sstevel@tonic-gate 		    EC_WORD(rep->re_fsize));
175*7c478bd9Sstevel@tonic-gate 		return (0);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 	/* LINTED */
178*7c478bd9Sstevel@tonic-gate 	*((Xword *)off) += *value;
179*7c478bd9Sstevel@tonic-gate 	return (1);
180*7c478bd9Sstevel@tonic-gate }
181