xref: /openbsd-src/libexec/ld.so/resolve.h (revision 50cabf597b9f08e4a6ce9f3b1cc5275048a494e2)
1 /*	$OpenBSD: resolve.h,v 1.44 2005/10/06 21:53:10 kurt 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 struct load_list {
37 	struct load_list *next;
38 	void		*start;
39 	size_t		size;
40 	int		prot;
41 	Elf_Addr	moff;
42 	long		foff;
43 };
44 
45 /*
46  *  Structure describing a loaded object.
47  *  The head of this struct must be compatible
48  *  with struct link_map in sys/link.h
49  */
50 typedef struct elf_object elf_object_t;
51 struct elf_object {
52 	Elf_Addr load_addr;		/* Real load address */
53 	char	*load_name;		/* Pointer to object name */
54 	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
55 	struct elf_object *next;
56 	struct elf_object *prev;
57 /* End struct link_map compatible */
58 	Elf_Addr load_offs;		/* Load offset from link address */
59 
60 	struct load_list *load_list;
61 
62 	u_int32_t  load_size;
63 	Elf_Addr	got_addr;
64 	Elf_Addr	got_start;
65 	size_t		got_size;
66 	Elf_Addr	plt_start;
67 	size_t		plt_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 		} u;
98 	} Dyn;
99 #define dyn Dyn.u
100 
101 	struct elf_object *dep_next;	/* Shadow objects for resolve search */
102 
103 	int		status;
104 #define	STAT_RELOC_DONE	0x01
105 #define	STAT_GOT_DONE	0x02
106 #define	STAT_INIT_DONE	0x04
107 #define	STAT_FINI_DONE	0x08
108 #define	STAT_FINI_READY	0x10
109 #define	STAT_UNLOADED	0x20
110 
111 	Elf_Phdr	*phdrp;
112 	int		phdrc;
113 
114 	int		obj_type;
115 #define	OBJTYPE_LDR	1
116 #define	OBJTYPE_EXE	2
117 #define	OBJTYPE_LIB	3
118 #define	OBJTYPE_DLO	4
119 	int		obj_flags;
120 
121 	Elf_Word	*buckets;
122 	u_int32_t	nbuckets;
123 	Elf_Word	*chains;
124 	u_int32_t	nchains;
125 	Elf_Dyn		*dynamic;
126 
127 	TAILQ_HEAD(,dep_node)	child_list;
128 	TAILQ_HEAD(,dep_node)	dload_list;
129 	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
130 
131 	int		refcount;	/* dep libs only */
132 	int		opencount;	/* # dlopen() & exe */
133 	int		grprefcount;	/* load group refs */
134 
135 	/* object that caused this module to be loaded, used in symbol lookup */
136 	elf_object_t	*load_object;
137 
138 	/* for object confirmation */
139 	dev_t	dev;
140 	ino_t inode;
141 };
142 
143 struct dep_node {
144 	TAILQ_ENTRY(dep_node) next_sib;
145 	elf_object_t *data;
146 };
147 
148 void _dl_rt_resolve(void);
149 
150 void _dl_add_object(elf_object_t *object);
151 elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
152     const long *, const int objtype, const long laddr, const long loff);
153 void	_dl_remove_object(elf_object_t *object);
154 void	_dl_cleanup_objects(void);
155 
156 elf_object_t *_dl_lookup_object(const char *objname);
157 elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
158 elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
159 
160 int  _dl_md_reloc(elf_object_t *object, int rel, int relsz);
161 void _dl_md_reloc_got(elf_object_t *object, int lazy);
162 
163 Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this,
164     int flags, const Elf_Sym *ref_sym, elf_object_t *object,
165     const elf_object_t **pobj);
166 Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
167     const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym,
168     const elf_object_t **pobj);
169 /*
170  * defines for _dl_find_symbol() flag field, three bits of meaning
171  * myself	- clear: search all objects,	set: search only this object
172  * warnnotfound - clear: no warning,		set: warn if not found
173  * inplt	- clear: possible plt ref	set: real matching function.
174  *
175  * inplt - due to how ELF handles function addresses in shared libraries
176  * &func may actually refer to the plt entry in the main program
177  * rather than the actual function address in the .so file.
178  * This rather bizarre behavior is documented in the SVR4 ABI.
179  * when getting the function address to relocate a PLT entry
180  * the 'real' function address is necessary, not the possible PLT address.
181  */
182 /* myself */
183 #define SYM_SEARCH_ALL		0x00
184 #define SYM_SEARCH_SELF		0x01
185 #define SYM_SEARCH_OTHER	0x02
186 #define SYM_SEARCH_NEXT		0x04
187 #define SYM_SEARCH_OBJ		0x08
188 /* warnnotfound */
189 #define SYM_NOWARNNOTFOUND	0x00
190 #define SYM_WARNNOTFOUND	0x10
191 /* inplt */
192 #define SYM_NOTPLT		0x00
193 #define SYM_PLT			0x20
194 
195 #define SYM_DLSYM		0x40
196 
197 int _dl_rtld(elf_object_t *object);
198 void _dl_call_init(elf_object_t *object);
199 void _dl_link_sub(elf_object_t *dep, elf_object_t *p);
200 void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
201 void _dl_link_dlopen(elf_object_t *dep);
202 void _dl_unlink_dlopen(elf_object_t *dep);
203 void _dl_notify_unload_shlib(elf_object_t *object);
204 void _dl_unload_shlib(elf_object_t *object);
205 void _dl_unload_dlopen(void);
206 
207 void _dl_run_all_dtors(void);
208 
209 Elf_Addr _dl_bind(elf_object_t *object, int index);
210 
211 int	_dl_match_file(struct sod *sodp, char *name, int namelen);
212 char	*_dl_find_shlib(struct sod *sodp, const char *searchpath, int nohints);
213 void	_dl_load_list_free(struct load_list *load_list);
214 
215 void	_dl_thread_kern_go(void);
216 void	_dl_thread_kern_stop(void);
217 
218 extern elf_object_t *_dl_objects;
219 extern elf_object_t *_dl_last_object;
220 
221 extern elf_object_t *_dl_loading_object;
222 
223 extern const char *_dl_progname;
224 extern struct r_debug *_dl_debug_map;
225 
226 extern int  _dl_pagesz;
227 extern int  _dl_errno;
228 
229 extern char *_dl_libpath;
230 extern char *_dl_preload;
231 extern char *_dl_bindnow;
232 extern char *_dl_traceld;
233 extern char *_dl_debug;
234 
235 #define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
236 
237 #define	DL_NOT_FOUND		1
238 #define	DL_CANT_OPEN		2
239 #define	DL_NOT_ELF		3
240 #define	DL_CANT_OPEN_REF	4
241 #define	DL_CANT_MMAP		5
242 #define	DL_NO_SYMBOL		6
243 #define	DL_INVALID_HANDLE	7
244 #define	DL_INVALID_CTL		8
245 #define	DL_NO_OBJECT		9
246 #define	DL_CANT_FIND_OBJ	10
247 #define	DL_CANT_LOAD_OBJ	11
248 
249 #define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
250 #define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
251 
252 /* symbol lookup cache */
253 typedef struct sym_cache {
254 	const elf_object_t *obj;
255 	const Elf_Sym	*sym;
256 	int flags;
257 } sym_cache;
258 
259 extern sym_cache *_dl_symcache;
260 extern int _dl_symcachestat_hits;
261 extern int _dl_symcachestat_lookups;
262 TAILQ_HEAD(dlochld, dep_node);
263 extern struct dlochld _dlopened_child_list;
264 
265 
266 #endif /* _RESOLVE_H_ */
267