xref: /netbsd/libexec/ld.elf_so/rtld.h (revision 6550d01e)
1 /*	$NetBSD: rtld.h,v 1.100 2011/01/25 12:25:43 skrll 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_Name_Entry {
89 	STAILQ_ENTRY(Struct_Name_Entry)	link;
90 	char	name[1];
91 } Name_Entry;
92 
93 typedef struct Struct_Needed_Entry {
94 	struct Struct_Needed_Entry *next;
95 	struct Struct_Obj_Entry *obj;
96 	unsigned long   name;	/* Offset of name in string table */
97 } Needed_Entry;
98 
99 typedef struct _rtld_search_path_t {
100 	struct _rtld_search_path_t *sp_next;
101 	const char     *sp_path;
102 	size_t          sp_pathlen;
103 } Search_Path;
104 
105 
106 #define RTLD_MAX_ENTRY 10
107 #define RTLD_MAX_LIBRARY 4
108 #define RTLD_MAX_CTL 2
109 typedef struct _rtld_library_xform_t {
110 	struct _rtld_library_xform_t *next;
111 	char *name;
112 	const char *ctlname;
113 	struct {
114 		char *value;
115 		char *library[RTLD_MAX_LIBRARY];
116 	} entry[RTLD_MAX_ENTRY];
117 } Library_Xform;
118 
119 /*
120  * Shared object descriptor.
121  *
122  * Items marked with "(%)" are dynamically allocated, and must be freed
123  * when the structure is destroyed.
124  *
125  * The layout of this structure needs to be preserved because pre-2.0 binaries
126  * hard-coded the location of dlopen() and friends.
127  */
128 
129 #define RTLD_MAGIC	0xd550b87a
130 #define RTLD_VERSION	1
131 
132 typedef struct Struct_Obj_Entry {
133 	Elf32_Word      magic;		/* Magic number (sanity check) */
134 	Elf32_Word      version;	/* Version number of struct format */
135 
136 	struct Struct_Obj_Entry *next;
137 	char           *path;		/* Pathname of underlying file (%) */
138 	int             refcount;
139 	int             dl_refcount;	/* Number of times loaded by dlopen */
140 
141 	/* These items are computed by map_object() or by digest_phdr(). */
142 	caddr_t         mapbase;	/* Base address of mapped region */
143 	size_t          mapsize;	/* Size of mapped region in bytes */
144 	size_t          textsize;	/* Size of text segment in bytes */
145 	Elf_Addr        vaddrbase;	/* Base address in shared object file */
146 	caddr_t         relocbase;	/* Reloc const = mapbase - *vaddrbase */
147 	Elf_Dyn        *dynamic;	/* Dynamic section */
148 	caddr_t         entry;		/* Entry point */
149 	const Elf_Phdr *phdr;		/* Program header (may be xmalloc'ed) */
150 	size_t		phsize;		/* Size of program header in bytes */
151 
152 	/* Items from the dynamic section. */
153 	Elf_Addr       *pltgot;		/* PLTGOT table */
154 	const Elf_Rel  *rel;		/* Relocation entries */
155 	const Elf_Rel  *rellim;		/* Limit of Relocation entries */
156 	const Elf_Rela *rela;		/* Relocation entries */
157 	const Elf_Rela *relalim;	/* Limit of Relocation entries */
158 	const Elf_Rel  *pltrel;		/* PLT relocation entries */
159 	const Elf_Rel  *pltrellim;	/* Limit of PLT relocation entries */
160 	const Elf_Rela *pltrela;	/* PLT relocation entries */
161 	const Elf_Rela *pltrelalim;	/* Limit of PLT relocation entries */
162 	const Elf_Sym  *symtab;		/* Symbol table */
163 	const char     *strtab;		/* String table */
164 	unsigned long   strsize;	/* Size in bytes of string table */
165 #ifdef __mips__
166 	Elf_Word        local_gotno;	/* Number of local GOT entries */
167 	Elf_Word        symtabno;	/* Number of dynamic symbols */
168 	Elf_Word        gotsym;		/* First dynamic symbol in GOT */
169 #endif
170 
171 	const Elf_Symindx *buckets;	/* Hash table buckets array */
172 	unsigned long	unused1;	/* Used to be nbuckets */
173 	const Elf_Symindx *chains;	/* Hash table chain array */
174 	unsigned long   nchains;	/* Number of chains */
175 
176 	Search_Path    *rpaths;		/* Search path specified in object */
177 	Needed_Entry   *needed;		/* Shared objects needed by this (%) */
178 
179 	void            (*init)(void); 	/* Initialization function to call */
180 	void            (*fini)(void);	/* Termination function to call */
181 
182 	/* Entry points for dlopen() and friends. */
183 	void           *(*dlopen)(const char *, int);
184 	void           *(*dlsym)(void *, const char *);
185 	char           *(*dlerror)(void);
186 	int             (*dlclose)(void *);
187 	int             (*dladdr)(const void *, Dl_info *);
188 
189 	u_int32_t	mainprog:1,	/* True if this is the main program */
190 	        	rtld:1,		/* True if this is the dynamic linker */
191 			textrel:1,	/* True if there are relocations to
192 					 * text seg */
193 			symbolic:1,	/* True if generated with
194 					 * "-Bsymbolic" */
195 			printed:1,	/* True if ldd has printed it */
196 			isdynamic:1,	/* True if this is a pure PIC object */
197 			mainref:1,	/* True if on _rtld_list_main */
198 			globalref:1,	/* True if on _rtld_list_global */
199 			init_done:1,	/* True if .init has been added */
200 			init_called:1,	/* True if .init function has been
201 					 * called */
202 			fini_called:1,	/* True if .fini function has been
203 					 * called */
204 			z_now:1,	/* True if object's symbols should be
205 					   bound immediately */
206 			z_nodelete:1,	/* True if object should never be
207 					   unloaded */
208 			z_initfirst:1,	/* True if object's .init/.fini take
209 					 * priority over others */
210 			z_noopen:1,	/* True if object should never be
211 					   dlopen'ed */
212 			phdr_loaded:1,	/* Phdr is loaded and doesn't need to
213 					 * be freed. */
214 			ref_nodel:1;	/* Refcount increased to prevent dlclose */
215 
216 	struct link_map linkmap;	/* for GDB */
217 
218 	/* These items are computed by map_object() or by digest_phdr(). */
219 	const char     *interp;	/* Pathname of the interpreter, if any */
220 	Objlist         dldags;	/* Object belongs to these dlopened DAGs (%) */
221 	Objlist         dagmembers;	/* DAG has these members (%) */
222 	dev_t           dev;		/* Object's filesystem's device */
223 	ino_t           ino;		/* Object's inode number */
224 
225 	void		*ehdr;
226 
227 	uint32_t        nbuckets;	/* Number of buckets */
228 	uint32_t        nbuckets_m;	/* Precomputed for fast remainder */
229 	uint8_t         nbuckets_s1;
230 	uint8_t         nbuckets_s2;
231 	size_t		pathlen;	/* Pathname length */
232 	STAILQ_HEAD(, Struct_Name_Entry) names;	/* List of names for this object we
233 						   know about. */
234 #ifdef __powerpc__
235 	Elf_Addr       *gotptr;		/* GOT table (secure-plt only) */
236 #endif
237 } Obj_Entry;
238 
239 typedef struct Struct_DoneList {
240 	const Obj_Entry **objs;		/* Array of object pointers */
241 	unsigned int num_alloc;		/* Allocated size of the array */
242 	unsigned int num_used;		/* Number of array slots used */
243 } DoneList;
244 
245 
246 #if defined(_RTLD_SOURCE)
247 
248 extern struct r_debug _rtld_debug;
249 extern Search_Path *_rtld_default_paths;
250 extern Obj_Entry *_rtld_objlist;
251 extern Obj_Entry **_rtld_objtail;
252 extern u_int _rtld_objcount;
253 extern u_int _rtld_objloads;
254 extern Obj_Entry *_rtld_objmain;
255 extern Obj_Entry _rtld_objself;
256 extern Search_Path *_rtld_paths;
257 extern Library_Xform *_rtld_xforms;
258 extern bool _rtld_trust;
259 extern Objlist _rtld_list_global;
260 extern Objlist _rtld_list_main;
261 extern Elf_Sym _rtld_sym_zero;
262 
263 #define	RTLD_MODEMASK 0x3
264 
265 /* Flags for _rtld_load_object() and friends. */
266 #define	_RTLD_GLOBAL	0x01	/* Add object to global DAG. */
267 #define	_RTLD_MAIN	0x02
268 #define	_RTLD_NOLOAD	0x04	/* dlopen() specified RTLD_NOLOAD. */
269 #define	_RTLD_DLOPEN	0x08	/* Load_object() called from dlopen(). */
270 
271 /* rtld.c */
272 
273 /* We export these symbols using _rtld_symbol_lookup and is_exported. */
274 __dso_public char *dlerror(void);
275 __dso_public void *dlopen(const char *, int);
276 __dso_public void *dlsym(void *, const char *);
277 __dso_public int dlclose(void *);
278 __dso_public int dladdr(const void *, Dl_info *);
279 __dso_public int dlinfo(void *, int, void *);
280 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
281     void *);
282 
283 /* These aren't exported */
284 void _rtld_error(const char *, ...)
285      __attribute__((__format__(__printf__,1,2)));
286 void _rtld_die(void) __attribute__((__noreturn__));
287 void *_rtld_objmain_sym(const char *);
288 __dso_public void _rtld_debug_state(void);
289 void _rtld_linkmap_add(Obj_Entry *);
290 void _rtld_linkmap_delete(Obj_Entry *);
291 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
292 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
293 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
294 void _rtld_ref_dag(Obj_Entry *);
295 
296 /* expand.c */
297 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
298     const char *);
299 
300 /* headers.c */
301 void _rtld_digest_dynamic(const char *, Obj_Entry *);
302 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
303 
304 /* load.c */
305 Obj_Entry *_rtld_load_object(const char *, int);
306 int _rtld_load_needed_objects(Obj_Entry *, int);
307 int _rtld_preload(const char *);
308 
309 #define	OBJ_ERR	(Obj_Entry *)(-1)
310 /* path.c */
311 void _rtld_add_paths(const char *, Search_Path **, const char *);
312 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
313     const char *);
314 int _rtld_sysctl(const char *, void *, size_t *);
315 
316 /* reloc.c */
317 int _rtld_do_copy_relocations(const Obj_Entry *);
318 int _rtld_relocate_objects(Obj_Entry *, bool);
319 int _rtld_relocate_nonplt_objects(Obj_Entry *);
320 int _rtld_relocate_plt_lazy(const Obj_Entry *);
321 int _rtld_relocate_plt_objects(const Obj_Entry *);
322 void _rtld_setup_pltgot(const Obj_Entry *);
323 
324 /* search.c */
325 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
326 
327 /* symbol.c */
328 unsigned long _rtld_elf_hash(const char *);
329 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
330     const Obj_Entry *, bool);
331 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
332     const Obj_Entry **, bool);
333 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
334     const Obj_Entry **, bool);
335 
336 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
337     const Objlist *, const Obj_Entry **, bool, DoneList *);
338 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
339     const Obj_Entry *, const Obj_Entry **, bool);
340 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
341     const Needed_Entry *, const Obj_Entry **, bool,
342     DoneList *, DoneList *);
343 #ifdef COMBRELOC
344 void _rtld_combreloc_reset(const Obj_Entry *);
345 #endif
346 
347 /* map_object.c */
348 struct stat;
349 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
350 void _rtld_obj_free(Obj_Entry *);
351 Obj_Entry *_rtld_obj_new(void);
352 
353 /* function descriptors */
354 #ifdef __HAVE_FUNCTION_DESCRIPTORS
355 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
356     const Elf_Sym *, Elf_Addr);
357 const void *_rtld_function_descriptor_function(const void *);
358 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
359 
360 #endif /* _RTLD_SOURCE */
361 
362 #endif /* RTLD_H */
363