xref: /titanic_44/usr/src/head/link.h (revision a31148363f598def767ac48c5d82e1572e44b935)
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  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _LINK_H
27 #define	_LINK_H
28 
29 #include <sys/link.h>
30 
31 #ifndef _ASM
32 #include <libelf.h>
33 #include <sys/types.h>
34 #include <dlfcn.h>
35 #endif
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #ifndef _ASM
42 /*
43  * ld support library calls
44  */
45 #ifdef __STDC__
46 extern uint_t	ld_version(uint_t);
47 extern void	ld_input_done(uint_t *);
48 
49 extern void	ld_start(const char *, const Elf32_Half, const char *);
50 extern void	ld_atexit(int);
51 extern void	ld_open(const char **, const char **, int *, int, Elf **,
52 			Elf *, size_t, const Elf_Kind);
53 extern void	ld_file(const char *, const Elf_Kind, int, Elf *);
54 extern void	ld_input_section(const char *, Elf32_Shdr **, Elf32_Word,
55 			Elf_Data *, Elf *, uint_t *);
56 extern void	ld_section(const char *, Elf32_Shdr *, Elf32_Word,
57 			Elf_Data *, Elf *);
58 
59 #if defined(_LP64) || defined(_LONGLONG_TYPE)
60 extern void	ld_start64(const char *, const Elf64_Half, const char *);
61 extern void	ld_atexit64(int);
62 extern void	ld_open64(const char **, const char **, int *, int, Elf **,
63 			Elf *, size_t, const Elf_Kind);
64 extern void	ld_file64(const char *, const Elf_Kind, int, Elf *);
65 extern void	ld_input_section64(const char *, Elf64_Shdr **, Elf64_Word,
66 			Elf_Data *, Elf *, uint_t *);
67 extern void	ld_section64(const char *, Elf64_Shdr *, Elf64_Word,
68 			Elf_Data *, Elf *);
69 
70 #endif /* (defined(_LP64) || defined(_LONGLONG_TYPE) */
71 #else
72 extern void	ld_version();
73 extern void	ld_input_done();
74 
75 extern void	ld_start();
76 extern void	ld_atexit();
77 extern void	ld_open();
78 extern void	ld_file();
79 extern void	ld_input_section();
80 extern void	ld_section();
81 
82 #if defined(_LP64) || defined(_LONGLONG_TYPE)
83 extern void	ld_start64();
84 extern void	ld_atexit64();
85 extern void	ld_open64();
86 extern void	ld_file64();
87 extern void	ld_input_section64();
88 extern void	ld_section64();
89 
90 #endif /* (defined(_LP64) || defined(_LONGLONG_TYPE) */
91 #endif /* __STDC__ */
92 
93 /*
94  * ld_version() version values.
95  */
96 #define	LD_SUP_VNONE	0
97 #define	LD_SUP_VERSION1	1
98 #define	LD_SUP_VERSION2	2
99 #define	LD_SUP_VERSION3	3
100 #define	LD_SUP_VCURRENT	LD_SUP_VERSION3
101 
102 /*
103  * Flags passed to ld support calls.
104  */
105 #define	LD_SUP_DERIVED		0x1	/* derived filename */
106 #define	LD_SUP_INHERITED	0x2	/* file inherited from .so DT_NEEDED */
107 #define	LD_SUP_EXTRACTED	0x4	/* file extracted from archive */
108 #endif
109 
110 /*
111  * Runtime link-map identifiers.
112  */
113 #define	LM_ID_BASE		0x00
114 #define	LM_ID_LDSO		0x01
115 #define	LM_ID_NUM		2
116 
117 #define	LM_ID_BRAND		0xfd	/* brand emulation linkmap objs */
118 #define	LM_ID_NONE		0xfe	/* no link map specified */
119 #define	LM_ID_NEWLM		0xff	/* create a new link-map */
120 
121 /*
122  * Runtime Link-Edit Auditing.
123  */
124 #define	LAV_NONE		0
125 #define	LAV_VERSION1		1
126 #define	LAV_VERSION2		2
127 #define	LAV_VERSION3		3
128 #define	LAV_VERSION4		4
129 #define	LAV_CURRENT		LAV_VERSION4
130 #define	LAV_NUM			5
131 
132 /*
133  * Flags that can be or'd into the la_objopen() return code
134  */
135 #define	LA_FLG_BINDTO		0x0001	/* audit symbinds TO this object */
136 #define	LA_FLG_BINDFROM		0x0002	/* audit symbinding FROM this object */
137 
138 /*
139  * Flags that can be or'd into the 'flags' argument of la_symbind()
140  */
141 #define	LA_SYMB_NOPLTENTER	0x0001	/* disable pltenter for this symbol */
142 #define	LA_SYMB_NOPLTEXIT	0x0002	/* disable pltexit for this symbol */
143 #define	LA_SYMB_STRUCTCALL	0x0004	/* this function call passes a */
144 					/*	structure as it's return code */
145 #define	LA_SYMB_DLSYM		0x0008	/* this symbol bindings is due to */
146 					/*	a call to dlsym() */
147 #define	LA_SYMB_ALTVALUE	0x0010	/* alternate symbol binding returned */
148 					/*	by la_symbind() */
149 
150 /*
151  * Flags that describe the object passed to la_objsearch()
152  */
153 #define	LA_SER_ORIG		0x001	/* original (needed) name */
154 #define	LA_SER_LIBPATH		0x002	/* LD_LIBRARY_PATH entry prepended */
155 #define	LA_SER_RUNPATH		0x004	/* runpath entry prepended */
156 #define	LA_SER_CONFIG		0x008	/* configuration entry prepended */
157 #define	LA_SER_DEFAULT		0x040	/* default path prepended */
158 #define	LA_SER_SECURE		0x080	/* default (secure) path prepended */
159 
160 #define	LA_SER_MASK		0xfff	/* mask of known flags */
161 
162 /*
163  * Flags that describe the la_activity()
164  */
165 #define	LA_ACT_CONSISTENT	0x00	/* add/deletion of objects complete */
166 #define	LA_ACT_ADD		0x01	/* objects being added */
167 #define	LA_ACT_DELETE		0x02	/* objects being deleted */
168 
169 
170 #ifndef	_KERNEL
171 #ifndef	_ASM
172 
173 #if defined(_LP64)
174 typedef long	lagreg_t;
175 #else
176 typedef int	lagreg_t;
177 #endif
178 
179 struct _la_sparc_regs {
180 	lagreg_t	lr_rego0;
181 	lagreg_t	lr_rego1;
182 	lagreg_t	lr_rego2;
183 	lagreg_t	lr_rego3;
184 	lagreg_t	lr_rego4;
185 	lagreg_t	lr_rego5;
186 	lagreg_t	lr_rego6;
187 	lagreg_t	lr_rego7;
188 };
189 
190 #if defined(_LP64)
191 typedef struct _la_sparc_regs	La_sparcv9_regs;
192 typedef struct {
193 	lagreg_t	lr_rsp;
194 	lagreg_t	lr_rbp;
195 	lagreg_t	lr_rdi;	    /* arg1 */
196 	lagreg_t	lr_rsi;	    /* arg2 */
197 	lagreg_t	lr_rdx;	    /* arg3 */
198 	lagreg_t	lr_rcx;	    /* arg4 */
199 	lagreg_t	lr_r8;	    /* arg5 */
200 	lagreg_t	lr_r9;	    /* arg6 */
201 } La_amd64_regs;
202 #else
203 typedef struct _la_sparc_regs	La_sparcv8_regs;
204 typedef struct {
205 	lagreg_t	lr_esp;
206 	lagreg_t	lr_ebp;
207 } La_i86_regs;
208 #endif
209 
210 #if	!defined(_SYS_INT_TYPES_H)
211 #if	defined(_LP64) || defined(_I32LPx)
212 typedef unsigned long		uintptr_t;
213 #else
214 typedef	unsigned int		uintptr_t;
215 #endif
216 #endif
217 
218 
219 #ifdef	__STDC__
220 extern uint_t		la_version(uint_t);
221 extern void		la_activity(uintptr_t *, uint_t);
222 extern void		la_preinit(uintptr_t *);
223 extern char		*la_objsearch(const char *, uintptr_t *, uint_t);
224 extern uint_t		la_objopen(Link_map *, Lmid_t, uintptr_t *);
225 extern uint_t		la_objclose(uintptr_t *);
226 extern int		la_objfilter(uintptr_t *, const char *, uintptr_t *,
227 				uint_t);
228 #if	defined(_LP64)
229 extern uintptr_t	la_amd64_pltenter(Elf64_Sym *, uint_t, uintptr_t *,
230 				uintptr_t *, La_amd64_regs *,	uint_t *,
231 				const char *);
232 extern uintptr_t	la_symbind64(Elf64_Sym *, uint_t, uintptr_t *,
233 				uintptr_t *, uint_t *, const char *);
234 extern uintptr_t	la_sparcv9_pltenter(Elf64_Sym *, uint_t, uintptr_t *,
235 				uintptr_t *, La_sparcv9_regs *,	uint_t *,
236 				const char *);
237 extern uintptr_t	la_pltexit64(Elf64_Sym *, uint_t, uintptr_t *,
238 				uintptr_t *, uintptr_t, const char *);
239 #else  /* !defined(_LP64) */
240 extern uintptr_t	la_symbind32(Elf32_Sym *, uint_t, uintptr_t *,
241 				uintptr_t *, uint_t *);
242 extern uintptr_t	la_sparcv8_pltenter(Elf32_Sym *, uint_t, uintptr_t *,
243 				uintptr_t *, La_sparcv8_regs *, uint_t *);
244 extern uintptr_t	la_i86_pltenter(Elf32_Sym *, uint_t, uintptr_t *,
245 				uintptr_t *, La_i86_regs *, uint_t *);
246 extern uintptr_t	la_pltexit(Elf32_Sym *, uint_t, uintptr_t *,
247 				uintptr_t *, uintptr_t);
248 #endif /* _LP64 */
249 #else  /* __STDC__ */
250 extern uint_t		la_version();
251 extern void		la_preinit();
252 extern uint_t		la_objopen();
253 extern uint_t		la_objclose();
254 extern int		la_objfilter();
255 #if	defined(_LP64)
256 extern uintptr_t	la_sparcv9_pltenter();
257 extern uintptr_t	la_pltexit64();
258 extern uintptr_t	la_symbind64();
259 #else  /* _ILP32 */
260 extern uintptr_t	la_sparcv8_pltenter();
261 extern uintptr_t	la_i86_pltenter();
262 extern uintptr_t	la_pltexit();
263 extern uintptr_t	la_symbind32();
264 #endif /* _LP64 */
265 #endif /* __STDC__ */
266 
267 #ifdef	__STDC__
268 /*
269  * The ElfW() macro is a GNU/Linux feature, provided as support for
270  * the dl_phdr_info structure used by dl_phdr_iterate(), which also
271  * originated under Linux. Given an ELF data type, without the ElfXX_
272  * prefix, it supplies the appropriate prefix (Elf32_ or Elf64_) for
273  * the ELFCLASS of the code being compiled.
274  *
275  * Note that ElfW() is not suitable in situations in which the ELFCLASS
276  * of the code being compiled does not match that of the objects that
277  * code is intended to operate on (e.g. a 32-bit link-editor building
278  * a 64-bit object). The macros defined in <sys/machelf.h> are
279  * recommended in such cases.
280  */
281 #ifdef _LP64
282 #define	ElfW(type)	Elf64_ ## type
283 #else
284 #define	ElfW(type)	Elf32_ ## type
285 #endif
286 
287 /*
288  * The callback function to dl_interate_phdr() receives a pointer
289  * to a structure of this type.
290  *
291  * dlpi_addr is defined such that the address of any segment in
292  * the program header array can be calculated as:
293  *
294  *	addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
295  *
296  * It is therefore 0 for ET_EXEC objects, and the base address at
297  * which the object is mapped otherwise.
298  */
299 struct dl_phdr_info {
300 	ElfW(Addr)		dlpi_addr;	/* Base address of object */
301 	const char		*dlpi_name;	/* Null-terminated obj name */
302 	const ElfW(Phdr)	*dlpi_phdr;	/* Ptr to ELF program hdr arr */
303 	ElfW(Half)		dlpi_phnum;	/* # of items in dlpi_phdr[] */
304 
305 	/*
306 	 * Note: Following members were introduced after the first version
307 	 * of this structure was available.  The dl_iterate_phdr() callback
308 	 * function is passed a 'size' argument giving the size of the info
309 	 * structure, and must compare that size to the offset of these fields
310 	 * before accessing them to ensure that they are present.
311 	 */
312 
313 	/* Incremented when a new object is mapped into the process */
314 	u_longlong_t		dlpi_adds;
315 	/* Incremented when an object is unmapped from the process */
316 	u_longlong_t		dlpi_subs;
317 };
318 
319 extern  int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
320 	    void *);
321 #endif /* __STDC__ */
322 
323 #endif	/* _ASM */
324 #endif /* _KERNEL */
325 
326 
327 #ifdef __cplusplus
328 }
329 #endif
330 
331 #endif	/* _LINK_H */
332