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