resolve.h revision 1.87
1/*	$OpenBSD: resolve.h,v 1.87 2018/11/28 03:18:00 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	Elf_Dyn		*dynamic;
163
164	TAILQ_HEAD(,dep_node)	child_list;	/* direct dep libs of object */
165	TAILQ_HEAD(,dep_node)	grpsym_list;	/* ordered complete dep list */
166	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
167
168	int		refcount;	/* dep libs only */
169	int		opencount;	/* # dlopen() & exe */
170	int		grprefcount;	/* load group refs */
171#define OBJECT_REF_CNT(object) \
172    ((object->refcount + object->opencount + object->grprefcount))
173#define OBJECT_DLREF_CNT(object) \
174    ((object->opencount + object->grprefcount))
175
176	/* object that caused this module to be loaded, used in symbol lookup */
177	elf_object_t	*load_object;
178	struct sod	sod;
179
180	/* for object confirmation */
181	dev_t	dev;
182	ino_t inode;
183
184	/* thread local storage info */
185	Elf_Addr	tls_fsize;
186	Elf_Addr	tls_msize;
187	Elf_Addr	tls_align;
188	const void	*tls_static_data;
189	int		tls_offset;
190
191	/* relro bits */
192	Elf_Addr	relro_addr;
193	Elf_Addr	relro_size;
194
195	/* generation number of last grpsym insert on this object */
196	unsigned int grpsym_gen;
197
198	char **rpath;
199	char **runpath;
200
201	/* nonzero if trace enabled for this object */
202	int traced;
203};
204
205struct dep_node {
206	TAILQ_ENTRY(dep_node) next_sib;
207	elf_object_t *data;
208};
209
210
211/* Please don't rename or make hidden; gdb(1) knows about these. */
212Elf_Addr _dl_bind(elf_object_t *object, int index);
213void	_dl_debug_state(void);
214
215/* exported to the application */
216extern char *__progname;
217
218__BEGIN_HIDDEN_DECLS
219void _dl_add_object(elf_object_t *object);
220elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
221    Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase,
222    const long obase);
223void	_dl_remove_object(elf_object_t *object);
224void	_dl_cleanup_objects(void);
225
226elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
227elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
228
229int _dl_md_reloc(elf_object_t *object, int rel, int relsz);
230int _dl_md_reloc_got(elf_object_t *object, int lazy);
231
232Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this,
233    int flags, const Elf_Sym *ref_sym, elf_object_t *object,
234    const elf_object_t **pobj);
235Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
236    const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym,
237    const elf_object_t **pobj);
238/*
239 * defines for _dl_find_symbol() flag field, three bits of meaning
240 * myself	- clear: search all objects,	set: search only this object
241 * warnnotfound - clear: no warning,		set: warn if not found
242 * inplt	- clear: possible plt ref	set: real matching function.
243 *
244 * inplt - due to how ELF handles function addresses in shared libraries
245 * &func may actually refer to the plt entry in the main program
246 * rather than the actual function address in the .so file.
247 * This rather bizarre behavior is documented in the SVR4 ABI.
248 * when getting the function address to relocate a PLT entry
249 * the 'real' function address is necessary, not the possible PLT address.
250 */
251/* myself */
252#define SYM_SEARCH_ALL		0x00
253#define SYM_SEARCH_SELF		0x01
254#define SYM_SEARCH_OTHER	0x02
255#define SYM_SEARCH_NEXT		0x04
256/* warnnotfound */
257#define SYM_NOWARNNOTFOUND	0x00
258#define SYM_WARNNOTFOUND	0x10
259/* inplt */
260#define SYM_NOTPLT		0x00
261#define SYM_PLT			0x20
262
263#define SYM_DLSYM		0x40
264
265int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
266int _dl_rtld(elf_object_t *object);
267void _dl_call_init(elf_object_t *object);
268void _dl_link_child(elf_object_t *dep, elf_object_t *p);
269void _dl_link_grpsym(elf_object_t *object, int checklist);
270void _dl_cache_grpsym_list(elf_object_t *object);
271void _dl_cache_grpsym_list_setup(elf_object_t *object);
272void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
273void _dl_link_dlopen(elf_object_t *dep);
274void _dl_unlink_dlopen(elf_object_t *dep);
275void _dl_notify_unload_shlib(elf_object_t *object);
276void _dl_unload_shlib(elf_object_t *object);
277void _dl_unload_dlopen(void);
278
279void _dl_run_all_dtors(void);
280
281int	_dl_match_file(struct sod *sodp, const char *name, int namelen);
282char	*_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints);
283void	_dl_load_list_free(struct load_list *load_list);
284
285typedef void lock_cb(int);
286void	_dl_thread_kern_go(lock_cb *);
287lock_cb	*_dl_thread_kern_stop(void);
288
289char	*_dl_getenv(const char *, char **);
290void	_dl_unsetenv(const char *, char **);
291
292void	_dl_trace_setup(char **);
293void	_dl_trace_object_setup(elf_object_t *);
294int	_dl_trace_plt(const elf_object_t *, const char *);
295
296/* tib.c */
297void	_dl_allocate_tls_offsets(void);
298void	_dl_allocate_first_tib(void);
299void	_dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr,
300	    const char *_libname);
301extern int _dl_tib_static_done;
302
303extern elf_object_t *_dl_objects;
304extern elf_object_t *_dl_last_object;
305
306extern elf_object_t *_dl_loading_object;
307
308extern struct r_debug *_dl_debug_map;
309
310extern int  _dl_pagesz;
311extern int  _dl_errno;
312
313extern char **_dl_libpath;
314
315extern int _dl_bindnow;
316extern int _dl_traceld;
317extern int _dl_debug;
318
319extern char *_dl_preload;
320extern char *_dl_tracefmt1;
321extern char *_dl_tracefmt2;
322extern char *_dl_traceprog;
323
324extern int _dl_trust;
325
326#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
327
328#define	DL_NOT_FOUND		1
329#define	DL_CANT_OPEN		2
330#define	DL_NOT_ELF		3
331#define	DL_CANT_OPEN_REF	4
332#define	DL_CANT_MMAP		5
333#define	DL_NO_SYMBOL		6
334#define	DL_INVALID_HANDLE	7
335#define	DL_INVALID_CTL		8
336#define	DL_NO_OBJECT		9
337#define	DL_CANT_FIND_OBJ	10
338#define	DL_CANT_LOAD_OBJ	11
339#define	DL_INVALID_MODE		12
340
341#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
342#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
343
344/* symbol lookup cache */
345typedef struct sym_cache {
346	const elf_object_t *obj;
347	const Elf_Sym	*sym;
348	int flags;
349} sym_cache;
350
351extern sym_cache *_dl_symcache;
352extern int _dl_symcachestat_hits;
353extern int _dl_symcachestat_lookups;
354TAILQ_HEAD(dlochld, dep_node);
355extern struct dlochld _dlopened_child_list;
356__END_HIDDEN_DECLS
357
358#endif /* _RESOLVE_H_ */
359