resolve.h revision 1.78
1/*	$OpenBSD: resolve.h,v 1.78 2016/07/04 21:15:06 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 <link.h>
34#include <dlfcn.h>
35
36struct load_list {
37	struct load_list *next;
38	void		*start;
39	size_t		size;
40	int		prot;
41	Elf_Addr	moff;
42	long		foff;
43};
44
45/*
46 *  Structure describing a loaded object.
47 *  The head of this struct must be compatible
48 *  with struct link_map in sys/link.h
49 */
50typedef struct elf_object elf_object_t;
51struct elf_object {
52	Elf_Addr obj_base;		/* object's address '0' base */
53	char	*load_name;		/* Pointer to object name */
54	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
55	struct elf_object *next;
56	struct elf_object *prev;
57/* End struct link_map compatible */
58	Elf_Addr load_base;		/* Base address of loadable segments */
59
60	struct load_list *load_list;
61
62	u_int32_t  load_size;
63
64	union {
65		u_long		info[DT_NUM + DT_PROCNUM];
66		struct {
67			Elf_Addr	null;		/* Not used */
68			Elf_Addr	needed;		/* Not used */
69			Elf_Addr	pltrelsz;
70			Elf_Addr	*pltgot;
71			Elf_Addr	*hash;
72			const char	*strtab;
73			const Elf_Sym	*symtab;
74			Elf_RelA	*rela;
75			Elf_Addr	relasz;
76			Elf_Addr	relaent;
77			Elf_Addr	strsz;
78			Elf_Addr	syment;
79			void		(*init)(void);
80			void		(*fini)(void);
81			const char	*soname;
82			const char	*rpath;
83			Elf_Addr	symbolic;
84			Elf_Rel	*rel;
85			Elf_Addr	relsz;
86			Elf_Addr	relent;
87			Elf_Addr	pltrel;
88			Elf_Addr	debug;
89			Elf_Addr	textrel;
90			Elf_Addr	jmprel;
91		} u;
92	} Dyn;
93#define dyn Dyn.u
94
95	Elf_Addr	relacount;	/* DT_RELACOUNT */
96	Elf_Addr	relcount;	/* DT_RELCOUNT */
97
98	int		status;
99#define	STAT_RELOC_DONE	0x01
100#define	STAT_GOT_DONE	0x02
101#define	STAT_INIT_DONE	0x04
102#define	STAT_FINI_DONE	0x08
103#define	STAT_FINI_READY	0x10
104#define	STAT_UNLOADED	0x20
105#define	STAT_NODELETE	0x40
106#define	STAT_VISITED	0x80
107
108	Elf_Phdr	*phdrp;
109	int		phdrc;
110
111	int		obj_type;
112#define	OBJTYPE_LDR	1
113#define	OBJTYPE_EXE	2
114#define	OBJTYPE_LIB	3
115#define	OBJTYPE_DLO	4
116	int		obj_flags;	/* c.f. <sys/exec_elf.h> DF_1_* */
117
118	Elf_Word	*buckets;
119	u_int32_t	nbuckets;
120	Elf_Word	*chains;
121	u_int32_t	nchains;
122	Elf_Dyn		*dynamic;
123
124	TAILQ_HEAD(,dep_node)	child_list;	/* direct dep libs of object */
125	TAILQ_HEAD(,dep_node)	grpsym_list;	/* ordered complete dep list */
126	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
127
128	int		refcount;	/* dep libs only */
129	int		opencount;	/* # dlopen() & exe */
130	int		grprefcount;	/* load group refs */
131#define OBJECT_REF_CNT(object) \
132    ((object->refcount + object->opencount + object->grprefcount))
133#define OBJECT_DLREF_CNT(object) \
134    ((object->opencount + object->grprefcount))
135
136	/* object that caused this module to be loaded, used in symbol lookup */
137	elf_object_t	*load_object;
138	struct sod	sod;
139
140	/* for object confirmation */
141	dev_t	dev;
142	ino_t inode;
143
144	/* thread local storage info */
145	Elf_Addr	tls_fsize;
146	Elf_Addr	tls_msize;
147	Elf_Addr	tls_align;
148	const void	*tls_static_data;
149	int		tls_offset;
150
151	/* generation number of last grpsym insert on this object */
152	unsigned int grpsym_gen;
153
154	char **rpath;
155
156	/* nonzero if trace enabled for this object */
157	int traced;
158};
159
160struct dep_node {
161	TAILQ_ENTRY(dep_node) next_sib;
162	elf_object_t *data;
163};
164
165
166/* Please don't rename or make hidden; gdb(1) knows about these. */
167Elf_Addr _dl_bind(elf_object_t *object, int index);
168void	_dl_debug_state(void);
169
170/* exported to the application */
171extern char *__progname;
172
173__BEGIN_HIDDEN_DECLS
174void _dl_add_object(elf_object_t *object);
175elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
176    Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase,
177    const long obase);
178void	_dl_remove_object(elf_object_t *object);
179void	_dl_cleanup_objects(void);
180void	*_dl_protect_segment(elf_object_t *_object, Elf_Addr _addr,
181	    const char *_start_sym, const char *_end_sym, int _prot);
182
183elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
184elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
185
186int _dl_md_reloc(elf_object_t *object, int rel, int relsz);
187int _dl_md_reloc_got(elf_object_t *object, int lazy);
188
189Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this,
190    int flags, const Elf_Sym *ref_sym, elf_object_t *object,
191    const elf_object_t **pobj);
192Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
193    const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym,
194    const elf_object_t **pobj);
195/*
196 * defines for _dl_find_symbol() flag field, three bits of meaning
197 * myself	- clear: search all objects,	set: search only this object
198 * warnnotfound - clear: no warning,		set: warn if not found
199 * inplt	- clear: possible plt ref	set: real matching function.
200 *
201 * inplt - due to how ELF handles function addresses in shared libraries
202 * &func may actually refer to the plt entry in the main program
203 * rather than the actual function address in the .so file.
204 * This rather bizarre behavior is documented in the SVR4 ABI.
205 * when getting the function address to relocate a PLT entry
206 * the 'real' function address is necessary, not the possible PLT address.
207 */
208/* myself */
209#define SYM_SEARCH_ALL		0x00
210#define SYM_SEARCH_SELF		0x01
211#define SYM_SEARCH_OTHER	0x02
212#define SYM_SEARCH_NEXT		0x04
213#define SYM_SEARCH_OBJ		0x08
214/* warnnotfound */
215#define SYM_NOWARNNOTFOUND	0x00
216#define SYM_WARNNOTFOUND	0x10
217/* inplt */
218#define SYM_NOTPLT		0x00
219#define SYM_PLT			0x20
220
221#define SYM_DLSYM		0x40
222
223int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
224int _dl_rtld(elf_object_t *object);
225void _dl_call_init(elf_object_t *object);
226void _dl_link_child(elf_object_t *dep, elf_object_t *p);
227void _dl_link_grpsym(elf_object_t *object, int checklist);
228void _dl_cache_grpsym_list(elf_object_t *object);
229void _dl_cache_grpsym_list_setup(elf_object_t *object);
230void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
231void _dl_link_dlopen(elf_object_t *dep);
232void _dl_unlink_dlopen(elf_object_t *dep);
233void _dl_notify_unload_shlib(elf_object_t *object);
234void _dl_unload_shlib(elf_object_t *object);
235void _dl_unload_dlopen(void);
236
237void _dl_run_all_dtors(void);
238
239int	_dl_match_file(struct sod *sodp, const char *name, int namelen);
240char	*_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints);
241void	_dl_load_list_free(struct load_list *load_list);
242
243void	_dl_thread_kern_go(void);
244void	_dl_thread_kern_stop(void);
245
246char	*_dl_getenv(const char *, char **);
247void	_dl_unsetenv(const char *, char **);
248
249void	_dl_trace_setup(char **);
250void	_dl_trace_object_setup(elf_object_t *);
251int	_dl_trace_plt(const elf_object_t *, const char *);
252
253/* tib.c */
254void	_dl_allocate_tls_offsets(void);
255void	_dl_allocate_first_tib(void);
256void	_dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr,
257	    const char *_libname);
258extern int _dl_tib_static_done;
259
260extern elf_object_t *_dl_objects;
261extern elf_object_t *_dl_last_object;
262
263extern elf_object_t *_dl_loading_object;
264
265extern struct r_debug *_dl_debug_map;
266
267extern int  _dl_pagesz;
268extern int  _dl_errno;
269
270extern char **_dl_libpath;
271
272extern char *_dl_preload;
273extern char *_dl_bindnow;
274extern char *_dl_traceld;
275extern char *_dl_tracefmt1;
276extern char *_dl_tracefmt2;
277extern char *_dl_traceprog;
278extern char *_dl_debug;
279
280extern int _dl_trust;
281
282#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
283
284#define	DL_NOT_FOUND		1
285#define	DL_CANT_OPEN		2
286#define	DL_NOT_ELF		3
287#define	DL_CANT_OPEN_REF	4
288#define	DL_CANT_MMAP		5
289#define	DL_NO_SYMBOL		6
290#define	DL_INVALID_HANDLE	7
291#define	DL_INVALID_CTL		8
292#define	DL_NO_OBJECT		9
293#define	DL_CANT_FIND_OBJ	10
294#define	DL_CANT_LOAD_OBJ	11
295#define	DL_INVALID_MODE		12
296
297#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
298#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
299
300/* symbol lookup cache */
301typedef struct sym_cache {
302	const elf_object_t *obj;
303	const Elf_Sym	*sym;
304	int flags;
305} sym_cache;
306
307extern sym_cache *_dl_symcache;
308extern int _dl_symcachestat_hits;
309extern int _dl_symcachestat_lookups;
310TAILQ_HEAD(dlochld, dep_node);
311extern struct dlochld _dlopened_child_list;
312__END_HIDDEN_DECLS
313
314#endif /* _RESOLVE_H_ */
315