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