resolve.h revision 1.98
1/*	$OpenBSD: resolve.h,v 1.98 2021/06/02 07:29:03 semarie 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
71typedef struct elf_object elf_object_t;
72
73struct object_vector {
74	int		len;
75	int		alloc;
76	elf_object_t 	**vec;
77};
78void	object_vec_grow(struct object_vector *_vec, int _more);
79
80/*
81 *  Structure describing a loaded object.
82 *  The head of this struct must be compatible
83 *  with struct link_map in sys/link.h
84 */
85struct elf_object {
86	Elf_Addr obj_base;		/* object's address '0' base */
87	char	*load_name;		/* Pointer to object name */
88	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
89	struct elf_object *next;
90	struct elf_object *prev;
91/* End struct link_map compatible */
92	Elf_Addr load_base;		/* Base address of loadable segments */
93
94	struct load_list *load_list;
95
96	u_int32_t  load_size;
97
98	union {
99		u_long		info[DT_NUM + DT_PROCNUM];
100		struct {
101			Elf_Addr	null;		/* Not used */
102			Elf_Addr	needed;		/* Not used */
103			Elf_Addr	pltrelsz;
104			Elf_Addr	*pltgot;
105			Elf_Addr	*hash;
106			const char	*strtab;
107			const Elf_Sym	*symtab;
108			Elf_RelA	*rela;
109			Elf_Addr	relasz;
110			Elf_Addr	relaent;
111			Elf_Addr	strsz;
112			Elf_Addr	syment;
113			initfunc	*init;
114			initfunc	*fini;
115			const char	*soname;
116			const char	*rpath;
117			Elf_Addr	symbolic;
118			Elf_Rel		*rel;
119			Elf_Addr	relsz;
120			Elf_Addr	relent;
121			Elf_Addr	pltrel;
122			Elf_Addr	debug;
123			Elf_Addr	textrel;
124			Elf_Addr	jmprel;
125			Elf_Addr	bind_now;
126			initarrayfunc	**init_array;
127			initfunc	**fini_array;
128			Elf_Addr	init_arraysz;
129			Elf_Addr	fini_arraysz;
130			const char	*runpath;
131			Elf_Addr	flags;
132			Elf_Addr	encoding;
133			initarrayfunc	**preinit_array;
134			Elf_Addr	preinit_arraysz;
135		} u;
136	} Dyn;
137#define dyn Dyn.u
138
139	Elf_Addr	relacount;	/* DT_RELACOUNT */
140	Elf_Addr	relcount;	/* DT_RELCOUNT */
141
142	int		status;
143#define	STAT_RELOC_DONE		0x001
144#define	STAT_GOT_DONE		0x002
145#define	STAT_INIT_DONE		0x004
146#define	STAT_FINI_DONE		0x008
147#define	STAT_FINI_READY		0x010
148#define	STAT_UNLOADED		0x020
149#define	STAT_NODELETE		0x040
150#define	STAT_GNU_HASH		0x080
151#define	STAT_VISIT_INITFIRST	0x100
152#define	STAT_VISIT_INIT		0x200
153
154	Elf_Phdr	*phdrp;
155	int		phdrc;
156
157	int		obj_type;
158#define	OBJTYPE_LDR	1
159#define	OBJTYPE_EXE	2
160#define	OBJTYPE_LIB	3
161#define	OBJTYPE_DLO	4
162	int		obj_flags;	/* c.f. <sys/exec_elf.h> DF_1_* */
163
164	/* shared by ELF and GNU hash */
165	u_int32_t	nbuckets;
166	u_int32_t	nchains;	/* really, number of symbols */
167	union {
168		struct {
169			/* specific to ELF hash */
170			const Elf_Hash_Word	*buckets;
171			const Elf_Hash_Word	*chains;
172		} u_elf;
173		struct {
174			/* specific to GNU hash */
175			const Elf_Word		*buckets;
176			const Elf_Word		*chains;
177			const Elf_Addr		*bloom;
178			Elf_Word		mask_bm;
179			Elf_Word		shift2;
180			Elf_Word		symndx;
181		} u_gnu;
182	} hash_u;
183#define buckets_elf	hash_u.u_elf.buckets
184#define chains_elf	hash_u.u_elf.chains
185#define buckets_gnu	hash_u.u_gnu.buckets
186#define chains_gnu	hash_u.u_gnu.chains
187#define bloom_gnu	hash_u.u_gnu.bloom
188#define mask_bm_gnu	hash_u.u_gnu.mask_bm
189#define shift2_gnu	hash_u.u_gnu.shift2
190#define symndx_gnu	hash_u.u_gnu.symndx
191
192	struct object_vector	child_vec;	/* direct dep libs of object */
193	struct object_vector	grpsym_vec;	/* ordered complete dep list */
194	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
195
196	int		refcount;	/* dep libs only */
197	int		opencount;	/* # dlopen() & exe */
198	int		grprefcount;	/* load group refs */
199#define OBJECT_REF_CNT(object) \
200    ((object->refcount + object->opencount + object->grprefcount))
201#define OBJECT_DLREF_CNT(object) \
202    ((object->opencount + object->grprefcount))
203
204	/* object that caused this module to be loaded, used in symbol lookup */
205	elf_object_t	*load_object;
206	struct sod	sod;
207
208	/* for object confirmation */
209	dev_t	dev;
210	ino_t inode;
211
212	/* thread local storage info */
213	Elf_Addr	tls_fsize;
214	Elf_Addr	tls_msize;
215	Elf_Addr	tls_align;
216	const void	*tls_static_data;
217	int		tls_offset;
218
219	/* relro bits */
220	Elf_Addr	relro_addr;
221	Elf_Addr	relro_size;
222
223	/* generation number of last grpsym insert on this object */
224	unsigned int grpsym_gen;
225
226	char **rpath;
227	char **runpath;
228
229	/* nonzero if trace enabled for this object */
230	int traced;
231};
232
233struct dep_node {
234	TAILQ_ENTRY(dep_node) next_sib;
235	elf_object_t *data;
236};
237
238
239/* Please don't rename or make hidden; gdb(1) knows about these. */
240Elf_Addr _dl_bind(elf_object_t *object, int index);
241void	_dl_debug_state(void);
242
243/* exported to the application */
244extern char *__progname;
245
246__BEGIN_HIDDEN_DECLS
247void _dl_handle_nodelete(elf_object_t *_object);
248void _dl_add_object(elf_object_t *object);
249elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
250    Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase,
251    const long obase);
252void	_dl_remove_object(elf_object_t *object);
253void	_dl_cleanup_objects(void);
254
255elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
256elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
257
258int _dl_md_reloc(elf_object_t *object, int rel, int relsz);
259int _dl_md_reloc_got(elf_object_t *object, int lazy);
260
261struct sym_res {
262	const Elf_Sym		*sym;
263	const elf_object_t	*obj;
264};
265
266struct sym_res _dl_find_symbol(const char *name, int flags,
267    const Elf_Sym *ref_sym, elf_object_t *object);
268
269/*
270 * defines for _dl_find_symbol() flag field, three bits of meaning
271 * myself	- clear: search all objects,	set: search only this object
272 * warnnotfound - clear: no warning,		set: warn if not found
273 * inplt	- clear: possible plt ref	set: real matching function.
274 *
275 * inplt - due to how ELF handles function addresses in shared libraries
276 * &func may actually refer to the plt entry in the main program
277 * rather than the actual function address in the .so file.
278 * This rather bizarre behavior is documented in the SVR4 ABI.
279 * when getting the function address to relocate a PLT entry
280 * the 'real' function address is necessary, not the possible PLT address.
281 */
282/* myself */
283#define SYM_SEARCH_ALL		0x00
284#define SYM_SEARCH_SELF		0x01
285#define SYM_SEARCH_OTHER	0x02
286#define SYM_SEARCH_NEXT		0x04
287/* warnnotfound */
288#define SYM_NOWARNNOTFOUND	0x00
289#define SYM_WARNNOTFOUND	0x10
290/* inplt */
291#define SYM_NOTPLT		0x00
292#define SYM_PLT			0x20
293
294#define SYM_DLSYM		0x40
295
296int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
297int _dl_rtld(elf_object_t *object);
298void _dl_call_init(elf_object_t *object);
299void _dl_link_child(elf_object_t *dep, elf_object_t *p);
300void _dl_link_grpsym(elf_object_t *object);
301void _dl_cache_grpsym_list_setup(elf_object_t *_object);
302void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
303void _dl_link_dlopen(elf_object_t *dep);
304void _dl_unlink_dlopen(elf_object_t *dep);
305void _dl_notify_unload_shlib(elf_object_t *object);
306void _dl_unload_shlib(elf_object_t *object);
307void _dl_unload_dlopen(void);
308
309void _dl_run_all_dtors(void);
310
311int	_dl_match_file(struct sod *sodp, const char *name, int namelen);
312char	*_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints);
313void	_dl_load_list_free(struct load_list *load_list);
314
315typedef void lock_cb(int);
316void	_dl_thread_kern_go(lock_cb *);
317lock_cb	*_dl_thread_kern_stop(void);
318
319char	*_dl_getenv(const char *, char **) __boot;
320void	_dl_unsetenv(const char *, char **) __boot;
321
322void	_dl_trace_setup(char **) __boot;
323void	_dl_trace_object_setup(elf_object_t *);
324int	_dl_trace_plt(const elf_object_t *, const char *);
325
326/* tib.c */
327void	_dl_allocate_tls_offsets(void) __boot;
328void	_dl_allocate_first_tib(void) __boot;
329void	_dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr,
330	    const char *_libname);
331extern int _dl_tib_static_done;
332
333extern elf_object_t *_dl_objects;
334extern int object_count;	/* how many objects are currently loaded */
335
336extern elf_object_t *_dl_loading_object;
337
338extern struct r_debug *_dl_debug_map;
339
340extern int  _dl_pagesz;
341extern int  _dl_errno;
342
343extern char **_dl_libpath;
344
345extern int _dl_bindnow;
346extern int _dl_traceld;
347extern int _dl_debug;
348
349extern char *_dl_preload;
350extern char *_dl_tracefmt1;
351extern char *_dl_tracefmt2;
352extern char *_dl_traceprog;
353
354extern void *_dl_exec_hint;
355
356extern int _dl_trust;
357
358#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
359
360#define	DL_NOT_FOUND		1
361#define	DL_CANT_OPEN		2
362#define	DL_NOT_ELF		3
363#define	DL_CANT_OPEN_REF	4
364#define	DL_CANT_MMAP		5
365#define	DL_NO_SYMBOL		6
366#define	DL_INVALID_HANDLE	7
367#define	DL_INVALID_CTL		8
368#define	DL_NO_OBJECT		9
369#define	DL_CANT_FIND_OBJ	10
370#define	DL_CANT_LOAD_OBJ	11
371#define	DL_INVALID_MODE		12
372
373#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
374#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
375
376/* symbol lookup cache */
377typedef struct sym_cache {
378	const elf_object_t *obj;
379	const Elf_Sym	*sym;
380	int flags;
381} sym_cache;
382
383TAILQ_HEAD(dlochld, dep_node);
384extern struct dlochld _dlopened_child_list;
385__END_HIDDEN_DECLS
386
387#endif /* _RESOLVE_H_ */
388