resolve.h revision 1.94
1/*	$OpenBSD: resolve.h,v 1.94 2019/08/04 23:51:45 guenther Exp $ */
2
3/*
4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#ifndef _RESOLVE_H_
30#define _RESOLVE_H_
31
32#include <sys/queue.h>
33#include <dlfcn.h>
34#include <link.h>
35#include <tib.h>
36
37#define __relro		__attribute__((section(".data.rel.ro")))
38
39#ifndef __boot
40# if DO_CLEAN_BOOT
41#  define __boot	__attribute__((section(".boot.text")))
42#  define __boot_data	__attribute__((section(".boot.data")))
43# else
44#  define __boot
45#  define __boot_data
46# endif
47#endif
48
49/* Number of low tags that are used saved internally (0 .. DT_NUM-1) */
50#define DT_NUM	(DT_PREINIT_ARRAYSZ + 1)
51
52struct load_list {
53	struct load_list *next;
54	void		*start;
55	size_t		size;
56	int		prot;
57	Elf_Addr	moff;
58	long		foff;
59};
60
61typedef void initarrayfunc(int, const char **, char **, dl_cb_cb *);
62typedef void initfunc(void);	/* also fini and fini_array functions */
63
64/* Alpha uses 8byte entries for DT_HASH */
65#ifdef __alpha__
66typedef uint64_t Elf_Hash_Word;
67#else
68typedef uint32_t Elf_Hash_Word;
69#endif
70
71/*
72 *  Structure describing a loaded object.
73 *  The head of this struct must be compatible
74 *  with struct link_map in sys/link.h
75 */
76typedef struct elf_object elf_object_t;
77struct elf_object {
78	Elf_Addr obj_base;		/* object's address '0' base */
79	char	*load_name;		/* Pointer to object name */
80	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
81	struct elf_object *next;
82	struct elf_object *prev;
83/* End struct link_map compatible */
84	Elf_Addr load_base;		/* Base address of loadable segments */
85
86	struct load_list *load_list;
87
88	u_int32_t  load_size;
89
90	union {
91		u_long		info[DT_NUM + DT_PROCNUM];
92		struct {
93			Elf_Addr	null;		/* Not used */
94			Elf_Addr	needed;		/* Not used */
95			Elf_Addr	pltrelsz;
96			Elf_Addr	*pltgot;
97			Elf_Addr	*hash;
98			const char	*strtab;
99			const Elf_Sym	*symtab;
100			Elf_RelA	*rela;
101			Elf_Addr	relasz;
102			Elf_Addr	relaent;
103			Elf_Addr	strsz;
104			Elf_Addr	syment;
105			initfunc	*init;
106			initfunc	*fini;
107			const char	*soname;
108			const char	*rpath;
109			Elf_Addr	symbolic;
110			Elf_Rel		*rel;
111			Elf_Addr	relsz;
112			Elf_Addr	relent;
113			Elf_Addr	pltrel;
114			Elf_Addr	debug;
115			Elf_Addr	textrel;
116			Elf_Addr	jmprel;
117			Elf_Addr	bind_now;
118			initarrayfunc	**init_array;
119			initfunc	**fini_array;
120			Elf_Addr	init_arraysz;
121			Elf_Addr	fini_arraysz;
122			const char	*runpath;
123			Elf_Addr	flags;
124			Elf_Addr	encoding;
125			initarrayfunc	**preinit_array;
126			Elf_Addr	preinit_arraysz;
127		} u;
128	} Dyn;
129#define dyn Dyn.u
130
131	Elf_Addr	relacount;	/* DT_RELACOUNT */
132	Elf_Addr	relcount;	/* DT_RELCOUNT */
133
134	int		status;
135#define	STAT_RELOC_DONE		0x001
136#define	STAT_GOT_DONE		0x002
137#define	STAT_INIT_DONE		0x004
138#define	STAT_FINI_DONE		0x008
139#define	STAT_FINI_READY		0x010
140#define	STAT_UNLOADED		0x020
141#define	STAT_NODELETE		0x040
142#define	STAT_GNU_HASH		0x080
143#define	STAT_VISIT_INITFIRST	0x100
144#define	STAT_VISIT_INIT		0x200
145
146	Elf_Phdr	*phdrp;
147	int		phdrc;
148
149	int		obj_type;
150#define	OBJTYPE_LDR	1
151#define	OBJTYPE_EXE	2
152#define	OBJTYPE_LIB	3
153#define	OBJTYPE_DLO	4
154	int		obj_flags;	/* c.f. <sys/exec_elf.h> DF_1_* */
155
156	/* shared by ELF and GNU hash */
157	u_int32_t	nbuckets;
158	u_int32_t	nchains;	/* really, number of symbols */
159	union {
160		struct {
161			/* specific to ELF hash */
162			const Elf_Hash_Word	*buckets;
163			const Elf_Hash_Word	*chains;
164		} u_elf;
165		struct {
166			/* specific to GNU hash */
167			const Elf_Word		*buckets;
168			const Elf_Word		*chains;
169			const Elf_Addr		*bloom;
170			Elf_Word		mask_bm;
171			Elf_Word		shift2;
172			Elf_Word		symndx;
173		} u_gnu;
174	} hash_u;
175#define buckets_elf	hash_u.u_elf.buckets
176#define chains_elf	hash_u.u_elf.chains
177#define buckets_gnu	hash_u.u_gnu.buckets
178#define chains_gnu	hash_u.u_gnu.chains
179#define bloom_gnu	hash_u.u_gnu.bloom
180#define mask_bm_gnu	hash_u.u_gnu.mask_bm
181#define shift2_gnu	hash_u.u_gnu.shift2
182#define symndx_gnu	hash_u.u_gnu.symndx
183
184	TAILQ_HEAD(,dep_node)	child_list;	/* direct dep libs of object */
185	TAILQ_HEAD(,dep_node)	grpsym_list;	/* ordered complete dep list */
186	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
187
188	int		refcount;	/* dep libs only */
189	int		opencount;	/* # dlopen() & exe */
190	int		grprefcount;	/* load group refs */
191#define OBJECT_REF_CNT(object) \
192    ((object->refcount + object->opencount + object->grprefcount))
193#define OBJECT_DLREF_CNT(object) \
194    ((object->opencount + object->grprefcount))
195
196	/* object that caused this module to be loaded, used in symbol lookup */
197	elf_object_t	*load_object;
198	struct sod	sod;
199
200	/* for object confirmation */
201	dev_t	dev;
202	ino_t inode;
203
204	/* thread local storage info */
205	Elf_Addr	tls_fsize;
206	Elf_Addr	tls_msize;
207	Elf_Addr	tls_align;
208	const void	*tls_static_data;
209	int		tls_offset;
210
211	/* relro bits */
212	Elf_Addr	relro_addr;
213	Elf_Addr	relro_size;
214
215	/* generation number of last grpsym insert on this object */
216	unsigned int grpsym_gen;
217
218	char **rpath;
219	char **runpath;
220
221	/* nonzero if trace enabled for this object */
222	int traced;
223};
224
225struct dep_node {
226	TAILQ_ENTRY(dep_node) next_sib;
227	elf_object_t *data;
228};
229
230
231/* Please don't rename or make hidden; gdb(1) knows about these. */
232Elf_Addr _dl_bind(elf_object_t *object, int index);
233void	_dl_debug_state(void);
234
235/* exported to the application */
236extern char *__progname;
237
238__BEGIN_HIDDEN_DECLS
239void _dl_add_object(elf_object_t *object);
240elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
241    Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase,
242    const long obase);
243void	_dl_remove_object(elf_object_t *object);
244void	_dl_cleanup_objects(void);
245
246elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
247elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
248
249int _dl_md_reloc(elf_object_t *object, int rel, int relsz);
250int _dl_md_reloc_got(elf_object_t *object, int lazy);
251
252struct sym_res {
253	const Elf_Sym		*sym;
254	const elf_object_t	*obj;
255};
256
257struct sym_res _dl_find_symbol(const char *name, int flags,
258    const Elf_Sym *ref_sym, elf_object_t *object);
259
260/*
261 * defines for _dl_find_symbol() flag field, three bits of meaning
262 * myself	- clear: search all objects,	set: search only this object
263 * warnnotfound - clear: no warning,		set: warn if not found
264 * inplt	- clear: possible plt ref	set: real matching function.
265 *
266 * inplt - due to how ELF handles function addresses in shared libraries
267 * &func may actually refer to the plt entry in the main program
268 * rather than the actual function address in the .so file.
269 * This rather bizarre behavior is documented in the SVR4 ABI.
270 * when getting the function address to relocate a PLT entry
271 * the 'real' function address is necessary, not the possible PLT address.
272 */
273/* myself */
274#define SYM_SEARCH_ALL		0x00
275#define SYM_SEARCH_SELF		0x01
276#define SYM_SEARCH_OTHER	0x02
277#define SYM_SEARCH_NEXT		0x04
278/* warnnotfound */
279#define SYM_NOWARNNOTFOUND	0x00
280#define SYM_WARNNOTFOUND	0x10
281/* inplt */
282#define SYM_NOTPLT		0x00
283#define SYM_PLT			0x20
284
285#define SYM_DLSYM		0x40
286
287int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
288int _dl_rtld(elf_object_t *object);
289void _dl_call_init(elf_object_t *object);
290void _dl_link_child(elf_object_t *dep, elf_object_t *p);
291void _dl_link_grpsym(elf_object_t *object, int checklist);
292void _dl_cache_grpsym_list(elf_object_t *object);
293void _dl_cache_grpsym_list_setup(elf_object_t *object);
294void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
295void _dl_link_dlopen(elf_object_t *dep);
296void _dl_unlink_dlopen(elf_object_t *dep);
297void _dl_notify_unload_shlib(elf_object_t *object);
298void _dl_unload_shlib(elf_object_t *object);
299void _dl_unload_dlopen(void);
300
301void _dl_run_all_dtors(void);
302
303int	_dl_match_file(struct sod *sodp, const char *name, int namelen);
304char	*_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints);
305void	_dl_load_list_free(struct load_list *load_list);
306
307typedef void lock_cb(int);
308void	_dl_thread_kern_go(lock_cb *);
309lock_cb	*_dl_thread_kern_stop(void);
310
311char	*_dl_getenv(const char *, char **) __boot;
312void	_dl_unsetenv(const char *, char **) __boot;
313
314void	_dl_trace_setup(char **) __boot;
315void	_dl_trace_object_setup(elf_object_t *);
316int	_dl_trace_plt(const elf_object_t *, const char *);
317
318/* tib.c */
319void	_dl_allocate_tls_offsets(void) __boot;
320void	_dl_allocate_first_tib(void) __boot;
321void	_dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr,
322	    const char *_libname);
323extern int _dl_tib_static_done;
324
325extern elf_object_t *_dl_objects;
326extern elf_object_t *_dl_last_object;
327
328extern elf_object_t *_dl_loading_object;
329
330extern struct r_debug *_dl_debug_map;
331
332extern int  _dl_pagesz;
333extern int  _dl_errno;
334
335extern char **_dl_libpath;
336
337extern int _dl_bindnow;
338extern int _dl_traceld;
339extern int _dl_debug;
340
341extern char *_dl_preload;
342extern char *_dl_tracefmt1;
343extern char *_dl_tracefmt2;
344extern char *_dl_traceprog;
345
346extern int _dl_trust;
347
348#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
349
350#define	DL_NOT_FOUND		1
351#define	DL_CANT_OPEN		2
352#define	DL_NOT_ELF		3
353#define	DL_CANT_OPEN_REF	4
354#define	DL_CANT_MMAP		5
355#define	DL_NO_SYMBOL		6
356#define	DL_INVALID_HANDLE	7
357#define	DL_INVALID_CTL		8
358#define	DL_NO_OBJECT		9
359#define	DL_CANT_FIND_OBJ	10
360#define	DL_CANT_LOAD_OBJ	11
361#define	DL_INVALID_MODE		12
362
363#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
364#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
365
366/* symbol lookup cache */
367typedef struct sym_cache {
368	const elf_object_t *obj;
369	const Elf_Sym	*sym;
370	int flags;
371} sym_cache;
372
373TAILQ_HEAD(dlochld, dep_node);
374extern struct dlochld _dlopened_child_list;
375__END_HIDDEN_DECLS
376
377#endif /* _RESOLVE_H_ */
378