xref: /netbsd-src/libexec/ld.elf_so/rtld.h (revision b62fc9e20372b08e1785ff6d769312d209fa2005)
1 /*	$NetBSD: rtld.h,v 1.91 2010/04/05 14:01:26 joerg Exp $	 */
2 
3 /*
4  * Copyright 1996 John D. Polstra.
5  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by John Polstra.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef RTLD_H
35 #define RTLD_H
36 
37 #include <dlfcn.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <sys/exec_elf.h>
44 #include "rtldenv.h"
45 #include "link.h"
46 
47 #if defined(_RTLD_SOURCE)
48 
49 #ifndef	RTLD_DEFAULT_LIBRARY_PATH
50 #define	RTLD_DEFAULT_LIBRARY_PATH	"/usr/lib"
51 #endif
52 #define _PATH_LD_HINTS			"/etc/ld.so.conf"
53 
54 extern size_t _rtld_pagesz;
55 
56 #define round_down(x)	((x) & ~(_rtld_pagesz - 1))
57 #define round_up(x)	round_down((x) + _rtld_pagesz - 1)
58 
59 #define NEW(type)	((type *) xmalloc(sizeof(type)))
60 #define CNEW(type)	((type *) xcalloc(sizeof(type)))
61 
62 /*
63  * Fill in a DoneList with an allocation large enough to hold all of
64  * the currently-loaded objects. Keep this in a macro since it calls
65  * alloca and we want that to occur within the scope of the caller.
66  */
67 #define _rtld_donelist_init(dlp)					\
68     ((dlp)->num_alloc = _rtld_objcount,					\
69     (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])),	\
70     assert((dlp)->objs != NULL),					\
71     (dlp)->num_used = 0)
72 
73 #endif /* _RTLD_SOURCE */
74 
75 /*
76  * C++ has mandated the use of the following keywords for its new boolean
77  * type.  We might as well follow their lead.
78  */
79 struct Struct_Obj_Entry;
80 
81 typedef struct Struct_Objlist_Entry {
82 	SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
83 	struct Struct_Obj_Entry *obj;
84 } Objlist_Entry;
85 
86 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
87 
88 typedef struct Struct_Needed_Entry {
89 	struct Struct_Needed_Entry *next;
90 	struct Struct_Obj_Entry *obj;
91 	unsigned long   name;	/* Offset of name in string table */
92 } Needed_Entry;
93 
94 typedef struct _rtld_search_path_t {
95 	struct _rtld_search_path_t *sp_next;
96 	const char     *sp_path;
97 	size_t          sp_pathlen;
98 } Search_Path;
99 
100 
101 #define RTLD_MAX_ENTRY 10
102 #define RTLD_MAX_LIBRARY 4
103 #define RTLD_MAX_CTL 2
104 typedef struct _rtld_library_xform_t {
105 	struct _rtld_library_xform_t *next;
106 	char *name;
107 	const char *ctlname;
108 	struct {
109 		char *value;
110 		char *library[RTLD_MAX_LIBRARY];
111 	} entry[RTLD_MAX_ENTRY];
112 } Library_Xform;
113 
114 /*
115  * Shared object descriptor.
116  *
117  * Items marked with "(%)" are dynamically allocated, and must be freed
118  * when the structure is destroyed.
119  */
120 
121 #define RTLD_MAGIC	0xd550b87a
122 #define RTLD_VERSION	1
123 #define	RTLD_MAIN	0x800
124 
125 typedef struct Struct_Obj_Entry {
126 	Elf32_Word      magic;		/* Magic number (sanity check) */
127 	Elf32_Word      version;	/* Version number of struct format */
128 
129 	struct Struct_Obj_Entry *next;
130 	char           *path;		/* Pathname of underlying file (%) */
131 	int             refcount;
132 	int             dl_refcount;	/* Number of times loaded by dlopen */
133 
134 	/* These items are computed by map_object() or by digest_phdr(). */
135 	caddr_t         mapbase;	/* Base address of mapped region */
136 	size_t          mapsize;	/* Size of mapped region in bytes */
137 	size_t          textsize;	/* Size of text segment in bytes */
138 	Elf_Addr        vaddrbase;	/* Base address in shared object file */
139 	caddr_t         relocbase;	/* Reloc const = mapbase - *vaddrbase */
140 	Elf_Dyn        *dynamic;	/* Dynamic section */
141 	caddr_t         entry;		/* Entry point */
142 	const Elf_Phdr *__junk001;
143 	size_t		pathlen;	/* Pathname length */
144 
145 	/* Items from the dynamic section. */
146 	Elf_Addr       *pltgot;		/* PLTGOT table */
147 	const Elf_Rel  *rel;		/* Relocation entries */
148 	const Elf_Rel  *rellim;		/* Limit of Relocation entries */
149 	const Elf_Rela *rela;		/* Relocation entries */
150 	const Elf_Rela *relalim;	/* Limit of Relocation entries */
151 	const Elf_Rel  *pltrel;		/* PLT relocation entries */
152 	const Elf_Rel  *pltrellim;	/* Limit of PLT relocation entries */
153 	const Elf_Rela *pltrela;	/* PLT relocation entries */
154 	const Elf_Rela *pltrelalim;	/* Limit of PLT relocation entries */
155 	const Elf_Sym  *symtab;		/* Symbol table */
156 	const char     *strtab;		/* String table */
157 	unsigned long   strsize;	/* Size in bytes of string table */
158 #ifdef __mips__
159 	Elf_Word        local_gotno;	/* Number of local GOT entries */
160 	Elf_Word        symtabno;	/* Number of dynamic symbols */
161 	Elf_Word        gotsym;		/* First dynamic symbol in GOT */
162 #endif
163 
164 	const Elf_Word *buckets;	/* Hash table buckets array */
165 	uint32_t        nbuckets;	/* Number of buckets */
166 	uint32_t        nbuckets_m;	/* Precomputed for fast remainder */
167 	uint8_t         nbuckets_s1;
168 	uint8_t         nbuckets_s2;
169 	const Elf_Word *chains;		/* Hash table chain array */
170 	unsigned long   nchains;	/* Number of chains */
171 
172 	Search_Path    *rpaths;		/* Search path specified in object */
173 	Needed_Entry   *needed;		/* Shared objects needed by this (%) */
174 
175 	void            (*init)(void); 	/* Initialization function to call */
176 	void            (*fini)(void);	/* Termination function to call */
177 
178 	/* Entry points for dlopen() and friends. */
179 	void           *(*dlopen)(const char *, int);
180 	void           *(*dlsym)(void *, const char *);
181 	char           *(*dlerror)(void);
182 	int             (*dlclose)(void *);
183 	int             (*dladdr)(const void *, Dl_info *);
184 
185 	u_int32_t	mainprog:1,	/* True if this is the main program */
186 	        	rtld:1,		/* True if this is the dynamic linker */
187 			textrel:1,	/* True if there are relocations to
188 					 * text seg */
189 			symbolic:1,	/* True if generated with
190 					 * "-Bsymbolic" */
191 			printed:1,	/* True if ldd has printed it */
192 			isdynamic:1,	/* True if this is a pure PIC object */
193 			mainref:1,	/* True if on _rtld_list_main */
194 			globalref:1,	/* True if on _rtld_list_global */
195 			init_done:1,	/* True if .init has been added */
196 			init_called:1,	/* True if .init function has been
197 					 * called */
198 			fini_called:1,	/* True if .fini function has been
199 					 * called */
200 			initfirst:1;	/* True if object's .init/.fini take
201 					* priority over others */
202 
203 	struct link_map linkmap;	/* for GDB */
204 
205 	/* These items are computed by map_object() or by digest_phdr(). */
206 	const char     *interp;	/* Pathname of the interpreter, if any */
207 	Objlist         dldags;	/* Object belongs to these dlopened DAGs (%) */
208 	Objlist         dagmembers;	/* DAG has these members (%) */
209 	dev_t           dev;		/* Object's filesystem's device */
210 	ino_t           ino;		/* Object's inode number */
211 
212 	void		*ehdr;
213 } Obj_Entry;
214 
215 typedef struct Struct_DoneList {
216 	const Obj_Entry **objs;		/* Array of object pointers */
217 	unsigned int num_alloc;		/* Allocated size of the array */
218 	unsigned int num_used;		/* Number of array slots used */
219 } DoneList;
220 
221 
222 #if defined(_RTLD_SOURCE)
223 
224 extern struct r_debug _rtld_debug;
225 extern Search_Path *_rtld_default_paths;
226 extern Obj_Entry *_rtld_objlist;
227 extern Obj_Entry **_rtld_objtail;
228 extern int _rtld_objcount;
229 extern Obj_Entry *_rtld_objmain;
230 extern Obj_Entry _rtld_objself;
231 extern Search_Path *_rtld_paths;
232 extern Library_Xform *_rtld_xforms;
233 extern bool _rtld_trust;
234 extern Objlist _rtld_list_global;
235 extern Objlist _rtld_list_main;
236 extern Elf_Sym _rtld_sym_zero;
237 
238 /* rtld.c */
239 
240 /*
241  * We export these symbols using _rtld_symbol_lookup and is_exported.
242  */
243 char *dlerror(void);
244 void *dlopen(const char *, int);
245 void *dlsym(void *, const char *);
246 int dlclose(void *);
247 int dladdr(const void *, Dl_info *);
248 int dlinfo(void *, int, void *);
249 
250 void _rtld_error(const char *, ...)
251      __attribute__((__format__(__printf__,1,2)));
252 void _rtld_die(void) __attribute__((__noreturn__));
253 void *_rtld_objmain_sym(const char *);
254 void _rtld_debug_state(void);
255 void _rtld_linkmap_add(Obj_Entry *);
256 void _rtld_linkmap_delete(Obj_Entry *);
257 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
258 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
259 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
260 
261 /* expand.c */
262 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
263     const char *);
264 
265 /* headers.c */
266 void _rtld_digest_dynamic(const char *, Obj_Entry *);
267 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
268 
269 /* load.c */
270 Obj_Entry *_rtld_load_object(const char *, int);
271 int _rtld_load_needed_objects(Obj_Entry *, int);
272 int _rtld_preload(const char *);
273 
274 /* path.c */
275 void _rtld_add_paths(const char *, Search_Path **, const char *);
276 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
277     const char *);
278 int _rtld_sysctl(const char *, void *, size_t *);
279 
280 /* reloc.c */
281 int _rtld_do_copy_relocations(const Obj_Entry *);
282 int _rtld_relocate_objects(Obj_Entry *, bool);
283 int _rtld_relocate_nonplt_objects(const Obj_Entry *);
284 int _rtld_relocate_plt_lazy(const Obj_Entry *);
285 int _rtld_relocate_plt_objects(const Obj_Entry *);
286 void _rtld_setup_pltgot(const Obj_Entry *);
287 
288 /* search.c */
289 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
290 
291 /* symbol.c */
292 unsigned long _rtld_elf_hash(const char *);
293 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
294     const Obj_Entry *, bool);
295 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
296     const Obj_Entry **, bool);
297 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
298     const Obj_Entry **, bool);
299 
300 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
301     const Objlist *, const Obj_Entry **, bool, DoneList *);
302 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
303     const Obj_Entry *, const Obj_Entry **, bool);
304 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
305     const Needed_Entry *, const Obj_Entry **, bool,
306     DoneList *, DoneList *);
307 #ifdef COMBRELOC
308 void _rtld_combreloc_reset(const Obj_Entry *);
309 #endif
310 
311 /* map_object.c */
312 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
313 void _rtld_obj_free(Obj_Entry *);
314 Obj_Entry *_rtld_obj_new(void);
315 
316 /* function descriptors */
317 #ifdef __HAVE_FUNCTION_DESCRIPTORS
318 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
319     const Elf_Sym *, Elf_Addr);
320 const void *_rtld_function_descriptor_function(const void *);
321 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
322 
323 #endif /* _RTLD_SOURCE */
324 
325 #endif /* RTLD_H */
326