resolve.h revision 1.88
1/*	$OpenBSD: resolve.h,v 1.88 2018/12/05 04:28:32 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
36#define __relro		__attribute__((section(".data.rel.ro")))
37
38/* Number of low tags that are used saved internally (0 .. DT_NUM-1) */
39#define DT_NUM	(DT_PREINIT_ARRAYSZ + 1)
40
41struct load_list {
42	struct load_list *next;
43	void		*start;
44	size_t		size;
45	int		prot;
46	Elf_Addr	moff;
47	long		foff;
48};
49
50/*
51 *  Structure describing a loaded object.
52 *  The head of this struct must be compatible
53 *  with struct link_map in sys/link.h
54 */
55typedef struct elf_object elf_object_t;
56struct elf_object {
57	Elf_Addr obj_base;		/* object's address '0' base */
58	char	*load_name;		/* Pointer to object name */
59	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
60	struct elf_object *next;
61	struct elf_object *prev;
62/* End struct link_map compatible */
63	Elf_Addr load_base;		/* Base address of loadable segments */
64
65	struct load_list *load_list;
66
67	u_int32_t  load_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			Elf_Addr	bind_now;
97			void		(**init_array)(void);
98			void		(**fini_array)(void);
99			Elf_Addr	init_arraysz;
100			Elf_Addr	fini_arraysz;
101			const char	*runpath;
102			Elf_Addr	flags;
103			Elf_Addr	encoding;
104			void		(**preinit_array)(void);
105			Elf_Addr	preinit_arraysz;
106		} u;
107	} Dyn;
108#define dyn Dyn.u
109
110	Elf_Addr	relacount;	/* DT_RELACOUNT */
111	Elf_Addr	relcount;	/* DT_RELCOUNT */
112
113	int		status;
114#define	STAT_RELOC_DONE	0x01
115#define	STAT_GOT_DONE	0x02
116#define	STAT_INIT_DONE	0x04
117#define	STAT_FINI_DONE	0x08
118#define	STAT_FINI_READY	0x10
119#define	STAT_UNLOADED	0x20
120#define	STAT_NODELETE	0x40
121#define	STAT_VISITED	0x80
122#define	STAT_GNU_HASH	0x100
123
124	Elf_Phdr	*phdrp;
125	int		phdrc;
126
127	int		obj_type;
128#define	OBJTYPE_LDR	1
129#define	OBJTYPE_EXE	2
130#define	OBJTYPE_LIB	3
131#define	OBJTYPE_DLO	4
132	int		obj_flags;	/* c.f. <sys/exec_elf.h> DF_1_* */
133
134	/* shared by ELF and GNU hash */
135	u_int32_t	nbuckets;
136	u_int32_t	nchains;	/* really, number of symbols */
137	union {
138		struct {
139			/* specific to ELF hash */
140			const Elf_Word	*buckets;
141			const Elf_Word	*chains;
142		} u_elf;
143		struct {
144			/* specific to GNU hash */
145			const Elf32_Word	*buckets;
146			const Elf32_Word	*chains;
147			const Elf_Addr		*bloom;
148			Elf32_Word		mask_bm;
149			Elf32_Word		shift2;
150			Elf32_Word		symndx;
151		} u_gnu;
152	} hash_u;
153#define buckets_elf	hash_u.u_elf.buckets
154#define chains_elf	hash_u.u_elf.chains
155#define buckets_gnu	hash_u.u_gnu.buckets
156#define chains_gnu	hash_u.u_gnu.chains
157#define bloom_gnu	hash_u.u_gnu.bloom
158#define mask_bm_gnu	hash_u.u_gnu.mask_bm
159#define shift2_gnu	hash_u.u_gnu.shift2
160#define symndx_gnu	hash_u.u_gnu.symndx
161
162	TAILQ_HEAD(,dep_node)	child_list;	/* direct dep libs of object */
163	TAILQ_HEAD(,dep_node)	grpsym_list;	/* ordered complete dep list */
164	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
165
166	int		refcount;	/* dep libs only */
167	int		opencount;	/* # dlopen() & exe */
168	int		grprefcount;	/* load group refs */
169#define OBJECT_REF_CNT(object) \
170    ((object->refcount + object->opencount + object->grprefcount))
171#define OBJECT_DLREF_CNT(object) \
172    ((object->opencount + object->grprefcount))
173
174	/* object that caused this module to be loaded, used in symbol lookup */
175	elf_object_t	*load_object;
176	struct sod	sod;
177
178	/* for object confirmation */
179	dev_t	dev;
180	ino_t inode;
181
182	/* thread local storage info */
183	Elf_Addr	tls_fsize;
184	Elf_Addr	tls_msize;
185	Elf_Addr	tls_align;
186	const void	*tls_static_data;
187	int		tls_offset;
188
189	/* relro bits */
190	Elf_Addr	relro_addr;
191	Elf_Addr	relro_size;
192
193	/* generation number of last grpsym insert on this object */
194	unsigned int grpsym_gen;
195
196	char **rpath;
197	char **runpath;
198
199	/* nonzero if trace enabled for this object */
200	int traced;
201};
202
203struct dep_node {
204	TAILQ_ENTRY(dep_node) next_sib;
205	elf_object_t *data;
206};
207
208
209/* Please don't rename or make hidden; gdb(1) knows about these. */
210Elf_Addr _dl_bind(elf_object_t *object, int index);
211void	_dl_debug_state(void);
212
213/* exported to the application */
214extern char *__progname;
215
216__BEGIN_HIDDEN_DECLS
217void _dl_add_object(elf_object_t *object);
218elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
219    Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase,
220    const long obase);
221void	_dl_remove_object(elf_object_t *object);
222void	_dl_cleanup_objects(void);
223
224elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
225elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
226
227int _dl_md_reloc(elf_object_t *object, int rel, int relsz);
228int _dl_md_reloc_got(elf_object_t *object, int lazy);
229
230Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this,
231    int flags, const Elf_Sym *ref_sym, elf_object_t *object,
232    const elf_object_t **pobj);
233Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
234    const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym,
235    const elf_object_t **pobj);
236/*
237 * defines for _dl_find_symbol() flag field, three bits of meaning
238 * myself	- clear: search all objects,	set: search only this object
239 * warnnotfound - clear: no warning,		set: warn if not found
240 * inplt	- clear: possible plt ref	set: real matching function.
241 *
242 * inplt - due to how ELF handles function addresses in shared libraries
243 * &func may actually refer to the plt entry in the main program
244 * rather than the actual function address in the .so file.
245 * This rather bizarre behavior is documented in the SVR4 ABI.
246 * when getting the function address to relocate a PLT entry
247 * the 'real' function address is necessary, not the possible PLT address.
248 */
249/* myself */
250#define SYM_SEARCH_ALL		0x00
251#define SYM_SEARCH_SELF		0x01
252#define SYM_SEARCH_OTHER	0x02
253#define SYM_SEARCH_NEXT		0x04
254/* warnnotfound */
255#define SYM_NOWARNNOTFOUND	0x00
256#define SYM_WARNNOTFOUND	0x10
257/* inplt */
258#define SYM_NOTPLT		0x00
259#define SYM_PLT			0x20
260
261#define SYM_DLSYM		0x40
262
263int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
264int _dl_rtld(elf_object_t *object);
265void _dl_call_init(elf_object_t *object);
266void _dl_link_child(elf_object_t *dep, elf_object_t *p);
267void _dl_link_grpsym(elf_object_t *object, int checklist);
268void _dl_cache_grpsym_list(elf_object_t *object);
269void _dl_cache_grpsym_list_setup(elf_object_t *object);
270void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
271void _dl_link_dlopen(elf_object_t *dep);
272void _dl_unlink_dlopen(elf_object_t *dep);
273void _dl_notify_unload_shlib(elf_object_t *object);
274void _dl_unload_shlib(elf_object_t *object);
275void _dl_unload_dlopen(void);
276
277void _dl_run_all_dtors(void);
278
279int	_dl_match_file(struct sod *sodp, const char *name, int namelen);
280char	*_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints);
281void	_dl_load_list_free(struct load_list *load_list);
282
283typedef void lock_cb(int);
284void	_dl_thread_kern_go(lock_cb *);
285lock_cb	*_dl_thread_kern_stop(void);
286
287char	*_dl_getenv(const char *, char **);
288void	_dl_unsetenv(const char *, char **);
289
290void	_dl_trace_setup(char **);
291void	_dl_trace_object_setup(elf_object_t *);
292int	_dl_trace_plt(const elf_object_t *, const char *);
293
294/* tib.c */
295void	_dl_allocate_tls_offsets(void);
296void	_dl_allocate_first_tib(void);
297void	_dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr,
298	    const char *_libname);
299extern int _dl_tib_static_done;
300
301extern elf_object_t *_dl_objects;
302extern elf_object_t *_dl_last_object;
303
304extern elf_object_t *_dl_loading_object;
305
306extern struct r_debug *_dl_debug_map;
307
308extern int  _dl_pagesz;
309extern int  _dl_errno;
310
311extern char **_dl_libpath;
312
313extern int _dl_bindnow;
314extern int _dl_traceld;
315extern int _dl_debug;
316
317extern char *_dl_preload;
318extern char *_dl_tracefmt1;
319extern char *_dl_tracefmt2;
320extern char *_dl_traceprog;
321
322extern int _dl_trust;
323
324#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
325
326#define	DL_NOT_FOUND		1
327#define	DL_CANT_OPEN		2
328#define	DL_NOT_ELF		3
329#define	DL_CANT_OPEN_REF	4
330#define	DL_CANT_MMAP		5
331#define	DL_NO_SYMBOL		6
332#define	DL_INVALID_HANDLE	7
333#define	DL_INVALID_CTL		8
334#define	DL_NO_OBJECT		9
335#define	DL_CANT_FIND_OBJ	10
336#define	DL_CANT_LOAD_OBJ	11
337#define	DL_INVALID_MODE		12
338
339#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
340#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
341
342/* symbol lookup cache */
343typedef struct sym_cache {
344	const elf_object_t *obj;
345	const Elf_Sym	*sym;
346	int flags;
347} sym_cache;
348
349extern sym_cache *_dl_symcache;
350extern int _dl_symcachestat_hits;
351extern int _dl_symcachestat_lookups;
352TAILQ_HEAD(dlochld, dep_node);
353extern struct dlochld _dlopened_child_list;
354__END_HIDDEN_DECLS
355
356#endif /* _RESOLVE_H_ */
357