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