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