xref: /freebsd/libexec/rtld-elf/rtld.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
3  * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
4  * Copyright 2009, 2010, 2011 Konstantin Belousov <kib@FreeBSD.ORG>.
5  * All rights reserved.
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 OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Dynamic linker for ELF.
32  *
33  * John Polstra <jdp@polstra.com>.
34  */
35 
36 #ifndef __GNUC__
37 #error "GCC is needed to compile this file"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44 #include <sys/sysctl.h>
45 #include <sys/uio.h>
46 #include <sys/utsname.h>
47 #include <sys/ktrace.h>
48 
49 #include <dlfcn.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "debug.h"
60 #include "rtld.h"
61 #include "libmap.h"
62 #include "rtld_tls.h"
63 #include "rtld_printf.h"
64 
65 #ifndef COMPAT_32BIT
66 #define PATH_RTLD	"/libexec/ld-elf.so.1"
67 #else
68 #define PATH_RTLD	"/libexec/ld-elf32.so.1"
69 #endif
70 
71 /* Types. */
72 typedef void (*func_ptr_type)();
73 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
74 
75 /*
76  * Function declarations.
77  */
78 static const char *basename(const char *);
79 static void die(void) __dead2;
80 static void digest_dynamic1(Obj_Entry *, int, const Elf_Dyn **,
81     const Elf_Dyn **);
82 static void digest_dynamic2(Obj_Entry *, const Elf_Dyn *, const Elf_Dyn *);
83 static void digest_dynamic(Obj_Entry *, int);
84 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
85 static Obj_Entry *dlcheck(void *);
86 static Obj_Entry *dlopen_object(const char *name, Obj_Entry *refobj,
87     int lo_flags, int mode);
88 static Obj_Entry *do_load_object(int, const char *, char *, struct stat *, int);
89 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
90 static bool donelist_check(DoneList *, const Obj_Entry *);
91 static void errmsg_restore(char *);
92 static char *errmsg_save(void);
93 static void *fill_search_info(const char *, size_t, void *);
94 static char *find_library(const char *, const Obj_Entry *);
95 static const char *gethints(void);
96 static void init_dag(Obj_Entry *);
97 static void init_rtld(caddr_t, Elf_Auxinfo **);
98 static void initlist_add_neededs(Needed_Entry *, Objlist *);
99 static void initlist_add_objects(Obj_Entry *, Obj_Entry **, Objlist *);
100 static void linkmap_add(Obj_Entry *);
101 static void linkmap_delete(Obj_Entry *);
102 static void load_filtees(Obj_Entry *, int flags, RtldLockState *);
103 static void unload_filtees(Obj_Entry *);
104 static int load_needed_objects(Obj_Entry *, int);
105 static int load_preload_objects(void);
106 static Obj_Entry *load_object(const char *, const Obj_Entry *, int);
107 static void map_stacks_exec(RtldLockState *);
108 static Obj_Entry *obj_from_addr(const void *);
109 static void objlist_call_fini(Objlist *, Obj_Entry *, RtldLockState *);
110 static void objlist_call_init(Objlist *, RtldLockState *);
111 static void objlist_clear(Objlist *);
112 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
113 static void objlist_init(Objlist *);
114 static void objlist_push_head(Objlist *, Obj_Entry *);
115 static void objlist_push_tail(Objlist *, Obj_Entry *);
116 static void objlist_remove(Objlist *, Obj_Entry *);
117 static void *path_enumerate(const char *, path_enum_proc, void *);
118 static int relocate_objects(Obj_Entry *, bool, Obj_Entry *, RtldLockState *);
119 static int resolve_objects_ifunc(Obj_Entry *first, bool bind_now,
120     RtldLockState *lockstate);
121 static int rtld_dirname(const char *, char *);
122 static int rtld_dirname_abs(const char *, char *);
123 static void rtld_exit(void);
124 static char *search_library_path(const char *, const char *);
125 static const void **get_program_var_addr(const char *, RtldLockState *);
126 static void set_program_var(const char *, const void *);
127 static int symlook_default(SymLook *, const Obj_Entry *refobj);
128 static int symlook_global(SymLook *, DoneList *);
129 static void symlook_init_from_req(SymLook *, const SymLook *);
130 static int symlook_list(SymLook *, const Objlist *, DoneList *);
131 static int symlook_needed(SymLook *, const Needed_Entry *, DoneList *);
132 static int symlook_obj1(SymLook *, const Obj_Entry *);
133 static void trace_loaded_objects(Obj_Entry *);
134 static void unlink_object(Obj_Entry *);
135 static void unload_object(Obj_Entry *);
136 static void unref_dag(Obj_Entry *);
137 static void ref_dag(Obj_Entry *);
138 static int origin_subst_one(char **, const char *, const char *,
139   const char *, char *);
140 static char *origin_subst(const char *, const char *);
141 static int  rtld_verify_versions(const Objlist *);
142 static int  rtld_verify_object_versions(Obj_Entry *);
143 static void object_add_name(Obj_Entry *, const char *);
144 static int  object_match_name(const Obj_Entry *, const char *);
145 static void ld_utrace_log(int, void *, void *, size_t, int, const char *);
146 static void rtld_fill_dl_phdr_info(const Obj_Entry *obj,
147     struct dl_phdr_info *phdr_info);
148 
149 void r_debug_state(struct r_debug *, struct link_map *) __noinline;
150 
151 /*
152  * Data declarations.
153  */
154 static char *error_message;	/* Message for dlerror(), or NULL */
155 struct r_debug r_debug;		/* for GDB; */
156 static bool libmap_disable;	/* Disable libmap */
157 static bool ld_loadfltr;	/* Immediate filters processing */
158 static char *libmap_override;	/* Maps to use in addition to libmap.conf */
159 static bool trust;		/* False for setuid and setgid programs */
160 static bool dangerous_ld_env;	/* True if environment variables have been
161 				   used to affect the libraries loaded */
162 static char *ld_bind_now;	/* Environment variable for immediate binding */
163 static char *ld_debug;		/* Environment variable for debugging */
164 static char *ld_library_path;	/* Environment variable for search path */
165 static char *ld_preload;	/* Environment variable for libraries to
166 				   load first */
167 static char *ld_elf_hints_path;	/* Environment variable for alternative hints path */
168 static char *ld_tracing;	/* Called from ldd to print libs */
169 static char *ld_utrace;		/* Use utrace() to log events. */
170 static Obj_Entry *obj_list;	/* Head of linked list of shared objects */
171 static Obj_Entry **obj_tail;	/* Link field of last object in list */
172 static Obj_Entry *obj_main;	/* The main program shared object */
173 static Obj_Entry obj_rtld;	/* The dynamic linker shared object */
174 static unsigned int obj_count;	/* Number of objects in obj_list */
175 static unsigned int obj_loads;	/* Number of objects in obj_list */
176 
177 static Objlist list_global =	/* Objects dlopened with RTLD_GLOBAL */
178   STAILQ_HEAD_INITIALIZER(list_global);
179 static Objlist list_main =	/* Objects loaded at program startup */
180   STAILQ_HEAD_INITIALIZER(list_main);
181 static Objlist list_fini =	/* Objects needing fini() calls */
182   STAILQ_HEAD_INITIALIZER(list_fini);
183 
184 Elf_Sym sym_zero;		/* For resolving undefined weak refs. */
185 
186 #define GDB_STATE(s,m)	r_debug.r_state = s; r_debug_state(&r_debug,m);
187 
188 extern Elf_Dyn _DYNAMIC;
189 #pragma weak _DYNAMIC
190 #ifndef RTLD_IS_DYNAMIC
191 #define	RTLD_IS_DYNAMIC()	(&_DYNAMIC != NULL)
192 #endif
193 
194 int osreldate, pagesize;
195 
196 static int stack_prot = PROT_READ | PROT_WRITE | RTLD_DEFAULT_STACK_EXEC;
197 static int max_stack_flags;
198 
199 /*
200  * Global declarations normally provided by crt1.  The dynamic linker is
201  * not built with crt1, so we have to provide them ourselves.
202  */
203 char *__progname;
204 char **environ;
205 
206 /*
207  * Globals to control TLS allocation.
208  */
209 size_t tls_last_offset;		/* Static TLS offset of last module */
210 size_t tls_last_size;		/* Static TLS size of last module */
211 size_t tls_static_space;	/* Static TLS space allocated */
212 int tls_dtv_generation = 1;	/* Used to detect when dtv size changes  */
213 int tls_max_index = 1;		/* Largest module index allocated */
214 
215 /*
216  * Fill in a DoneList with an allocation large enough to hold all of
217  * the currently-loaded objects.  Keep this as a macro since it calls
218  * alloca and we want that to occur within the scope of the caller.
219  */
220 #define donelist_init(dlp)					\
221     ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]),	\
222     assert((dlp)->objs != NULL),				\
223     (dlp)->num_alloc = obj_count,				\
224     (dlp)->num_used = 0)
225 
226 #define	UTRACE_DLOPEN_START		1
227 #define	UTRACE_DLOPEN_STOP		2
228 #define	UTRACE_DLCLOSE_START		3
229 #define	UTRACE_DLCLOSE_STOP		4
230 #define	UTRACE_LOAD_OBJECT		5
231 #define	UTRACE_UNLOAD_OBJECT		6
232 #define	UTRACE_ADD_RUNDEP		7
233 #define	UTRACE_PRELOAD_FINISHED		8
234 #define	UTRACE_INIT_CALL		9
235 #define	UTRACE_FINI_CALL		10
236 
237 struct utrace_rtld {
238 	char sig[4];			/* 'RTLD' */
239 	int event;
240 	void *handle;
241 	void *mapbase;			/* Used for 'parent' and 'init/fini' */
242 	size_t mapsize;
243 	int refcnt;			/* Used for 'mode' */
244 	char name[MAXPATHLEN];
245 };
246 
247 #define	LD_UTRACE(e, h, mb, ms, r, n) do {			\
248 	if (ld_utrace != NULL)					\
249 		ld_utrace_log(e, h, mb, ms, r, n);		\
250 } while (0)
251 
252 static void
253 ld_utrace_log(int event, void *handle, void *mapbase, size_t mapsize,
254     int refcnt, const char *name)
255 {
256 	struct utrace_rtld ut;
257 
258 	ut.sig[0] = 'R';
259 	ut.sig[1] = 'T';
260 	ut.sig[2] = 'L';
261 	ut.sig[3] = 'D';
262 	ut.event = event;
263 	ut.handle = handle;
264 	ut.mapbase = mapbase;
265 	ut.mapsize = mapsize;
266 	ut.refcnt = refcnt;
267 	bzero(ut.name, sizeof(ut.name));
268 	if (name)
269 		strlcpy(ut.name, name, sizeof(ut.name));
270 	utrace(&ut, sizeof(ut));
271 }
272 
273 /*
274  * Main entry point for dynamic linking.  The first argument is the
275  * stack pointer.  The stack is expected to be laid out as described
276  * in the SVR4 ABI specification, Intel 386 Processor Supplement.
277  * Specifically, the stack pointer points to a word containing
278  * ARGC.  Following that in the stack is a null-terminated sequence
279  * of pointers to argument strings.  Then comes a null-terminated
280  * sequence of pointers to environment strings.  Finally, there is a
281  * sequence of "auxiliary vector" entries.
282  *
283  * The second argument points to a place to store the dynamic linker's
284  * exit procedure pointer and the third to a place to store the main
285  * program's object.
286  *
287  * The return value is the main program's entry point.
288  */
289 func_ptr_type
290 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
291 {
292     Elf_Auxinfo *aux_info[AT_COUNT];
293     int i;
294     int argc;
295     char **argv;
296     char **env;
297     Elf_Auxinfo *aux;
298     Elf_Auxinfo *auxp;
299     const char *argv0;
300     Objlist_Entry *entry;
301     Obj_Entry *obj;
302     Obj_Entry **preload_tail;
303     Objlist initlist;
304     RtldLockState lockstate;
305 
306     /*
307      * On entry, the dynamic linker itself has not been relocated yet.
308      * Be very careful not to reference any global data until after
309      * init_rtld has returned.  It is OK to reference file-scope statics
310      * and string constants, and to call static and global functions.
311      */
312 
313     /* Find the auxiliary vector on the stack. */
314     argc = *sp++;
315     argv = (char **) sp;
316     sp += argc + 1;	/* Skip over arguments and NULL terminator */
317     env = (char **) sp;
318     while (*sp++ != 0)	/* Skip over environment, and NULL terminator */
319 	;
320     aux = (Elf_Auxinfo *) sp;
321 
322     /* Digest the auxiliary vector. */
323     for (i = 0;  i < AT_COUNT;  i++)
324 	aux_info[i] = NULL;
325     for (auxp = aux;  auxp->a_type != AT_NULL;  auxp++) {
326 	if (auxp->a_type < AT_COUNT)
327 	    aux_info[auxp->a_type] = auxp;
328     }
329 
330     /* Initialize and relocate ourselves. */
331     assert(aux_info[AT_BASE] != NULL);
332     init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr, aux_info);
333 
334     __progname = obj_rtld.path;
335     argv0 = argv[0] != NULL ? argv[0] : "(null)";
336     environ = env;
337 
338     trust = !issetugid();
339 
340     ld_bind_now = getenv(LD_ "BIND_NOW");
341     /*
342      * If the process is tainted, then we un-set the dangerous environment
343      * variables.  The process will be marked as tainted until setuid(2)
344      * is called.  If any child process calls setuid(2) we do not want any
345      * future processes to honor the potentially un-safe variables.
346      */
347     if (!trust) {
348         if (unsetenv(LD_ "PRELOAD") || unsetenv(LD_ "LIBMAP") ||
349 	    unsetenv(LD_ "LIBRARY_PATH") || unsetenv(LD_ "LIBMAP_DISABLE") ||
350 	    unsetenv(LD_ "DEBUG") || unsetenv(LD_ "ELF_HINTS_PATH") ||
351 	    unsetenv(LD_ "LOADFLTR")) {
352 		_rtld_error("environment corrupt; aborting");
353 		die();
354 	}
355     }
356     ld_debug = getenv(LD_ "DEBUG");
357     libmap_disable = getenv(LD_ "LIBMAP_DISABLE") != NULL;
358     libmap_override = getenv(LD_ "LIBMAP");
359     ld_library_path = getenv(LD_ "LIBRARY_PATH");
360     ld_preload = getenv(LD_ "PRELOAD");
361     ld_elf_hints_path = getenv(LD_ "ELF_HINTS_PATH");
362     ld_loadfltr = getenv(LD_ "LOADFLTR") != NULL;
363     dangerous_ld_env = libmap_disable || (libmap_override != NULL) ||
364 	(ld_library_path != NULL) || (ld_preload != NULL) ||
365 	(ld_elf_hints_path != NULL) || ld_loadfltr;
366     ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS");
367     ld_utrace = getenv(LD_ "UTRACE");
368 
369     if ((ld_elf_hints_path == NULL) || strlen(ld_elf_hints_path) == 0)
370 	ld_elf_hints_path = _PATH_ELF_HINTS;
371 
372     if (ld_debug != NULL && *ld_debug != '\0')
373 	debug = 1;
374     dbg("%s is initialized, base address = %p", __progname,
375 	(caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
376     dbg("RTLD dynamic = %p", obj_rtld.dynamic);
377     dbg("RTLD pltgot  = %p", obj_rtld.pltgot);
378 
379     dbg("initializing thread locks");
380     lockdflt_init();
381 
382     /*
383      * Load the main program, or process its program header if it is
384      * already loaded.
385      */
386     if (aux_info[AT_EXECFD] != NULL) {	/* Load the main program. */
387 	int fd = aux_info[AT_EXECFD]->a_un.a_val;
388 	dbg("loading main program");
389 	obj_main = map_object(fd, argv0, NULL);
390 	close(fd);
391 	if (obj_main == NULL)
392 	    die();
393 	max_stack_flags = obj->stack_flags;
394     } else {				/* Main program already loaded. */
395 	const Elf_Phdr *phdr;
396 	int phnum;
397 	caddr_t entry;
398 
399 	dbg("processing main program's program header");
400 	assert(aux_info[AT_PHDR] != NULL);
401 	phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
402 	assert(aux_info[AT_PHNUM] != NULL);
403 	phnum = aux_info[AT_PHNUM]->a_un.a_val;
404 	assert(aux_info[AT_PHENT] != NULL);
405 	assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
406 	assert(aux_info[AT_ENTRY] != NULL);
407 	entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
408 	if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
409 	    die();
410     }
411 
412     if (aux_info[AT_EXECPATH] != 0) {
413 	    char *kexecpath;
414 	    char buf[MAXPATHLEN];
415 
416 	    kexecpath = aux_info[AT_EXECPATH]->a_un.a_ptr;
417 	    dbg("AT_EXECPATH %p %s", kexecpath, kexecpath);
418 	    if (kexecpath[0] == '/')
419 		    obj_main->path = kexecpath;
420 	    else if (getcwd(buf, sizeof(buf)) == NULL ||
421 		     strlcat(buf, "/", sizeof(buf)) >= sizeof(buf) ||
422 		     strlcat(buf, kexecpath, sizeof(buf)) >= sizeof(buf))
423 		    obj_main->path = xstrdup(argv0);
424 	    else
425 		    obj_main->path = xstrdup(buf);
426     } else {
427 	    dbg("No AT_EXECPATH");
428 	    obj_main->path = xstrdup(argv0);
429     }
430     dbg("obj_main path %s", obj_main->path);
431     obj_main->mainprog = true;
432 
433     if (aux_info[AT_STACKPROT] != NULL &&
434       aux_info[AT_STACKPROT]->a_un.a_val != 0)
435 	    stack_prot = aux_info[AT_STACKPROT]->a_un.a_val;
436 
437     /*
438      * Get the actual dynamic linker pathname from the executable if
439      * possible.  (It should always be possible.)  That ensures that
440      * gdb will find the right dynamic linker even if a non-standard
441      * one is being used.
442      */
443     if (obj_main->interp != NULL &&
444       strcmp(obj_main->interp, obj_rtld.path) != 0) {
445 	free(obj_rtld.path);
446 	obj_rtld.path = xstrdup(obj_main->interp);
447         __progname = obj_rtld.path;
448     }
449 
450     digest_dynamic(obj_main, 0);
451 
452     linkmap_add(obj_main);
453     linkmap_add(&obj_rtld);
454 
455     /* Link the main program into the list of objects. */
456     *obj_tail = obj_main;
457     obj_tail = &obj_main->next;
458     obj_count++;
459     obj_loads++;
460     /* Make sure we don't call the main program's init and fini functions. */
461     obj_main->init = obj_main->fini = (Elf_Addr)NULL;
462 
463     /* Initialize a fake symbol for resolving undefined weak references. */
464     sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
465     sym_zero.st_shndx = SHN_UNDEF;
466     sym_zero.st_value = -(uintptr_t)obj_main->relocbase;
467 
468     if (!libmap_disable)
469         libmap_disable = (bool)lm_init(libmap_override);
470 
471     dbg("loading LD_PRELOAD libraries");
472     if (load_preload_objects() == -1)
473 	die();
474     preload_tail = obj_tail;
475 
476     dbg("loading needed objects");
477     if (load_needed_objects(obj_main, 0) == -1)
478 	die();
479 
480     /* Make a list of all objects loaded at startup. */
481     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
482 	objlist_push_tail(&list_main, obj);
483     	obj->refcount++;
484     }
485 
486     dbg("checking for required versions");
487     if (rtld_verify_versions(&list_main) == -1 && !ld_tracing)
488 	die();
489 
490     if (ld_tracing) {		/* We're done */
491 	trace_loaded_objects(obj_main);
492 	exit(0);
493     }
494 
495     if (getenv(LD_ "DUMP_REL_PRE") != NULL) {
496        dump_relocations(obj_main);
497        exit (0);
498     }
499 
500     /*
501      * Processing tls relocations requires having the tls offsets
502      * initialized.  Prepare offsets before starting initial
503      * relocation processing.
504      */
505     dbg("initializing initial thread local storage offsets");
506     STAILQ_FOREACH(entry, &list_main, link) {
507 	/*
508 	 * Allocate all the initial objects out of the static TLS
509 	 * block even if they didn't ask for it.
510 	 */
511 	allocate_tls_offset(entry->obj);
512     }
513 
514     if (relocate_objects(obj_main,
515       ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld, NULL) == -1)
516 	die();
517 
518     if (resolve_objects_ifunc(obj_main,
519       ld_bind_now != NULL && *ld_bind_now != '\0', NULL) == -1)
520 	die();
521 
522     dbg("doing copy relocations");
523     if (do_copy_relocations(obj_main) == -1)
524 	die();
525 
526     if (getenv(LD_ "DUMP_REL_POST") != NULL) {
527        dump_relocations(obj_main);
528        exit (0);
529     }
530 
531     /*
532      * Setup TLS for main thread.  This must be done after the
533      * relocations are processed, since tls initialization section
534      * might be the subject for relocations.
535      */
536     dbg("initializing initial thread local storage");
537     allocate_initial_tls(obj_list);
538 
539     dbg("initializing key program variables");
540     set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
541     set_program_var("environ", env);
542     set_program_var("__elf_aux_vector", aux);
543 
544     /* Make a list of init functions to call. */
545     objlist_init(&initlist);
546     initlist_add_objects(obj_list, preload_tail, &initlist);
547 
548     r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
549 
550     map_stacks_exec(NULL);
551 
552     wlock_acquire(rtld_bind_lock, &lockstate);
553     objlist_call_init(&initlist, &lockstate);
554     objlist_clear(&initlist);
555     dbg("loading filtees");
556     for (obj = obj_list->next; obj != NULL; obj = obj->next) {
557 	if (ld_loadfltr || obj->z_loadfltr)
558 	    load_filtees(obj, 0, &lockstate);
559     }
560     lock_release(rtld_bind_lock, &lockstate);
561 
562     dbg("transferring control to program entry point = %p", obj_main->entry);
563 
564     /* Return the exit procedure and the program entry point. */
565     *exit_proc = rtld_exit;
566     *objp = obj_main;
567     return (func_ptr_type) obj_main->entry;
568 }
569 
570 void *
571 rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
572 {
573 	void *ptr;
574 	Elf_Addr target;
575 
576 	ptr = (void *)make_function_pointer(def, obj);
577 	target = ((Elf_Addr (*)(void))ptr)();
578 	return ((void *)target);
579 }
580 
581 Elf_Addr
582 _rtld_bind(Obj_Entry *obj, Elf_Size reloff)
583 {
584     const Elf_Rel *rel;
585     const Elf_Sym *def;
586     const Obj_Entry *defobj;
587     Elf_Addr *where;
588     Elf_Addr target;
589     RtldLockState lockstate;
590 
591     rlock_acquire(rtld_bind_lock, &lockstate);
592     if (sigsetjmp(lockstate.env, 0) != 0)
593 	    lock_upgrade(rtld_bind_lock, &lockstate);
594     if (obj->pltrel)
595 	rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
596     else
597 	rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
598 
599     where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
600     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL,
601 	&lockstate);
602     if (def == NULL)
603 	die();
604     if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC)
605 	target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
606     else
607 	target = (Elf_Addr)(defobj->relocbase + def->st_value);
608 
609     dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
610       defobj->strtab + def->st_name, basename(obj->path),
611       (void *)target, basename(defobj->path));
612 
613     /*
614      * Write the new contents for the jmpslot. Note that depending on
615      * architecture, the value which we need to return back to the
616      * lazy binding trampoline may or may not be the target
617      * address. The value returned from reloc_jmpslot() is the value
618      * that the trampoline needs.
619      */
620     target = reloc_jmpslot(where, target, defobj, obj, rel);
621     lock_release(rtld_bind_lock, &lockstate);
622     return target;
623 }
624 
625 /*
626  * Error reporting function.  Use it like printf.  If formats the message
627  * into a buffer, and sets things up so that the next call to dlerror()
628  * will return the message.
629  */
630 void
631 _rtld_error(const char *fmt, ...)
632 {
633     static char buf[512];
634     va_list ap;
635 
636     va_start(ap, fmt);
637     rtld_vsnprintf(buf, sizeof buf, fmt, ap);
638     error_message = buf;
639     va_end(ap);
640 }
641 
642 /*
643  * Return a dynamically-allocated copy of the current error message, if any.
644  */
645 static char *
646 errmsg_save(void)
647 {
648     return error_message == NULL ? NULL : xstrdup(error_message);
649 }
650 
651 /*
652  * Restore the current error message from a copy which was previously saved
653  * by errmsg_save().  The copy is freed.
654  */
655 static void
656 errmsg_restore(char *saved_msg)
657 {
658     if (saved_msg == NULL)
659 	error_message = NULL;
660     else {
661 	_rtld_error("%s", saved_msg);
662 	free(saved_msg);
663     }
664 }
665 
666 static const char *
667 basename(const char *name)
668 {
669     const char *p = strrchr(name, '/');
670     return p != NULL ? p + 1 : name;
671 }
672 
673 static struct utsname uts;
674 
675 static int
676 origin_subst_one(char **res, const char *real, const char *kw, const char *subst,
677     char *may_free)
678 {
679     const char *p, *p1;
680     char *res1;
681     int subst_len;
682     int kw_len;
683 
684     res1 = *res = NULL;
685     p = real;
686     subst_len = kw_len = 0;
687     for (;;) {
688 	 p1 = strstr(p, kw);
689 	 if (p1 != NULL) {
690 	     if (subst_len == 0) {
691 		 subst_len = strlen(subst);
692 		 kw_len = strlen(kw);
693 	     }
694 	     if (*res == NULL) {
695 		 *res = xmalloc(PATH_MAX);
696 		 res1 = *res;
697 	     }
698 	     if ((res1 - *res) + subst_len + (p1 - p) >= PATH_MAX) {
699 		 _rtld_error("Substitution of %s in %s cannot be performed",
700 		     kw, real);
701 		 if (may_free != NULL)
702 		     free(may_free);
703 		 free(res);
704 		 return (false);
705 	     }
706 	     memcpy(res1, p, p1 - p);
707 	     res1 += p1 - p;
708 	     memcpy(res1, subst, subst_len);
709 	     res1 += subst_len;
710 	     p = p1 + kw_len;
711 	 } else {
712 	    if (*res == NULL) {
713 		if (may_free != NULL)
714 		    *res = may_free;
715 		else
716 		    *res = xstrdup(real);
717 		return (true);
718 	    }
719 	    *res1 = '\0';
720 	    if (may_free != NULL)
721 		free(may_free);
722 	    if (strlcat(res1, p, PATH_MAX - (res1 - *res)) >= PATH_MAX) {
723 		free(res);
724 		return (false);
725 	    }
726 	    return (true);
727 	 }
728     }
729 }
730 
731 static char *
732 origin_subst(const char *real, const char *origin_path)
733 {
734     char *res1, *res2, *res3, *res4;
735 
736     if (uts.sysname[0] == '\0') {
737 	if (uname(&uts) != 0) {
738 	    _rtld_error("utsname failed: %d", errno);
739 	    return (NULL);
740 	}
741     }
742     if (!origin_subst_one(&res1, real, "$ORIGIN", origin_path, NULL) ||
743 	!origin_subst_one(&res2, res1, "$OSNAME", uts.sysname, res1) ||
744 	!origin_subst_one(&res3, res2, "$OSREL", uts.release, res2) ||
745 	!origin_subst_one(&res4, res3, "$PLATFORM", uts.machine, res3))
746 	    return (NULL);
747     return (res4);
748 }
749 
750 static void
751 die(void)
752 {
753     const char *msg = dlerror();
754 
755     if (msg == NULL)
756 	msg = "Fatal error";
757     rtld_fdputstr(STDERR_FILENO, msg);
758     _exit(1);
759 }
760 
761 /*
762  * Process a shared object's DYNAMIC section, and save the important
763  * information in its Obj_Entry structure.
764  */
765 static void
766 digest_dynamic1(Obj_Entry *obj, int early, const Elf_Dyn **dyn_rpath,
767     const Elf_Dyn **dyn_soname)
768 {
769     const Elf_Dyn *dynp;
770     Needed_Entry **needed_tail = &obj->needed;
771     Needed_Entry **needed_filtees_tail = &obj->needed_filtees;
772     Needed_Entry **needed_aux_filtees_tail = &obj->needed_aux_filtees;
773     int plttype = DT_REL;
774 
775     *dyn_rpath = NULL;
776     *dyn_soname = NULL;
777 
778     obj->bind_now = false;
779     for (dynp = obj->dynamic;  dynp->d_tag != DT_NULL;  dynp++) {
780 	switch (dynp->d_tag) {
781 
782 	case DT_REL:
783 	    obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
784 	    break;
785 
786 	case DT_RELSZ:
787 	    obj->relsize = dynp->d_un.d_val;
788 	    break;
789 
790 	case DT_RELENT:
791 	    assert(dynp->d_un.d_val == sizeof(Elf_Rel));
792 	    break;
793 
794 	case DT_JMPREL:
795 	    obj->pltrel = (const Elf_Rel *)
796 	      (obj->relocbase + dynp->d_un.d_ptr);
797 	    break;
798 
799 	case DT_PLTRELSZ:
800 	    obj->pltrelsize = dynp->d_un.d_val;
801 	    break;
802 
803 	case DT_RELA:
804 	    obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
805 	    break;
806 
807 	case DT_RELASZ:
808 	    obj->relasize = dynp->d_un.d_val;
809 	    break;
810 
811 	case DT_RELAENT:
812 	    assert(dynp->d_un.d_val == sizeof(Elf_Rela));
813 	    break;
814 
815 	case DT_PLTREL:
816 	    plttype = dynp->d_un.d_val;
817 	    assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
818 	    break;
819 
820 	case DT_SYMTAB:
821 	    obj->symtab = (const Elf_Sym *)
822 	      (obj->relocbase + dynp->d_un.d_ptr);
823 	    break;
824 
825 	case DT_SYMENT:
826 	    assert(dynp->d_un.d_val == sizeof(Elf_Sym));
827 	    break;
828 
829 	case DT_STRTAB:
830 	    obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
831 	    break;
832 
833 	case DT_STRSZ:
834 	    obj->strsize = dynp->d_un.d_val;
835 	    break;
836 
837 	case DT_VERNEED:
838 	    obj->verneed = (const Elf_Verneed *) (obj->relocbase +
839 		dynp->d_un.d_val);
840 	    break;
841 
842 	case DT_VERNEEDNUM:
843 	    obj->verneednum = dynp->d_un.d_val;
844 	    break;
845 
846 	case DT_VERDEF:
847 	    obj->verdef = (const Elf_Verdef *) (obj->relocbase +
848 		dynp->d_un.d_val);
849 	    break;
850 
851 	case DT_VERDEFNUM:
852 	    obj->verdefnum = dynp->d_un.d_val;
853 	    break;
854 
855 	case DT_VERSYM:
856 	    obj->versyms = (const Elf_Versym *)(obj->relocbase +
857 		dynp->d_un.d_val);
858 	    break;
859 
860 	case DT_HASH:
861 	    {
862 		const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
863 		  (obj->relocbase + dynp->d_un.d_ptr);
864 		obj->nbuckets = hashtab[0];
865 		obj->nchains = hashtab[1];
866 		obj->buckets = hashtab + 2;
867 		obj->chains = obj->buckets + obj->nbuckets;
868 	    }
869 	    break;
870 
871 	case DT_NEEDED:
872 	    if (!obj->rtld) {
873 		Needed_Entry *nep = NEW(Needed_Entry);
874 		nep->name = dynp->d_un.d_val;
875 		nep->obj = NULL;
876 		nep->next = NULL;
877 
878 		*needed_tail = nep;
879 		needed_tail = &nep->next;
880 	    }
881 	    break;
882 
883 	case DT_FILTER:
884 	    if (!obj->rtld) {
885 		Needed_Entry *nep = NEW(Needed_Entry);
886 		nep->name = dynp->d_un.d_val;
887 		nep->obj = NULL;
888 		nep->next = NULL;
889 
890 		*needed_filtees_tail = nep;
891 		needed_filtees_tail = &nep->next;
892 	    }
893 	    break;
894 
895 	case DT_AUXILIARY:
896 	    if (!obj->rtld) {
897 		Needed_Entry *nep = NEW(Needed_Entry);
898 		nep->name = dynp->d_un.d_val;
899 		nep->obj = NULL;
900 		nep->next = NULL;
901 
902 		*needed_aux_filtees_tail = nep;
903 		needed_aux_filtees_tail = &nep->next;
904 	    }
905 	    break;
906 
907 	case DT_PLTGOT:
908 	    obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
909 	    break;
910 
911 	case DT_TEXTREL:
912 	    obj->textrel = true;
913 	    break;
914 
915 	case DT_SYMBOLIC:
916 	    obj->symbolic = true;
917 	    break;
918 
919 	case DT_RPATH:
920 	case DT_RUNPATH:	/* XXX: process separately */
921 	    /*
922 	     * We have to wait until later to process this, because we
923 	     * might not have gotten the address of the string table yet.
924 	     */
925 	    *dyn_rpath = dynp;
926 	    break;
927 
928 	case DT_SONAME:
929 	    *dyn_soname = dynp;
930 	    break;
931 
932 	case DT_INIT:
933 	    obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
934 	    break;
935 
936 	case DT_FINI:
937 	    obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
938 	    break;
939 
940 	/*
941 	 * Don't process DT_DEBUG on MIPS as the dynamic section
942 	 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
943 	 */
944 
945 #ifndef __mips__
946 	case DT_DEBUG:
947 	    /* XXX - not implemented yet */
948 	    if (!early)
949 		dbg("Filling in DT_DEBUG entry");
950 	    ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
951 	    break;
952 #endif
953 
954 	case DT_FLAGS:
955 		if ((dynp->d_un.d_val & DF_ORIGIN) && trust)
956 		    obj->z_origin = true;
957 		if (dynp->d_un.d_val & DF_SYMBOLIC)
958 		    obj->symbolic = true;
959 		if (dynp->d_un.d_val & DF_TEXTREL)
960 		    obj->textrel = true;
961 		if (dynp->d_un.d_val & DF_BIND_NOW)
962 		    obj->bind_now = true;
963 		/*if (dynp->d_un.d_val & DF_STATIC_TLS)
964 		    ;*/
965 	    break;
966 #ifdef __mips__
967 	case DT_MIPS_LOCAL_GOTNO:
968 		obj->local_gotno = dynp->d_un.d_val;
969 	    break;
970 
971 	case DT_MIPS_SYMTABNO:
972 		obj->symtabno = dynp->d_un.d_val;
973 		break;
974 
975 	case DT_MIPS_GOTSYM:
976 		obj->gotsym = dynp->d_un.d_val;
977 		break;
978 
979 	case DT_MIPS_RLD_MAP:
980 #ifdef notyet
981 		if (!early)
982 			dbg("Filling in DT_DEBUG entry");
983 		((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
984 #endif
985 		break;
986 #endif
987 
988 	case DT_FLAGS_1:
989 		if (dynp->d_un.d_val & DF_1_NOOPEN)
990 		    obj->z_noopen = true;
991 		if ((dynp->d_un.d_val & DF_1_ORIGIN) && trust)
992 		    obj->z_origin = true;
993 		/*if (dynp->d_un.d_val & DF_1_GLOBAL)
994 		    XXX ;*/
995 		if (dynp->d_un.d_val & DF_1_BIND_NOW)
996 		    obj->bind_now = true;
997 		if (dynp->d_un.d_val & DF_1_NODELETE)
998 		    obj->z_nodelete = true;
999 		if (dynp->d_un.d_val & DF_1_LOADFLTR)
1000 		    obj->z_loadfltr = true;
1001 	    break;
1002 
1003 	default:
1004 	    if (!early) {
1005 		dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag,
1006 		    (long)dynp->d_tag);
1007 	    }
1008 	    break;
1009 	}
1010     }
1011 
1012     obj->traced = false;
1013 
1014     if (plttype == DT_RELA) {
1015 	obj->pltrela = (const Elf_Rela *) obj->pltrel;
1016 	obj->pltrel = NULL;
1017 	obj->pltrelasize = obj->pltrelsize;
1018 	obj->pltrelsize = 0;
1019     }
1020 }
1021 
1022 static void
1023 digest_dynamic2(Obj_Entry *obj, const Elf_Dyn *dyn_rpath,
1024     const Elf_Dyn *dyn_soname)
1025 {
1026 
1027     if (obj->z_origin && obj->origin_path == NULL) {
1028 	obj->origin_path = xmalloc(PATH_MAX);
1029 	if (rtld_dirname_abs(obj->path, obj->origin_path) == -1)
1030 	    die();
1031     }
1032 
1033     if (dyn_rpath != NULL) {
1034 	obj->rpath = (char *)obj->strtab + dyn_rpath->d_un.d_val;
1035 	if (obj->z_origin)
1036 	    obj->rpath = origin_subst(obj->rpath, obj->origin_path);
1037     }
1038 
1039     if (dyn_soname != NULL)
1040 	object_add_name(obj, obj->strtab + dyn_soname->d_un.d_val);
1041 }
1042 
1043 static void
1044 digest_dynamic(Obj_Entry *obj, int early)
1045 {
1046 	const Elf_Dyn *dyn_rpath;
1047 	const Elf_Dyn *dyn_soname;
1048 
1049 	digest_dynamic1(obj, early, &dyn_rpath, &dyn_soname);
1050 	digest_dynamic2(obj, dyn_rpath, dyn_soname);
1051 }
1052 
1053 /*
1054  * Process a shared object's program header.  This is used only for the
1055  * main program, when the kernel has already loaded the main program
1056  * into memory before calling the dynamic linker.  It creates and
1057  * returns an Obj_Entry structure.
1058  */
1059 static Obj_Entry *
1060 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
1061 {
1062     Obj_Entry *obj;
1063     const Elf_Phdr *phlimit = phdr + phnum;
1064     const Elf_Phdr *ph;
1065     int nsegs = 0;
1066 
1067     obj = obj_new();
1068     for (ph = phdr;  ph < phlimit;  ph++) {
1069 	if (ph->p_type != PT_PHDR)
1070 	    continue;
1071 
1072 	obj->phdr = phdr;
1073 	obj->phsize = ph->p_memsz;
1074 	obj->relocbase = (caddr_t)phdr - ph->p_vaddr;
1075 	break;
1076     }
1077 
1078     obj->stack_flags = PF_X | PF_R | PF_W;
1079 
1080     for (ph = phdr;  ph < phlimit;  ph++) {
1081 	switch (ph->p_type) {
1082 
1083 	case PT_INTERP:
1084 	    obj->interp = (const char *)(ph->p_vaddr + obj->relocbase);
1085 	    break;
1086 
1087 	case PT_LOAD:
1088 	    if (nsegs == 0) {	/* First load segment */
1089 		obj->vaddrbase = trunc_page(ph->p_vaddr);
1090 		obj->mapbase = obj->vaddrbase + obj->relocbase;
1091 		obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
1092 		  obj->vaddrbase;
1093 	    } else {		/* Last load segment */
1094 		obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
1095 		  obj->vaddrbase;
1096 	    }
1097 	    nsegs++;
1098 	    break;
1099 
1100 	case PT_DYNAMIC:
1101 	    obj->dynamic = (const Elf_Dyn *)(ph->p_vaddr + obj->relocbase);
1102 	    break;
1103 
1104 	case PT_TLS:
1105 	    obj->tlsindex = 1;
1106 	    obj->tlssize = ph->p_memsz;
1107 	    obj->tlsalign = ph->p_align;
1108 	    obj->tlsinitsize = ph->p_filesz;
1109 	    obj->tlsinit = (void*)(ph->p_vaddr + obj->relocbase);
1110 	    break;
1111 
1112 	case PT_GNU_STACK:
1113 	    obj->stack_flags = ph->p_flags;
1114 	    break;
1115 	}
1116     }
1117     if (nsegs < 1) {
1118 	_rtld_error("%s: too few PT_LOAD segments", path);
1119 	return NULL;
1120     }
1121 
1122     obj->entry = entry;
1123     return obj;
1124 }
1125 
1126 static Obj_Entry *
1127 dlcheck(void *handle)
1128 {
1129     Obj_Entry *obj;
1130 
1131     for (obj = obj_list;  obj != NULL;  obj = obj->next)
1132 	if (obj == (Obj_Entry *) handle)
1133 	    break;
1134 
1135     if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
1136 	_rtld_error("Invalid shared object handle %p", handle);
1137 	return NULL;
1138     }
1139     return obj;
1140 }
1141 
1142 /*
1143  * If the given object is already in the donelist, return true.  Otherwise
1144  * add the object to the list and return false.
1145  */
1146 static bool
1147 donelist_check(DoneList *dlp, const Obj_Entry *obj)
1148 {
1149     unsigned int i;
1150 
1151     for (i = 0;  i < dlp->num_used;  i++)
1152 	if (dlp->objs[i] == obj)
1153 	    return true;
1154     /*
1155      * Our donelist allocation should always be sufficient.  But if
1156      * our threads locking isn't working properly, more shared objects
1157      * could have been loaded since we allocated the list.  That should
1158      * never happen, but we'll handle it properly just in case it does.
1159      */
1160     if (dlp->num_used < dlp->num_alloc)
1161 	dlp->objs[dlp->num_used++] = obj;
1162     return false;
1163 }
1164 
1165 /*
1166  * Hash function for symbol table lookup.  Don't even think about changing
1167  * this.  It is specified by the System V ABI.
1168  */
1169 unsigned long
1170 elf_hash(const char *name)
1171 {
1172     const unsigned char *p = (const unsigned char *) name;
1173     unsigned long h = 0;
1174     unsigned long g;
1175 
1176     while (*p != '\0') {
1177 	h = (h << 4) + *p++;
1178 	if ((g = h & 0xf0000000) != 0)
1179 	    h ^= g >> 24;
1180 	h &= ~g;
1181     }
1182     return h;
1183 }
1184 
1185 /*
1186  * Find the library with the given name, and return its full pathname.
1187  * The returned string is dynamically allocated.  Generates an error
1188  * message and returns NULL if the library cannot be found.
1189  *
1190  * If the second argument is non-NULL, then it refers to an already-
1191  * loaded shared object, whose library search path will be searched.
1192  *
1193  * The search order is:
1194  *   LD_LIBRARY_PATH
1195  *   rpath in the referencing file
1196  *   ldconfig hints
1197  *   /lib:/usr/lib
1198  */
1199 static char *
1200 find_library(const char *xname, const Obj_Entry *refobj)
1201 {
1202     char *pathname;
1203     char *name;
1204 
1205     if (strchr(xname, '/') != NULL) {	/* Hard coded pathname */
1206 	if (xname[0] != '/' && !trust) {
1207 	    _rtld_error("Absolute pathname required for shared object \"%s\"",
1208 	      xname);
1209 	    return NULL;
1210 	}
1211 	if (refobj != NULL && refobj->z_origin)
1212 	    return origin_subst(xname, refobj->origin_path);
1213 	else
1214 	    return xstrdup(xname);
1215     }
1216 
1217     if (libmap_disable || (refobj == NULL) ||
1218 	(name = lm_find(refobj->path, xname)) == NULL)
1219 	name = (char *)xname;
1220 
1221     dbg(" Searching for \"%s\"", name);
1222 
1223     if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
1224       (refobj != NULL &&
1225       (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
1226       (pathname = search_library_path(name, gethints())) != NULL ||
1227       (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
1228 	return pathname;
1229 
1230     if(refobj != NULL && refobj->path != NULL) {
1231 	_rtld_error("Shared object \"%s\" not found, required by \"%s\"",
1232 	  name, basename(refobj->path));
1233     } else {
1234 	_rtld_error("Shared object \"%s\" not found", name);
1235     }
1236     return NULL;
1237 }
1238 
1239 /*
1240  * Given a symbol number in a referencing object, find the corresponding
1241  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
1242  * no definition was found.  Returns a pointer to the Obj_Entry of the
1243  * defining object via the reference parameter DEFOBJ_OUT.
1244  */
1245 const Elf_Sym *
1246 find_symdef(unsigned long symnum, const Obj_Entry *refobj,
1247     const Obj_Entry **defobj_out, int flags, SymCache *cache,
1248     RtldLockState *lockstate)
1249 {
1250     const Elf_Sym *ref;
1251     const Elf_Sym *def;
1252     const Obj_Entry *defobj;
1253     SymLook req;
1254     const char *name;
1255     int res;
1256 
1257     /*
1258      * If we have already found this symbol, get the information from
1259      * the cache.
1260      */
1261     if (symnum >= refobj->nchains)
1262 	return NULL;	/* Bad object */
1263     if (cache != NULL && cache[symnum].sym != NULL) {
1264 	*defobj_out = cache[symnum].obj;
1265 	return cache[symnum].sym;
1266     }
1267 
1268     ref = refobj->symtab + symnum;
1269     name = refobj->strtab + ref->st_name;
1270     def = NULL;
1271     defobj = NULL;
1272 
1273     /*
1274      * We don't have to do a full scale lookup if the symbol is local.
1275      * We know it will bind to the instance in this load module; to
1276      * which we already have a pointer (ie ref). By not doing a lookup,
1277      * we not only improve performance, but it also avoids unresolvable
1278      * symbols when local symbols are not in the hash table. This has
1279      * been seen with the ia64 toolchain.
1280      */
1281     if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
1282 	if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
1283 	    _rtld_error("%s: Bogus symbol table entry %lu", refobj->path,
1284 		symnum);
1285 	}
1286 	symlook_init(&req, name);
1287 	req.flags = flags;
1288 	req.ventry = fetch_ventry(refobj, symnum);
1289 	req.lockstate = lockstate;
1290 	res = symlook_default(&req, refobj);
1291 	if (res == 0) {
1292 	    def = req.sym_out;
1293 	    defobj = req.defobj_out;
1294 	}
1295     } else {
1296 	def = ref;
1297 	defobj = refobj;
1298     }
1299 
1300     /*
1301      * If we found no definition and the reference is weak, treat the
1302      * symbol as having the value zero.
1303      */
1304     if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
1305 	def = &sym_zero;
1306 	defobj = obj_main;
1307     }
1308 
1309     if (def != NULL) {
1310 	*defobj_out = defobj;
1311 	/* Record the information in the cache to avoid subsequent lookups. */
1312 	if (cache != NULL) {
1313 	    cache[symnum].sym = def;
1314 	    cache[symnum].obj = defobj;
1315 	}
1316     } else {
1317 	if (refobj != &obj_rtld)
1318 	    _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
1319     }
1320     return def;
1321 }
1322 
1323 /*
1324  * Return the search path from the ldconfig hints file, reading it if
1325  * necessary.  Returns NULL if there are problems with the hints file,
1326  * or if the search path there is empty.
1327  */
1328 static const char *
1329 gethints(void)
1330 {
1331     static char *hints;
1332 
1333     if (hints == NULL) {
1334 	int fd;
1335 	struct elfhints_hdr hdr;
1336 	char *p;
1337 
1338 	/* Keep from trying again in case the hints file is bad. */
1339 	hints = "";
1340 
1341 	if ((fd = open(ld_elf_hints_path, O_RDONLY)) == -1)
1342 	    return NULL;
1343 	if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
1344 	  hdr.magic != ELFHINTS_MAGIC ||
1345 	  hdr.version != 1) {
1346 	    close(fd);
1347 	    return NULL;
1348 	}
1349 	p = xmalloc(hdr.dirlistlen + 1);
1350 	if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 ||
1351 	  read(fd, p, hdr.dirlistlen + 1) != (ssize_t)hdr.dirlistlen + 1) {
1352 	    free(p);
1353 	    close(fd);
1354 	    return NULL;
1355 	}
1356 	hints = p;
1357 	close(fd);
1358     }
1359     return hints[0] != '\0' ? hints : NULL;
1360 }
1361 
1362 static void
1363 init_dag(Obj_Entry *root)
1364 {
1365     const Needed_Entry *needed;
1366     const Objlist_Entry *elm;
1367     DoneList donelist;
1368 
1369     if (root->dag_inited)
1370 	return;
1371     donelist_init(&donelist);
1372 
1373     /* Root object belongs to own DAG. */
1374     objlist_push_tail(&root->dldags, root);
1375     objlist_push_tail(&root->dagmembers, root);
1376     donelist_check(&donelist, root);
1377 
1378     /*
1379      * Add dependencies of root object to DAG in breadth order
1380      * by exploiting the fact that each new object get added
1381      * to the tail of the dagmembers list.
1382      */
1383     STAILQ_FOREACH(elm, &root->dagmembers, link) {
1384 	for (needed = elm->obj->needed; needed != NULL; needed = needed->next) {
1385 	    if (needed->obj == NULL || donelist_check(&donelist, needed->obj))
1386 		continue;
1387 	    objlist_push_tail(&needed->obj->dldags, root);
1388 	    objlist_push_tail(&root->dagmembers, needed->obj);
1389 	}
1390     }
1391     root->dag_inited = true;
1392 }
1393 
1394 /*
1395  * Initialize the dynamic linker.  The argument is the address at which
1396  * the dynamic linker has been mapped into memory.  The primary task of
1397  * this function is to relocate the dynamic linker.
1398  */
1399 static void
1400 init_rtld(caddr_t mapbase, Elf_Auxinfo **aux_info)
1401 {
1402     Obj_Entry objtmp;	/* Temporary rtld object */
1403     const Elf_Dyn *dyn_rpath;
1404     const Elf_Dyn *dyn_soname;
1405 
1406     /*
1407      * Conjure up an Obj_Entry structure for the dynamic linker.
1408      *
1409      * The "path" member can't be initialized yet because string constants
1410      * cannot yet be accessed. Below we will set it correctly.
1411      */
1412     memset(&objtmp, 0, sizeof(objtmp));
1413     objtmp.path = NULL;
1414     objtmp.rtld = true;
1415     objtmp.mapbase = mapbase;
1416 #ifdef PIC
1417     objtmp.relocbase = mapbase;
1418 #endif
1419     if (RTLD_IS_DYNAMIC()) {
1420 	objtmp.dynamic = rtld_dynamic(&objtmp);
1421 	digest_dynamic1(&objtmp, 1, &dyn_rpath, &dyn_soname);
1422 	assert(objtmp.needed == NULL);
1423 #if !defined(__mips__)
1424 	/* MIPS has a bogus DT_TEXTREL. */
1425 	assert(!objtmp.textrel);
1426 #endif
1427 
1428 	/*
1429 	 * Temporarily put the dynamic linker entry into the object list, so
1430 	 * that symbols can be found.
1431 	 */
1432 
1433 	relocate_objects(&objtmp, true, &objtmp, NULL);
1434     }
1435 
1436     /* Initialize the object list. */
1437     obj_tail = &obj_list;
1438 
1439     /* Now that non-local variables can be accesses, copy out obj_rtld. */
1440     memcpy(&obj_rtld, &objtmp, sizeof(obj_rtld));
1441 
1442     if (aux_info[AT_PAGESZ] != NULL)
1443 	    pagesize = aux_info[AT_PAGESZ]->a_un.a_val;
1444     if (aux_info[AT_OSRELDATE] != NULL)
1445 	    osreldate = aux_info[AT_OSRELDATE]->a_un.a_val;
1446 
1447     digest_dynamic2(&obj_rtld, dyn_rpath, dyn_soname);
1448 
1449     /* Replace the path with a dynamically allocated copy. */
1450     obj_rtld.path = xstrdup(PATH_RTLD);
1451 
1452     r_debug.r_brk = r_debug_state;
1453     r_debug.r_state = RT_CONSISTENT;
1454 }
1455 
1456 /*
1457  * Add the init functions from a needed object list (and its recursive
1458  * needed objects) to "list".  This is not used directly; it is a helper
1459  * function for initlist_add_objects().  The write lock must be held
1460  * when this function is called.
1461  */
1462 static void
1463 initlist_add_neededs(Needed_Entry *needed, Objlist *list)
1464 {
1465     /* Recursively process the successor needed objects. */
1466     if (needed->next != NULL)
1467 	initlist_add_neededs(needed->next, list);
1468 
1469     /* Process the current needed object. */
1470     if (needed->obj != NULL)
1471 	initlist_add_objects(needed->obj, &needed->obj->next, list);
1472 }
1473 
1474 /*
1475  * Scan all of the DAGs rooted in the range of objects from "obj" to
1476  * "tail" and add their init functions to "list".  This recurses over
1477  * the DAGs and ensure the proper init ordering such that each object's
1478  * needed libraries are initialized before the object itself.  At the
1479  * same time, this function adds the objects to the global finalization
1480  * list "list_fini" in the opposite order.  The write lock must be
1481  * held when this function is called.
1482  */
1483 static void
1484 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list)
1485 {
1486     if (obj->init_scanned || obj->init_done)
1487 	return;
1488     obj->init_scanned = true;
1489 
1490     /* Recursively process the successor objects. */
1491     if (&obj->next != tail)
1492 	initlist_add_objects(obj->next, tail, list);
1493 
1494     /* Recursively process the needed objects. */
1495     if (obj->needed != NULL)
1496 	initlist_add_neededs(obj->needed, list);
1497 
1498     /* Add the object to the init list. */
1499     if (obj->init != (Elf_Addr)NULL)
1500 	objlist_push_tail(list, obj);
1501 
1502     /* Add the object to the global fini list in the reverse order. */
1503     if (obj->fini != (Elf_Addr)NULL && !obj->on_fini_list) {
1504 	objlist_push_head(&list_fini, obj);
1505 	obj->on_fini_list = true;
1506     }
1507 }
1508 
1509 #ifndef FPTR_TARGET
1510 #define FPTR_TARGET(f)	((Elf_Addr) (f))
1511 #endif
1512 
1513 static void
1514 free_needed_filtees(Needed_Entry *n)
1515 {
1516     Needed_Entry *needed, *needed1;
1517 
1518     for (needed = n; needed != NULL; needed = needed->next) {
1519 	if (needed->obj != NULL) {
1520 	    dlclose(needed->obj);
1521 	    needed->obj = NULL;
1522 	}
1523     }
1524     for (needed = n; needed != NULL; needed = needed1) {
1525 	needed1 = needed->next;
1526 	free(needed);
1527     }
1528 }
1529 
1530 static void
1531 unload_filtees(Obj_Entry *obj)
1532 {
1533 
1534     free_needed_filtees(obj->needed_filtees);
1535     obj->needed_filtees = NULL;
1536     free_needed_filtees(obj->needed_aux_filtees);
1537     obj->needed_aux_filtees = NULL;
1538     obj->filtees_loaded = false;
1539 }
1540 
1541 static void
1542 load_filtee1(Obj_Entry *obj, Needed_Entry *needed, int flags)
1543 {
1544 
1545     for (; needed != NULL; needed = needed->next) {
1546 	needed->obj = dlopen_object(obj->strtab + needed->name, obj,
1547 	  flags, ((ld_loadfltr || obj->z_loadfltr) ? RTLD_NOW : RTLD_LAZY) |
1548 	  RTLD_LOCAL);
1549     }
1550 }
1551 
1552 static void
1553 load_filtees(Obj_Entry *obj, int flags, RtldLockState *lockstate)
1554 {
1555 
1556     lock_restart_for_upgrade(lockstate);
1557     if (!obj->filtees_loaded) {
1558 	load_filtee1(obj, obj->needed_filtees, flags);
1559 	load_filtee1(obj, obj->needed_aux_filtees, flags);
1560 	obj->filtees_loaded = true;
1561     }
1562 }
1563 
1564 static int
1565 process_needed(Obj_Entry *obj, Needed_Entry *needed, int flags)
1566 {
1567     Obj_Entry *obj1;
1568 
1569     for (; needed != NULL; needed = needed->next) {
1570 	obj1 = needed->obj = load_object(obj->strtab + needed->name, obj,
1571 	  flags & ~RTLD_LO_NOLOAD);
1572 	if (obj1 == NULL && !ld_tracing && (flags & RTLD_LO_FILTEES) == 0)
1573 	    return (-1);
1574 	if (obj1 != NULL && obj1->z_nodelete && !obj1->ref_nodel) {
1575 	    dbg("obj %s nodelete", obj1->path);
1576 	    init_dag(obj1);
1577 	    ref_dag(obj1);
1578 	    obj1->ref_nodel = true;
1579 	}
1580     }
1581     return (0);
1582 }
1583 
1584 /*
1585  * Given a shared object, traverse its list of needed objects, and load
1586  * each of them.  Returns 0 on success.  Generates an error message and
1587  * returns -1 on failure.
1588  */
1589 static int
1590 load_needed_objects(Obj_Entry *first, int flags)
1591 {
1592     Obj_Entry *obj;
1593 
1594     for (obj = first;  obj != NULL;  obj = obj->next) {
1595 	if (process_needed(obj, obj->needed, flags) == -1)
1596 	    return (-1);
1597     }
1598     return (0);
1599 }
1600 
1601 static int
1602 load_preload_objects(void)
1603 {
1604     char *p = ld_preload;
1605     static const char delim[] = " \t:;";
1606 
1607     if (p == NULL)
1608 	return 0;
1609 
1610     p += strspn(p, delim);
1611     while (*p != '\0') {
1612 	size_t len = strcspn(p, delim);
1613 	char savech;
1614 
1615 	savech = p[len];
1616 	p[len] = '\0';
1617 	if (load_object(p, NULL, 0) == NULL)
1618 	    return -1;	/* XXX - cleanup */
1619 	p[len] = savech;
1620 	p += len;
1621 	p += strspn(p, delim);
1622     }
1623     LD_UTRACE(UTRACE_PRELOAD_FINISHED, NULL, NULL, 0, 0, NULL);
1624     return 0;
1625 }
1626 
1627 /*
1628  * Load a shared object into memory, if it is not already loaded.
1629  *
1630  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
1631  * on failure.
1632  */
1633 static Obj_Entry *
1634 load_object(const char *name, const Obj_Entry *refobj, int flags)
1635 {
1636     Obj_Entry *obj;
1637     int fd = -1;
1638     struct stat sb;
1639     char *path;
1640 
1641     for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
1642 	if (object_match_name(obj, name))
1643 	    return obj;
1644 
1645     path = find_library(name, refobj);
1646     if (path == NULL)
1647 	return NULL;
1648 
1649     /*
1650      * If we didn't find a match by pathname, open the file and check
1651      * again by device and inode.  This avoids false mismatches caused
1652      * by multiple links or ".." in pathnames.
1653      *
1654      * To avoid a race, we open the file and use fstat() rather than
1655      * using stat().
1656      */
1657     if ((fd = open(path, O_RDONLY)) == -1) {
1658 	_rtld_error("Cannot open \"%s\"", path);
1659 	free(path);
1660 	return NULL;
1661     }
1662     if (fstat(fd, &sb) == -1) {
1663 	_rtld_error("Cannot fstat \"%s\"", path);
1664 	close(fd);
1665 	free(path);
1666 	return NULL;
1667     }
1668     for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
1669 	if (obj->ino == sb.st_ino && obj->dev == sb.st_dev)
1670 	    break;
1671     if (obj != NULL) {
1672 	object_add_name(obj, name);
1673 	free(path);
1674 	close(fd);
1675 	return obj;
1676     }
1677     if (flags & RTLD_LO_NOLOAD) {
1678 	free(path);
1679 	close(fd);
1680 	return (NULL);
1681     }
1682 
1683     /* First use of this object, so we must map it in */
1684     obj = do_load_object(fd, name, path, &sb, flags);
1685     if (obj == NULL)
1686 	free(path);
1687     close(fd);
1688 
1689     return obj;
1690 }
1691 
1692 static Obj_Entry *
1693 do_load_object(int fd, const char *name, char *path, struct stat *sbp,
1694   int flags)
1695 {
1696     Obj_Entry *obj;
1697     struct statfs fs;
1698 
1699     /*
1700      * but first, make sure that environment variables haven't been
1701      * used to circumvent the noexec flag on a filesystem.
1702      */
1703     if (dangerous_ld_env) {
1704 	if (fstatfs(fd, &fs) != 0) {
1705 	    _rtld_error("Cannot fstatfs \"%s\"", path);
1706 		return NULL;
1707 	}
1708 	if (fs.f_flags & MNT_NOEXEC) {
1709 	    _rtld_error("Cannot execute objects on %s\n", fs.f_mntonname);
1710 	    return NULL;
1711 	}
1712     }
1713     dbg("loading \"%s\"", path);
1714     obj = map_object(fd, path, sbp);
1715     if (obj == NULL)
1716         return NULL;
1717 
1718     object_add_name(obj, name);
1719     obj->path = path;
1720     digest_dynamic(obj, 0);
1721     if (obj->z_noopen && (flags & (RTLD_LO_DLOPEN | RTLD_LO_TRACE)) ==
1722       RTLD_LO_DLOPEN) {
1723 	dbg("refusing to load non-loadable \"%s\"", obj->path);
1724 	_rtld_error("Cannot dlopen non-loadable %s", obj->path);
1725 	munmap(obj->mapbase, obj->mapsize);
1726 	obj_free(obj);
1727 	return (NULL);
1728     }
1729 
1730     *obj_tail = obj;
1731     obj_tail = &obj->next;
1732     obj_count++;
1733     obj_loads++;
1734     linkmap_add(obj);	/* for GDB & dlinfo() */
1735     max_stack_flags |= obj->stack_flags;
1736 
1737     dbg("  %p .. %p: %s", obj->mapbase,
1738          obj->mapbase + obj->mapsize - 1, obj->path);
1739     if (obj->textrel)
1740 	dbg("  WARNING: %s has impure text", obj->path);
1741     LD_UTRACE(UTRACE_LOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0,
1742 	obj->path);
1743 
1744     return obj;
1745 }
1746 
1747 static Obj_Entry *
1748 obj_from_addr(const void *addr)
1749 {
1750     Obj_Entry *obj;
1751 
1752     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
1753 	if (addr < (void *) obj->mapbase)
1754 	    continue;
1755 	if (addr < (void *) (obj->mapbase + obj->mapsize))
1756 	    return obj;
1757     }
1758     return NULL;
1759 }
1760 
1761 /*
1762  * Call the finalization functions for each of the objects in "list"
1763  * belonging to the DAG of "root" and referenced once. If NULL "root"
1764  * is specified, every finalization function will be called regardless
1765  * of the reference count and the list elements won't be freed. All of
1766  * the objects are expected to have non-NULL fini functions.
1767  */
1768 static void
1769 objlist_call_fini(Objlist *list, Obj_Entry *root, RtldLockState *lockstate)
1770 {
1771     Objlist_Entry *elm;
1772     char *saved_msg;
1773 
1774     assert(root == NULL || root->refcount == 1);
1775 
1776     /*
1777      * Preserve the current error message since a fini function might
1778      * call into the dynamic linker and overwrite it.
1779      */
1780     saved_msg = errmsg_save();
1781     do {
1782 	STAILQ_FOREACH(elm, list, link) {
1783 	    if (root != NULL && (elm->obj->refcount != 1 ||
1784 	      objlist_find(&root->dagmembers, elm->obj) == NULL))
1785 		continue;
1786 	    dbg("calling fini function for %s at %p", elm->obj->path,
1787 	        (void *)elm->obj->fini);
1788 	    LD_UTRACE(UTRACE_FINI_CALL, elm->obj, (void *)elm->obj->fini, 0, 0,
1789 		elm->obj->path);
1790 	    /* Remove object from fini list to prevent recursive invocation. */
1791 	    STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1792 	    /*
1793 	     * XXX: If a dlopen() call references an object while the
1794 	     * fini function is in progress, we might end up trying to
1795 	     * unload the referenced object in dlclose() or the object
1796 	     * won't be unloaded although its fini function has been
1797 	     * called.
1798 	     */
1799 	    lock_release(rtld_bind_lock, lockstate);
1800 	    call_initfini_pointer(elm->obj, elm->obj->fini);
1801 	    wlock_acquire(rtld_bind_lock, lockstate);
1802 	    /* No need to free anything if process is going down. */
1803 	    if (root != NULL)
1804 	    	free(elm);
1805 	    /*
1806 	     * We must restart the list traversal after every fini call
1807 	     * because a dlclose() call from the fini function or from
1808 	     * another thread might have modified the reference counts.
1809 	     */
1810 	    break;
1811 	}
1812     } while (elm != NULL);
1813     errmsg_restore(saved_msg);
1814 }
1815 
1816 /*
1817  * Call the initialization functions for each of the objects in
1818  * "list".  All of the objects are expected to have non-NULL init
1819  * functions.
1820  */
1821 static void
1822 objlist_call_init(Objlist *list, RtldLockState *lockstate)
1823 {
1824     Objlist_Entry *elm;
1825     Obj_Entry *obj;
1826     char *saved_msg;
1827 
1828     /*
1829      * Clean init_scanned flag so that objects can be rechecked and
1830      * possibly initialized earlier if any of vectors called below
1831      * cause the change by using dlopen.
1832      */
1833     for (obj = obj_list;  obj != NULL;  obj = obj->next)
1834 	obj->init_scanned = false;
1835 
1836     /*
1837      * Preserve the current error message since an init function might
1838      * call into the dynamic linker and overwrite it.
1839      */
1840     saved_msg = errmsg_save();
1841     STAILQ_FOREACH(elm, list, link) {
1842 	if (elm->obj->init_done) /* Initialized early. */
1843 	    continue;
1844 	dbg("calling init function for %s at %p", elm->obj->path,
1845 	    (void *)elm->obj->init);
1846 	LD_UTRACE(UTRACE_INIT_CALL, elm->obj, (void *)elm->obj->init, 0, 0,
1847 	    elm->obj->path);
1848 	/*
1849 	 * Race: other thread might try to use this object before current
1850 	 * one completes the initilization. Not much can be done here
1851 	 * without better locking.
1852 	 */
1853 	elm->obj->init_done = true;
1854 	lock_release(rtld_bind_lock, lockstate);
1855 	call_initfini_pointer(elm->obj, elm->obj->init);
1856 	wlock_acquire(rtld_bind_lock, lockstate);
1857     }
1858     errmsg_restore(saved_msg);
1859 }
1860 
1861 static void
1862 objlist_clear(Objlist *list)
1863 {
1864     Objlist_Entry *elm;
1865 
1866     while (!STAILQ_EMPTY(list)) {
1867 	elm = STAILQ_FIRST(list);
1868 	STAILQ_REMOVE_HEAD(list, link);
1869 	free(elm);
1870     }
1871 }
1872 
1873 static Objlist_Entry *
1874 objlist_find(Objlist *list, const Obj_Entry *obj)
1875 {
1876     Objlist_Entry *elm;
1877 
1878     STAILQ_FOREACH(elm, list, link)
1879 	if (elm->obj == obj)
1880 	    return elm;
1881     return NULL;
1882 }
1883 
1884 static void
1885 objlist_init(Objlist *list)
1886 {
1887     STAILQ_INIT(list);
1888 }
1889 
1890 static void
1891 objlist_push_head(Objlist *list, Obj_Entry *obj)
1892 {
1893     Objlist_Entry *elm;
1894 
1895     elm = NEW(Objlist_Entry);
1896     elm->obj = obj;
1897     STAILQ_INSERT_HEAD(list, elm, link);
1898 }
1899 
1900 static void
1901 objlist_push_tail(Objlist *list, Obj_Entry *obj)
1902 {
1903     Objlist_Entry *elm;
1904 
1905     elm = NEW(Objlist_Entry);
1906     elm->obj = obj;
1907     STAILQ_INSERT_TAIL(list, elm, link);
1908 }
1909 
1910 static void
1911 objlist_remove(Objlist *list, Obj_Entry *obj)
1912 {
1913     Objlist_Entry *elm;
1914 
1915     if ((elm = objlist_find(list, obj)) != NULL) {
1916 	STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1917 	free(elm);
1918     }
1919 }
1920 
1921 /*
1922  * Relocate newly-loaded shared objects.  The argument is a pointer to
1923  * the Obj_Entry for the first such object.  All objects from the first
1924  * to the end of the list of objects are relocated.  Returns 0 on success,
1925  * or -1 on failure.
1926  */
1927 static int
1928 relocate_objects(Obj_Entry *first, bool bind_now, Obj_Entry *rtldobj,
1929     RtldLockState *lockstate)
1930 {
1931     Obj_Entry *obj;
1932 
1933     for (obj = first;  obj != NULL;  obj = obj->next) {
1934 	if (obj != rtldobj)
1935 	    dbg("relocating \"%s\"", obj->path);
1936 	if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL ||
1937 	    obj->symtab == NULL || obj->strtab == NULL) {
1938 	    _rtld_error("%s: Shared object has no run-time symbol table",
1939 	      obj->path);
1940 	    return -1;
1941 	}
1942 
1943 	if (obj->textrel) {
1944 	    /* There are relocations to the write-protected text segment. */
1945 	    if (mprotect(obj->mapbase, obj->textsize,
1946 	      PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
1947 		_rtld_error("%s: Cannot write-enable text segment: %s",
1948 		  obj->path, strerror(errno));
1949 		return -1;
1950 	    }
1951 	}
1952 
1953 	/* Process the non-PLT relocations. */
1954 	if (reloc_non_plt(obj, rtldobj, lockstate))
1955 		return -1;
1956 
1957 	if (obj->textrel) {	/* Re-protected the text segment. */
1958 	    if (mprotect(obj->mapbase, obj->textsize,
1959 	      PROT_READ|PROT_EXEC) == -1) {
1960 		_rtld_error("%s: Cannot write-protect text segment: %s",
1961 		  obj->path, strerror(errno));
1962 		return -1;
1963 	    }
1964 	}
1965 
1966 
1967 	/* Set the special PLT or GOT entries. */
1968 	init_pltgot(obj);
1969 
1970 	/* Process the PLT relocations. */
1971 	if (reloc_plt(obj) == -1)
1972 	    return -1;
1973 	/* Relocate the jump slots if we are doing immediate binding. */
1974 	if (obj->bind_now || bind_now)
1975 	    if (reloc_jmpslots(obj, lockstate) == -1)
1976 		return -1;
1977 
1978 	/*
1979 	 * Set up the magic number and version in the Obj_Entry.  These
1980 	 * were checked in the crt1.o from the original ElfKit, so we
1981 	 * set them for backward compatibility.
1982 	 */
1983 	obj->magic = RTLD_MAGIC;
1984 	obj->version = RTLD_VERSION;
1985     }
1986 
1987     return (0);
1988 }
1989 
1990 /*
1991  * The handling of R_MACHINE_IRELATIVE relocations and jumpslots
1992  * referencing STT_GNU_IFUNC symbols is postponed till the other
1993  * relocations are done.  The indirect functions specified as
1994  * ifunc are allowed to call other symbols, so we need to have
1995  * objects relocated before asking for resolution from indirects.
1996  *
1997  * The R_MACHINE_IRELATIVE slots are resolved in greedy fashion,
1998  * instead of the usual lazy handling of PLT slots.  It is
1999  * consistent with how GNU does it.
2000  */
2001 static int
2002 resolve_object_ifunc(Obj_Entry *obj, bool bind_now, RtldLockState *lockstate)
2003 {
2004 	if (obj->irelative && reloc_iresolve(obj, lockstate) == -1)
2005 		return (-1);
2006 	if ((obj->bind_now || bind_now) && obj->gnu_ifunc &&
2007 	    reloc_gnu_ifunc(obj, lockstate) == -1)
2008 		return (-1);
2009 	return (0);
2010 }
2011 
2012 static int
2013 resolve_objects_ifunc(Obj_Entry *first, bool bind_now, RtldLockState *lockstate)
2014 {
2015 	Obj_Entry *obj;
2016 
2017 	for (obj = first;  obj != NULL;  obj = obj->next) {
2018 		if (resolve_object_ifunc(obj, bind_now, lockstate) == -1)
2019 			return (-1);
2020 	}
2021 	return (0);
2022 }
2023 
2024 static int
2025 initlist_objects_ifunc(Objlist *list, bool bind_now, RtldLockState *lockstate)
2026 {
2027 	Objlist_Entry *elm;
2028 
2029 	STAILQ_FOREACH(elm, list, link) {
2030 		if (resolve_object_ifunc(elm->obj, bind_now, lockstate) == -1)
2031 			return (-1);
2032 	}
2033 	return (0);
2034 }
2035 
2036 /*
2037  * Cleanup procedure.  It will be called (by the atexit mechanism) just
2038  * before the process exits.
2039  */
2040 static void
2041 rtld_exit(void)
2042 {
2043     RtldLockState lockstate;
2044 
2045     wlock_acquire(rtld_bind_lock, &lockstate);
2046     dbg("rtld_exit()");
2047     objlist_call_fini(&list_fini, NULL, &lockstate);
2048     /* No need to remove the items from the list, since we are exiting. */
2049     if (!libmap_disable)
2050         lm_fini();
2051     lock_release(rtld_bind_lock, &lockstate);
2052 }
2053 
2054 static void *
2055 path_enumerate(const char *path, path_enum_proc callback, void *arg)
2056 {
2057 #ifdef COMPAT_32BIT
2058     const char *trans;
2059 #endif
2060     if (path == NULL)
2061 	return (NULL);
2062 
2063     path += strspn(path, ":;");
2064     while (*path != '\0') {
2065 	size_t len;
2066 	char  *res;
2067 
2068 	len = strcspn(path, ":;");
2069 #ifdef COMPAT_32BIT
2070 	trans = lm_findn(NULL, path, len);
2071 	if (trans)
2072 	    res = callback(trans, strlen(trans), arg);
2073 	else
2074 #endif
2075 	res = callback(path, len, arg);
2076 
2077 	if (res != NULL)
2078 	    return (res);
2079 
2080 	path += len;
2081 	path += strspn(path, ":;");
2082     }
2083 
2084     return (NULL);
2085 }
2086 
2087 struct try_library_args {
2088     const char	*name;
2089     size_t	 namelen;
2090     char	*buffer;
2091     size_t	 buflen;
2092 };
2093 
2094 static void *
2095 try_library_path(const char *dir, size_t dirlen, void *param)
2096 {
2097     struct try_library_args *arg;
2098 
2099     arg = param;
2100     if (*dir == '/' || trust) {
2101 	char *pathname;
2102 
2103 	if (dirlen + 1 + arg->namelen + 1 > arg->buflen)
2104 		return (NULL);
2105 
2106 	pathname = arg->buffer;
2107 	strncpy(pathname, dir, dirlen);
2108 	pathname[dirlen] = '/';
2109 	strcpy(pathname + dirlen + 1, arg->name);
2110 
2111 	dbg("  Trying \"%s\"", pathname);
2112 	if (access(pathname, F_OK) == 0) {		/* We found it */
2113 	    pathname = xmalloc(dirlen + 1 + arg->namelen + 1);
2114 	    strcpy(pathname, arg->buffer);
2115 	    return (pathname);
2116 	}
2117     }
2118     return (NULL);
2119 }
2120 
2121 static char *
2122 search_library_path(const char *name, const char *path)
2123 {
2124     char *p;
2125     struct try_library_args arg;
2126 
2127     if (path == NULL)
2128 	return NULL;
2129 
2130     arg.name = name;
2131     arg.namelen = strlen(name);
2132     arg.buffer = xmalloc(PATH_MAX);
2133     arg.buflen = PATH_MAX;
2134 
2135     p = path_enumerate(path, try_library_path, &arg);
2136 
2137     free(arg.buffer);
2138 
2139     return (p);
2140 }
2141 
2142 int
2143 dlclose(void *handle)
2144 {
2145     Obj_Entry *root;
2146     RtldLockState lockstate;
2147 
2148     wlock_acquire(rtld_bind_lock, &lockstate);
2149     root = dlcheck(handle);
2150     if (root == NULL) {
2151 	lock_release(rtld_bind_lock, &lockstate);
2152 	return -1;
2153     }
2154     LD_UTRACE(UTRACE_DLCLOSE_START, handle, NULL, 0, root->dl_refcount,
2155 	root->path);
2156 
2157     /* Unreference the object and its dependencies. */
2158     root->dl_refcount--;
2159 
2160     if (root->refcount == 1) {
2161 	/*
2162 	 * The object will be no longer referenced, so we must unload it.
2163 	 * First, call the fini functions.
2164 	 */
2165 	objlist_call_fini(&list_fini, root, &lockstate);
2166 
2167 	unref_dag(root);
2168 
2169 	/* Finish cleaning up the newly-unreferenced objects. */
2170 	GDB_STATE(RT_DELETE,&root->linkmap);
2171 	unload_object(root);
2172 	GDB_STATE(RT_CONSISTENT,NULL);
2173     } else
2174 	unref_dag(root);
2175 
2176     LD_UTRACE(UTRACE_DLCLOSE_STOP, handle, NULL, 0, 0, NULL);
2177     lock_release(rtld_bind_lock, &lockstate);
2178     return 0;
2179 }
2180 
2181 char *
2182 dlerror(void)
2183 {
2184     char *msg = error_message;
2185     error_message = NULL;
2186     return msg;
2187 }
2188 
2189 /*
2190  * This function is deprecated and has no effect.
2191  */
2192 void
2193 dllockinit(void *context,
2194 	   void *(*lock_create)(void *context),
2195            void (*rlock_acquire)(void *lock),
2196            void (*wlock_acquire)(void *lock),
2197            void (*lock_release)(void *lock),
2198            void (*lock_destroy)(void *lock),
2199 	   void (*context_destroy)(void *context))
2200 {
2201     static void *cur_context;
2202     static void (*cur_context_destroy)(void *);
2203 
2204     /* Just destroy the context from the previous call, if necessary. */
2205     if (cur_context_destroy != NULL)
2206 	cur_context_destroy(cur_context);
2207     cur_context = context;
2208     cur_context_destroy = context_destroy;
2209 }
2210 
2211 void *
2212 dlopen(const char *name, int mode)
2213 {
2214     RtldLockState lockstate;
2215     int lo_flags;
2216 
2217     LD_UTRACE(UTRACE_DLOPEN_START, NULL, NULL, 0, mode, name);
2218     ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1";
2219     if (ld_tracing != NULL) {
2220 	rlock_acquire(rtld_bind_lock, &lockstate);
2221 	if (sigsetjmp(lockstate.env, 0) != 0)
2222 	    lock_upgrade(rtld_bind_lock, &lockstate);
2223 	environ = (char **)*get_program_var_addr("environ", &lockstate);
2224 	lock_release(rtld_bind_lock, &lockstate);
2225     }
2226     lo_flags = RTLD_LO_DLOPEN;
2227     if (mode & RTLD_NODELETE)
2228 	    lo_flags |= RTLD_LO_NODELETE;
2229     if (mode & RTLD_NOLOAD)
2230 	    lo_flags |= RTLD_LO_NOLOAD;
2231     if (ld_tracing != NULL)
2232 	    lo_flags |= RTLD_LO_TRACE;
2233 
2234     return (dlopen_object(name, obj_main, lo_flags,
2235       mode & (RTLD_MODEMASK | RTLD_GLOBAL)));
2236 }
2237 
2238 static void
2239 dlopen_cleanup(Obj_Entry *obj)
2240 {
2241 
2242 	obj->dl_refcount--;
2243 	unref_dag(obj);
2244 	if (obj->refcount == 0)
2245 		unload_object(obj);
2246 }
2247 
2248 static Obj_Entry *
2249 dlopen_object(const char *name, Obj_Entry *refobj, int lo_flags, int mode)
2250 {
2251     Obj_Entry **old_obj_tail;
2252     Obj_Entry *obj;
2253     Objlist initlist;
2254     RtldLockState lockstate;
2255     int result;
2256 
2257     objlist_init(&initlist);
2258 
2259     wlock_acquire(rtld_bind_lock, &lockstate);
2260     GDB_STATE(RT_ADD,NULL);
2261 
2262     old_obj_tail = obj_tail;
2263     obj = NULL;
2264     if (name == NULL) {
2265 	obj = obj_main;
2266 	obj->refcount++;
2267     } else {
2268 	obj = load_object(name, refobj, lo_flags);
2269     }
2270 
2271     if (obj) {
2272 	obj->dl_refcount++;
2273 	if (mode & RTLD_GLOBAL && objlist_find(&list_global, obj) == NULL)
2274 	    objlist_push_tail(&list_global, obj);
2275 	if (*old_obj_tail != NULL) {		/* We loaded something new. */
2276 	    assert(*old_obj_tail == obj);
2277 	    result = load_needed_objects(obj, lo_flags & RTLD_LO_DLOPEN);
2278 	    init_dag(obj);
2279 	    ref_dag(obj);
2280 	    if (result != -1)
2281 		result = rtld_verify_versions(&obj->dagmembers);
2282 	    if (result != -1 && ld_tracing)
2283 		goto trace;
2284 	    if (result == -1 || (relocate_objects(obj, (mode & RTLD_MODEMASK)
2285 	      == RTLD_NOW, &obj_rtld, &lockstate)) == -1) {
2286 		dlopen_cleanup(obj);
2287 		obj = NULL;
2288 	    } else {
2289 		/* Make list of init functions to call. */
2290 		initlist_add_objects(obj, &obj->next, &initlist);
2291 	    }
2292 	} else {
2293 
2294 	    /*
2295 	     * Bump the reference counts for objects on this DAG.  If
2296 	     * this is the first dlopen() call for the object that was
2297 	     * already loaded as a dependency, initialize the dag
2298 	     * starting at it.
2299 	     */
2300 	    init_dag(obj);
2301 	    ref_dag(obj);
2302 
2303 	    if ((lo_flags & RTLD_LO_TRACE) != 0)
2304 		goto trace;
2305 	}
2306 	if (obj != NULL && ((lo_flags & RTLD_LO_NODELETE) != 0 ||
2307 	  obj->z_nodelete) && !obj->ref_nodel) {
2308 	    dbg("obj %s nodelete", obj->path);
2309 	    ref_dag(obj);
2310 	    obj->z_nodelete = obj->ref_nodel = true;
2311 	}
2312     }
2313 
2314     LD_UTRACE(UTRACE_DLOPEN_STOP, obj, NULL, 0, obj ? obj->dl_refcount : 0,
2315 	name);
2316     GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL);
2317 
2318     map_stacks_exec(&lockstate);
2319 
2320     if (initlist_objects_ifunc(&initlist, (mode & RTLD_MODEMASK) == RTLD_NOW,
2321       &lockstate) == -1) {
2322 	objlist_clear(&initlist);
2323 	dlopen_cleanup(obj);
2324 	lock_release(rtld_bind_lock, &lockstate);
2325 	return (NULL);
2326     }
2327 
2328     /* Call the init functions. */
2329     objlist_call_init(&initlist, &lockstate);
2330     objlist_clear(&initlist);
2331     lock_release(rtld_bind_lock, &lockstate);
2332     return obj;
2333 trace:
2334     trace_loaded_objects(obj);
2335     lock_release(rtld_bind_lock, &lockstate);
2336     exit(0);
2337 }
2338 
2339 static void *
2340 do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve,
2341     int flags)
2342 {
2343     DoneList donelist;
2344     const Obj_Entry *obj, *defobj;
2345     const Elf_Sym *def;
2346     SymLook req;
2347     RtldLockState lockstate;
2348     int res;
2349 
2350     def = NULL;
2351     defobj = NULL;
2352     symlook_init(&req, name);
2353     req.ventry = ve;
2354     req.flags = flags | SYMLOOK_IN_PLT;
2355     req.lockstate = &lockstate;
2356 
2357     rlock_acquire(rtld_bind_lock, &lockstate);
2358     if (sigsetjmp(lockstate.env, 0) != 0)
2359 	    lock_upgrade(rtld_bind_lock, &lockstate);
2360     if (handle == NULL || handle == RTLD_NEXT ||
2361 	handle == RTLD_DEFAULT || handle == RTLD_SELF) {
2362 
2363 	if ((obj = obj_from_addr(retaddr)) == NULL) {
2364 	    _rtld_error("Cannot determine caller's shared object");
2365 	    lock_release(rtld_bind_lock, &lockstate);
2366 	    return NULL;
2367 	}
2368 	if (handle == NULL) {	/* Just the caller's shared object. */
2369 	    res = symlook_obj(&req, obj);
2370 	    if (res == 0) {
2371 		def = req.sym_out;
2372 		defobj = req.defobj_out;
2373 	    }
2374 	} else if (handle == RTLD_NEXT || /* Objects after caller's */
2375 		   handle == RTLD_SELF) { /* ... caller included */
2376 	    if (handle == RTLD_NEXT)
2377 		obj = obj->next;
2378 	    for (; obj != NULL; obj = obj->next) {
2379 		res = symlook_obj(&req, obj);
2380 		if (res == 0) {
2381 		    if (def == NULL ||
2382 		      ELF_ST_BIND(req.sym_out->st_info) != STB_WEAK) {
2383 			def = req.sym_out;
2384 			defobj = req.defobj_out;
2385 			if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2386 			    break;
2387 		    }
2388 		}
2389 	    }
2390 	    /*
2391 	     * Search the dynamic linker itself, and possibly resolve the
2392 	     * symbol from there.  This is how the application links to
2393 	     * dynamic linker services such as dlopen.
2394 	     */
2395 	    if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2396 		res = symlook_obj(&req, &obj_rtld);
2397 		if (res == 0) {
2398 		    def = req.sym_out;
2399 		    defobj = req.defobj_out;
2400 		}
2401 	    }
2402 	} else {
2403 	    assert(handle == RTLD_DEFAULT);
2404 	    res = symlook_default(&req, obj);
2405 	    if (res == 0) {
2406 		defobj = req.defobj_out;
2407 		def = req.sym_out;
2408 	    }
2409 	}
2410     } else {
2411 	if ((obj = dlcheck(handle)) == NULL) {
2412 	    lock_release(rtld_bind_lock, &lockstate);
2413 	    return NULL;
2414 	}
2415 
2416 	donelist_init(&donelist);
2417 	if (obj->mainprog) {
2418             /* Handle obtained by dlopen(NULL, ...) implies global scope. */
2419 	    res = symlook_global(&req, &donelist);
2420 	    if (res == 0) {
2421 		def = req.sym_out;
2422 		defobj = req.defobj_out;
2423 	    }
2424 	    /*
2425 	     * Search the dynamic linker itself, and possibly resolve the
2426 	     * symbol from there.  This is how the application links to
2427 	     * dynamic linker services such as dlopen.
2428 	     */
2429 	    if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2430 		res = symlook_obj(&req, &obj_rtld);
2431 		if (res == 0) {
2432 		    def = req.sym_out;
2433 		    defobj = req.defobj_out;
2434 		}
2435 	    }
2436 	}
2437 	else {
2438 	    /* Search the whole DAG rooted at the given object. */
2439 	    res = symlook_list(&req, &obj->dagmembers, &donelist);
2440 	    if (res == 0) {
2441 		def = req.sym_out;
2442 		defobj = req.defobj_out;
2443 	    }
2444 	}
2445     }
2446 
2447     if (def != NULL) {
2448 	lock_release(rtld_bind_lock, &lockstate);
2449 
2450 	/*
2451 	 * The value required by the caller is derived from the value
2452 	 * of the symbol. For the ia64 architecture, we need to
2453 	 * construct a function descriptor which the caller can use to
2454 	 * call the function with the right 'gp' value. For other
2455 	 * architectures and for non-functions, the value is simply
2456 	 * the relocated value of the symbol.
2457 	 */
2458 	if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
2459 	    return (make_function_pointer(def, defobj));
2460 	else if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC)
2461 	    return (rtld_resolve_ifunc(defobj, def));
2462 	else
2463 	    return (defobj->relocbase + def->st_value);
2464     }
2465 
2466     _rtld_error("Undefined symbol \"%s\"", name);
2467     lock_release(rtld_bind_lock, &lockstate);
2468     return NULL;
2469 }
2470 
2471 void *
2472 dlsym(void *handle, const char *name)
2473 {
2474 	return do_dlsym(handle, name, __builtin_return_address(0), NULL,
2475 	    SYMLOOK_DLSYM);
2476 }
2477 
2478 dlfunc_t
2479 dlfunc(void *handle, const char *name)
2480 {
2481 	union {
2482 		void *d;
2483 		dlfunc_t f;
2484 	} rv;
2485 
2486 	rv.d = do_dlsym(handle, name, __builtin_return_address(0), NULL,
2487 	    SYMLOOK_DLSYM);
2488 	return (rv.f);
2489 }
2490 
2491 void *
2492 dlvsym(void *handle, const char *name, const char *version)
2493 {
2494 	Ver_Entry ventry;
2495 
2496 	ventry.name = version;
2497 	ventry.file = NULL;
2498 	ventry.hash = elf_hash(version);
2499 	ventry.flags= 0;
2500 	return do_dlsym(handle, name, __builtin_return_address(0), &ventry,
2501 	    SYMLOOK_DLSYM);
2502 }
2503 
2504 int
2505 _rtld_addr_phdr(const void *addr, struct dl_phdr_info *phdr_info)
2506 {
2507     const Obj_Entry *obj;
2508     RtldLockState lockstate;
2509 
2510     rlock_acquire(rtld_bind_lock, &lockstate);
2511     obj = obj_from_addr(addr);
2512     if (obj == NULL) {
2513         _rtld_error("No shared object contains address");
2514 	lock_release(rtld_bind_lock, &lockstate);
2515         return (0);
2516     }
2517     rtld_fill_dl_phdr_info(obj, phdr_info);
2518     lock_release(rtld_bind_lock, &lockstate);
2519     return (1);
2520 }
2521 
2522 int
2523 dladdr(const void *addr, Dl_info *info)
2524 {
2525     const Obj_Entry *obj;
2526     const Elf_Sym *def;
2527     void *symbol_addr;
2528     unsigned long symoffset;
2529     RtldLockState lockstate;
2530 
2531     rlock_acquire(rtld_bind_lock, &lockstate);
2532     obj = obj_from_addr(addr);
2533     if (obj == NULL) {
2534         _rtld_error("No shared object contains address");
2535 	lock_release(rtld_bind_lock, &lockstate);
2536         return 0;
2537     }
2538     info->dli_fname = obj->path;
2539     info->dli_fbase = obj->mapbase;
2540     info->dli_saddr = (void *)0;
2541     info->dli_sname = NULL;
2542 
2543     /*
2544      * Walk the symbol list looking for the symbol whose address is
2545      * closest to the address sent in.
2546      */
2547     for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
2548         def = obj->symtab + symoffset;
2549 
2550         /*
2551          * For skip the symbol if st_shndx is either SHN_UNDEF or
2552          * SHN_COMMON.
2553          */
2554         if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
2555             continue;
2556 
2557         /*
2558          * If the symbol is greater than the specified address, or if it
2559          * is further away from addr than the current nearest symbol,
2560          * then reject it.
2561          */
2562         symbol_addr = obj->relocbase + def->st_value;
2563         if (symbol_addr > addr || symbol_addr < info->dli_saddr)
2564             continue;
2565 
2566         /* Update our idea of the nearest symbol. */
2567         info->dli_sname = obj->strtab + def->st_name;
2568         info->dli_saddr = symbol_addr;
2569 
2570         /* Exact match? */
2571         if (info->dli_saddr == addr)
2572             break;
2573     }
2574     lock_release(rtld_bind_lock, &lockstate);
2575     return 1;
2576 }
2577 
2578 int
2579 dlinfo(void *handle, int request, void *p)
2580 {
2581     const Obj_Entry *obj;
2582     RtldLockState lockstate;
2583     int error;
2584 
2585     rlock_acquire(rtld_bind_lock, &lockstate);
2586 
2587     if (handle == NULL || handle == RTLD_SELF) {
2588 	void *retaddr;
2589 
2590 	retaddr = __builtin_return_address(0);	/* __GNUC__ only */
2591 	if ((obj = obj_from_addr(retaddr)) == NULL)
2592 	    _rtld_error("Cannot determine caller's shared object");
2593     } else
2594 	obj = dlcheck(handle);
2595 
2596     if (obj == NULL) {
2597 	lock_release(rtld_bind_lock, &lockstate);
2598 	return (-1);
2599     }
2600 
2601     error = 0;
2602     switch (request) {
2603     case RTLD_DI_LINKMAP:
2604 	*((struct link_map const **)p) = &obj->linkmap;
2605 	break;
2606     case RTLD_DI_ORIGIN:
2607 	error = rtld_dirname(obj->path, p);
2608 	break;
2609 
2610     case RTLD_DI_SERINFOSIZE:
2611     case RTLD_DI_SERINFO:
2612 	error = do_search_info(obj, request, (struct dl_serinfo *)p);
2613 	break;
2614 
2615     default:
2616 	_rtld_error("Invalid request %d passed to dlinfo()", request);
2617 	error = -1;
2618     }
2619 
2620     lock_release(rtld_bind_lock, &lockstate);
2621 
2622     return (error);
2623 }
2624 
2625 static void
2626 rtld_fill_dl_phdr_info(const Obj_Entry *obj, struct dl_phdr_info *phdr_info)
2627 {
2628 
2629 	phdr_info->dlpi_addr = (Elf_Addr)obj->relocbase;
2630 	phdr_info->dlpi_name = STAILQ_FIRST(&obj->names) ?
2631 	    STAILQ_FIRST(&obj->names)->name : obj->path;
2632 	phdr_info->dlpi_phdr = obj->phdr;
2633 	phdr_info->dlpi_phnum = obj->phsize / sizeof(obj->phdr[0]);
2634 	phdr_info->dlpi_tls_modid = obj->tlsindex;
2635 	phdr_info->dlpi_tls_data = obj->tlsinit;
2636 	phdr_info->dlpi_adds = obj_loads;
2637 	phdr_info->dlpi_subs = obj_loads - obj_count;
2638 }
2639 
2640 int
2641 dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param)
2642 {
2643     struct dl_phdr_info phdr_info;
2644     const Obj_Entry *obj;
2645     RtldLockState bind_lockstate, phdr_lockstate;
2646     int error;
2647 
2648     wlock_acquire(rtld_phdr_lock, &phdr_lockstate);
2649     rlock_acquire(rtld_bind_lock, &bind_lockstate);
2650 
2651     error = 0;
2652 
2653     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
2654 	rtld_fill_dl_phdr_info(obj, &phdr_info);
2655 	if ((error = callback(&phdr_info, sizeof phdr_info, param)) != 0)
2656 		break;
2657 
2658     }
2659     lock_release(rtld_bind_lock, &bind_lockstate);
2660     lock_release(rtld_phdr_lock, &phdr_lockstate);
2661 
2662     return (error);
2663 }
2664 
2665 struct fill_search_info_args {
2666     int		 request;
2667     unsigned int flags;
2668     Dl_serinfo  *serinfo;
2669     Dl_serpath  *serpath;
2670     char	*strspace;
2671 };
2672 
2673 static void *
2674 fill_search_info(const char *dir, size_t dirlen, void *param)
2675 {
2676     struct fill_search_info_args *arg;
2677 
2678     arg = param;
2679 
2680     if (arg->request == RTLD_DI_SERINFOSIZE) {
2681 	arg->serinfo->dls_cnt ++;
2682 	arg->serinfo->dls_size += sizeof(Dl_serpath) + dirlen + 1;
2683     } else {
2684 	struct dl_serpath *s_entry;
2685 
2686 	s_entry = arg->serpath;
2687 	s_entry->dls_name  = arg->strspace;
2688 	s_entry->dls_flags = arg->flags;
2689 
2690 	strncpy(arg->strspace, dir, dirlen);
2691 	arg->strspace[dirlen] = '\0';
2692 
2693 	arg->strspace += dirlen + 1;
2694 	arg->serpath++;
2695     }
2696 
2697     return (NULL);
2698 }
2699 
2700 static int
2701 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
2702 {
2703     struct dl_serinfo _info;
2704     struct fill_search_info_args args;
2705 
2706     args.request = RTLD_DI_SERINFOSIZE;
2707     args.serinfo = &_info;
2708 
2709     _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
2710     _info.dls_cnt  = 0;
2711 
2712     path_enumerate(ld_library_path, fill_search_info, &args);
2713     path_enumerate(obj->rpath, fill_search_info, &args);
2714     path_enumerate(gethints(), fill_search_info, &args);
2715     path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args);
2716 
2717 
2718     if (request == RTLD_DI_SERINFOSIZE) {
2719 	info->dls_size = _info.dls_size;
2720 	info->dls_cnt = _info.dls_cnt;
2721 	return (0);
2722     }
2723 
2724     if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
2725 	_rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2726 	return (-1);
2727     }
2728 
2729     args.request  = RTLD_DI_SERINFO;
2730     args.serinfo  = info;
2731     args.serpath  = &info->dls_serpath[0];
2732     args.strspace = (char *)&info->dls_serpath[_info.dls_cnt];
2733 
2734     args.flags = LA_SER_LIBPATH;
2735     if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
2736 	return (-1);
2737 
2738     args.flags = LA_SER_RUNPATH;
2739     if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
2740 	return (-1);
2741 
2742     args.flags = LA_SER_CONFIG;
2743     if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
2744 	return (-1);
2745 
2746     args.flags = LA_SER_DEFAULT;
2747     if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
2748 	return (-1);
2749     return (0);
2750 }
2751 
2752 static int
2753 rtld_dirname(const char *path, char *bname)
2754 {
2755     const char *endp;
2756 
2757     /* Empty or NULL string gets treated as "." */
2758     if (path == NULL || *path == '\0') {
2759 	bname[0] = '.';
2760 	bname[1] = '\0';
2761 	return (0);
2762     }
2763 
2764     /* Strip trailing slashes */
2765     endp = path + strlen(path) - 1;
2766     while (endp > path && *endp == '/')
2767 	endp--;
2768 
2769     /* Find the start of the dir */
2770     while (endp > path && *endp != '/')
2771 	endp--;
2772 
2773     /* Either the dir is "/" or there are no slashes */
2774     if (endp == path) {
2775 	bname[0] = *endp == '/' ? '/' : '.';
2776 	bname[1] = '\0';
2777 	return (0);
2778     } else {
2779 	do {
2780 	    endp--;
2781 	} while (endp > path && *endp == '/');
2782     }
2783 
2784     if (endp - path + 2 > PATH_MAX)
2785     {
2786 	_rtld_error("Filename is too long: %s", path);
2787 	return(-1);
2788     }
2789 
2790     strncpy(bname, path, endp - path + 1);
2791     bname[endp - path + 1] = '\0';
2792     return (0);
2793 }
2794 
2795 static int
2796 rtld_dirname_abs(const char *path, char *base)
2797 {
2798 	char base_rel[PATH_MAX];
2799 
2800 	if (rtld_dirname(path, base) == -1)
2801 		return (-1);
2802 	if (base[0] == '/')
2803 		return (0);
2804 	if (getcwd(base_rel, sizeof(base_rel)) == NULL ||
2805 	    strlcat(base_rel, "/", sizeof(base_rel)) >= sizeof(base_rel) ||
2806 	    strlcat(base_rel, base, sizeof(base_rel)) >= sizeof(base_rel))
2807 		return (-1);
2808 	strcpy(base, base_rel);
2809 	return (0);
2810 }
2811 
2812 static void
2813 linkmap_add(Obj_Entry *obj)
2814 {
2815     struct link_map *l = &obj->linkmap;
2816     struct link_map *prev;
2817 
2818     obj->linkmap.l_name = obj->path;
2819     obj->linkmap.l_addr = obj->mapbase;
2820     obj->linkmap.l_ld = obj->dynamic;
2821 #ifdef __mips__
2822     /* GDB needs load offset on MIPS to use the symbols */
2823     obj->linkmap.l_offs = obj->relocbase;
2824 #endif
2825 
2826     if (r_debug.r_map == NULL) {
2827 	r_debug.r_map = l;
2828 	return;
2829     }
2830 
2831     /*
2832      * Scan to the end of the list, but not past the entry for the
2833      * dynamic linker, which we want to keep at the very end.
2834      */
2835     for (prev = r_debug.r_map;
2836       prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
2837       prev = prev->l_next)
2838 	;
2839 
2840     /* Link in the new entry. */
2841     l->l_prev = prev;
2842     l->l_next = prev->l_next;
2843     if (l->l_next != NULL)
2844 	l->l_next->l_prev = l;
2845     prev->l_next = l;
2846 }
2847 
2848 static void
2849 linkmap_delete(Obj_Entry *obj)
2850 {
2851     struct link_map *l = &obj->linkmap;
2852 
2853     if (l->l_prev == NULL) {
2854 	if ((r_debug.r_map = l->l_next) != NULL)
2855 	    l->l_next->l_prev = NULL;
2856 	return;
2857     }
2858 
2859     if ((l->l_prev->l_next = l->l_next) != NULL)
2860 	l->l_next->l_prev = l->l_prev;
2861 }
2862 
2863 /*
2864  * Function for the debugger to set a breakpoint on to gain control.
2865  *
2866  * The two parameters allow the debugger to easily find and determine
2867  * what the runtime loader is doing and to whom it is doing it.
2868  *
2869  * When the loadhook trap is hit (r_debug_state, set at program
2870  * initialization), the arguments can be found on the stack:
2871  *
2872  *  +8   struct link_map *m
2873  *  +4   struct r_debug  *rd
2874  *  +0   RetAddr
2875  */
2876 void
2877 r_debug_state(struct r_debug* rd, struct link_map *m)
2878 {
2879     /*
2880      * The following is a hack to force the compiler to emit calls to
2881      * this function, even when optimizing.  If the function is empty,
2882      * the compiler is not obliged to emit any code for calls to it,
2883      * even when marked __noinline.  However, gdb depends on those
2884      * calls being made.
2885      */
2886     __asm __volatile("" : : : "memory");
2887 }
2888 
2889 /*
2890  * Get address of the pointer variable in the main program.
2891  * Prefer non-weak symbol over the weak one.
2892  */
2893 static const void **
2894 get_program_var_addr(const char *name, RtldLockState *lockstate)
2895 {
2896     SymLook req;
2897     DoneList donelist;
2898 
2899     symlook_init(&req, name);
2900     req.lockstate = lockstate;
2901     donelist_init(&donelist);
2902     if (symlook_global(&req, &donelist) != 0)
2903 	return (NULL);
2904     if (ELF_ST_TYPE(req.sym_out->st_info) == STT_FUNC)
2905 	return ((const void **)make_function_pointer(req.sym_out,
2906 	  req.defobj_out));
2907     else if (ELF_ST_TYPE(req.sym_out->st_info) == STT_GNU_IFUNC)
2908 	return ((const void **)rtld_resolve_ifunc(req.defobj_out, req.sym_out));
2909     else
2910 	return ((const void **)(req.defobj_out->relocbase +
2911 	  req.sym_out->st_value));
2912 }
2913 
2914 /*
2915  * Set a pointer variable in the main program to the given value.  This
2916  * is used to set key variables such as "environ" before any of the
2917  * init functions are called.
2918  */
2919 static void
2920 set_program_var(const char *name, const void *value)
2921 {
2922     const void **addr;
2923 
2924     if ((addr = get_program_var_addr(name, NULL)) != NULL) {
2925 	dbg("\"%s\": *%p <-- %p", name, addr, value);
2926 	*addr = value;
2927     }
2928 }
2929 
2930 /*
2931  * Search the global objects, including dependencies and main object,
2932  * for the given symbol.
2933  */
2934 static int
2935 symlook_global(SymLook *req, DoneList *donelist)
2936 {
2937     SymLook req1;
2938     const Objlist_Entry *elm;
2939     int res;
2940 
2941     symlook_init_from_req(&req1, req);
2942 
2943     /* Search all objects loaded at program start up. */
2944     if (req->defobj_out == NULL ||
2945       ELF_ST_BIND(req->sym_out->st_info) == STB_WEAK) {
2946 	res = symlook_list(&req1, &list_main, donelist);
2947 	if (res == 0 && (req->defobj_out == NULL ||
2948 	  ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) {
2949 	    req->sym_out = req1.sym_out;
2950 	    req->defobj_out = req1.defobj_out;
2951 	    assert(req->defobj_out != NULL);
2952 	}
2953     }
2954 
2955     /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2956     STAILQ_FOREACH(elm, &list_global, link) {
2957 	if (req->defobj_out != NULL &&
2958 	  ELF_ST_BIND(req->sym_out->st_info) != STB_WEAK)
2959 	    break;
2960 	res = symlook_list(&req1, &elm->obj->dagmembers, donelist);
2961 	if (res == 0 && (req->defobj_out == NULL ||
2962 	  ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) {
2963 	    req->sym_out = req1.sym_out;
2964 	    req->defobj_out = req1.defobj_out;
2965 	    assert(req->defobj_out != NULL);
2966 	}
2967     }
2968 
2969     return (req->sym_out != NULL ? 0 : ESRCH);
2970 }
2971 
2972 /*
2973  * Given a symbol name in a referencing object, find the corresponding
2974  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
2975  * no definition was found.  Returns a pointer to the Obj_Entry of the
2976  * defining object via the reference parameter DEFOBJ_OUT.
2977  */
2978 static int
2979 symlook_default(SymLook *req, const Obj_Entry *refobj)
2980 {
2981     DoneList donelist;
2982     const Objlist_Entry *elm;
2983     SymLook req1;
2984     int res;
2985 
2986     donelist_init(&donelist);
2987     symlook_init_from_req(&req1, req);
2988 
2989     /* Look first in the referencing object if linked symbolically. */
2990     if (refobj->symbolic && !donelist_check(&donelist, refobj)) {
2991 	res = symlook_obj(&req1, refobj);
2992 	if (res == 0) {
2993 	    req->sym_out = req1.sym_out;
2994 	    req->defobj_out = req1.defobj_out;
2995 	    assert(req->defobj_out != NULL);
2996 	}
2997     }
2998 
2999     symlook_global(req, &donelist);
3000 
3001     /* Search all dlopened DAGs containing the referencing object. */
3002     STAILQ_FOREACH(elm, &refobj->dldags, link) {
3003 	if (req->sym_out != NULL &&
3004 	  ELF_ST_BIND(req->sym_out->st_info) != STB_WEAK)
3005 	    break;
3006 	res = symlook_list(&req1, &elm->obj->dagmembers, &donelist);
3007 	if (res == 0 && (req->sym_out == NULL ||
3008 	  ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) {
3009 	    req->sym_out = req1.sym_out;
3010 	    req->defobj_out = req1.defobj_out;
3011 	    assert(req->defobj_out != NULL);
3012 	}
3013     }
3014 
3015     /*
3016      * Search the dynamic linker itself, and possibly resolve the
3017      * symbol from there.  This is how the application links to
3018      * dynamic linker services such as dlopen.
3019      */
3020     if (req->sym_out == NULL ||
3021       ELF_ST_BIND(req->sym_out->st_info) == STB_WEAK) {
3022 	res = symlook_obj(&req1, &obj_rtld);
3023 	if (res == 0) {
3024 	    req->sym_out = req1.sym_out;
3025 	    req->defobj_out = req1.defobj_out;
3026 	    assert(req->defobj_out != NULL);
3027 	}
3028     }
3029 
3030     return (req->sym_out != NULL ? 0 : ESRCH);
3031 }
3032 
3033 static int
3034 symlook_list(SymLook *req, const Objlist *objlist, DoneList *dlp)
3035 {
3036     const Elf_Sym *def;
3037     const Obj_Entry *defobj;
3038     const Objlist_Entry *elm;
3039     SymLook req1;
3040     int res;
3041 
3042     def = NULL;
3043     defobj = NULL;
3044     STAILQ_FOREACH(elm, objlist, link) {
3045 	if (donelist_check(dlp, elm->obj))
3046 	    continue;
3047 	symlook_init_from_req(&req1, req);
3048 	if ((res = symlook_obj(&req1, elm->obj)) == 0) {
3049 	    if (def == NULL || ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK) {
3050 		def = req1.sym_out;
3051 		defobj = req1.defobj_out;
3052 		if (ELF_ST_BIND(def->st_info) != STB_WEAK)
3053 		    break;
3054 	    }
3055 	}
3056     }
3057     if (def != NULL) {
3058 	req->sym_out = def;
3059 	req->defobj_out = defobj;
3060 	return (0);
3061     }
3062     return (ESRCH);
3063 }
3064 
3065 /*
3066  * Search the chain of DAGS cointed to by the given Needed_Entry
3067  * for a symbol of the given name.  Each DAG is scanned completely
3068  * before advancing to the next one.  Returns a pointer to the symbol,
3069  * or NULL if no definition was found.
3070  */
3071 static int
3072 symlook_needed(SymLook *req, const Needed_Entry *needed, DoneList *dlp)
3073 {
3074     const Elf_Sym *def;
3075     const Needed_Entry *n;
3076     const Obj_Entry *defobj;
3077     SymLook req1;
3078     int res;
3079 
3080     def = NULL;
3081     defobj = NULL;
3082     symlook_init_from_req(&req1, req);
3083     for (n = needed; n != NULL; n = n->next) {
3084 	if (n->obj == NULL ||
3085 	    (res = symlook_list(&req1, &n->obj->dagmembers, dlp)) != 0)
3086 	    continue;
3087 	if (def == NULL || ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK) {
3088 	    def = req1.sym_out;
3089 	    defobj = req1.defobj_out;
3090 	    if (ELF_ST_BIND(def->st_info) != STB_WEAK)
3091 		break;
3092 	}
3093     }
3094     if (def != NULL) {
3095 	req->sym_out = def;
3096 	req->defobj_out = defobj;
3097 	return (0);
3098     }
3099     return (ESRCH);
3100 }
3101 
3102 /*
3103  * Search the symbol table of a single shared object for a symbol of
3104  * the given name and version, if requested.  Returns a pointer to the
3105  * symbol, or NULL if no definition was found.  If the object is
3106  * filter, return filtered symbol from filtee.
3107  *
3108  * The symbol's hash value is passed in for efficiency reasons; that
3109  * eliminates many recomputations of the hash value.
3110  */
3111 int
3112 symlook_obj(SymLook *req, const Obj_Entry *obj)
3113 {
3114     DoneList donelist;
3115     SymLook req1;
3116     int res, mres;
3117 
3118     mres = symlook_obj1(req, obj);
3119     if (mres == 0) {
3120 	if (obj->needed_filtees != NULL) {
3121 	    load_filtees(__DECONST(Obj_Entry *, obj), 0, req->lockstate);
3122 	    donelist_init(&donelist);
3123 	    symlook_init_from_req(&req1, req);
3124 	    res = symlook_needed(&req1, obj->needed_filtees, &donelist);
3125 	    if (res == 0) {
3126 		req->sym_out = req1.sym_out;
3127 		req->defobj_out = req1.defobj_out;
3128 	    }
3129 	    return (res);
3130 	}
3131 	if (obj->needed_aux_filtees != NULL) {
3132 	    load_filtees(__DECONST(Obj_Entry *, obj), 0, req->lockstate);
3133 	    donelist_init(&donelist);
3134 	    symlook_init_from_req(&req1, req);
3135 	    res = symlook_needed(&req1, obj->needed_aux_filtees, &donelist);
3136 	    if (res == 0) {
3137 		req->sym_out = req1.sym_out;
3138 		req->defobj_out = req1.defobj_out;
3139 		return (res);
3140 	    }
3141 	}
3142     }
3143     return (mres);
3144 }
3145 
3146 static int
3147 symlook_obj1(SymLook *req, const Obj_Entry *obj)
3148 {
3149     unsigned long symnum;
3150     const Elf_Sym *vsymp;
3151     Elf_Versym verndx;
3152     int vcount;
3153 
3154     if (obj->buckets == NULL)
3155 	return (ESRCH);
3156 
3157     vsymp = NULL;
3158     vcount = 0;
3159     symnum = obj->buckets[req->hash % obj->nbuckets];
3160 
3161     for (; symnum != STN_UNDEF; symnum = obj->chains[symnum]) {
3162 	const Elf_Sym *symp;
3163 	const char *strp;
3164 
3165 	if (symnum >= obj->nchains)
3166 	    return (ESRCH);	/* Bad object */
3167 
3168 	symp = obj->symtab + symnum;
3169 	strp = obj->strtab + symp->st_name;
3170 
3171 	switch (ELF_ST_TYPE(symp->st_info)) {
3172 	case STT_FUNC:
3173 	case STT_NOTYPE:
3174 	case STT_OBJECT:
3175 	case STT_GNU_IFUNC:
3176 	    if (symp->st_value == 0)
3177 		continue;
3178 		/* fallthrough */
3179 	case STT_TLS:
3180 	    if (symp->st_shndx != SHN_UNDEF)
3181 		break;
3182 #ifndef __mips__
3183 	    else if (((req->flags & SYMLOOK_IN_PLT) == 0) &&
3184 		 (ELF_ST_TYPE(symp->st_info) == STT_FUNC))
3185 		break;
3186 		/* fallthrough */
3187 #endif
3188 	default:
3189 	    continue;
3190 	}
3191 	if (req->name[0] != strp[0] || strcmp(req->name, strp) != 0)
3192 	    continue;
3193 
3194 	if (req->ventry == NULL) {
3195 	    if (obj->versyms != NULL) {
3196 		verndx = VER_NDX(obj->versyms[symnum]);
3197 		if (verndx > obj->vernum) {
3198 		    _rtld_error("%s: symbol %s references wrong version %d",
3199 			obj->path, obj->strtab + symnum, verndx);
3200 		    continue;
3201 		}
3202 		/*
3203 		 * If we are not called from dlsym (i.e. this is a normal
3204 		 * relocation from unversioned binary), accept the symbol
3205 		 * immediately if it happens to have first version after
3206 		 * this shared object became versioned. Otherwise, if
3207 		 * symbol is versioned and not hidden, remember it. If it
3208 		 * is the only symbol with this name exported by the
3209 		 * shared object, it will be returned as a match at the
3210 		 * end of the function. If symbol is global (verndx < 2)
3211 		 * accept it unconditionally.
3212 		 */
3213 		if ((req->flags & SYMLOOK_DLSYM) == 0 &&
3214 		  verndx == VER_NDX_GIVEN) {
3215 		    req->sym_out = symp;
3216 		    req->defobj_out = obj;
3217 		    return (0);
3218 		}
3219 		else if (verndx >= VER_NDX_GIVEN) {
3220 		    if ((obj->versyms[symnum] & VER_NDX_HIDDEN) == 0) {
3221 			if (vsymp == NULL)
3222 			    vsymp = symp;
3223 			vcount ++;
3224 		    }
3225 		    continue;
3226 		}
3227 	    }
3228 	    req->sym_out = symp;
3229 	    req->defobj_out = obj;
3230 	    return (0);
3231 	} else {
3232 	    if (obj->versyms == NULL) {
3233 		if (object_match_name(obj, req->ventry->name)) {
3234 		    _rtld_error("%s: object %s should provide version %s for "
3235 			"symbol %s", obj_rtld.path, obj->path,
3236 			req->ventry->name, obj->strtab + symnum);
3237 		    continue;
3238 		}
3239 	    } else {
3240 		verndx = VER_NDX(obj->versyms[symnum]);
3241 		if (verndx > obj->vernum) {
3242 		    _rtld_error("%s: symbol %s references wrong version %d",
3243 			obj->path, obj->strtab + symnum, verndx);
3244 		    continue;
3245 		}
3246 		if (obj->vertab[verndx].hash != req->ventry->hash ||
3247 		    strcmp(obj->vertab[verndx].name, req->ventry->name)) {
3248 		    /*
3249 		     * Version does not match. Look if this is a global symbol
3250 		     * and if it is not hidden. If global symbol (verndx < 2)
3251 		     * is available, use it. Do not return symbol if we are
3252 		     * called by dlvsym, because dlvsym looks for a specific
3253 		     * version and default one is not what dlvsym wants.
3254 		     */
3255 		    if ((req->flags & SYMLOOK_DLSYM) ||
3256 			(obj->versyms[symnum] & VER_NDX_HIDDEN) ||
3257 			(verndx >= VER_NDX_GIVEN))
3258 			continue;
3259 		}
3260 	    }
3261 	    req->sym_out = symp;
3262 	    req->defobj_out = obj;
3263 	    return (0);
3264 	}
3265     }
3266     if (vcount == 1) {
3267 	req->sym_out = vsymp;
3268 	req->defobj_out = obj;
3269 	return (0);
3270     }
3271     return (ESRCH);
3272 }
3273 
3274 static void
3275 trace_loaded_objects(Obj_Entry *obj)
3276 {
3277     char	*fmt1, *fmt2, *fmt, *main_local, *list_containers;
3278     int		c;
3279 
3280     if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
3281 	main_local = "";
3282 
3283     if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL)
3284 	fmt1 = "\t%o => %p (%x)\n";
3285 
3286     if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL)
3287 	fmt2 = "\t%o (%x)\n";
3288 
3289     list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL");
3290 
3291     for (; obj; obj = obj->next) {
3292 	Needed_Entry		*needed;
3293 	char			*name, *path;
3294 	bool			is_lib;
3295 
3296 	if (list_containers && obj->needed != NULL)
3297 	    rtld_printf("%s:\n", obj->path);
3298 	for (needed = obj->needed; needed; needed = needed->next) {
3299 	    if (needed->obj != NULL) {
3300 		if (needed->obj->traced && !list_containers)
3301 		    continue;
3302 		needed->obj->traced = true;
3303 		path = needed->obj->path;
3304 	    } else
3305 		path = "not found";
3306 
3307 	    name = (char *)obj->strtab + needed->name;
3308 	    is_lib = strncmp(name, "lib", 3) == 0;	/* XXX - bogus */
3309 
3310 	    fmt = is_lib ? fmt1 : fmt2;
3311 	    while ((c = *fmt++) != '\0') {
3312 		switch (c) {
3313 		default:
3314 		    rtld_putchar(c);
3315 		    continue;
3316 		case '\\':
3317 		    switch (c = *fmt) {
3318 		    case '\0':
3319 			continue;
3320 		    case 'n':
3321 			rtld_putchar('\n');
3322 			break;
3323 		    case 't':
3324 			rtld_putchar('\t');
3325 			break;
3326 		    }
3327 		    break;
3328 		case '%':
3329 		    switch (c = *fmt) {
3330 		    case '\0':
3331 			continue;
3332 		    case '%':
3333 		    default:
3334 			rtld_putchar(c);
3335 			break;
3336 		    case 'A':
3337 			rtld_putstr(main_local);
3338 			break;
3339 		    case 'a':
3340 			rtld_putstr(obj_main->path);
3341 			break;
3342 		    case 'o':
3343 			rtld_putstr(name);
3344 			break;
3345 #if 0
3346 		    case 'm':
3347 			rtld_printf("%d", sodp->sod_major);
3348 			break;
3349 		    case 'n':
3350 			rtld_printf("%d", sodp->sod_minor);
3351 			break;
3352 #endif
3353 		    case 'p':
3354 			rtld_putstr(path);
3355 			break;
3356 		    case 'x':
3357 			rtld_printf("%p", needed->obj ? needed->obj->mapbase :
3358 			  0);
3359 			break;
3360 		    }
3361 		    break;
3362 		}
3363 		++fmt;
3364 	    }
3365 	}
3366     }
3367 }
3368 
3369 /*
3370  * Unload a dlopened object and its dependencies from memory and from
3371  * our data structures.  It is assumed that the DAG rooted in the
3372  * object has already been unreferenced, and that the object has a
3373  * reference count of 0.
3374  */
3375 static void
3376 unload_object(Obj_Entry *root)
3377 {
3378     Obj_Entry *obj;
3379     Obj_Entry **linkp;
3380 
3381     assert(root->refcount == 0);
3382 
3383     /*
3384      * Pass over the DAG removing unreferenced objects from
3385      * appropriate lists.
3386      */
3387     unlink_object(root);
3388 
3389     /* Unmap all objects that are no longer referenced. */
3390     linkp = &obj_list->next;
3391     while ((obj = *linkp) != NULL) {
3392 	if (obj->refcount == 0) {
3393 	    LD_UTRACE(UTRACE_UNLOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0,
3394 		obj->path);
3395 	    dbg("unloading \"%s\"", obj->path);
3396 	    unload_filtees(root);
3397 	    munmap(obj->mapbase, obj->mapsize);
3398 	    linkmap_delete(obj);
3399 	    *linkp = obj->next;
3400 	    obj_count--;
3401 	    obj_free(obj);
3402 	} else
3403 	    linkp = &obj->next;
3404     }
3405     obj_tail = linkp;
3406 }
3407 
3408 static void
3409 unlink_object(Obj_Entry *root)
3410 {
3411     Objlist_Entry *elm;
3412 
3413     if (root->refcount == 0) {
3414 	/* Remove the object from the RTLD_GLOBAL list. */
3415 	objlist_remove(&list_global, root);
3416 
3417     	/* Remove the object from all objects' DAG lists. */
3418     	STAILQ_FOREACH(elm, &root->dagmembers, link) {
3419 	    objlist_remove(&elm->obj->dldags, root);
3420 	    if (elm->obj != root)
3421 		unlink_object(elm->obj);
3422 	}
3423     }
3424 }
3425 
3426 static void
3427 ref_dag(Obj_Entry *root)
3428 {
3429     Objlist_Entry *elm;
3430 
3431     assert(root->dag_inited);
3432     STAILQ_FOREACH(elm, &root->dagmembers, link)
3433 	elm->obj->refcount++;
3434 }
3435 
3436 static void
3437 unref_dag(Obj_Entry *root)
3438 {
3439     Objlist_Entry *elm;
3440 
3441     assert(root->dag_inited);
3442     STAILQ_FOREACH(elm, &root->dagmembers, link)
3443 	elm->obj->refcount--;
3444 }
3445 
3446 /*
3447  * Common code for MD __tls_get_addr().
3448  */
3449 void *
3450 tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset)
3451 {
3452     Elf_Addr* dtv = *dtvp;
3453     RtldLockState lockstate;
3454 
3455     /* Check dtv generation in case new modules have arrived */
3456     if (dtv[0] != tls_dtv_generation) {
3457 	Elf_Addr* newdtv;
3458 	int to_copy;
3459 
3460 	wlock_acquire(rtld_bind_lock, &lockstate);
3461 	newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
3462 	to_copy = dtv[1];
3463 	if (to_copy > tls_max_index)
3464 	    to_copy = tls_max_index;
3465 	memcpy(&newdtv[2], &dtv[2], to_copy * sizeof(Elf_Addr));
3466 	newdtv[0] = tls_dtv_generation;
3467 	newdtv[1] = tls_max_index;
3468 	free(dtv);
3469 	lock_release(rtld_bind_lock, &lockstate);
3470 	dtv = *dtvp = newdtv;
3471     }
3472 
3473     /* Dynamically allocate module TLS if necessary */
3474     if (!dtv[index + 1]) {
3475 	/* Signal safe, wlock will block out signals. */
3476 	    wlock_acquire(rtld_bind_lock, &lockstate);
3477 	if (!dtv[index + 1])
3478 	    dtv[index + 1] = (Elf_Addr)allocate_module_tls(index);
3479 	lock_release(rtld_bind_lock, &lockstate);
3480     }
3481     return (void*) (dtv[index + 1] + offset);
3482 }
3483 
3484 /* XXX not sure what variants to use for arm. */
3485 
3486 #if defined(__ia64__) || defined(__powerpc__)
3487 
3488 /*
3489  * Allocate Static TLS using the Variant I method.
3490  */
3491 void *
3492 allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign)
3493 {
3494     Obj_Entry *obj;
3495     char *tcb;
3496     Elf_Addr **tls;
3497     Elf_Addr *dtv;
3498     Elf_Addr addr;
3499     int i;
3500 
3501     if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE)
3502 	return (oldtcb);
3503 
3504     assert(tcbsize >= TLS_TCB_SIZE);
3505     tcb = calloc(1, tls_static_space - TLS_TCB_SIZE + tcbsize);
3506     tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE);
3507 
3508     if (oldtcb != NULL) {
3509 	memcpy(tls, oldtcb, tls_static_space);
3510 	free(oldtcb);
3511 
3512 	/* Adjust the DTV. */
3513 	dtv = tls[0];
3514 	for (i = 0; i < dtv[1]; i++) {
3515 	    if (dtv[i+2] >= (Elf_Addr)oldtcb &&
3516 		dtv[i+2] < (Elf_Addr)oldtcb + tls_static_space) {
3517 		dtv[i+2] = dtv[i+2] - (Elf_Addr)oldtcb + (Elf_Addr)tls;
3518 	    }
3519 	}
3520     } else {
3521 	dtv = calloc(tls_max_index + 2, sizeof(Elf_Addr));
3522 	tls[0] = dtv;
3523 	dtv[0] = tls_dtv_generation;
3524 	dtv[1] = tls_max_index;
3525 
3526 	for (obj = objs; obj; obj = obj->next) {
3527 	    if (obj->tlsoffset > 0) {
3528 		addr = (Elf_Addr)tls + obj->tlsoffset;
3529 		if (obj->tlsinitsize > 0)
3530 		    memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize);
3531 		if (obj->tlssize > obj->tlsinitsize)
3532 		    memset((void*) (addr + obj->tlsinitsize), 0,
3533 			   obj->tlssize - obj->tlsinitsize);
3534 		dtv[obj->tlsindex + 1] = addr;
3535 	    }
3536 	}
3537     }
3538 
3539     return (tcb);
3540 }
3541 
3542 void
3543 free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
3544 {
3545     Elf_Addr *dtv;
3546     Elf_Addr tlsstart, tlsend;
3547     int dtvsize, i;
3548 
3549     assert(tcbsize >= TLS_TCB_SIZE);
3550 
3551     tlsstart = (Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE;
3552     tlsend = tlsstart + tls_static_space;
3553 
3554     dtv = *(Elf_Addr **)tlsstart;
3555     dtvsize = dtv[1];
3556     for (i = 0; i < dtvsize; i++) {
3557 	if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] >= tlsend)) {
3558 	    free((void*)dtv[i+2]);
3559 	}
3560     }
3561     free(dtv);
3562     free(tcb);
3563 }
3564 
3565 #endif
3566 
3567 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
3568     defined(__arm__) || defined(__mips__)
3569 
3570 /*
3571  * Allocate Static TLS using the Variant II method.
3572  */
3573 void *
3574 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
3575 {
3576     Obj_Entry *obj;
3577     size_t size;
3578     char *tls;
3579     Elf_Addr *dtv, *olddtv;
3580     Elf_Addr segbase, oldsegbase, addr;
3581     int i;
3582 
3583     size = round(tls_static_space, tcbalign);
3584 
3585     assert(tcbsize >= 2*sizeof(Elf_Addr));
3586     tls = calloc(1, size + tcbsize);
3587     dtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
3588 
3589     segbase = (Elf_Addr)(tls + size);
3590     ((Elf_Addr*)segbase)[0] = segbase;
3591     ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
3592 
3593     dtv[0] = tls_dtv_generation;
3594     dtv[1] = tls_max_index;
3595 
3596     if (oldtls) {
3597 	/*
3598 	 * Copy the static TLS block over whole.
3599 	 */
3600 	oldsegbase = (Elf_Addr) oldtls;
3601 	memcpy((void *)(segbase - tls_static_space),
3602 	       (const void *)(oldsegbase - tls_static_space),
3603 	       tls_static_space);
3604 
3605 	/*
3606 	 * If any dynamic TLS blocks have been created tls_get_addr(),
3607 	 * move them over.
3608 	 */
3609 	olddtv = ((Elf_Addr**)oldsegbase)[1];
3610 	for (i = 0; i < olddtv[1]; i++) {
3611 	    if (olddtv[i+2] < oldsegbase - size || olddtv[i+2] > oldsegbase) {
3612 		dtv[i+2] = olddtv[i+2];
3613 		olddtv[i+2] = 0;
3614 	    }
3615 	}
3616 
3617 	/*
3618 	 * We assume that this block was the one we created with
3619 	 * allocate_initial_tls().
3620 	 */
3621 	free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
3622     } else {
3623 	for (obj = objs; obj; obj = obj->next) {
3624 	    if (obj->tlsoffset) {
3625 		addr = segbase - obj->tlsoffset;
3626 		memset((void*) (addr + obj->tlsinitsize),
3627 		       0, obj->tlssize - obj->tlsinitsize);
3628 		if (obj->tlsinit)
3629 		    memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize);
3630 		dtv[obj->tlsindex + 1] = addr;
3631 	    }
3632 	}
3633     }
3634 
3635     return (void*) segbase;
3636 }
3637 
3638 void
3639 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
3640 {
3641     size_t size;
3642     Elf_Addr* dtv;
3643     int dtvsize, i;
3644     Elf_Addr tlsstart, tlsend;
3645 
3646     /*
3647      * Figure out the size of the initial TLS block so that we can
3648      * find stuff which ___tls_get_addr() allocated dynamically.
3649      */
3650     size = round(tls_static_space, tcbalign);
3651 
3652     dtv = ((Elf_Addr**)tls)[1];
3653     dtvsize = dtv[1];
3654     tlsend = (Elf_Addr) tls;
3655     tlsstart = tlsend - size;
3656     for (i = 0; i < dtvsize; i++) {
3657 	if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] > tlsend)) {
3658 	    free((void*) dtv[i+2]);
3659 	}
3660     }
3661 
3662     free((void*) tlsstart);
3663     free((void*) dtv);
3664 }
3665 
3666 #endif
3667 
3668 /*
3669  * Allocate TLS block for module with given index.
3670  */
3671 void *
3672 allocate_module_tls(int index)
3673 {
3674     Obj_Entry* obj;
3675     char* p;
3676 
3677     for (obj = obj_list; obj; obj = obj->next) {
3678 	if (obj->tlsindex == index)
3679 	    break;
3680     }
3681     if (!obj) {
3682 	_rtld_error("Can't find module with TLS index %d", index);
3683 	die();
3684     }
3685 
3686     p = malloc(obj->tlssize);
3687     if (p == NULL) {
3688 	_rtld_error("Cannot allocate TLS block for index %d", index);
3689 	die();
3690     }
3691     memcpy(p, obj->tlsinit, obj->tlsinitsize);
3692     memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
3693 
3694     return p;
3695 }
3696 
3697 bool
3698 allocate_tls_offset(Obj_Entry *obj)
3699 {
3700     size_t off;
3701 
3702     if (obj->tls_done)
3703 	return true;
3704 
3705     if (obj->tlssize == 0) {
3706 	obj->tls_done = true;
3707 	return true;
3708     }
3709 
3710     if (obj->tlsindex == 1)
3711 	off = calculate_first_tls_offset(obj->tlssize, obj->tlsalign);
3712     else
3713 	off = calculate_tls_offset(tls_last_offset, tls_last_size,
3714 				   obj->tlssize, obj->tlsalign);
3715 
3716     /*
3717      * If we have already fixed the size of the static TLS block, we
3718      * must stay within that size. When allocating the static TLS, we
3719      * leave a small amount of space spare to be used for dynamically
3720      * loading modules which use static TLS.
3721      */
3722     if (tls_static_space) {
3723 	if (calculate_tls_end(off, obj->tlssize) > tls_static_space)
3724 	    return false;
3725     }
3726 
3727     tls_last_offset = obj->tlsoffset = off;
3728     tls_last_size = obj->tlssize;
3729     obj->tls_done = true;
3730 
3731     return true;
3732 }
3733 
3734 void
3735 free_tls_offset(Obj_Entry *obj)
3736 {
3737 
3738     /*
3739      * If we were the last thing to allocate out of the static TLS
3740      * block, we give our space back to the 'allocator'. This is a
3741      * simplistic workaround to allow libGL.so.1 to be loaded and
3742      * unloaded multiple times.
3743      */
3744     if (calculate_tls_end(obj->tlsoffset, obj->tlssize)
3745 	== calculate_tls_end(tls_last_offset, tls_last_size)) {
3746 	tls_last_offset -= obj->tlssize;
3747 	tls_last_size = 0;
3748     }
3749 }
3750 
3751 void *
3752 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
3753 {
3754     void *ret;
3755     RtldLockState lockstate;
3756 
3757     wlock_acquire(rtld_bind_lock, &lockstate);
3758     ret = allocate_tls(obj_list, oldtls, tcbsize, tcbalign);
3759     lock_release(rtld_bind_lock, &lockstate);
3760     return (ret);
3761 }
3762 
3763 void
3764 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
3765 {
3766     RtldLockState lockstate;
3767 
3768     wlock_acquire(rtld_bind_lock, &lockstate);
3769     free_tls(tcb, tcbsize, tcbalign);
3770     lock_release(rtld_bind_lock, &lockstate);
3771 }
3772 
3773 static void
3774 object_add_name(Obj_Entry *obj, const char *name)
3775 {
3776     Name_Entry *entry;
3777     size_t len;
3778 
3779     len = strlen(name);
3780     entry = malloc(sizeof(Name_Entry) + len);
3781 
3782     if (entry != NULL) {
3783 	strcpy(entry->name, name);
3784 	STAILQ_INSERT_TAIL(&obj->names, entry, link);
3785     }
3786 }
3787 
3788 static int
3789 object_match_name(const Obj_Entry *obj, const char *name)
3790 {
3791     Name_Entry *entry;
3792 
3793     STAILQ_FOREACH(entry, &obj->names, link) {
3794 	if (strcmp(name, entry->name) == 0)
3795 	    return (1);
3796     }
3797     return (0);
3798 }
3799 
3800 static Obj_Entry *
3801 locate_dependency(const Obj_Entry *obj, const char *name)
3802 {
3803     const Objlist_Entry *entry;
3804     const Needed_Entry *needed;
3805 
3806     STAILQ_FOREACH(entry, &list_main, link) {
3807 	if (object_match_name(entry->obj, name))
3808 	    return entry->obj;
3809     }
3810 
3811     for (needed = obj->needed;  needed != NULL;  needed = needed->next) {
3812 	if (strcmp(obj->strtab + needed->name, name) == 0 ||
3813 	  (needed->obj != NULL && object_match_name(needed->obj, name))) {
3814 	    /*
3815 	     * If there is DT_NEEDED for the name we are looking for,
3816 	     * we are all set.  Note that object might not be found if
3817 	     * dependency was not loaded yet, so the function can
3818 	     * return NULL here.  This is expected and handled
3819 	     * properly by the caller.
3820 	     */
3821 	    return (needed->obj);
3822 	}
3823     }
3824     _rtld_error("%s: Unexpected inconsistency: dependency %s not found",
3825 	obj->path, name);
3826     die();
3827 }
3828 
3829 static int
3830 check_object_provided_version(Obj_Entry *refobj, const Obj_Entry *depobj,
3831     const Elf_Vernaux *vna)
3832 {
3833     const Elf_Verdef *vd;
3834     const char *vername;
3835 
3836     vername = refobj->strtab + vna->vna_name;
3837     vd = depobj->verdef;
3838     if (vd == NULL) {
3839 	_rtld_error("%s: version %s required by %s not defined",
3840 	    depobj->path, vername, refobj->path);
3841 	return (-1);
3842     }
3843     for (;;) {
3844 	if (vd->vd_version != VER_DEF_CURRENT) {
3845 	    _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3846 		depobj->path, vd->vd_version);
3847 	    return (-1);
3848 	}
3849 	if (vna->vna_hash == vd->vd_hash) {
3850 	    const Elf_Verdaux *aux = (const Elf_Verdaux *)
3851 		((char *)vd + vd->vd_aux);
3852 	    if (strcmp(vername, depobj->strtab + aux->vda_name) == 0)
3853 		return (0);
3854 	}
3855 	if (vd->vd_next == 0)
3856 	    break;
3857 	vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3858     }
3859     if (vna->vna_flags & VER_FLG_WEAK)
3860 	return (0);
3861     _rtld_error("%s: version %s required by %s not found",
3862 	depobj->path, vername, refobj->path);
3863     return (-1);
3864 }
3865 
3866 static int
3867 rtld_verify_object_versions(Obj_Entry *obj)
3868 {
3869     const Elf_Verneed *vn;
3870     const Elf_Verdef  *vd;
3871     const Elf_Verdaux *vda;
3872     const Elf_Vernaux *vna;
3873     const Obj_Entry *depobj;
3874     int maxvernum, vernum;
3875 
3876     maxvernum = 0;
3877     /*
3878      * Walk over defined and required version records and figure out
3879      * max index used by any of them. Do very basic sanity checking
3880      * while there.
3881      */
3882     vn = obj->verneed;
3883     while (vn != NULL) {
3884 	if (vn->vn_version != VER_NEED_CURRENT) {
3885 	    _rtld_error("%s: Unsupported version %d of Elf_Verneed entry",
3886 		obj->path, vn->vn_version);
3887 	    return (-1);
3888 	}
3889 	vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux);
3890 	for (;;) {
3891 	    vernum = VER_NEED_IDX(vna->vna_other);
3892 	    if (vernum > maxvernum)
3893 		maxvernum = vernum;
3894 	    if (vna->vna_next == 0)
3895 		 break;
3896 	    vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next);
3897 	}
3898 	if (vn->vn_next == 0)
3899 	    break;
3900 	vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next);
3901     }
3902 
3903     vd = obj->verdef;
3904     while (vd != NULL) {
3905 	if (vd->vd_version != VER_DEF_CURRENT) {
3906 	    _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3907 		obj->path, vd->vd_version);
3908 	    return (-1);
3909 	}
3910 	vernum = VER_DEF_IDX(vd->vd_ndx);
3911 	if (vernum > maxvernum)
3912 		maxvernum = vernum;
3913 	if (vd->vd_next == 0)
3914 	    break;
3915 	vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3916     }
3917 
3918     if (maxvernum == 0)
3919 	return (0);
3920 
3921     /*
3922      * Store version information in array indexable by version index.
3923      * Verify that object version requirements are satisfied along the
3924      * way.
3925      */
3926     obj->vernum = maxvernum + 1;
3927     obj->vertab = calloc(obj->vernum, sizeof(Ver_Entry));
3928 
3929     vd = obj->verdef;
3930     while (vd != NULL) {
3931 	if ((vd->vd_flags & VER_FLG_BASE) == 0) {
3932 	    vernum = VER_DEF_IDX(vd->vd_ndx);
3933 	    assert(vernum <= maxvernum);
3934 	    vda = (const Elf_Verdaux *)((char *)vd + vd->vd_aux);
3935 	    obj->vertab[vernum].hash = vd->vd_hash;
3936 	    obj->vertab[vernum].name = obj->strtab + vda->vda_name;
3937 	    obj->vertab[vernum].file = NULL;
3938 	    obj->vertab[vernum].flags = 0;
3939 	}
3940 	if (vd->vd_next == 0)
3941 	    break;
3942 	vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3943     }
3944 
3945     vn = obj->verneed;
3946     while (vn != NULL) {
3947 	depobj = locate_dependency(obj, obj->strtab + vn->vn_file);
3948 	if (depobj == NULL)
3949 	    return (-1);
3950 	vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux);
3951 	for (;;) {
3952 	    if (check_object_provided_version(obj, depobj, vna))
3953 		return (-1);
3954 	    vernum = VER_NEED_IDX(vna->vna_other);
3955 	    assert(vernum <= maxvernum);
3956 	    obj->vertab[vernum].hash = vna->vna_hash;
3957 	    obj->vertab[vernum].name = obj->strtab + vna->vna_name;
3958 	    obj->vertab[vernum].file = obj->strtab + vn->vn_file;
3959 	    obj->vertab[vernum].flags = (vna->vna_other & VER_NEED_HIDDEN) ?
3960 		VER_INFO_HIDDEN : 0;
3961 	    if (vna->vna_next == 0)
3962 		 break;
3963 	    vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next);
3964 	}
3965 	if (vn->vn_next == 0)
3966 	    break;
3967 	vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next);
3968     }
3969     return 0;
3970 }
3971 
3972 static int
3973 rtld_verify_versions(const Objlist *objlist)
3974 {
3975     Objlist_Entry *entry;
3976     int rc;
3977 
3978     rc = 0;
3979     STAILQ_FOREACH(entry, objlist, link) {
3980 	/*
3981 	 * Skip dummy objects or objects that have their version requirements
3982 	 * already checked.
3983 	 */
3984 	if (entry->obj->strtab == NULL || entry->obj->vertab != NULL)
3985 	    continue;
3986 	if (rtld_verify_object_versions(entry->obj) == -1) {
3987 	    rc = -1;
3988 	    if (ld_tracing == NULL)
3989 		break;
3990 	}
3991     }
3992     if (rc == 0 || ld_tracing != NULL)
3993     	rc = rtld_verify_object_versions(&obj_rtld);
3994     return rc;
3995 }
3996 
3997 const Ver_Entry *
3998 fetch_ventry(const Obj_Entry *obj, unsigned long symnum)
3999 {
4000     Elf_Versym vernum;
4001 
4002     if (obj->vertab) {
4003 	vernum = VER_NDX(obj->versyms[symnum]);
4004 	if (vernum >= obj->vernum) {
4005 	    _rtld_error("%s: symbol %s has wrong verneed value %d",
4006 		obj->path, obj->strtab + symnum, vernum);
4007 	} else if (obj->vertab[vernum].hash != 0) {
4008 	    return &obj->vertab[vernum];
4009 	}
4010     }
4011     return NULL;
4012 }
4013 
4014 int
4015 _rtld_get_stack_prot(void)
4016 {
4017 
4018 	return (stack_prot);
4019 }
4020 
4021 static void
4022 map_stacks_exec(RtldLockState *lockstate)
4023 {
4024 	void (*thr_map_stacks_exec)(void);
4025 
4026 	if ((max_stack_flags & PF_X) == 0 || (stack_prot & PROT_EXEC) != 0)
4027 		return;
4028 	thr_map_stacks_exec = (void (*)(void))(uintptr_t)
4029 	    get_program_var_addr("__pthread_map_stacks_exec", lockstate);
4030 	if (thr_map_stacks_exec != NULL) {
4031 		stack_prot |= PROT_EXEC;
4032 		thr_map_stacks_exec();
4033 	}
4034 }
4035 
4036 void
4037 symlook_init(SymLook *dst, const char *name)
4038 {
4039 
4040 	bzero(dst, sizeof(*dst));
4041 	dst->name = name;
4042 	dst->hash = elf_hash(name);
4043 }
4044 
4045 static void
4046 symlook_init_from_req(SymLook *dst, const SymLook *src)
4047 {
4048 
4049 	dst->name = src->name;
4050 	dst->hash = src->hash;
4051 	dst->ventry = src->ventry;
4052 	dst->flags = src->flags;
4053 	dst->defobj_out = NULL;
4054 	dst->sym_out = NULL;
4055 	dst->lockstate = src->lockstate;
4056 }
4057 
4058 /*
4059  * Overrides for libc_pic-provided functions.
4060  */
4061 
4062 int
4063 __getosreldate(void)
4064 {
4065 	size_t len;
4066 	int oid[2];
4067 	int error, osrel;
4068 
4069 	if (osreldate != 0)
4070 		return (osreldate);
4071 
4072 	oid[0] = CTL_KERN;
4073 	oid[1] = KERN_OSRELDATE;
4074 	osrel = 0;
4075 	len = sizeof(osrel);
4076 	error = sysctl(oid, 2, &osrel, &len, NULL, 0);
4077 	if (error == 0 && osrel > 0 && len == sizeof(osrel))
4078 		osreldate = osrel;
4079 	return (osreldate);
4080 }
4081 
4082 /*
4083  * No unresolved symbols for rtld.
4084  */
4085 void
4086 __pthread_cxa_finalize(struct dl_phdr_info *a)
4087 {
4088 }
4089