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