xref: /freebsd/libexec/rtld-elf/rtld.c (revision 2b944ee2b959e9b29fd72dcbf87aad8ad5537bc4)
1 /*-
2  * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 /*
29  * Dynamic linker for ELF.
30  *
31  * John Polstra <jdp@polstra.com>.
32  */
33 
34 #ifndef __GNUC__
35 #error "GCC is needed to compile this file"
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/mman.h>
40 #include <sys/stat.h>
41 
42 #include <dlfcn.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "debug.h"
53 #include "rtld.h"
54 
55 #define END_SYM		"_end"
56 #define PATH_RTLD	"/usr/libexec/ld-elf.so.1"
57 
58 /* Types. */
59 typedef void (*func_ptr_type)();
60 
61 typedef struct Struct_LockInfo {
62     void *context;		/* Client context for creating locks */
63     void *thelock;		/* The one big lock */
64     /* Methods */
65     void (*rlock_acquire)(void *lock);
66     void (*wlock_acquire)(void *lock);
67     void (*lock_release)(void *lock);
68     void (*lock_destroy)(void *lock);
69     void (*context_destroy)(void *context);
70 } LockInfo;
71 
72 /*
73  * Function declarations.
74  */
75 static const char *basename(const char *);
76 static void die(void);
77 static void digest_dynamic(Obj_Entry *);
78 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
79 static Obj_Entry *dlcheck(void *);
80 static char *find_library(const char *, const Obj_Entry *);
81 static void funclist_call(Funclist *);
82 static void funclist_clear(Funclist *);
83 static void funclist_init(Funclist *);
84 static void funclist_push_head(Funclist *, InitFunc);
85 static void funclist_push_tail(Funclist *, InitFunc);
86 static const char *gethints(void);
87 static void init_dag(Obj_Entry *);
88 static void init_dag1(Obj_Entry *root, Obj_Entry *obj);
89 static void init_rtld(caddr_t);
90 static bool is_exported(const Elf_Sym *);
91 static void linkmap_add(Obj_Entry *);
92 static void linkmap_delete(Obj_Entry *);
93 static int load_needed_objects(Obj_Entry *);
94 static int load_preload_objects(void);
95 static Obj_Entry *load_object(char *);
96 static void lock_nop(void *);
97 static Obj_Entry *obj_from_addr(const void *);
98 static void objlist_add(Objlist *, Obj_Entry *);
99 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
100 static void objlist_remove(Objlist *, Obj_Entry *);
101 static int relocate_objects(Obj_Entry *, bool);
102 static void rtld_exit(void);
103 static char *search_library_path(const char *, const char *);
104 static void set_program_var(const char *, const void *);
105 static const Elf_Sym *symlook_list(const char *, unsigned long,
106   Objlist *, const Obj_Entry **, bool in_plt);
107 static void trace_loaded_objects(Obj_Entry *obj);
108 static void unload_object(Obj_Entry *);
109 static void unref_dag(Obj_Entry *);
110 
111 void r_debug_state(void);
112 void xprintf(const char *, ...);
113 
114 /*
115  * Data declarations.
116  */
117 static char *error_message;	/* Message for dlerror(), or NULL */
118 struct r_debug r_debug;	/* for GDB; */
119 static bool trust;		/* False for setuid and setgid programs */
120 static char *ld_bind_now;	/* Environment variable for immediate binding */
121 static char *ld_debug;		/* Environment variable for debugging */
122 static char *ld_library_path;	/* Environment variable for search path */
123 static char *ld_preload;	/* Environment variable for libraries to
124 				   load first */
125 static char *ld_tracing;	/* Called from ldd to print libs */
126 static Obj_Entry *obj_list;	/* Head of linked list of shared objects */
127 static Obj_Entry **obj_tail;	/* Link field of last object in list */
128 static Obj_Entry *obj_main;	/* The main program shared object */
129 static Obj_Entry obj_rtld;	/* The dynamic linker shared object */
130 static unsigned long curmark;	/* Current mark value */
131 
132 static Objlist list_global =	/* Objects dlopened with RTLD_GLOBAL */
133   STAILQ_HEAD_INITIALIZER(list_global);
134 static Objlist list_main =	/* Objects loaded at program startup */
135   STAILQ_HEAD_INITIALIZER(list_main);
136 
137 static LockInfo lockinfo;
138 
139 static Elf_Sym sym_zero;	/* For resolving undefined weak refs. */
140 
141 #define GDB_STATE(s)	r_debug.r_state = s; r_debug_state();
142 
143 extern Elf_Dyn _DYNAMIC;
144 #pragma weak _DYNAMIC
145 
146 /*
147  * These are the functions the dynamic linker exports to application
148  * programs.  They are the only symbols the dynamic linker is willing
149  * to export from itself.
150  */
151 static func_ptr_type exports[] = {
152     (func_ptr_type) &_rtld_error,
153     (func_ptr_type) &dlclose,
154     (func_ptr_type) &dlerror,
155     (func_ptr_type) &dlopen,
156     (func_ptr_type) &dlsym,
157     (func_ptr_type) &dladdr,
158     (func_ptr_type) &dllockinit,
159     NULL
160 };
161 
162 /*
163  * Global declarations normally provided by crt1.  The dynamic linker is
164  * not built with crt1, so we have to provide them ourselves.
165  */
166 char *__progname;
167 char **environ;
168 
169 static __inline void
170 rlock_acquire(void)
171 {
172     lockinfo.rlock_acquire(lockinfo.thelock);
173 }
174 
175 static __inline void
176 wlock_acquire(void)
177 {
178     lockinfo.wlock_acquire(lockinfo.thelock);
179 }
180 
181 static __inline void
182 lock_release(void)
183 {
184     lockinfo.lock_release(lockinfo.thelock);
185 }
186 
187 /*
188  * Main entry point for dynamic linking.  The first argument is the
189  * stack pointer.  The stack is expected to be laid out as described
190  * in the SVR4 ABI specification, Intel 386 Processor Supplement.
191  * Specifically, the stack pointer points to a word containing
192  * ARGC.  Following that in the stack is a null-terminated sequence
193  * of pointers to argument strings.  Then comes a null-terminated
194  * sequence of pointers to environment strings.  Finally, there is a
195  * sequence of "auxiliary vector" entries.
196  *
197  * The second argument points to a place to store the dynamic linker's
198  * exit procedure pointer and the third to a place to store the main
199  * program's object.
200  *
201  * The return value is the main program's entry point.
202  */
203 func_ptr_type
204 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
205 {
206     Elf_Auxinfo *aux_info[AT_COUNT];
207     int i;
208     int argc;
209     char **argv;
210     char **env;
211     Elf_Auxinfo *aux;
212     Elf_Auxinfo *auxp;
213     const char *argv0;
214     Obj_Entry *obj;
215     Funclist initlist;
216 
217     /*
218      * On entry, the dynamic linker itself has not been relocated yet.
219      * Be very careful not to reference any global data until after
220      * init_rtld has returned.  It is OK to reference file-scope statics
221      * and string constants, and to call static and global functions.
222      */
223 
224     /* Find the auxiliary vector on the stack. */
225     argc = *sp++;
226     argv = (char **) sp;
227     sp += argc + 1;	/* Skip over arguments and NULL terminator */
228     env = (char **) sp;
229     while (*sp++ != 0)	/* Skip over environment, and NULL terminator */
230 	;
231     aux = (Elf_Auxinfo *) sp;
232 
233     /* Digest the auxiliary vector. */
234     for (i = 0;  i < AT_COUNT;  i++)
235 	aux_info[i] = NULL;
236     for (auxp = aux;  auxp->a_type != AT_NULL;  auxp++) {
237 	if (auxp->a_type < AT_COUNT)
238 	    aux_info[auxp->a_type] = auxp;
239     }
240 
241     /* Initialize and relocate ourselves. */
242     assert(aux_info[AT_BASE] != NULL);
243     init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
244 
245     __progname = obj_rtld.path;
246     argv0 = argv[0] != NULL ? argv[0] : "(null)";
247     environ = env;
248 
249     trust = geteuid() == getuid() && getegid() == getgid();
250 
251     ld_bind_now = getenv("LD_BIND_NOW");
252     if (trust) {
253 	ld_debug = getenv("LD_DEBUG");
254 	ld_library_path = getenv("LD_LIBRARY_PATH");
255 	ld_preload = getenv("LD_PRELOAD");
256     }
257     ld_tracing = getenv("LD_TRACE_LOADED_OBJECTS");
258 
259     if (ld_debug != NULL && *ld_debug != '\0')
260 	debug = 1;
261     dbg("%s is initialized, base address = %p", __progname,
262 	(caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
263     dbg("RTLD dynamic = %p", obj_rtld.dynamic);
264     dbg("RTLD pltgot  = %p", obj_rtld.pltgot);
265 
266     /*
267      * Load the main program, or process its program header if it is
268      * already loaded.
269      */
270     if (aux_info[AT_EXECFD] != NULL) {	/* Load the main program. */
271 	int fd = aux_info[AT_EXECFD]->a_un.a_val;
272 	dbg("loading main program");
273 	obj_main = map_object(fd, argv0, NULL);
274 	close(fd);
275 	if (obj_main == NULL)
276 	    die();
277     } else {				/* Main program already loaded. */
278 	const Elf_Phdr *phdr;
279 	int phnum;
280 	caddr_t entry;
281 
282 	dbg("processing main program's program header");
283 	assert(aux_info[AT_PHDR] != NULL);
284 	phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
285 	assert(aux_info[AT_PHNUM] != NULL);
286 	phnum = aux_info[AT_PHNUM]->a_un.a_val;
287 	assert(aux_info[AT_PHENT] != NULL);
288 	assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
289 	assert(aux_info[AT_ENTRY] != NULL);
290 	entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
291 	if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
292 	    die();
293     }
294 
295     obj_main->path = xstrdup(argv0);
296     obj_main->mainprog = true;
297 
298     /*
299      * Get the actual dynamic linker pathname from the executable if
300      * possible.  (It should always be possible.)  That ensures that
301      * gdb will find the right dynamic linker even if a non-standard
302      * one is being used.
303      */
304     if (obj_main->interp != NULL &&
305       strcmp(obj_main->interp, obj_rtld.path) != 0) {
306 	free(obj_rtld.path);
307 	obj_rtld.path = xstrdup(obj_main->interp);
308     }
309 
310     digest_dynamic(obj_main);
311 
312     linkmap_add(obj_main);
313     linkmap_add(&obj_rtld);
314 
315     /* Link the main program into the list of objects. */
316     *obj_tail = obj_main;
317     obj_tail = &obj_main->next;
318     obj_main->refcount++;
319 
320     /* Initialize a fake symbol for resolving undefined weak references. */
321     sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
322     sym_zero.st_shndx = SHN_ABS;
323 
324     dbg("loading LD_PRELOAD libraries");
325     if (load_preload_objects() == -1)
326 	die();
327 
328     dbg("loading needed objects");
329     if (load_needed_objects(obj_main) == -1)
330 	die();
331 
332     /*
333      * Make a list of all objects loaded at startup.  Also construct
334      * the list of init functions to call, in reverse order.
335      */
336     funclist_init(&initlist);
337     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
338 	objlist_add(&list_main, obj);
339 	if (obj->init != NULL && !obj->mainprog)
340 	    funclist_push_head(&initlist, obj->init);
341     }
342 
343     if (ld_tracing) {		/* We're done */
344 	trace_loaded_objects(obj_main);
345 	exit(0);
346     }
347 
348     if (relocate_objects(obj_main,
349 	ld_bind_now != NULL && *ld_bind_now != '\0') == -1)
350 	die();
351 
352     dbg("doing copy relocations");
353     if (do_copy_relocations(obj_main) == -1)
354 	die();
355 
356     dbg("initializing key program variables");
357     set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
358     set_program_var("environ", env);
359 
360     dbg("initializing default locks");
361     dllockinit(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
362 
363     r_debug_state();		/* say hello to gdb! */
364 
365     funclist_call(&initlist);
366     wlock_acquire();
367     funclist_clear(&initlist);
368     lock_release();
369 
370     dbg("transferring control to program entry point = %p", obj_main->entry);
371 
372     /* Return the exit procedure and the program entry point. */
373     *exit_proc = rtld_exit;
374     *objp = obj_main;
375     return (func_ptr_type) obj_main->entry;
376 }
377 
378 Elf_Addr
379 _rtld_bind(Obj_Entry *obj, Elf_Word reloff)
380 {
381     const Elf_Rel *rel;
382     const Elf_Sym *def;
383     const Obj_Entry *defobj;
384     Elf_Addr *where;
385     Elf_Addr target;
386 
387     wlock_acquire();
388     if (obj->pltrel)
389 	rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
390     else
391 	rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
392 
393     where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
394     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
395     if (def == NULL)
396 	die();
397 
398     target = (Elf_Addr)(defobj->relocbase + def->st_value);
399 
400     dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
401       defobj->strtab + def->st_name, basename(obj->path),
402       (void *)target, basename(defobj->path));
403 
404     reloc_jmpslot(where, target);
405     lock_release();
406     return target;
407 }
408 
409 /*
410  * Error reporting function.  Use it like printf.  If formats the message
411  * into a buffer, and sets things up so that the next call to dlerror()
412  * will return the message.
413  */
414 void
415 _rtld_error(const char *fmt, ...)
416 {
417     static char buf[512];
418     va_list ap;
419 
420     va_start(ap, fmt);
421     vsnprintf(buf, sizeof buf, fmt, ap);
422     error_message = buf;
423     va_end(ap);
424 }
425 
426 static const char *
427 basename(const char *name)
428 {
429     const char *p = strrchr(name, '/');
430     return p != NULL ? p + 1 : name;
431 }
432 
433 static void
434 die(void)
435 {
436     const char *msg = dlerror();
437 
438     if (msg == NULL)
439 	msg = "Fatal error";
440     errx(1, "%s", msg);
441 }
442 
443 /*
444  * Process a shared object's DYNAMIC section, and save the important
445  * information in its Obj_Entry structure.
446  */
447 static void
448 digest_dynamic(Obj_Entry *obj)
449 {
450     const Elf_Dyn *dynp;
451     Needed_Entry **needed_tail = &obj->needed;
452     const Elf_Dyn *dyn_rpath = NULL;
453     int plttype = DT_REL;
454 
455     for (dynp = obj->dynamic;  dynp->d_tag != DT_NULL;  dynp++) {
456 	switch (dynp->d_tag) {
457 
458 	case DT_REL:
459 	    obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
460 	    break;
461 
462 	case DT_RELSZ:
463 	    obj->relsize = dynp->d_un.d_val;
464 	    break;
465 
466 	case DT_RELENT:
467 	    assert(dynp->d_un.d_val == sizeof(Elf_Rel));
468 	    break;
469 
470 	case DT_JMPREL:
471 	    obj->pltrel = (const Elf_Rel *)
472 	      (obj->relocbase + dynp->d_un.d_ptr);
473 	    break;
474 
475 	case DT_PLTRELSZ:
476 	    obj->pltrelsize = dynp->d_un.d_val;
477 	    break;
478 
479 	case DT_RELA:
480 	    obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
481 	    break;
482 
483 	case DT_RELASZ:
484 	    obj->relasize = dynp->d_un.d_val;
485 	    break;
486 
487 	case DT_RELAENT:
488 	    assert(dynp->d_un.d_val == sizeof(Elf_Rela));
489 	    break;
490 
491 	case DT_PLTREL:
492 	    plttype = dynp->d_un.d_val;
493 	    assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
494 	    break;
495 
496 	case DT_SYMTAB:
497 	    obj->symtab = (const Elf_Sym *)
498 	      (obj->relocbase + dynp->d_un.d_ptr);
499 	    break;
500 
501 	case DT_SYMENT:
502 	    assert(dynp->d_un.d_val == sizeof(Elf_Sym));
503 	    break;
504 
505 	case DT_STRTAB:
506 	    obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
507 	    break;
508 
509 	case DT_STRSZ:
510 	    obj->strsize = dynp->d_un.d_val;
511 	    break;
512 
513 	case DT_HASH:
514 	    {
515 		const Elf_Addr *hashtab = (const Elf_Addr *)
516 		  (obj->relocbase + dynp->d_un.d_ptr);
517 		obj->nbuckets = hashtab[0];
518 		obj->nchains = hashtab[1];
519 		obj->buckets = hashtab + 2;
520 		obj->chains = obj->buckets + obj->nbuckets;
521 	    }
522 	    break;
523 
524 	case DT_NEEDED:
525 	    if (!obj->rtld) {
526 		Needed_Entry *nep = NEW(Needed_Entry);
527 		nep->name = dynp->d_un.d_val;
528 		nep->obj = NULL;
529 		nep->next = NULL;
530 
531 		*needed_tail = nep;
532 		needed_tail = &nep->next;
533 	    }
534 	    break;
535 
536 	case DT_PLTGOT:
537 	    obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
538 	    break;
539 
540 	case DT_TEXTREL:
541 	    obj->textrel = true;
542 	    break;
543 
544 	case DT_SYMBOLIC:
545 	    obj->symbolic = true;
546 	    break;
547 
548 	case DT_RPATH:
549 	    /*
550 	     * We have to wait until later to process this, because we
551 	     * might not have gotten the address of the string table yet.
552 	     */
553 	    dyn_rpath = dynp;
554 	    break;
555 
556 	case DT_SONAME:
557 	    /* Not used by the dynamic linker. */
558 	    break;
559 
560 	case DT_INIT:
561 	    obj->init = (InitFunc) (obj->relocbase + dynp->d_un.d_ptr);
562 	    break;
563 
564 	case DT_FINI:
565 	    obj->fini = (InitFunc) (obj->relocbase + dynp->d_un.d_ptr);
566 	    break;
567 
568 	case DT_DEBUG:
569 	    /* XXX - not implemented yet */
570 	    dbg("Filling in DT_DEBUG entry");
571 	    ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
572 	    break;
573 
574 	default:
575 	    dbg("Ignoring d_tag %d = %#x", dynp->d_tag, dynp->d_tag);
576 	    break;
577 	}
578     }
579 
580     obj->traced = false;
581 
582     if (plttype == DT_RELA) {
583 	obj->pltrela = (const Elf_Rela *) obj->pltrel;
584 	obj->pltrel = NULL;
585 	obj->pltrelasize = obj->pltrelsize;
586 	obj->pltrelsize = 0;
587     }
588 
589     if (dyn_rpath != NULL)
590 	obj->rpath = obj->strtab + dyn_rpath->d_un.d_val;
591 }
592 
593 /*
594  * Process a shared object's program header.  This is used only for the
595  * main program, when the kernel has already loaded the main program
596  * into memory before calling the dynamic linker.  It creates and
597  * returns an Obj_Entry structure.
598  */
599 static Obj_Entry *
600 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
601 {
602     Obj_Entry *obj;
603     const Elf_Phdr *phlimit = phdr + phnum;
604     const Elf_Phdr *ph;
605     int nsegs = 0;
606 
607     obj = obj_new();
608     for (ph = phdr;  ph < phlimit;  ph++) {
609 	switch (ph->p_type) {
610 
611 	case PT_PHDR:
612 	    if ((const Elf_Phdr *)ph->p_vaddr != phdr) {
613 		_rtld_error("%s: invalid PT_PHDR", path);
614 		return NULL;
615 	    }
616 	    obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
617 	    obj->phsize = ph->p_memsz;
618 	    break;
619 
620 	case PT_INTERP:
621 	    obj->interp = (const char *) ph->p_vaddr;
622 	    break;
623 
624 	case PT_LOAD:
625 	    if (nsegs >= 2) {
626 		_rtld_error("%s: too many PT_LOAD segments", path);
627 		return NULL;
628 	    }
629 	    if (nsegs == 0) {	/* First load segment */
630 		obj->vaddrbase = trunc_page(ph->p_vaddr);
631 		obj->mapbase = (caddr_t) obj->vaddrbase;
632 		obj->relocbase = obj->mapbase - obj->vaddrbase;
633 		obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
634 		  obj->vaddrbase;
635 	    } else {		/* Last load segment */
636 		obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
637 		  obj->vaddrbase;
638 	    }
639 	    nsegs++;
640 	    break;
641 
642 	case PT_DYNAMIC:
643 	    obj->dynamic = (const Elf_Dyn *) ph->p_vaddr;
644 	    break;
645 	}
646     }
647     if (nsegs < 2) {
648 	_rtld_error("%s: too few PT_LOAD segments", path);
649 	return NULL;
650     }
651 
652     obj->entry = entry;
653     return obj;
654 }
655 
656 static Obj_Entry *
657 dlcheck(void *handle)
658 {
659     Obj_Entry *obj;
660 
661     for (obj = obj_list;  obj != NULL;  obj = obj->next)
662 	if (obj == (Obj_Entry *) handle)
663 	    break;
664 
665     if (obj == NULL || obj->dl_refcount == 0) {
666 	_rtld_error("Invalid shared object handle %p", handle);
667 	return NULL;
668     }
669     return obj;
670 }
671 
672 /*
673  * Hash function for symbol table lookup.  Don't even think about changing
674  * this.  It is specified by the System V ABI.
675  */
676 unsigned long
677 elf_hash(const char *name)
678 {
679     const unsigned char *p = (const unsigned char *) name;
680     unsigned long h = 0;
681     unsigned long g;
682 
683     while (*p != '\0') {
684 	h = (h << 4) + *p++;
685 	if ((g = h & 0xf0000000) != 0)
686 	    h ^= g >> 24;
687 	h &= ~g;
688     }
689     return h;
690 }
691 
692 /*
693  * Find the library with the given name, and return its full pathname.
694  * The returned string is dynamically allocated.  Generates an error
695  * message and returns NULL if the library cannot be found.
696  *
697  * If the second argument is non-NULL, then it refers to an already-
698  * loaded shared object, whose library search path will be searched.
699  *
700  * The search order is:
701  *   rpath in the referencing file
702  *   LD_LIBRARY_PATH
703  *   ldconfig hints
704  *   /usr/lib
705  */
706 static char *
707 find_library(const char *name, const Obj_Entry *refobj)
708 {
709     char *pathname;
710 
711     if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
712 	if (name[0] != '/' && !trust) {
713 	    _rtld_error("Absolute pathname required for shared object \"%s\"",
714 	      name);
715 	    return NULL;
716 	}
717 	return xstrdup(name);
718     }
719 
720     dbg(" Searching for \"%s\"", name);
721 
722     if ((refobj != NULL &&
723       (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
724       (pathname = search_library_path(name, ld_library_path)) != NULL ||
725       (pathname = search_library_path(name, gethints())) != NULL ||
726       (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
727 	return pathname;
728 
729     _rtld_error("Shared object \"%s\" not found", name);
730     return NULL;
731 }
732 
733 /*
734  * Given a symbol number in a referencing object, find the corresponding
735  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
736  * no definition was found.  Returns a pointer to the Obj_Entry of the
737  * defining object via the reference parameter DEFOBJ_OUT.
738  */
739 const Elf_Sym *
740 find_symdef(unsigned long symnum, Obj_Entry *refobj,
741     const Obj_Entry **defobj_out, bool in_plt)
742 {
743     const Elf_Sym *ref;
744     const Elf_Sym *def;
745     const Elf_Sym *symp;
746     const Obj_Entry *obj;
747     const Obj_Entry *defobj;
748     const Objlist_Entry *elm;
749     const char *name;
750     unsigned long hash;
751 
752     ref = refobj->symtab + symnum;
753     name = refobj->strtab + ref->st_name;
754     hash = elf_hash(name);
755     def = NULL;
756     defobj = NULL;
757     curmark++;
758 
759     if (refobj->symbolic) {	/* Look first in the referencing object */
760 	symp = symlook_obj(name, hash, refobj, in_plt);
761 	refobj->mark = curmark;
762 	if (symp != NULL) {
763 	    def = symp;
764 	    defobj = refobj;
765 	}
766     }
767 
768     /* Search all objects loaded at program start up. */
769     if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
770 	symp = symlook_list(name, hash, &list_main, &obj, in_plt);
771 	if (symp != NULL &&
772 	  (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
773 	    def = symp;
774 	    defobj = obj;
775 	}
776     }
777 
778     /* Search all dlopened DAGs containing the referencing object. */
779     STAILQ_FOREACH(elm, &refobj->dldags, link) {
780 	if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
781 	    break;
782 	symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt);
783 	if (symp != NULL &&
784 	  (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
785 	    def = symp;
786 	    defobj = obj;
787 	}
788     }
789 
790     /* Search all RTLD_GLOBAL objects. */
791     if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
792 	symp = symlook_list(name, hash, &list_global, &obj, in_plt);
793 	if (symp != NULL &&
794 	  (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
795 	    def = symp;
796 	    defobj = obj;
797 	}
798     }
799 
800     /*
801      * Search the dynamic linker itself, and possibly resolve the
802      * symbol from there.  This is how the application links to
803      * dynamic linker services such as dlopen.  Only the values listed
804      * in the "exports" array can be resolved from the dynamic linker.
805      */
806     if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
807 	symp = symlook_obj(name, hash, &obj_rtld, in_plt);
808 	if (symp != NULL && is_exported(symp)) {
809 	    def = symp;
810 	    defobj = &obj_rtld;
811 	}
812     }
813 
814     /*
815      * If we found no definition and the reference is weak, treat the
816      * symbol as having the value zero.
817      */
818     if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
819 	def = &sym_zero;
820 	defobj = obj_main;
821     }
822 
823     if (def != NULL)
824 	*defobj_out = defobj;
825     else
826 	_rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
827     return def;
828 }
829 
830 static void
831 funclist_call(Funclist *list)
832 {
833     Funclist_Entry *elm;
834 
835     STAILQ_FOREACH(elm, list, link) {
836 	dbg("calling init/fini function at %p", elm->func);
837 	(*elm->func)();
838     }
839 }
840 
841 static void
842 funclist_clear(Funclist *list)
843 {
844     Funclist_Entry *elm;
845 
846     while (!STAILQ_EMPTY(list)) {
847 	elm = STAILQ_FIRST(list);
848 	STAILQ_REMOVE_HEAD(list, link);
849 	free(elm);
850     }
851 }
852 
853 static void
854 funclist_init(Funclist *list)
855 {
856     STAILQ_INIT(list);
857 }
858 
859 static void
860 funclist_push_head(Funclist *list, InitFunc func)
861 {
862     Funclist_Entry *elm;
863 
864     elm = NEW(Funclist_Entry);
865     elm->func = func;
866     STAILQ_INSERT_HEAD(list, elm, link);
867 }
868 
869 static void
870 funclist_push_tail(Funclist *list, InitFunc func)
871 {
872     Funclist_Entry *elm;
873 
874     elm = NEW(Funclist_Entry);
875     elm->func = func;
876     STAILQ_INSERT_TAIL(list, elm, link);
877 }
878 
879 /*
880  * Return the search path from the ldconfig hints file, reading it if
881  * necessary.  Returns NULL if there are problems with the hints file,
882  * or if the search path there is empty.
883  */
884 static const char *
885 gethints(void)
886 {
887     static char *hints;
888 
889     if (hints == NULL) {
890 	int fd;
891 	struct elfhints_hdr hdr;
892 	char *p;
893 
894 	/* Keep from trying again in case the hints file is bad. */
895 	hints = "";
896 
897 	if ((fd = open(_PATH_ELF_HINTS, O_RDONLY)) == -1)
898 	    return NULL;
899 	if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
900 	  hdr.magic != ELFHINTS_MAGIC ||
901 	  hdr.version != 1) {
902 	    close(fd);
903 	    return NULL;
904 	}
905 	p = xmalloc(hdr.dirlistlen + 1);
906 	if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 ||
907 	  read(fd, p, hdr.dirlistlen + 1) != hdr.dirlistlen + 1) {
908 	    free(p);
909 	    close(fd);
910 	    return NULL;
911 	}
912 	hints = p;
913 	close(fd);
914     }
915     return hints[0] != '\0' ? hints : NULL;
916 }
917 
918 static void
919 init_dag(Obj_Entry *root)
920 {
921     curmark++;
922     init_dag1(root, root);
923 }
924 
925 static void
926 init_dag1(Obj_Entry *root, Obj_Entry *obj)
927 {
928     const Needed_Entry *needed;
929 
930     if (obj->mark == curmark)
931 	return;
932     obj->mark = curmark;
933     objlist_add(&obj->dldags, root);
934     objlist_add(&root->dagmembers, obj);
935     for (needed = obj->needed;  needed != NULL;  needed = needed->next)
936 	if (needed->obj != NULL)
937 	    init_dag1(root, needed->obj);
938 }
939 
940 /*
941  * Initialize the dynamic linker.  The argument is the address at which
942  * the dynamic linker has been mapped into memory.  The primary task of
943  * this function is to relocate the dynamic linker.
944  */
945 static void
946 init_rtld(caddr_t mapbase)
947 {
948     /*
949      * Conjure up an Obj_Entry structure for the dynamic linker.
950      *
951      * The "path" member is supposed to be dynamically-allocated, but we
952      * aren't yet initialized sufficiently to do that.  Below we will
953      * replace the static version with a dynamically-allocated copy.
954      */
955     obj_rtld.path = PATH_RTLD;
956     obj_rtld.rtld = true;
957     obj_rtld.mapbase = mapbase;
958 #ifdef PIC
959     obj_rtld.relocbase = mapbase;
960 #endif
961     if (&_DYNAMIC != 0) {
962 	obj_rtld.dynamic = rtld_dynamic(&obj_rtld);
963 	digest_dynamic(&obj_rtld);
964 	assert(obj_rtld.needed == NULL);
965 	assert(!obj_rtld.textrel);
966 
967 	/*
968 	 * Temporarily put the dynamic linker entry into the object list, so
969 	 * that symbols can be found.
970 	 */
971 	obj_list = &obj_rtld;
972 	obj_tail = &obj_rtld.next;
973 
974 	relocate_objects(&obj_rtld, true);
975     }
976 
977     /* Make the object list empty again. */
978     obj_list = NULL;
979     obj_tail = &obj_list;
980 
981     /* Replace the path with a dynamically allocated copy. */
982     obj_rtld.path = xstrdup(obj_rtld.path);
983 
984     r_debug.r_brk = r_debug_state;
985     r_debug.r_state = RT_CONSISTENT;
986 }
987 
988 static bool
989 is_exported(const Elf_Sym *def)
990 {
991     func_ptr_type value;
992     const func_ptr_type *p;
993 
994     value = (func_ptr_type)(obj_rtld.relocbase + def->st_value);
995     for (p = exports;  *p != NULL;  p++)
996 	if (*p == value)
997 	    return true;
998     return false;
999 }
1000 
1001 /*
1002  * Given a shared object, traverse its list of needed objects, and load
1003  * each of them.  Returns 0 on success.  Generates an error message and
1004  * returns -1 on failure.
1005  */
1006 static int
1007 load_needed_objects(Obj_Entry *first)
1008 {
1009     Obj_Entry *obj;
1010 
1011     for (obj = first;  obj != NULL;  obj = obj->next) {
1012 	Needed_Entry *needed;
1013 
1014 	for (needed = obj->needed;  needed != NULL;  needed = needed->next) {
1015 	    const char *name = obj->strtab + needed->name;
1016 	    char *path = find_library(name, obj);
1017 
1018 	    needed->obj = NULL;
1019 	    if (path == NULL && !ld_tracing)
1020 		return -1;
1021 
1022 	    if (path) {
1023 		needed->obj = load_object(path);
1024 		if (needed->obj == NULL && !ld_tracing)
1025 		    return -1;		/* XXX - cleanup */
1026 	    }
1027 	}
1028     }
1029 
1030     return 0;
1031 }
1032 
1033 static int
1034 load_preload_objects(void)
1035 {
1036     char *p = ld_preload;
1037 
1038     if (p == NULL)
1039 	return NULL;
1040 
1041     p += strspn(p, ":;");
1042     while (*p != '\0') {
1043 	size_t len = strcspn(p, ":;");
1044 	char *path;
1045 	char savech;
1046 
1047 	savech = p[len];
1048 	p[len] = '\0';
1049 	if ((path = find_library(p, NULL)) == NULL)
1050 	    return -1;
1051 	if (load_object(path) == NULL)
1052 	    return -1;	/* XXX - cleanup */
1053 	p[len] = savech;
1054 	p += len;
1055 	p += strspn(p, ":;");
1056     }
1057     return 0;
1058 }
1059 
1060 /*
1061  * Load a shared object into memory, if it is not already loaded.  The
1062  * argument must be a string allocated on the heap.  This function assumes
1063  * responsibility for freeing it when necessary.
1064  *
1065  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
1066  * on failure.
1067  */
1068 static Obj_Entry *
1069 load_object(char *path)
1070 {
1071     Obj_Entry *obj;
1072     int fd = -1;
1073     struct stat sb;
1074 
1075     for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
1076 	if (strcmp(obj->path, path) == 0)
1077 	    break;
1078 
1079     /*
1080      * If we didn't find a match by pathname, open the file and check
1081      * again by device and inode.  This avoids false mismatches caused
1082      * by multiple links or ".." in pathnames.
1083      *
1084      * To avoid a race, we open the file and use fstat() rather than
1085      * using stat().
1086      */
1087     if (obj == NULL) {
1088 	if ((fd = open(path, O_RDONLY)) == -1) {
1089 	    _rtld_error("Cannot open \"%s\"", path);
1090 	    return NULL;
1091 	}
1092 	if (fstat(fd, &sb) == -1) {
1093 	    _rtld_error("Cannot fstat \"%s\"", path);
1094 	    close(fd);
1095 	    return NULL;
1096 	}
1097 	for (obj = obj_list->next;  obj != NULL;  obj = obj->next) {
1098 	    if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
1099 		close(fd);
1100 		break;
1101 	    }
1102 	}
1103     }
1104 
1105     if (obj == NULL) {	/* First use of this object, so we must map it in */
1106 	dbg("loading \"%s\"", path);
1107 	obj = map_object(fd, path, &sb);
1108 	close(fd);
1109 	if (obj == NULL) {
1110 	    free(path);
1111 	    return NULL;
1112 	}
1113 
1114 	obj->path = path;
1115 	digest_dynamic(obj);
1116 
1117 	*obj_tail = obj;
1118 	obj_tail = &obj->next;
1119 	linkmap_add(obj);	/* for GDB */
1120 
1121 	dbg("  %p .. %p: %s", obj->mapbase,
1122 	  obj->mapbase + obj->mapsize - 1, obj->path);
1123 	if (obj->textrel)
1124 	    dbg("  WARNING: %s has impure text", obj->path);
1125     } else
1126 	free(path);
1127 
1128     obj->refcount++;
1129     return obj;
1130 }
1131 
1132 static void
1133 lock_nop(void *lock)
1134 {
1135 }
1136 
1137 static Obj_Entry *
1138 obj_from_addr(const void *addr)
1139 {
1140     unsigned long endhash;
1141     Obj_Entry *obj;
1142 
1143     endhash = elf_hash(END_SYM);
1144     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
1145 	const Elf_Sym *endsym;
1146 
1147 	if (addr < (void *) obj->mapbase)
1148 	    continue;
1149 	if ((endsym = symlook_obj(END_SYM, endhash, obj, true)) == NULL)
1150 	    continue;	/* No "end" symbol?! */
1151 	if (addr < (void *) (obj->relocbase + endsym->st_value))
1152 	    return obj;
1153     }
1154     return NULL;
1155 }
1156 
1157 static void
1158 objlist_add(Objlist *list, Obj_Entry *obj)
1159 {
1160     Objlist_Entry *elm;
1161 
1162     elm = NEW(Objlist_Entry);
1163     elm->obj = obj;
1164     STAILQ_INSERT_TAIL(list, elm, link);
1165 }
1166 
1167 static Objlist_Entry *
1168 objlist_find(Objlist *list, const Obj_Entry *obj)
1169 {
1170     Objlist_Entry *elm;
1171 
1172     STAILQ_FOREACH(elm, list, link)
1173 	if (elm->obj == obj)
1174 	    return elm;
1175     return NULL;
1176 }
1177 
1178 static void
1179 objlist_remove(Objlist *list, Obj_Entry *obj)
1180 {
1181     Objlist_Entry *elm;
1182 
1183     if ((elm = objlist_find(list, obj)) != NULL) {
1184 	STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1185 	free(elm);
1186     }
1187 }
1188 
1189 /*
1190  * Relocate newly-loaded shared objects.  The argument is a pointer to
1191  * the Obj_Entry for the first such object.  All objects from the first
1192  * to the end of the list of objects are relocated.  Returns 0 on success,
1193  * or -1 on failure.
1194  */
1195 static int
1196 relocate_objects(Obj_Entry *first, bool bind_now)
1197 {
1198     Obj_Entry *obj;
1199 
1200     for (obj = first;  obj != NULL;  obj = obj->next) {
1201 	if (obj != &obj_rtld)
1202 	    dbg("relocating \"%s\"", obj->path);
1203 	if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL ||
1204 	    obj->symtab == NULL || obj->strtab == NULL) {
1205 	    _rtld_error("%s: Shared object has no run-time symbol table",
1206 	      obj->path);
1207 	    return -1;
1208 	}
1209 
1210 	if (obj->textrel) {
1211 	    /* There are relocations to the write-protected text segment. */
1212 	    if (mprotect(obj->mapbase, obj->textsize,
1213 	      PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
1214 		_rtld_error("%s: Cannot write-enable text segment: %s",
1215 		  obj->path, strerror(errno));
1216 		return -1;
1217 	    }
1218 	}
1219 
1220 	/* Process the non-PLT relocations. */
1221 	if (reloc_non_plt(obj, &obj_rtld))
1222 		return -1;
1223 
1224 	if (obj->textrel) {	/* Re-protected the text segment. */
1225 	    if (mprotect(obj->mapbase, obj->textsize,
1226 	      PROT_READ|PROT_EXEC) == -1) {
1227 		_rtld_error("%s: Cannot write-protect text segment: %s",
1228 		  obj->path, strerror(errno));
1229 		return -1;
1230 	    }
1231 	}
1232 
1233 	/* Process the PLT relocations. */
1234 	if (reloc_plt(obj, bind_now))
1235 		return -1;
1236 
1237 	/*
1238 	 * Set up the magic number and version in the Obj_Entry.  These
1239 	 * were checked in the crt1.o from the original ElfKit, so we
1240 	 * set them for backward compatibility.
1241 	 */
1242 	obj->magic = RTLD_MAGIC;
1243 	obj->version = RTLD_VERSION;
1244 
1245 	/* Set the special PLT or GOT entries. */
1246 	init_pltgot(obj);
1247     }
1248 
1249     return 0;
1250 }
1251 
1252 /*
1253  * Cleanup procedure.  It will be called (by the atexit mechanism) just
1254  * before the process exits.
1255  */
1256 static void
1257 rtld_exit(void)
1258 {
1259     Obj_Entry *obj;
1260 
1261     dbg("rtld_exit()");
1262     for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
1263 	if (obj->fini != NULL)
1264 	    (*obj->fini)();
1265 }
1266 
1267 static char *
1268 search_library_path(const char *name, const char *path)
1269 {
1270     size_t namelen = strlen(name);
1271     const char *p = path;
1272 
1273     if (p == NULL)
1274 	return NULL;
1275 
1276     p += strspn(p, ":;");
1277     while (*p != '\0') {
1278 	size_t len = strcspn(p, ":;");
1279 
1280 	if (*p == '/' || trust) {
1281 	    char *pathname;
1282 	    const char *dir = p;
1283 	    size_t dirlen = len;
1284 
1285 	    pathname = xmalloc(dirlen + 1 + namelen + 1);
1286 	    strncpy(pathname, dir, dirlen);
1287 	    pathname[dirlen] = '/';
1288 	    strcpy(pathname + dirlen + 1, name);
1289 
1290 	    dbg("  Trying \"%s\"", pathname);
1291 	    if (access(pathname, F_OK) == 0)		/* We found it */
1292 		return pathname;
1293 
1294 	    free(pathname);
1295 	}
1296 	p += len;
1297 	p += strspn(p, ":;");
1298     }
1299 
1300     return NULL;
1301 }
1302 
1303 int
1304 dlclose(void *handle)
1305 {
1306     Obj_Entry *root;
1307     Obj_Entry *obj;
1308     Funclist finilist;
1309 
1310     wlock_acquire();
1311     root = dlcheck(handle);
1312     if (root == NULL) {
1313 	lock_release();
1314 	return -1;
1315     }
1316 
1317     /* Unreference the object and its dependencies. */
1318     root->dl_refcount--;
1319     unref_dag(root);
1320 
1321     if (root->refcount == 0) {
1322 	/*
1323 	 * The object is no longer referenced, so we must unload it.
1324 	 * First, make a list of the fini functions and then call them
1325 	 * with no locks held.
1326 	 */
1327 	funclist_init(&finilist);
1328 	for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
1329 	    if (obj->refcount == 0 && obj->fini != NULL)
1330 		funclist_push_tail(&finilist, obj->fini);
1331 
1332 	lock_release();
1333 	funclist_call(&finilist);
1334 	wlock_acquire();
1335 	funclist_clear(&finilist);
1336 
1337 	/* Finish cleaning up the newly-unreferenced objects. */
1338 	GDB_STATE(RT_DELETE);
1339 	unload_object(root);
1340 	GDB_STATE(RT_CONSISTENT);
1341     }
1342     lock_release();
1343     return 0;
1344 }
1345 
1346 const char *
1347 dlerror(void)
1348 {
1349     char *msg = error_message;
1350     error_message = NULL;
1351     return msg;
1352 }
1353 
1354 void
1355 dllockinit(void *context,
1356 	   void *(*lock_create)(void *context),
1357            void (*rlock_acquire)(void *lock),
1358            void (*wlock_acquire)(void *lock),
1359            void (*lock_release)(void *lock),
1360            void (*lock_destroy)(void *lock),
1361 	   void (*context_destroy)(void *context))
1362 {
1363     /* NULL arguments mean reset to the built-in locks. */
1364     if (lock_create == NULL) {
1365 	context = NULL;
1366 	lock_create = lockdflt_create;
1367 	rlock_acquire = wlock_acquire = lockdflt_acquire;
1368 	lock_release = lockdflt_release;
1369 	lock_destroy = lockdflt_destroy;
1370 	context_destroy = NULL;
1371     }
1372 
1373     /* Temporarily set locking methods to no-ops. */
1374     lockinfo.rlock_acquire = lock_nop;
1375     lockinfo.wlock_acquire = lock_nop;
1376     lockinfo.lock_release = lock_nop;
1377 
1378     /* Release any existing locks and context. */
1379     if (lockinfo.lock_destroy != NULL)
1380 	lockinfo.lock_destroy(lockinfo.thelock);
1381     if (lockinfo.context_destroy != NULL)
1382 	lockinfo.context_destroy(lockinfo.context);
1383 
1384     /*
1385      * Allocate the locks we will need and call all the new locking
1386      * methods, to accomplish any needed lazy binding for the methods
1387      * themselves.
1388      */
1389     lockinfo.thelock = lock_create(lockinfo.context);
1390     rlock_acquire(lockinfo.thelock);
1391     lock_release(lockinfo.thelock);
1392     wlock_acquire(lockinfo.thelock);
1393     lock_release(lockinfo.thelock);
1394 
1395     /* Record the new method information. */
1396     lockinfo.context = context;
1397     lockinfo.rlock_acquire = rlock_acquire;
1398     lockinfo.wlock_acquire = wlock_acquire;
1399     lockinfo.lock_release = lock_release;
1400     lockinfo.lock_destroy = lock_destroy;
1401     lockinfo.context_destroy = context_destroy;
1402 }
1403 
1404 void *
1405 dlopen(const char *name, int mode)
1406 {
1407     Obj_Entry **old_obj_tail;
1408     Obj_Entry *obj;
1409     Obj_Entry *initobj;
1410     Funclist initlist;
1411 
1412     funclist_init(&initlist);
1413 
1414     wlock_acquire();
1415     GDB_STATE(RT_ADD);
1416 
1417     old_obj_tail = obj_tail;
1418     obj = NULL;
1419     if (name == NULL) {
1420 	obj = obj_main;
1421 	obj->refcount++;
1422     } else {
1423 	char *path = find_library(name, obj_main);
1424 	if (path != NULL)
1425 	    obj = load_object(path);
1426     }
1427 
1428     if (obj) {
1429 	obj->dl_refcount++;
1430 	if (mode & RTLD_GLOBAL && objlist_find(&list_global, obj) == NULL)
1431 	    objlist_add(&list_global, obj);
1432 	mode &= RTLD_MODEMASK;
1433 	if (*old_obj_tail != NULL) {		/* We loaded something new. */
1434 	    assert(*old_obj_tail == obj);
1435 
1436 	    if (load_needed_objects(obj) == -1 ||
1437 	      (init_dag(obj), relocate_objects(obj, mode == RTLD_NOW)) == -1) {
1438 		obj->dl_refcount--;
1439 		unref_dag(obj);
1440 		if (obj->refcount == 0)
1441 		    unload_object(obj);
1442 		obj = NULL;
1443 	    } else {
1444 		/* Make list of init functions to call, in reverse order */
1445 		for (initobj = obj; initobj != NULL; initobj = initobj->next)
1446 		    if (initobj->init != NULL)
1447 			funclist_push_head(&initlist, initobj->init);
1448 	    }
1449 	}
1450     }
1451 
1452     GDB_STATE(RT_CONSISTENT);
1453 
1454     /* Call the init functions with no locks held. */
1455     lock_release();
1456     funclist_call(&initlist);
1457     wlock_acquire();
1458     funclist_clear(&initlist);
1459     lock_release();
1460     return obj;
1461 }
1462 
1463 void *
1464 dlsym(void *handle, const char *name)
1465 {
1466     const Obj_Entry *obj;
1467     unsigned long hash;
1468     const Elf_Sym *def;
1469     const Obj_Entry *defobj;
1470 
1471     hash = elf_hash(name);
1472     def = NULL;
1473     defobj = NULL;
1474 
1475     wlock_acquire();
1476     if (handle == NULL || handle == RTLD_NEXT) {
1477 	void *retaddr;
1478 
1479 	retaddr = __builtin_return_address(0);	/* __GNUC__ only */
1480 	if ((obj = obj_from_addr(retaddr)) == NULL) {
1481 	    _rtld_error("Cannot determine caller's shared object");
1482 	    lock_release();
1483 	    return NULL;
1484 	}
1485 	if (handle == NULL) {	/* Just the caller's shared object. */
1486 	    def = symlook_obj(name, hash, obj, true);
1487 	    defobj = obj;
1488 	} else {		/* All the shared objects after the caller's */
1489 	    while ((obj = obj->next) != NULL) {
1490 		if ((def = symlook_obj(name, hash, obj, true)) != NULL) {
1491 		    defobj = obj;
1492 		    break;
1493 		}
1494 	    }
1495 	}
1496     } else {
1497 	if ((obj = dlcheck(handle)) == NULL) {
1498 	    lock_release();
1499 	    return NULL;
1500 	}
1501 
1502 	if (obj->mainprog) {
1503 	    /* Search main program and all libraries loaded by it. */
1504 	    curmark++;
1505 	    def = symlook_list(name, hash, &list_main, &defobj, true);
1506 	} else {
1507 	    /*
1508 	     * XXX - This isn't correct.  The search should include the whole
1509 	     * DAG rooted at the given object.
1510 	     */
1511 	    def = symlook_obj(name, hash, obj, true);
1512 	    defobj = obj;
1513 	}
1514     }
1515 
1516     if (def != NULL) {
1517 	lock_release();
1518 	return defobj->relocbase + def->st_value;
1519     }
1520 
1521     _rtld_error("Undefined symbol \"%s\"", name);
1522     lock_release();
1523     return NULL;
1524 }
1525 
1526 int
1527 dladdr(const void *addr, Dl_info *info)
1528 {
1529     const Obj_Entry *obj;
1530     const Elf_Sym *def;
1531     void *symbol_addr;
1532     unsigned long symoffset;
1533 
1534     wlock_acquire();
1535     obj = obj_from_addr(addr);
1536     if (obj == NULL) {
1537         _rtld_error("No shared object contains address");
1538 	lock_release();
1539         return 0;
1540     }
1541     info->dli_fname = obj->path;
1542     info->dli_fbase = obj->mapbase;
1543     info->dli_saddr = (void *)0;
1544     info->dli_sname = NULL;
1545 
1546     /*
1547      * Walk the symbol list looking for the symbol whose address is
1548      * closest to the address sent in.
1549      */
1550     for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1551         def = obj->symtab + symoffset;
1552 
1553         /*
1554          * For skip the symbol if st_shndx is either SHN_UNDEF or
1555          * SHN_COMMON.
1556          */
1557         if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1558             continue;
1559 
1560         /*
1561          * If the symbol is greater than the specified address, or if it
1562          * is further away from addr than the current nearest symbol,
1563          * then reject it.
1564          */
1565         symbol_addr = obj->relocbase + def->st_value;
1566         if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1567             continue;
1568 
1569         /* Update our idea of the nearest symbol. */
1570         info->dli_sname = obj->strtab + def->st_name;
1571         info->dli_saddr = symbol_addr;
1572 
1573         /* Exact match? */
1574         if (info->dli_saddr == addr)
1575             break;
1576     }
1577     lock_release();
1578     return 1;
1579 }
1580 
1581 static void
1582 linkmap_add(Obj_Entry *obj)
1583 {
1584     struct link_map *l = &obj->linkmap;
1585     struct link_map *prev;
1586 
1587     obj->linkmap.l_name = obj->path;
1588     obj->linkmap.l_addr = obj->mapbase;
1589     obj->linkmap.l_ld = obj->dynamic;
1590 #ifdef __mips__
1591     /* GDB needs load offset on MIPS to use the symbols */
1592     obj->linkmap.l_offs = obj->relocbase;
1593 #endif
1594 
1595     if (r_debug.r_map == NULL) {
1596 	r_debug.r_map = l;
1597 	return;
1598     }
1599 
1600     /*
1601      * Scan to the end of the list, but not past the entry for the
1602      * dynamic linker, which we want to keep at the very end.
1603      */
1604     for (prev = r_debug.r_map;
1605       prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
1606       prev = prev->l_next)
1607 	;
1608 
1609     /* Link in the new entry. */
1610     l->l_prev = prev;
1611     l->l_next = prev->l_next;
1612     if (l->l_next != NULL)
1613 	l->l_next->l_prev = l;
1614     prev->l_next = l;
1615 }
1616 
1617 static void
1618 linkmap_delete(Obj_Entry *obj)
1619 {
1620     struct link_map *l = &obj->linkmap;
1621 
1622     if (l->l_prev == NULL) {
1623 	if ((r_debug.r_map = l->l_next) != NULL)
1624 	    l->l_next->l_prev = NULL;
1625 	return;
1626     }
1627 
1628     if ((l->l_prev->l_next = l->l_next) != NULL)
1629 	l->l_next->l_prev = l->l_prev;
1630 }
1631 
1632 /*
1633  * Function for the debugger to set a breakpoint on to gain control.
1634  */
1635 void
1636 r_debug_state(void)
1637 {
1638 }
1639 
1640 /*
1641  * Set a pointer variable in the main program to the given value.  This
1642  * is used to set key variables such as "environ" before any of the
1643  * init functions are called.
1644  */
1645 static void
1646 set_program_var(const char *name, const void *value)
1647 {
1648     const Obj_Entry *obj;
1649     unsigned long hash;
1650 
1651     hash = elf_hash(name);
1652     for (obj = obj_main;  obj != NULL;  obj = obj->next) {
1653 	const Elf_Sym *def;
1654 
1655 	if ((def = symlook_obj(name, hash, obj, false)) != NULL) {
1656 	    const void **addr;
1657 
1658 	    addr = (const void **)(obj->relocbase + def->st_value);
1659 	    dbg("\"%s\": *%p <-- %p", name, addr, value);
1660 	    *addr = value;
1661 	    break;
1662 	}
1663     }
1664 }
1665 
1666 static const Elf_Sym *
1667 symlook_list(const char *name, unsigned long hash, Objlist *objlist,
1668   const Obj_Entry **defobj_out, bool in_plt)
1669 {
1670     const Elf_Sym *symp;
1671     const Elf_Sym *def;
1672     const Obj_Entry *defobj;
1673     const Objlist_Entry *elm;
1674 
1675     def = NULL;
1676     defobj = NULL;
1677     STAILQ_FOREACH(elm, objlist, link) {
1678 	if (elm->obj->mark == curmark)
1679 	    continue;
1680 	elm->obj->mark = curmark;
1681 	if ((symp = symlook_obj(name, hash, elm->obj, in_plt)) != NULL) {
1682 	    if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
1683 		def = symp;
1684 		defobj = elm->obj;
1685 		if (ELF_ST_BIND(def->st_info) != STB_WEAK)
1686 		    break;
1687 	    }
1688 	}
1689     }
1690     if (def != NULL)
1691 	*defobj_out = defobj;
1692     return def;
1693 }
1694 
1695 /*
1696  * Search the symbol table of a single shared object for a symbol of
1697  * the given name.  Returns a pointer to the symbol, or NULL if no
1698  * definition was found.
1699  *
1700  * The symbol's hash value is passed in for efficiency reasons; that
1701  * eliminates many recomputations of the hash value.
1702  */
1703 const Elf_Sym *
1704 symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
1705   bool in_plt)
1706 {
1707     if (obj->buckets != NULL) {
1708 	unsigned long symnum = obj->buckets[hash % obj->nbuckets];
1709 
1710 	while (symnum != STN_UNDEF) {
1711 	    const Elf_Sym *symp;
1712 	    const char *strp;
1713 
1714 	    if (symnum >= obj->nchains)
1715 		return NULL;	/* Bad object */
1716 	    symp = obj->symtab + symnum;
1717 	    strp = obj->strtab + symp->st_name;
1718 
1719 	    if (strcmp(name, strp) == 0)
1720 		return symp->st_shndx != SHN_UNDEF ||
1721 		  (!in_plt && symp->st_value != 0 &&
1722 		  ELF_ST_TYPE(symp->st_info) == STT_FUNC) ? symp : NULL;
1723 
1724 	    symnum = obj->chains[symnum];
1725 	}
1726     }
1727     return NULL;
1728 }
1729 
1730 static void
1731 trace_loaded_objects(Obj_Entry *obj)
1732 {
1733     char	*fmt1, *fmt2, *fmt, *main_local;
1734     int		c;
1735 
1736     if ((main_local = getenv("LD_TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
1737 	main_local = "";
1738 
1739     if ((fmt1 = getenv("LD_TRACE_LOADED_OBJECTS_FMT1")) == NULL)
1740 	fmt1 = "\t%o => %p (%x)\n";
1741 
1742     if ((fmt2 = getenv("LD_TRACE_LOADED_OBJECTS_FMT2")) == NULL)
1743 	fmt2 = "\t%o (%x)\n";
1744 
1745     for (; obj; obj = obj->next) {
1746 	Needed_Entry		*needed;
1747 	char			*name, *path;
1748 	bool			is_lib;
1749 
1750 	for (needed = obj->needed; needed; needed = needed->next) {
1751 	    if (needed->obj != NULL) {
1752 		if (needed->obj->traced)
1753 		    continue;
1754 		needed->obj->traced = true;
1755 		path = needed->obj->path;
1756 	    } else
1757 		path = "not found";
1758 
1759 	    name = (char *)obj->strtab + needed->name;
1760 	    is_lib = strncmp(name, "lib", 3) == 0;	/* XXX - bogus */
1761 
1762 	    fmt = is_lib ? fmt1 : fmt2;
1763 	    while ((c = *fmt++) != '\0') {
1764 		switch (c) {
1765 		default:
1766 		    putchar(c);
1767 		    continue;
1768 		case '\\':
1769 		    switch (c = *fmt) {
1770 		    case '\0':
1771 			continue;
1772 		    case 'n':
1773 			putchar('\n');
1774 			break;
1775 		    case 't':
1776 			putchar('\t');
1777 			break;
1778 		    }
1779 		    break;
1780 		case '%':
1781 		    switch (c = *fmt) {
1782 		    case '\0':
1783 			continue;
1784 		    case '%':
1785 		    default:
1786 			putchar(c);
1787 			break;
1788 		    case 'A':
1789 			printf("%s", main_local);
1790 			break;
1791 		    case 'a':
1792 			printf("%s", obj_main->path);
1793 			break;
1794 		    case 'o':
1795 			printf("%s", name);
1796 			break;
1797 #if 0
1798 		    case 'm':
1799 			printf("%d", sodp->sod_major);
1800 			break;
1801 		    case 'n':
1802 			printf("%d", sodp->sod_minor);
1803 			break;
1804 #endif
1805 		    case 'p':
1806 			printf("%s", path);
1807 			break;
1808 		    case 'x':
1809 			printf("%p", needed->obj ? needed->obj->mapbase : 0);
1810 			break;
1811 		    }
1812 		    break;
1813 		}
1814 		++fmt;
1815 	    }
1816 	}
1817     }
1818 }
1819 
1820 /*
1821  * Unload a dlopened object and its dependencies from memory and from
1822  * our data structures.  It is assumed that the DAG rooted in the
1823  * object has already been unreferenced, and that the object has a
1824  * reference count of 0.
1825  */
1826 static void
1827 unload_object(Obj_Entry *root)
1828 {
1829     Obj_Entry *obj;
1830     Obj_Entry **linkp;
1831     Objlist_Entry *elm;
1832 
1833     assert(root->refcount == 0);
1834 
1835     /* Remove the DAG from all objects' DAG lists. */
1836     STAILQ_FOREACH(elm, &root->dagmembers , link)
1837 	objlist_remove(&elm->obj->dldags, root);
1838 
1839     /* Remove the DAG from the RTLD_GLOBAL list. */
1840     objlist_remove(&list_global, root);
1841 
1842     /* Unmap all objects that are no longer referenced. */
1843     linkp = &obj_list->next;
1844     while ((obj = *linkp) != NULL) {
1845 	if (obj->refcount == 0) {
1846 	    dbg("unloading \"%s\"", obj->path);
1847 	    munmap(obj->mapbase, obj->mapsize);
1848 	    linkmap_delete(obj);
1849 	    *linkp = obj->next;
1850 	    obj_free(obj);
1851 	} else
1852 	    linkp = &obj->next;
1853     }
1854     obj_tail = linkp;
1855 }
1856 
1857 static void
1858 unref_dag(Obj_Entry *root)
1859 {
1860     const Needed_Entry *needed;
1861 
1862     assert(root->refcount != 0);
1863     root->refcount--;
1864     if (root->refcount == 0)
1865 	for (needed = root->needed;  needed != NULL;  needed = needed->next)
1866 	    if (needed->obj != NULL)
1867 		unref_dag(needed->obj);
1868 }
1869 
1870 /*
1871  * Non-mallocing printf, for use by malloc itself.
1872  * XXX - This doesn't belong in this module.
1873  */
1874 void
1875 xprintf(const char *fmt, ...)
1876 {
1877     char buf[256];
1878     va_list ap;
1879 
1880     va_start(ap, fmt);
1881     vsprintf(buf, fmt, ap);
1882     (void)write(1, buf, strlen(buf));
1883     va_end(ap);
1884 }
1885