xref: /titanic_51/usr/src/uts/common/krtld/reloc.h (revision a8ae99647dd4d540346030718e08a083b0fbdf42)
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 #ifndef	_RELOC_DOT_H
29 #define	_RELOC_DOT_H
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #if defined(_KERNEL)
34 #include <sys/machelf.h>
35 #include <sys/bootconf.h>
36 #include <sys/kobj.h>
37 #include <sys/kobj_impl.h>
38 #else
39 #include <machdep.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-fixed */
67 						/* 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 #define	FLG_RE_TLSINS		0x00100000	/* TLS instructino rel */
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  * Macros for testing relocation table flags
85  */
86 extern	const Rel_entry		reloc_table[];
87 
88 #define	IS_PLT(X)		((reloc_table[(X)].re_flags & \
89 					FLG_RE_PLTREL) != 0)
90 #define	IS_GOT_RELATIVE(X)	((reloc_table[(X)].re_flags & \
91 					FLG_RE_GOTADD) != 0)
92 #define	IS_GOT_PC(X)		((reloc_table[(X)].re_flags & \
93 					FLG_RE_GOTPC) != 0)
94 #define	IS_GOTPCREL(X)		((reloc_table[(X)].re_flags & \
95 					(FLG_RE_GOTPC | FLG_RE_GOTADD)) == \
96 					(FLG_RE_GOTPC | FLG_RE_GOTADD))
97 #define	IS_GOT_BASED(X)		((reloc_table[(X)].re_flags & \
98 					FLG_RE_GOTREL) != 0)
99 #define	IS_GOT_INS(X)		((reloc_table[(X)].re_flags &\
100 					FLG_RE_GOTOPINS) != 0)
101 #define	IS_PC_RELATIVE(X)	((reloc_table[(X)].re_flags & \
102 					FLG_RE_PCREL) != 0)
103 #define	IS_ADD_RELATIVE(X)	((reloc_table[(X)].re_flags & \
104 					FLG_RE_ADDRELATIVE) != 0)
105 #define	IS_REGISTER(X)		((reloc_table[(X)].re_flags & \
106 					FLG_RE_REGISTER) != 0)
107 #define	IS_FORMOFF(X)		((reloc_table[(X)].re_flags &\
108 					FLG_RE_FRMOFF) != 0)
109 #define	IS_NOTSUP(X)		((reloc_table[(X)].re_flags &\
110 					FLG_RE_NOTSUP) != 0)
111 #define	IS_SEG_RELATIVE(X)	((reloc_table[(X)].re_flags &\
112 					FLG_RE_SEGREL) != 0)
113 #define	IS_EXTOFFSET(X)		((reloc_table[(X)].re_flags &\
114 					FLG_RE_EXTOFFSET) != 0)
115 #define	IS_SEC_RELATIVE(X)	((reloc_table[(X)].re_flags &\
116 					FLG_RE_SECREL) != 0)
117 #define	IS_TLS_INS(X)		((reloc_table[(X)].re_flags &\
118 					FLG_RE_TLSINS) != 0)
119 #define	IS_TLS_GD(X)		((reloc_table[(X)].re_flags &\
120 					FLG_RE_TLSGD) != 0)
121 #define	IS_TLS_LD(X)		((reloc_table[(X)].re_flags &\
122 					FLG_RE_TLSLD) != 0)
123 #define	IS_TLS_IE(X)		((reloc_table[(X)].re_flags &\
124 					FLG_RE_TLSIE) != 0)
125 #define	IS_TLS_LE(X)		((reloc_table[(X)].re_flags &\
126 					FLG_RE_TLSLE) != 0)
127 #define	IS_TLS(X)		((reloc_table[(X)].re_flags &\
128 					(FLG_RE_TLSINS|FLG_RE_TLSGD| \
129 					FLG_RE_TLSLD|FLG_RE_TLSIE| \
130 					FLG_RE_TLSLE)) != 0)
131 #define	IS_LOCALBND(X)		((reloc_table[(X)].re_flags &\
132 					FLG_RE_LOCLBND) != 0)
133 
134 /*
135  * Functions.
136  */
137 extern	int	do_reloc(uchar_t, uchar_t *, Xword *, const char *,
138 		    const char *);
139 
140 /*
141  * Provide a macro to select the appropriate convension routine for this
142  * architecture.
143  */
144 #if defined(__amd64)
145 #define	CONV_RELOC_TYPE		conv_reloc_amd64_type_str
146 #elif defined(__i386)
147 #define	CONV_RELOC_TYPE		conv_reloc_386_type_str
148 #elif defined(__sparc)
149 #define	CONV_RELOC_TYPE		conv_reloc_SPARC_type_str
150 #else
151 #error platform not defined!
152 #endif
153 
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 extern const char	 *conv_reloc_386_type_str(uint_t);
183 extern const char	 *conv_reloc_amd64_type_str(uint_t);
184 extern const char	 *conv_reloc_SPARC_type_str(uint_t);
185 
186 /*
187  * Note:  Related to bug 4128755, dlerror() only keeps track of a single error
188  * string, and therefore must have errors reported through a single eprintf()
189  * call.  The kernel's _kobj_printf is somewhat more limited, and must receive
190  * messages with only one arguement to the format string.  The following macros
191  * are to straighted all this out because krtld and rtld share do_reloc().
192  */
193 #define	REL_ERR_UNIMPL(file, sym, rtype) \
194 	_kobj_printf(ops, MSG_REL_PREFIL, (file)); \
195 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
196 	_kobj_printf(ops, MSG_REL_UNIMPL, (int)(rtype))
197 
198 #define	REL_ERR_UNSUPSZ(file, sym, rtype, size) \
199 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
200 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
201 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
202 	_kobj_printf(ops, MSG_REL_UNSUPSZ, (int)(size))
203 
204 #define	REL_ERR_NONALIGN(file, sym, rtype, off) \
205 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
206 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
207 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
208 	_kobj_printf(ops, MSG_REL_NONALIGN, EC_OFF((off)))
209 
210 #define	REL_ERR_UNNOBITS(file, sym, rtype, nbits) \
211 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
212 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
213 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
214 	_kobj_printf(ops, MSG_REL_UNNOBITS, (nbits))
215 
216 #define	REL_ERR_LOSEBITS(file, sym, rtype, uvalue, nbits, off) \
217 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
218 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
219 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
220 	_kobj_printf(ops, MSG_REL_VALUE, EC_XWORD((uvalue))); \
221 	_kobj_printf(ops, MSG_REL_LOSEBITS, (int)(nbits)); \
222 	_kobj_printf(ops, MSG_REL_OFFSET, EC_ADDR((off)))
223 
224 #define	REL_ERR_NOFIT(file, sym, rtype, uvalue) \
225 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
226 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
227 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
228 	_kobj_printf(ops, MSG_REL_NOFIT, EC_XWORD((uvalue)))
229 
230 
231 #else	/* !_KERNEL */
232 
233 extern	const char *demangle(const char *);
234 
235 #define	REL_ERR_UNIMPL(file, sym, rtype) \
236 	(eprintf(ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), (file), \
237 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(rtype)))
238 
239 #define	REL_ERR_UNSUPSZ(file, sym, rtype, size) \
240 	(eprintf(ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ), \
241 	    CONV_RELOC_TYPE((rtype)), (file), \
242 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(size)))
243 
244 #define	REL_ERR_NONALIGN(file, sym, rtype, off) \
245 	(eprintf(ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN), \
246 	    CONV_RELOC_TYPE((rtype)), (file), \
247 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), EC_OFF((off))))
248 
249 #define	REL_ERR_UNNOBITS(file, sym, rtype, nbits) \
250 	(eprintf(ERR_FATAL, MSG_INTL(MSG_REL_UNNOBITS), \
251 	    CONV_RELOC_TYPE((rtype)), (file), \
252 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (nbits)))
253 
254 #define	REL_ERR_LOSEBITS(file, sym, rtype, uvalue, nbits, off) \
255 	(eprintf(ERR_FATAL,  MSG_INTL(MSG_REL_LOSEBITS), \
256 	    CONV_RELOC_TYPE((rtype)), (file), \
257 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
258 	    EC_XWORD((uvalue)), (nbits), EC_ADDR((off))))
259 
260 #define	REL_ERR_NOFIT(file, sym, rtype, uvalue) \
261 	(eprintf(ERR_FATAL, MSG_INTL(MSG_REL_NOFIT), \
262 	    CONV_RELOC_TYPE((rtype)), (file), \
263 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
264 	    EC_XWORD((uvalue))))
265 
266 #endif	/* _KERNEL */
267 
268 #ifdef	__cplusplus
269 }
270 #endif
271 
272 #endif /* _RELOC_DOT_H */
273