xref: /openbsd-src/libexec/ld.so/resolve.h (revision 3072fcbcde2672307d58a380a3bf9908a37a1e67)
1 /*	$OpenBSD: resolve.h,v 1.35 2005/04/06 00:16:53 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 <link.h>
33 #include <dlfcn.h>
34 
35 struct load_list {
36 	struct load_list *next;
37 	void		*start;
38 	size_t		size;
39 	int		prot;
40 	Elf_Addr	moff;
41 	long		foff;
42 };
43 
44 /*
45  *  Structure describing a loaded object.
46  *  The head of this struct must be compatible
47  *  with struct link_map in sys/link.h
48  */
49 typedef struct elf_object {
50 	Elf_Addr load_addr;		/* Real load address */
51 	char	*load_name;		/* Pointer to object name */
52 	Elf_Dyn *load_dyn;		/* Pointer to object dynamic data */
53 	struct elf_object *next;
54 	struct elf_object *prev;
55 /* End struct link_map compatible */
56 	Elf_Addr load_offs;		/* Load offset from link address */
57 
58 	struct load_list *load_list;
59 
60 	u_int32_t  load_size;
61 	Elf_Addr	got_addr;
62 	Elf_Addr	got_start;
63 	size_t		got_size;
64 	Elf_Addr	plt_start;
65 	size_t		plt_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 		} u;
96 	} Dyn;
97 #define dyn Dyn.u
98 
99 	struct elf_object *dep_next;	/* Shadow objects for resolve search */
100 
101 	int		status;
102 #define	STAT_RELOC_DONE	1
103 #define	STAT_GOT_DONE	2
104 #define	STAT_INIT_DONE	4
105 
106 	Elf_Phdr	*phdrp;
107 	int		phdrc;
108 
109 	int		refcount;
110 	int		obj_type;
111 #define	OBJTYPE_LDR	1
112 #define	OBJTYPE_EXE	2
113 #define	OBJTYPE_LIB	3
114 #define	OBJTYPE_DLO	4
115 	int		obj_flags;
116 
117 	Elf_Word	*buckets;
118 	u_int32_t	nbuckets;
119 	Elf_Word	*chains;
120 	u_int32_t	nchains;
121 	Elf_Dyn	*dynamic;
122 
123 	struct dep_node *first_child;
124 	struct dep_node *last_child;
125 
126 	/* for object confirmation */
127 	dev_t	dev;
128 	ino_t inode;
129 } elf_object_t;
130 
131 struct dep_node {
132 	struct dep_node *next_sibling;
133 	elf_object_t *data;
134 };
135 
136 extern void _dl_rt_resolve(void);
137 
138 void _dl_add_object(elf_object_t *object);
139 extern elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp,
140     const u_long *, const int objtype, const long laddr, const long loff);
141 extern void	_dl_remove_object(elf_object_t *object);
142 
143 extern elf_object_t *_dl_lookup_object(const char *objname);
144 extern elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int);
145 extern void	_dl_unload_shlib(elf_object_t *object);
146 elf_object_t *_dl_tryload_shlib(const char *libname, int type);
147 
148 extern int  _dl_md_reloc(elf_object_t *object, int rel, int relsz);
149 extern void _dl_md_reloc_got(elf_object_t *object, int lazy);
150 
151 Elf_Addr _dl_find_symbol(const char *name, elf_object_t *startlook,
152     const Elf_Sym **ref, const elf_object_t **pobj,
153     int flags, int sym_size, elf_object_t *object);
154 Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
155     elf_object_t *startlook, const Elf_Sym **ref, const elf_object_t **pobj,
156     int flags, int req_size);
157 /*
158  * defines for _dl_find_symbol() flag field, three bits of meaning
159  * myself	- clear: search all objects,	set: search only this object
160  * warnnotfound - clear: no warning,		set: warn if not found
161  * inplt	- clear: possible plt ref	set: real matching function.
162  *
163  * inplt - due to how ELF handles function addresses in shared libraries
164  * &func may actually refer to the plt entry in the main program
165  * rather than the actual function address in the .so file.
166  * This rather bizarre behavior is documented in the SVR4 ABI.
167  * when getting the function address to relocate a PLT entry
168  * the 'real' function address is necessary, not the possible PLT address.
169  */
170 /* myself */
171 #define SYM_SEARCH_ALL		0
172 #define SYM_SEARCH_SELF		1
173 /* warnnotfound */
174 #define SYM_NOWARNNOTFOUND	0
175 #define SYM_WARNNOTFOUND	2
176 /* inplt */
177 #define SYM_NOTPLT		0
178 #define SYM_PLT			4
179 
180 void _dl_rtld(elf_object_t *object);
181 void _dl_call_init(elf_object_t *object);
182 void _dl_link_sub(elf_object_t *dep, elf_object_t *p);
183 
184 void _dl_run_dtors(elf_object_t *object);
185 
186 Elf_Addr _dl_bind(elf_object_t *object, int index);
187 
188 int	_dl_match_file(struct sod *sodp, char *name, int namelen);
189 char	*_dl_find_shlib(struct sod *sodp, const char *searchpath, int nohints);
190 void	_dl_load_list_free(struct load_list *load_list);
191 
192 extern elf_object_t *_dl_objects;
193 extern elf_object_t *_dl_last_object;
194 
195 extern const char *_dl_progname;
196 extern struct r_debug *_dl_debug_map;
197 
198 extern int  _dl_pagesz;
199 extern int  _dl_errno;
200 
201 extern char *_dl_libpath;
202 extern char *_dl_preload;
203 extern char *_dl_bindnow;
204 extern char *_dl_traceld;
205 extern char *_dl_debug;
206 
207 #define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0)
208 
209 #define	DL_NOT_FOUND		1
210 #define	DL_CANT_OPEN		2
211 #define	DL_NOT_ELF		3
212 #define	DL_CANT_OPEN_REF	4
213 #define	DL_CANT_MMAP		5
214 #define	DL_NO_SYMBOL		6
215 #define	DL_INVALID_HANDLE	7
216 #define	DL_INVALID_CTL		8
217 #define	DL_NO_OBJECT		9
218 #define	DL_CANT_FIND_OBJ	10
219 
220 #define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1))
221 #define ELF_TRUNC(x,malign) ((x) & ~((malign)-1))
222 
223 /* symbol lookup cache */
224 typedef struct sym_cache {
225 	const elf_object_t *obj;
226 	const Elf_Sym	*sym;
227 	int flags;
228 } sym_cache;
229 
230 extern sym_cache *_dl_symcache;
231 
232 #endif /* _RESOLVE_H_ */
233