xref: /titanic_41/usr/src/uts/common/krtld/reloc.h (revision de777a601dfef76d8d54837de77dc672dce47498)
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 2007 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_SIZE		0x00002000	/* symbol size required */
72 
73 #define	FLG_RE_NOTSUP		0x00010000	/* relocation not supported */
74 
75 #define	FLG_RE_SEGREL		0x00040000	/* segment relative */
76 #define	FLG_RE_SECREL		0x00080000	/* section relative */
77 
78 #define	FLG_RE_TLSGD		0x00200000	/* TLS GD relocation */
79 #define	FLG_RE_TLSLD		0x00400000	/* TLS LD relocation */
80 #define	FLG_RE_TLSIE		0x00800000	/* TLS IE relocation */
81 #define	FLG_RE_TLSLE		0x01000000	/* TLS LE relocation */
82 #define	FLG_RE_LOCLBND		0x02000000	/* relocation must bind */
83 						/*    locally */
84 
85 /*
86  * In user land, redefine the relocation table and relocation engine to be
87  * class specific if necessary.  This allows both engines to reside in the
88  * intel/amd version of libld.
89  */
90 #if	!defined(_KERNEL)
91 #if	defined(_ELF64)
92 #define	do_reloc		do64_reloc
93 #define	reloc_table		reloc64_table
94 #else
95 #define	do_reloc		do32_reloc
96 #define	reloc_table		reloc32_table
97 #endif
98 #endif
99 
100 /*
101  * Relocation table and macros for testing relocation table flags.
102  */
103 extern	const Rel_entry		reloc_table[];
104 
105 #define	IS_PLT(X)		((reloc_table[(X)].re_flags & \
106 					FLG_RE_PLTREL) != 0)
107 #define	IS_GOT_RELATIVE(X)	((reloc_table[(X)].re_flags & \
108 					FLG_RE_GOTADD) != 0)
109 #define	IS_GOT_PC(X)		((reloc_table[(X)].re_flags & \
110 					FLG_RE_GOTPC) != 0)
111 #define	IS_GOTPCREL(X)		((reloc_table[(X)].re_flags & \
112 					(FLG_RE_GOTPC | FLG_RE_GOTADD)) == \
113 					(FLG_RE_GOTPC | FLG_RE_GOTADD))
114 #define	IS_GOT_BASED(X)		((reloc_table[(X)].re_flags & \
115 					FLG_RE_GOTREL) != 0)
116 #define	IS_GOT_OPINS(X)		((reloc_table[(X)].re_flags & \
117 					FLG_RE_GOTOPINS) != 0)
118 #define	IS_GOT_REQUIRED(X)	((reloc_table[(X)].re_flags & \
119 					(FLG_RE_GOTADD | FLG_RE_GOTREL | \
120 					FLG_RE_GOTPC | FLG_RE_GOTOPINS)) != 0)
121 #define	IS_PC_RELATIVE(X)	((reloc_table[(X)].re_flags & \
122 					FLG_RE_PCREL) != 0)
123 #define	IS_ADD_RELATIVE(X)	((reloc_table[(X)].re_flags & \
124 					FLG_RE_ADDRELATIVE) != 0)
125 #define	IS_REGISTER(X)		((reloc_table[(X)].re_flags & \
126 					FLG_RE_REGISTER) != 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 #define	IS_SIZE(X)		((reloc_table[(X)].re_flags &\
149 					FLG_RE_SIZE) != 0)
150 
151 /*
152  * Relocation engine.
153  */
154 extern	int	do_reloc(uchar_t, uchar_t *, Xword *, const char *,
155 		    const char *, void *);
156 
157 #if defined(_KERNEL)
158 /*
159  * These are macro's that are only needed for krtld.  Many of these are already
160  * defined in the sgs/include files referenced by ld and rtld
161  */
162 #define	S_MASK(n)	((1l << (n)) - 1l)
163 #define	S_INRANGE(v, n)	(((-(1l << (n)) - 1l) < (v)) && ((v) < (1l << (n))))
164 
165 /*
166  * Message strings used by doreloc().
167  */
168 #define	MSG_STR_UNKNOWN		"(unknown)"
169 
170 #define	MSG_REL_PREGEN		"relocation error: %s: "
171 #define	MSG_REL_PREFIL		"relocation error: file %s: "
172 #define	MSG_REL_FILE		"file %s: "
173 #define	MSG_REL_SYM		"symbol %s: "
174 #define	MSG_REL_VALUE		"value 0x%llx "
175 #define	MSG_REL_LOSEBITS	"loses %d bits at "
176 
177 #define	MSG_REL_UNIMPL		"unimplemented relocation type: %d"
178 #define	MSG_REL_UNSUPSZ		"offset size (%d bytes) is not supported"
179 #define	MSG_REL_NONALIGN	"offset 0x%llx is non-aligned"
180 #define	MSG_REL_UNNOBITS	"unsupported number of bits: %d"
181 #define	MSG_REL_OFFSET		"offset 0x%llx"
182 #define	MSG_REL_NOFIT		"value 0x%llx does not fit"
183 
184 /*
185  * Provide a macro to select the appropriate conversion routine for this
186  * architecture.
187  */
188 #if defined(__amd64)
189 
190 extern const char	*conv_reloc_amd64_type(Word);
191 #define	CONV_RELOC_TYPE	conv_reloc_amd64_type
192 
193 #elif defined(__i386)
194 
195 extern const char	*conv_reloc_386_type(Word);
196 #define	CONV_RELOC_TYPE	conv_reloc_386_type
197 
198 #elif defined(__sparc)
199 
200 extern const char	*conv_reloc_SPARC_type(Word);
201 #define	CONV_RELOC_TYPE	conv_reloc_SPARC_type
202 
203 #else
204 #error platform not defined!
205 #endif
206 
207 /*
208  * Note:  dlerror() only keeps track of a single error string, and therefore
209  * must have errors reported through a single eprintf() call.  The kernel's
210  * _kobj_printf is somewhat more limited, and must receive messages with only
211  * one argument to the format string.  The following macros account for these
212  * differences, as krtld and rtld share do_reloc().
213  */
214 #define	REL_ERR_UNIMPL(lml, file, sym, rtype) \
215 	_kobj_printf(ops, MSG_REL_PREFIL, (file)); \
216 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
217 	_kobj_printf(ops, MSG_REL_UNIMPL, (int)(rtype))
218 
219 #define	REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
220 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
221 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
222 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
223 	_kobj_printf(ops, MSG_REL_UNSUPSZ, (int)(size))
224 
225 #define	REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
226 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
227 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
228 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
229 	_kobj_printf(ops, MSG_REL_NONALIGN, EC_OFF((off)))
230 
231 #define	REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
232 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
233 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
234 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
235 	_kobj_printf(ops, MSG_REL_UNNOBITS, (nbits))
236 
237 #define	REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
238 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
239 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
240 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
241 	_kobj_printf(ops, MSG_REL_VALUE, EC_XWORD((uvalue))); \
242 	_kobj_printf(ops, MSG_REL_LOSEBITS, (int)(nbits)); \
243 	_kobj_printf(ops, MSG_REL_OFFSET, EC_NATPTR((off)))
244 
245 #define	REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
246 	_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
247 	_kobj_printf(ops, MSG_REL_FILE, (file)); \
248 	_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
249 	_kobj_printf(ops, MSG_REL_NOFIT, EC_XWORD((uvalue)))
250 
251 
252 #else	/* !_KERNEL */
253 
254 extern	const char *demangle(const char *);
255 
256 #define	REL_ERR_UNIMPL(lml, file, sym, rtype) \
257 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), (file), \
258 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(rtype)))
259 
260 #define	REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
261 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ), \
262 	    conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
263 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(size)))
264 
265 #define	REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
266 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN), \
267 	    conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
268 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), EC_OFF((off))))
269 
270 #define	REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
271 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNNOBITS), \
272 	    conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
273 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (nbits)))
274 
275 #define	REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
276 	(eprintf(lml, ERR_FATAL,  MSG_INTL(MSG_REL_LOSEBITS), \
277 	    conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
278 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
279 	    EC_XWORD((uvalue)), (nbits), EC_NATPTR((off))))
280 
281 #define	REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
282 	(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOFIT), \
283 	    conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
284 	    ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
285 	    EC_XWORD((uvalue))))
286 
287 #endif	/* _KERNEL */
288 
289 #ifdef	__cplusplus
290 }
291 #endif
292 
293 #endif /* _RELOC_DOT_H */
294