1 /**	$MirOS: src/libexec/ld.so/resolve.h,v 1.2 2006/08/30 04:28:25 tg Exp $ */
2 /*	$OpenBSD: resolve.h,v 1.52 2006/06/16 21:34:53 kettenis Exp $ */
3 
4 /*
5  * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef _RESOLVE_H_
31 #define _RESOLVE_H_
32 
33 #include <sys/queue.h>
34 #include <link.h>
35 #include <dlfcn.h>
36 #include <stdbool.h>
37 
38 struct load_list {
39 	struct load_list *next;
40 	void		*start;
41 	size_t		size;
42 	int		prot;
43 	Elf_Addr	moff;
44 	long		foff;
45 };
46 
47 /*
48  *  Structure describing a loaded object.
49  *  The head of this struct must be compatible
50  *  with struct link_map in sys/link.h
51  */
52 typedef struct elf_object elf_object_t;
53 struct elf_object {
54 	Elf_Addr load_addr;		/* Real load address */
55 	char	*load_name;		/* Pointer to object name */
56 	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
57 	struct elf_object *next;
58 	struct elf_object *prev;
59 /* End struct link_map compatible */
60 	Elf_Addr load_offs;		/* Load offset from link address */
61 
62 	struct load_list *load_list;
63 
64 	u_int32_t  load_size;
65 	Elf_Addr	got_addr;
66 	Elf_Addr	got_start;
67 	size_t		got_size;
68 	Elf_Addr	plt_start;
69 	size_t		plt_size;
70 
71 	union {
72 		u_long		info[DT_NUM + DT_PROCNUM];
73 		struct {
74 			Elf_Addr	null;		/* Not used */
75 			Elf_Addr	needed;		/* Not used */
76 			Elf_Addr	pltrelsz;
77 			Elf_Addr	*pltgot;
78 			Elf_Addr	*hash;
79 			const char	*strtab;
80 			const Elf_Sym	*symtab;
81 			Elf_RelA	*rela;
82 			Elf_Addr	relasz;
83 			Elf_Addr	relaent;
84 			Elf_Addr	strsz;
85 			Elf_Addr	syment;
86 			void		(*init)(void);
87 			void		(*fini)(void);
88 			const char	*soname;
89 			const char	*rpath;
90 			Elf_Addr	symbolic;
91 			Elf_Rel	*rel;
92 			Elf_Addr	relsz;
93 			Elf_Addr	relent;
94 			Elf_Addr	pltrel;
95 			Elf_Addr	debug;
96 			Elf_Addr	textrel;
97 			Elf_Addr	jmprel;
98 		} u;
99 	} Dyn;
100 #define dyn Dyn.u
101 
102 	int		status;
103 #define	STAT_RELOC_DONE	0x01
104 #define	STAT_GOT_DONE	0x02
105 #define	STAT_INIT_DONE	0x04
106 #define	STAT_FINI_DONE	0x08
107 #define	STAT_FINI_READY	0x10
108 #define	STAT_UNLOADED	0x20
109 
110 	Elf_Phdr	*phdrp;
111 	int		phdrc;
112 
113 	int		obj_type;
114 #define	OBJTYPE_LDR	1
115 #define	OBJTYPE_EXE	2
116 #define	OBJTYPE_LIB	3
117 #define	OBJTYPE_DLO	4
118 	int		obj_flags;
119 
120 	Elf_Word	*buckets;
121 	u_int32_t	nbuckets;
122 	Elf_Word	*chains;
123 	u_int32_t	nchains;
124 	Elf_Dyn		*dynamic;
125 
126 	TAILQ_HEAD(,dep_node)	child_list;	/* direct dep libs of object */
127 	TAILQ_HEAD(,dep_node)	grpsym_list;	/* ordered complete dep list */
128 	TAILQ_HEAD(,dep_node)	grpref_list;	/* refs to other load groups */
129 
130 	int		refcount;	/* dep libs only */
131 	int		opencount;	/* # dlopen() & exe */
132 	int		grprefcount;	/* load group refs */
133 #define OBJECT_REF_CNT(object) \
134     ((object->refcount + object->opencount + object->grprefcount))
135 #define OBJECT_DLREF_CNT(object) \
136     ((object->opencount + object->grprefcount))
137 
138 	/* object that caused this module to be loaded, used in symbol lookup */
139 	elf_object_t	*load_object;
140 
141 	void *prebind_data;
142 
143 	/* for object confirmation */
144 	dev_t	dev;
145 	ino_t inode;
146 };
147 
148 struct dep_node {
149 	TAILQ_ENTRY(dep_node) next_sib;
150 	elf_object_t *data;
151 };
152 
153 void _dl_rt_resolve(void);
154 
155 void _dl_add_object(elf_object_t *object);
156 elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
157     const long *, const int objtype, const long laddr, const long loff);
158 void	_dl_remove_object(elf_object_t *object);
159 void	_dl_cleanup_objects(void);
160 
161 elf_object_t *_dl_lookup_object(const char *objname);
162 elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
163 elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags);
164 
165 int  _dl_md_reloc(elf_object_t *object, int rel, int relsz);
166 void _dl_md_reloc_got(elf_object_t *object, int lazy);
167 
168 Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this,
169     int flags, const Elf_Sym *ref_sym, elf_object_t *object,
170     const elf_object_t **pobj);
171 Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
172     const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym,
173     const elf_object_t **pobj);
174 /*
175  * defines for _dl_find_symbol() flag field, three bits of meaning
176  * myself	- clear: search all objects,	set: search only this object
177  * warnnotfound - clear: no warning,		set: warn if not found
178  * inplt	- clear: possible plt ref	set: real matching function.
179  *
180  * inplt - due to how ELF handles function addresses in shared libraries
181  * &func may actually refer to the plt entry in the main program
182  * rather than the actual function address in the .so file.
183  * This rather bizarre behavior is documented in the SVR4 ABI.
184  * when getting the function address to relocate a PLT entry
185  * the 'real' function address is necessary, not the possible PLT address.
186  */
187 /* myself */
188 #define SYM_SEARCH_ALL		0x00
189 #define SYM_SEARCH_SELF		0x01
190 #define SYM_SEARCH_OTHER	0x02
191 #define SYM_SEARCH_NEXT		0x04
192 #define SYM_SEARCH_OBJ		0x08
193 /* warnnotfound */
194 #define SYM_NOWARNNOTFOUND	0x00
195 #define SYM_WARNNOTFOUND	0x10
196 /* inplt */
197 #define SYM_NOTPLT		0x00
198 #define SYM_PLT			0x20
199 
200 #define SYM_DLSYM		0x40
201 
202 int _dl_load_dep_libs(elf_object_t *object, int flags, int booting);
203 int _dl_rtld(elf_object_t *object);
204 void _dl_call_init(elf_object_t *object);
205 void _dl_link_child(elf_object_t *dep, elf_object_t *p);
206 void _dl_link_grpsym(elf_object_t *object);
207 void _dl_cache_grpsym_list(elf_object_t *object);
208 void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object);
209 void _dl_link_dlopen(elf_object_t *dep);
210 void _dl_unlink_dlopen(elf_object_t *dep);
211 void _dl_notify_unload_shlib(elf_object_t *object);
212 void _dl_unload_shlib(elf_object_t *object);
213 void _dl_unload_dlopen(void);
214 
215 void _dl_run_all_dtors(void);
216 
217 /* Please don't rename; gdb(1) knows about this. */
218 Elf_Addr _dl_bind(elf_object_t *object, int index);
219 
220 int	_dl_match_file(struct sod *sodp, char *name, int namelen);
221 char	*_dl_find_shlib(struct sod *sodp, const char *searchpath, int nohints);
222 void	_dl_load_list_free(struct load_list *load_list);
223 
224 void	_dl_thread_kern_go(void);
225 void	_dl_thread_kern_stop(void);
226 
227 extern elf_object_t *_dl_objects;
228 extern elf_object_t *_dl_last_object;
229 
230 extern elf_object_t *_dl_loading_object;
231 
232 extern const char *_dl_progname;
233 extern struct r_debug *_dl_debug_map;
234 
235 extern int  _dl_pagesz;
236 extern int  _dl_errno;
237 
238 extern char *_dl_libpath;
239 extern char *_dl_preload;
240 extern char *_dl_bindnow;
241 extern bool _dl_traceld;
242 extern char *_dl_debug;
243 
244 #define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
245 
246 #define	DL_NOT_FOUND		1
247 #define	DL_CANT_OPEN		2
248 #define	DL_NOT_ELF		3
249 #define	DL_CANT_OPEN_REF	4
250 #define	DL_CANT_MMAP		5
251 #define	DL_NO_SYMBOL		6
252 #define	DL_INVALID_HANDLE	7
253 #define	DL_INVALID_CTL		8
254 #define	DL_NO_OBJECT		9
255 #define	DL_CANT_FIND_OBJ	10
256 #define	DL_CANT_LOAD_OBJ	11
257 
258 #define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
259 #define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
260 
261 /* symbol lookup cache */
262 typedef struct sym_cache {
263 	const elf_object_t *obj;
264 	const Elf_Sym	*sym;
265 	int flags;
266 } sym_cache;
267 
268 extern sym_cache *_dl_symcache;
269 extern int _dl_symcachestat_hits;
270 extern int _dl_symcachestat_lookups;
271 TAILQ_HEAD(dlochld, dep_node);
272 extern struct dlochld _dlopened_child_list;
273 
274 
275 #endif /* _RESOLVE_H_ */
276