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