xref: /titanic_41/usr/src/uts/common/krtld/reloc.h (revision 4bac220845f606f60663ed6f3a2b88caa00ae87e)
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 #ifndef	_RELOC_DOT_H
28 #define	_RELOC_DOT_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #if defined(_KERNEL)
33 #include <sys/machelf.h>
34 #include <sys/bootconf.h>
35 #include <sys/kobj.h>
36 #include <sys/kobj_impl.h>
37 #else
38 #include <machdep.h>
39 #include <rtld.h>
40 #include <conv.h>
41 #endif /* _KERNEL */
42 
43 #include <relmach.h>
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 /*
50  * Global include file for relocation common code.
51  *
52  * Flags for reloc_entry->re_flags
53  */
54 #define	FLG_RE_NOTREL		0x00000000
55 #define	FLG_RE_GOTADD		0x00000001	/* create a GOT entry */
56 #define	FLG_RE_GOTREL		0x00000002	/* GOT based */
57 #define	FLG_RE_GOTPC		0x00000004	/* GOT - P */
58 #define	FLG_RE_GOTOPINS		0x00000008	/* GOTOP instruction */
59 #define	FLG_RE_PCREL		0x00000010
60 #define	FLG_RE_PLTREL		0x00000020
61 #define	FLG_RE_VERIFY		0x00000040	/* verify value fits */
62 #define	FLG_RE_UNALIGN		0x00000080	/* offset is not aligned */
63 #define	FLG_RE_WDISP16		0x00000100	/* funky sparc DISP16 rel */
64 #define	FLG_RE_SIGN		0x00000200	/* value is signed */
65 #define	FLG_RE_ADDRELATIVE	0x00000400	/* RELATIVE relocation */
66 						/*	required for non- */
67 						/*	fixed objects */
68 #define	FLG_RE_EXTOFFSET	0x00000800	/* extra offset required */
69 #define	FLG_RE_REGISTER		0x00001000	/* relocation initializes */
70 						/*    a REGISTER by OLO10 */
71 #define	FLG_RE_NOTSUP		0x00010000	/* relocation not supported */
72 
73 #define	FLG_RE_SEGREL		0x00040000	/* segment relative */
74 #define	FLG_RE_SECREL		0x00080000	/* section relative */
75 
76 #define	FLG_RE_TLSGD		0x00200000	/* TLS GD relocation */
77 #define	FLG_RE_TLSLD		0x00400000	/* TLS LD relocation */
78 #define	FLG_RE_TLSIE		0x00800000	/* TLS IE relocation */
79 #define	FLG_RE_TLSLE		0x01000000	/* TLS LE relocation */
80 #define	FLG_RE_LOCLBND		0x02000000	/* relocation must bind */
81 						/*    locally */
82 
83 /*
84  * In user land, redefine the relocation table and relocation engine to be
85  * class specific if necessary.  This allows both engines to reside in the
86  * intel/amd version of libld.
87  */
88 #if	!defined(_KERNEL)
89 #if	defined(_ELF64)
90 #define	do_reloc		do64_reloc
91 #define	reloc_table		reloc64_table
92 #else
93 #define	do_reloc		do32_reloc
94 #define	reloc_table		reloc32_table
95 #endif
96 #endif
97 
98 /*
99  * Relocation table and macros for testing relocation table flags.
100  */
101 extern	const Rel_entry		reloc_table[];
102 
103 #define	IS_PLT(X)		((reloc_table[(X)].re_flags & \
104 					FLG_RE_PLTREL) != 0)
105 #define	IS_GOT_RELATIVE(X)	((reloc_table[(X)].re_flags & \
106 					FLG_RE_GOTADD) != 0)
107 #define	IS_GOT_PC(X)		((reloc_table[(X)].re_flags & \
108 					FLG_RE_GOTPC) != 0)
109 #define	IS_GOTPCREL(X)		((reloc_table[(X)].re_flags & \
110 					(FLG_RE_GOTPC | FLG_RE_GOTADD)) == \
111 					(FLG_RE_GOTPC | FLG_RE_GOTADD))
112 #define	IS_GOT_BASED(X)		((reloc_table[(X)].re_flags & \
113 					FLG_RE_GOTREL) != 0)
114 #define	IS_GOT_INS(X)		((reloc_table[(X)].re_flags & \
115 					FLG_RE_GOTOPINS) != 0)
116 #define	IS_GOT_REQUIRED(X)	((reloc_table[(X)].re_flags & \
117 					(FLG_RE_GOTADD | FLG_RE_GOTREL | \
118 					FLG_RE_GOTPC | FLG_RE_GOTOPINS)) != 0)
119 #define	IS_PC_RELATIVE(X)	((reloc_table[(X)].re_flags & \
120 					FLG_RE_PCREL) != 0)
121 #define	IS_ADD_RELATIVE(X)	((reloc_table[(X)].re_flags & \
122 					FLG_RE_ADDRELATIVE) != 0)
123 #define	IS_REGISTER(X)		((reloc_table[(X)].re_flags & \
124 					FLG_RE_REGISTER) != 0)
125 #define	IS_FORMOFF(X)		((reloc_table[(X)].re_flags & \
126 					FLG_RE_FRMOFF) != 0)
127 #define	IS_NOTSUP(X)		((reloc_table[(X)].re_flags & \
128 					FLG_RE_NOTSUP) != 0)
129 #define	IS_SEG_RELATIVE(X)	((reloc_table[(X)].re_flags & \
130 					FLG_RE_SEGREL) != 0)
131 #define	IS_EXTOFFSET(X)		((reloc_table[(X)].re_flags & \
132 					FLG_RE_EXTOFFSET) != 0)
133 #define	IS_SEC_RELATIVE(X)	((reloc_table[(X)].re_flags & \
134 					FLG_RE_SECREL) != 0)
135 #define	IS_TLS_INS(X)		((reloc_table[(X)].re_flags & \
136 					(FLG_RE_TLSGD | FLG_RE_TLSLD | \
137 					FLG_RE_TLSIE | FLG_RE_TLSLE)) != 0)
138 #define	IS_TLS_GD(X)		((reloc_table[(X)].re_flags & \
139 					FLG_RE_TLSGD) != 0)
140 #define	IS_TLS_LD(X)		((reloc_table[(X)].re_flags & \
141 					FLG_RE_TLSLD) != 0)
142 #define	IS_TLS_IE(X)		((reloc_table[(X)].re_flags & \
143 					FLG_RE_TLSIE) != 0)
144 #define	IS_TLS_LE(X)		((reloc_table[(X)].re_flags & \
145 					FLG_RE_TLSLE) != 0)
146 #define	IS_LOCALBND(X)		((reloc_table[(X)].re_flags & \
147 					FLG_RE_LOCLBND) != 0)
148 
149 /*
150  * Relocation engine.
151  */
152 extern	int	do_reloc(uchar_t, uchar_t *, Xword *, const char *,
153 		    const char *, void *);
154 
155 #if defined(_KERNEL)
156 /*
157  * These are macro's that are only needed for krtld.  Many of these are already
158  * defined in the sgs/include files referenced by ld and rtld
159  */
160 #define	S_MASK(n)	((1l << (n)) - 1l)
161 #define	S_INRANGE(v, n)	(((-(1l << (n)) - 1l) < (v)) && ((v) < (1l << (n))))
162 
163 /*
164  * Message strings used by doreloc().
165  */
166 #define	MSG_STR_UNKNOWN		"(unknown)"
167 
168 #define	MSG_REL_PREGEN		"relocation error: %s: "
169 #define	MSG_REL_PREFIL		"relocation error: file %s: "
170 #define	MSG_REL_FILE		"file %s: "
171 #define	MSG_REL_SYM		"symbol %s: "
172 #define	MSG_REL_VALUE		"value 0x%llx "
173 #define	MSG_REL_LOSEBITS	"loses %d bits at "
174 
175 #define	MSG_REL_UNIMPL		"unimplemented relocation type: %d"
176 #define	MSG_REL_UNSUPSZ		"offset size (%d bytes) is not supported"
177 #define	MSG_REL_NONALIGN	"offset 0x%llx is non-aligned"
178 #define	MSG_REL_UNNOBITS	"unsupported number of bits: %d"
179 #define	MSG_REL_OFFSET		"offset 0x%llx"
180 #define	MSG_REL_NOFIT		"value 0x%llx does not fit"
181 
182 /*
183  * Provide a macro to select the appropriate conversion routine for this
184  * architecture.
185  */
186 #if defined(__amd64)
187 
188 extern const char	*conv_reloc_amd64_type(Word);
189 #define	CONV_RELOC_TYPE	conv_reloc_amd64_type
190 
191 #elif defined(__i386)
192 
193 extern const char	*conv_reloc_386_type(Word);
194 #define	CONV_RELOC_TYPE	conv_reloc_386_type
195 
196 #elif defined(__sparc)
197 
198 extern const char	*conv_reloc_SPARC_type(Word);
199 #define	CONV_RELOC_TYPE	conv_reloc_SPARC_type
200 
201 #else
202 #error platform not defined!
203 #endif
204 
205 /*
206  * Note:  dlerror() only keeps track of a single error string, and therefore
207  * must have errors reported through a single eprintf() call.  The kernel's
208  * _kobj_printf is somewhat more limited, and must receive messages with only
209  * one argument to the format string.  The following macros account for these
210  * differences, as krtld and rtld share do_reloc().
211  */
212 #define	REL_ERR_UNIMPL(lml, file, sym, rtype) \
213 	_kobj_printf(ops, MSG_REL_PREFIL, (file)); \
214 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
215 	_kobj_printf(ops, MSG_REL_UNIMPL, (int)(rtype))
216 
217 #define	REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
218 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
219 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
220 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
221 	_kobj_printf(ops, MSG_REL_UNSUPSZ, (int)(size))
222 
223 #define	REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
224 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
225 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
226 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
227 	_kobj_printf(ops, MSG_REL_NONALIGN, EC_OFF((off)))
228 
229 #define	REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
230 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
231 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
232 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
233 	_kobj_printf(ops, MSG_REL_UNNOBITS, (nbits))
234 
235 #define	REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
236 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
237 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
238 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
239 	_kobj_printf(ops, MSG_REL_VALUE, EC_XWORD((uvalue))); \
240 	_kobj_printf(ops, MSG_REL_LOSEBITS, (int)(nbits)); \
241 	_kobj_printf(ops, MSG_REL_OFFSET, EC_NATPTR((off)))
242 
243 #define	REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
244 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
245 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
246 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
247 	_kobj_printf(ops, MSG_REL_NOFIT, EC_XWORD((uvalue)))
248 
249 
250 #else	/* !_KERNEL */
251 
252 extern	const char *demangle(const char *);
253 
254 #define	REL_ERR_UNIMPL(lml, file, sym, rtype) \
255 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), (file), \
256 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(rtype)))
257 
258 #define	REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
259 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ), \
260 	    conv_reloc_type(M_MACH, (rtype), 0), (file), \
261 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(size)))
262 
263 #define	REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
264 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN), \
265 	    conv_reloc_type(M_MACH, (rtype), 0), (file), \
266 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), EC_OFF((off))))
267 
268 #define	REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
269 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNNOBITS), \
270 	    conv_reloc_type(M_MACH, (rtype), 0), (file), \
271 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (nbits)))
272 
273 #define	REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
274 	(eprintf(lml, ERR_FATAL,  MSG_INTL(MSG_REL_LOSEBITS), \
275 	    conv_reloc_type(M_MACH, (rtype), 0), (file), \
276 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
277 	    EC_XWORD((uvalue)), (nbits), EC_NATPTR((off))))
278 
279 #define	REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
280 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOFIT), \
281 	    conv_reloc_type(M_MACH, (rtype), 0), (file), \
282 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
283 	    EC_XWORD((uvalue))))
284 
285 #endif	/* _KERNEL */
286 
287 #ifdef	__cplusplus
288 }
289 #endif
290 
291 #endif /* _RELOC_DOT_H */
292