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