Sync changes from OpenBSD. Most importantly, this adds reverse proxy support.
[dragonfly.git] / libexec / rtld-elf / rtld.c
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: src/libexec/rtld-elf/rtld.c,v 1.43.2.15 2003/02/20 20:42:46 kan Exp $
27  * $DragonFly: src/libexec/rtld-elf/rtld.c,v 1.14 2005/02/24 03:23:01 joerg Exp $
28  */
29
30 /*
31  * Dynamic linker for ELF.
32  *
33  * John Polstra <jdp@polstra.com>.
34  */
35
36 #ifndef __GNUC__
37 #error "GCC is needed to compile this file"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <sys/resident.h>
44
45 #include <dlfcn.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "debug.h"
56 #include "rtld.h"
57
58 #define PATH_RTLD       "/usr/libexec/ld-elf.so.1"
59 #define LD_ARY_CACHE    16
60
61 /* Types. */
62 typedef void (*func_ptr_type)();
63 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
64
65 /*
66  * This structure provides a reentrant way to keep a list of objects and
67  * check which ones have already been processed in some way.
68  */
69 typedef struct Struct_DoneList {
70     const Obj_Entry **objs;             /* Array of object pointers */
71     unsigned int num_alloc;             /* Allocated size of the array */
72     unsigned int num_used;              /* Number of array slots used */
73 } DoneList;
74
75 /*
76  * Function declarations.
77  */
78 static void die(void);
79 static void digest_dynamic(Obj_Entry *);
80 static const char *_getenv_ld(const char *id);
81 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
82 static Obj_Entry *dlcheck(void *);
83 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
84 static bool donelist_check(DoneList *, const Obj_Entry *);
85 static void errmsg_restore(char *);
86 static char *errmsg_save(void);
87 static void *fill_search_info(const char *, size_t, void *);
88 static char *find_library(const char *, const Obj_Entry *);
89 static Obj_Entry *find_object(const char *);
90 static const char *gethints(void);
91 static void init_dag(Obj_Entry *);
92 static void init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *);
93 static void init_rtld(caddr_t);
94 static void initlist_add_neededs(Needed_Entry *needed, Objlist *list);
95 static void initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail,
96   Objlist *list);
97 static bool is_exported(const Elf_Sym *);
98 static void linkmap_add(Obj_Entry *);
99 static void linkmap_delete(Obj_Entry *);
100 static int load_needed_objects(Obj_Entry *);
101 static int load_preload_objects(void);
102 static Obj_Entry *load_object(char *);
103 static void lock_check(void);
104 static Obj_Entry *obj_from_addr(const void *);
105 static void objlist_call_fini(Objlist *);
106 static void objlist_call_init(Objlist *);
107 static void objlist_clear(Objlist *);
108 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
109 static void objlist_init(Objlist *);
110 static void objlist_push_head(Objlist *, Obj_Entry *);
111 static void objlist_push_tail(Objlist *, Obj_Entry *);
112 static void objlist_remove(Objlist *, Obj_Entry *);
113 static void objlist_remove_unref(Objlist *);
114 static void *path_enumerate(const char *, path_enum_proc, void *);
115 static int relocate_objects(Obj_Entry *, bool);
116 static int rtld_dirname(const char *, char *);
117 static void rtld_exit(void);
118 static char *search_library_path(const char *, const char *);
119 static const void **get_program_var_addr(const char *name);
120 static void set_program_var(const char *, const void *);
121 static const Elf_Sym *symlook_default(const char *, unsigned long hash,
122   const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt);
123 static const Elf_Sym *symlook_list(const char *, unsigned long,
124   Objlist *, const Obj_Entry **, bool in_plt, DoneList *);
125 static void trace_loaded_objects(Obj_Entry *obj);
126 static void unlink_object(Obj_Entry *);
127 static void unload_object(Obj_Entry *);
128 static void unref_dag(Obj_Entry *);
129
130 void r_debug_state(struct r_debug*, struct link_map*);
131
132 /*
133  * Data declarations.
134  */
135 static char *error_message;     /* Message for dlerror(), or NULL */
136 struct r_debug r_debug;         /* for GDB; */
137 static bool trust;              /* False for setuid and setgid programs */
138 static const char *ld_bind_now; /* Environment variable for immediate binding */
139 static const char *ld_debug;    /* Environment variable for debugging */
140 static const char *ld_library_path; /* Environment variable for search path */
141 static char *ld_preload;        /* Environment variable for libraries to
142                                    load first */
143 static const char *ld_tracing;  /* Called from ldd(1) to print libs */
144 static Obj_Entry *obj_list;     /* Head of linked list of shared objects */
145 static Obj_Entry **obj_tail;    /* Link field of last object in list */
146 static Obj_Entry **preload_tail;
147 static Obj_Entry *obj_main;     /* The main program shared object */
148 static Obj_Entry obj_rtld;      /* The dynamic linker shared object */
149 static unsigned int obj_count;  /* Number of objects in obj_list */
150 static int      ld_resident;    /* Non-zero if resident */
151 static const char *ld_ary[LD_ARY_CACHE];
152 static int      ld_index;
153
154 static Objlist list_global =    /* Objects dlopened with RTLD_GLOBAL */
155   STAILQ_HEAD_INITIALIZER(list_global);
156 static Objlist list_main =      /* Objects loaded at program startup */
157   STAILQ_HEAD_INITIALIZER(list_main);
158 static Objlist list_fini =      /* Objects needing fini() calls */
159   STAILQ_HEAD_INITIALIZER(list_fini);
160
161 static LockInfo lockinfo;
162
163 static Elf_Sym sym_zero;        /* For resolving undefined weak refs. */
164
165 #define GDB_STATE(s,m)  r_debug.r_state = s; r_debug_state(&r_debug,m);
166
167 extern Elf_Dyn _DYNAMIC;
168 #pragma weak _DYNAMIC
169
170 /*
171  * These are the functions the dynamic linker exports to application
172  * programs.  They are the only symbols the dynamic linker is willing
173  * to export from itself.
174  */
175 static func_ptr_type exports[] = {
176     (func_ptr_type) &_rtld_error,
177     (func_ptr_type) &dlclose,
178     (func_ptr_type) &dlerror,
179     (func_ptr_type) &dlopen,
180     (func_ptr_type) &dlsym,
181     (func_ptr_type) &dladdr,
182     (func_ptr_type) &dllockinit,
183     (func_ptr_type) &dlinfo,
184     NULL
185 };
186
187 /*
188  * Global declarations normally provided by crt1.  The dynamic linker is
189  * not built with crt1, so we have to provide them ourselves.
190  */
191 char *__progname;
192 char **environ;
193
194 /*
195  * Fill in a DoneList with an allocation large enough to hold all of
196  * the currently-loaded objects.  Keep this as a macro since it calls
197  * alloca and we want that to occur within the scope of the caller.
198  */
199 #define donelist_init(dlp)                                      \
200     ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]),   \
201     assert((dlp)->objs != NULL),                                \
202     (dlp)->num_alloc = obj_count,                               \
203     (dlp)->num_used = 0)
204
205 static __inline void
206 rlock_acquire(void)
207 {
208     lockinfo.rlock_acquire(lockinfo.thelock);
209     atomic_incr_int(&lockinfo.rcount);
210     lock_check();
211 }
212
213 static __inline void
214 wlock_acquire(void)
215 {
216     lockinfo.wlock_acquire(lockinfo.thelock);
217     atomic_incr_int(&lockinfo.wcount);
218     lock_check();
219 }
220
221 static __inline void
222 rlock_release(void)
223 {
224     atomic_decr_int(&lockinfo.rcount);
225     lockinfo.rlock_release(lockinfo.thelock);
226 }
227
228 static __inline void
229 wlock_release(void)
230 {
231     atomic_decr_int(&lockinfo.wcount);
232     lockinfo.wlock_release(lockinfo.thelock);
233 }
234
235 /*
236  * Main entry point for dynamic linking.  The first argument is the
237  * stack pointer.  The stack is expected to be laid out as described
238  * in the SVR4 ABI specification, Intel 386 Processor Supplement.
239  * Specifically, the stack pointer points to a word containing
240  * ARGC.  Following that in the stack is a null-terminated sequence
241  * of pointers to argument strings.  Then comes a null-terminated
242  * sequence of pointers to environment strings.  Finally, there is a
243  * sequence of "auxiliary vector" entries.
244  *
245  * The second argument points to a place to store the dynamic linker's
246  * exit procedure pointer and the third to a place to store the main
247  * program's object.
248  *
249  * The return value is the main program's entry point.
250  */
251 func_ptr_type
252 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
253 {
254     Elf_Auxinfo *aux_info[AT_COUNT];
255     int i;
256     int argc;
257     char **argv;
258     char **env;
259     Elf_Auxinfo *aux;
260     Elf_Auxinfo *auxp;
261     const char *argv0;
262     Obj_Entry *obj;
263     Objlist initlist;
264
265     ld_index = 0;       /* don't use old env cache in case we are resident */
266
267     /*
268      * On entry, the dynamic linker itself has not been relocated yet.
269      * Be very careful not to reference any global data until after
270      * init_rtld has returned.  It is OK to reference file-scope statics
271      * and string constants, and to call static and global functions.
272      */
273
274     /* Find the auxiliary vector on the stack. */
275     argc = *sp++;
276     argv = (char **) sp;
277     sp += argc + 1;     /* Skip over arguments and NULL terminator */
278     env = (char **) sp;
279
280     /*
281      * If we aren't already resident we have to dig out some more info.
282      * Note that auxinfo does not exist when we are resident.
283      */
284     if (ld_resident == 0) {
285         while (*sp++ != 0)      /* Skip over environment, and NULL terminator */
286             ;
287         aux = (Elf_Auxinfo *) sp;
288
289         /* Digest the auxiliary vector. */
290         for (i = 0;  i < AT_COUNT;  i++)
291             aux_info[i] = NULL;
292         for (auxp = aux;  auxp->a_type != AT_NULL;  auxp++) {
293             if (auxp->a_type < AT_COUNT)
294                 aux_info[auxp->a_type] = auxp;
295         }
296
297         /* Initialize and relocate ourselves. */
298         assert(aux_info[AT_BASE] != NULL);
299         init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
300     }
301
302     __progname = obj_rtld.path;
303     argv0 = argv[0] != NULL ? argv[0] : "(null)";
304     environ = env;
305
306     trust = (geteuid() == getuid()) && (getegid() == getgid());
307
308     ld_bind_now = _getenv_ld("LD_BIND_NOW");
309     if (trust) {
310         ld_debug = _getenv_ld("LD_DEBUG");
311         ld_library_path = _getenv_ld("LD_LIBRARY_PATH");
312         ld_preload = (char *)_getenv_ld("LD_PRELOAD");
313     }
314     ld_tracing = _getenv_ld("LD_TRACE_LOADED_OBJECTS");
315
316     if (ld_debug != NULL && *ld_debug != '\0')
317         debug = 1;
318     dbg("%s is initialized, base address = %p", __progname,
319         (caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
320     dbg("RTLD dynamic = %p", obj_rtld.dynamic);
321     dbg("RTLD pltgot  = %p", obj_rtld.pltgot);
322
323     /*
324      * If we are resident we can skip work that we have already done.
325      * Note that the stack is reset and there is no Elf_Auxinfo
326      * when running from a resident image, and the static globals setup
327      * between here and resident_skip will have already been setup.
328      */
329     if (ld_resident)
330         goto resident_skip1;
331
332     /*
333      * Load the main program, or process its program header if it is
334      * already loaded.
335      */
336     if (aux_info[AT_EXECFD] != NULL) {  /* Load the main program. */
337         int fd = aux_info[AT_EXECFD]->a_un.a_val;
338         dbg("loading main program");
339         obj_main = map_object(fd, argv0, NULL);
340         close(fd);
341         if (obj_main == NULL)
342             die();
343     } else {                            /* Main program already loaded. */
344         const Elf_Phdr *phdr;
345         int phnum;
346         caddr_t entry;
347
348         dbg("processing main program's program header");
349         assert(aux_info[AT_PHDR] != NULL);
350         phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
351         assert(aux_info[AT_PHNUM] != NULL);
352         phnum = aux_info[AT_PHNUM]->a_un.a_val;
353         assert(aux_info[AT_PHENT] != NULL);
354         assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
355         assert(aux_info[AT_ENTRY] != NULL);
356         entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
357         if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
358             die();
359     }
360
361     obj_main->path = xstrdup(argv0);
362     obj_main->mainprog = true;
363
364     /*
365      * Get the actual dynamic linker pathname from the executable if
366      * possible.  (It should always be possible.)  That ensures that
367      * gdb will find the right dynamic linker even if a non-standard
368      * one is being used.
369      */
370     if (obj_main->interp != NULL &&
371       strcmp(obj_main->interp, obj_rtld.path) != 0) {
372         free(obj_rtld.path);
373         obj_rtld.path = xstrdup(obj_main->interp);
374         __progname = obj_rtld.path;
375     }
376
377     digest_dynamic(obj_main);
378
379     linkmap_add(obj_main);
380     linkmap_add(&obj_rtld);
381
382     /* Link the main program into the list of objects. */
383     *obj_tail = obj_main;
384     obj_tail = &obj_main->next;
385     obj_count++;
386     obj_main->refcount++;
387     /* Make sure we don't call the main program's init and fini functions. */
388     obj_main->init = obj_main->fini = NULL;
389
390     /* Initialize a fake symbol for resolving undefined weak references. */
391     sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
392     sym_zero.st_shndx = SHN_ABS;
393
394     dbg("loading LD_PRELOAD libraries");
395     if (load_preload_objects() == -1)
396         die();
397     preload_tail = obj_tail;
398
399     dbg("loading needed objects");
400     if (load_needed_objects(obj_main) == -1)
401         die();
402
403     /* Make a list of all objects loaded at startup. */
404     for (obj = obj_list;  obj != NULL;  obj = obj->next)
405         objlist_push_tail(&list_main, obj);
406
407 resident_skip1:
408
409     if (ld_tracing) {           /* We're done */
410         trace_loaded_objects(obj_main);
411         exit(0);
412     }
413
414     if (ld_resident)            /* XXX clean this up! */
415         goto resident_skip2;
416
417     if (getenv("LD_DUMP_REL_PRE") != NULL) {
418        dump_relocations(obj_main);
419        exit (0);
420     }
421
422     if (relocate_objects(obj_main,
423         ld_bind_now != NULL && *ld_bind_now != '\0') == -1)
424         die();
425
426     dbg("doing copy relocations");
427     if (do_copy_relocations(obj_main) == -1)
428         die();
429
430 resident_skip2:
431
432     if (_getenv_ld("LD_RESIDENT_UNREGISTER_NOW")) {
433         if (exec_sys_unregister(-1) < 0) {
434             dbg("exec_sys_unregister failed %d\n", errno);
435             exit(errno);
436         }
437         dbg("exec_sys_unregister success\n");
438         exit(0);
439     }
440
441     if (getenv("LD_DUMP_REL_POST") != NULL) {
442        dump_relocations(obj_main);
443        exit (0);
444     }
445
446     dbg("initializing key program variables");
447     set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
448     set_program_var("environ", env);
449
450     if (_getenv_ld("LD_RESIDENT_REGISTER_NOW")) {
451         extern void resident_start(void);
452         ld_resident = 1;
453         if (exec_sys_register(resident_start) < 0) {
454             dbg("exec_sys_register failed %d\n", errno);
455             exit(errno);
456         }
457         dbg("exec_sys_register success\n");
458         exit(0);
459     }
460
461     dbg("initializing thread locks");
462     lockdflt_init(&lockinfo);
463     lockinfo.thelock = lockinfo.lock_create(lockinfo.context);
464
465     /* Make a list of init functions to call. */
466     objlist_init(&initlist);
467     initlist_add_objects(obj_list, preload_tail, &initlist);
468
469     r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
470
471     objlist_call_init(&initlist);
472     wlock_acquire();
473     objlist_clear(&initlist);
474     wlock_release();
475
476
477
478     dbg("transferring control to program entry point = %p", obj_main->entry);
479
480     /* Return the exit procedure and the program entry point. */
481     *exit_proc = rtld_exit;
482     *objp = obj_main;
483     return (func_ptr_type) obj_main->entry;
484 }
485
486 Elf_Addr
487 _rtld_bind(Obj_Entry *obj, Elf_Word reloff)
488 {
489     const Elf_Rel *rel;
490     const Elf_Sym *def;
491     const Obj_Entry *defobj;
492     Elf_Addr *where;
493     Elf_Addr target;
494
495     rlock_acquire();
496     if (obj->pltrel)
497         rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
498     else
499         rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
500
501     where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
502     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
503     if (def == NULL)
504         die();
505
506     target = (Elf_Addr)(defobj->relocbase + def->st_value);
507
508     dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
509       defobj->strtab + def->st_name, basename(obj->path),
510       (void *)target, basename(defobj->path));
511
512     reloc_jmpslot(where, target);
513     rlock_release();
514     return target;
515 }
516
517 /*
518  * Error reporting function.  Use it like printf.  If formats the message
519  * into a buffer, and sets things up so that the next call to dlerror()
520  * will return the message.
521  */
522 void
523 _rtld_error(const char *fmt, ...)
524 {
525     static char buf[512];
526     va_list ap;
527
528     va_start(ap, fmt);
529     vsnprintf(buf, sizeof buf, fmt, ap);
530     error_message = buf;
531     va_end(ap);
532 }
533
534 /*
535  * Return a dynamically-allocated copy of the current error message, if any.
536  */
537 static char *
538 errmsg_save(void)
539 {
540     return error_message == NULL ? NULL : xstrdup(error_message);
541 }
542
543 /*
544  * Restore the current error message from a copy which was previously saved
545  * by errmsg_save().  The copy is freed.
546  */
547 static void
548 errmsg_restore(char *saved_msg)
549 {
550     if (saved_msg == NULL)
551         error_message = NULL;
552     else {
553         _rtld_error("%s", saved_msg);
554         free(saved_msg);
555     }
556 }
557
558 const char *
559 basename(const char *name)
560 {
561     const char *p = strrchr(name, '/');
562     return p != NULL ? p + 1 : name;
563 }
564
565 static void
566 die(void)
567 {
568     const char *msg = dlerror();
569
570     if (msg == NULL)
571         msg = "Fatal error";
572     errx(1, "%s", msg);
573 }
574
575 /*
576  * Process a shared object's DYNAMIC section, and save the important
577  * information in its Obj_Entry structure.
578  */
579 static void
580 digest_dynamic(Obj_Entry *obj)
581 {
582     const Elf_Dyn *dynp;
583     Needed_Entry **needed_tail = &obj->needed;
584     const Elf_Dyn *dyn_rpath = NULL;
585     int plttype = DT_REL;
586
587     for (dynp = obj->dynamic;  dynp->d_tag != DT_NULL;  dynp++) {
588         switch (dynp->d_tag) {
589
590         case DT_REL:
591             obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
592             break;
593
594         case DT_RELSZ:
595             obj->relsize = dynp->d_un.d_val;
596             break;
597
598         case DT_RELENT:
599             assert(dynp->d_un.d_val == sizeof(Elf_Rel));
600             break;
601
602         case DT_JMPREL:
603             obj->pltrel = (const Elf_Rel *)
604               (obj->relocbase + dynp->d_un.d_ptr);
605             break;
606
607         case DT_PLTRELSZ:
608             obj->pltrelsize = dynp->d_un.d_val;
609             break;
610
611         case DT_RELA:
612             obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
613             break;
614
615         case DT_RELASZ:
616             obj->relasize = dynp->d_un.d_val;
617             break;
618
619         case DT_RELAENT:
620             assert(dynp->d_un.d_val == sizeof(Elf_Rela));
621             break;
622
623         case DT_PLTREL:
624             plttype = dynp->d_un.d_val;
625             assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
626             break;
627
628         case DT_SYMTAB:
629             obj->symtab = (const Elf_Sym *)
630               (obj->relocbase + dynp->d_un.d_ptr);
631             break;
632
633         case DT_SYMENT:
634             assert(dynp->d_un.d_val == sizeof(Elf_Sym));
635             break;
636
637         case DT_STRTAB:
638             obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
639             break;
640
641         case DT_STRSZ:
642             obj->strsize = dynp->d_un.d_val;
643             break;
644
645         case DT_HASH:
646             {
647                 const Elf_Addr *hashtab = (const Elf_Addr *)
648                   (obj->relocbase + dynp->d_un.d_ptr);
649                 obj->nbuckets = hashtab[0];
650                 obj->nchains = hashtab[1];
651                 obj->buckets = hashtab + 2;
652                 obj->chains = obj->buckets + obj->nbuckets;
653             }
654             break;
655
656         case DT_NEEDED:
657             if (!obj->rtld) {
658                 Needed_Entry *nep = NEW(Needed_Entry);
659                 nep->name = dynp->d_un.d_val;
660                 nep->obj = NULL;
661                 nep->next = NULL;
662
663                 *needed_tail = nep;
664                 needed_tail = &nep->next;
665             }
666             break;
667
668         case DT_PLTGOT:
669             obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
670             break;
671
672         case DT_TEXTREL:
673             obj->textrel = true;
674             break;
675
676         case DT_SYMBOLIC:
677             obj->symbolic = true;
678             break;
679
680         case DT_RPATH:
681         case DT_RUNPATH:        /* XXX: process separately */
682             /*
683              * We have to wait until later to process this, because we
684              * might not have gotten the address of the string table yet.
685              */
686             dyn_rpath = dynp;
687             break;
688
689         case DT_SONAME:
690             /* Not used by the dynamic linker. */
691             break;
692
693         case DT_INIT:
694             obj->init = (InitFunc) (obj->relocbase + dynp->d_un.d_ptr);
695             break;
696
697         case DT_FINI:
698             obj->fini = (InitFunc) (obj->relocbase + dynp->d_un.d_ptr);
699             break;
700
701         case DT_DEBUG:
702             /* XXX - not implemented yet */
703             dbg("Filling in DT_DEBUG entry");
704             ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
705             break;
706
707         case DT_FLAGS:
708                 if (dynp->d_un.d_val & DF_ORIGIN) {
709                     obj->origin_path = xmalloc(PATH_MAX);
710                     if (rtld_dirname(obj->path, obj->origin_path) == -1)
711                         die();
712                 }
713                 if (dynp->d_un.d_val & DF_SYMBOLIC)
714                     obj->symbolic = true;
715                 if (dynp->d_un.d_val & DF_TEXTREL)
716                     obj->textrel = true;
717                 if (dynp->d_un.d_val & DF_BIND_NOW)
718                     obj->bind_now = true;
719                 if (dynp->d_un.d_val & DF_STATIC_TLS)
720                     ;
721             break;
722
723         default:
724             dbg("Ignoring d_tag %d = %#x", dynp->d_tag, dynp->d_tag);
725             break;
726         }
727     }
728
729     obj->traced = false;
730
731     if (plttype == DT_RELA) {
732         obj->pltrela = (const Elf_Rela *) obj->pltrel;
733         obj->pltrel = NULL;
734         obj->pltrelasize = obj->pltrelsize;
735         obj->pltrelsize = 0;
736     }
737
738     if (dyn_rpath != NULL)
739         obj->rpath = obj->strtab + dyn_rpath->d_un.d_val;
740 }
741
742 /*
743  * Process a shared object's program header.  This is used only for the
744  * main program, when the kernel has already loaded the main program
745  * into memory before calling the dynamic linker.  It creates and
746  * returns an Obj_Entry structure.
747  */
748 static Obj_Entry *
749 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
750 {
751     Obj_Entry *obj;
752     const Elf_Phdr *phlimit = phdr + phnum;
753     const Elf_Phdr *ph;
754     int nsegs = 0;
755
756     obj = obj_new();
757     for (ph = phdr;  ph < phlimit;  ph++) {
758         switch (ph->p_type) {
759
760         case PT_PHDR:
761             if ((const Elf_Phdr *)ph->p_vaddr != phdr) {
762                 _rtld_error("%s: invalid PT_PHDR", path);
763                 return NULL;
764             }
765             obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
766             obj->phsize = ph->p_memsz;
767             break;
768
769         case PT_INTERP:
770             obj->interp = (const char *) ph->p_vaddr;
771             break;
772
773         case PT_LOAD:
774             if (nsegs == 0) {   /* First load segment */
775                 obj->vaddrbase = trunc_page(ph->p_vaddr);
776                 obj->mapbase = (caddr_t) obj->vaddrbase;
777                 obj->relocbase = obj->mapbase - obj->vaddrbase;
778                 obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
779                   obj->vaddrbase;
780             } else {            /* Last load segment */
781                 obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
782                   obj->vaddrbase;
783             }
784             nsegs++;
785             break;
786
787         case PT_DYNAMIC:
788             obj->dynamic = (const Elf_Dyn *) ph->p_vaddr;
789             break;
790         }
791     }
792     if (nsegs < 1) {
793         _rtld_error("%s: too few PT_LOAD segments", path);
794         return NULL;
795     }
796
797     obj->entry = entry;
798     return obj;
799 }
800
801 static Obj_Entry *
802 dlcheck(void *handle)
803 {
804     Obj_Entry *obj;
805
806     for (obj = obj_list;  obj != NULL;  obj = obj->next)
807         if (obj == (Obj_Entry *) handle)
808             break;
809
810     if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
811         _rtld_error("Invalid shared object handle %p", handle);
812         return NULL;
813     }
814     return obj;
815 }
816
817 /*
818  * If the given object is already in the donelist, return true.  Otherwise
819  * add the object to the list and return false.
820  */
821 static bool
822 donelist_check(DoneList *dlp, const Obj_Entry *obj)
823 {
824     unsigned int i;
825
826     for (i = 0;  i < dlp->num_used;  i++)
827         if (dlp->objs[i] == obj)
828             return true;
829     /*
830      * Our donelist allocation should always be sufficient.  But if
831      * our threads locking isn't working properly, more shared objects
832      * could have been loaded since we allocated the list.  That should
833      * never happen, but we'll handle it properly just in case it does.
834      */
835     if (dlp->num_used < dlp->num_alloc)
836         dlp->objs[dlp->num_used++] = obj;
837     return false;
838 }
839
840 /*
841  * Hash function for symbol table lookup.  Don't even think about changing
842  * this.  It is specified by the System V ABI.
843  */
844 unsigned long
845 elf_hash(const char *name)
846 {
847     const unsigned char *p = (const unsigned char *) name;
848     unsigned long h = 0;
849     unsigned long g;
850
851     while (*p != '\0') {
852         h = (h << 4) + *p++;
853         if ((g = h & 0xf0000000) != 0)
854             h ^= g >> 24;
855         h &= ~g;
856     }
857     return h;
858 }
859
860 /*
861  * Find the library with the given name, and return its full pathname.
862  * The returned string is dynamically allocated.  Generates an error
863  * message and returns NULL if the library cannot be found.
864  *
865  * If the second argument is non-NULL, then it refers to an already-
866  * loaded shared object, whose library search path will be searched.
867  *
868  * The search order is:
869  *   LD_LIBRARY_PATH
870  *   rpath in the referencing file
871  *   ldconfig hints
872  *   /usr/lib
873  */
874 static char *
875 find_library(const char *name, const Obj_Entry *refobj)
876 {
877     char *pathname;
878
879     if (strchr(name, '/') != NULL) {    /* Hard coded pathname */
880         if (name[0] != '/' && !trust) {
881             _rtld_error("Absolute pathname required for shared object \"%s\"",
882               name);
883             return NULL;
884         }
885         return xstrdup(name);
886     }
887
888     dbg(" Searching for \"%s\"", name);
889
890     if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
891       (refobj != NULL &&
892       (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
893       (pathname = search_library_path(name, gethints())) != NULL ||
894       (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
895         return pathname;
896
897     if(refobj != NULL && refobj->path != NULL) {
898         _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
899           name, basename(refobj->path));
900     } else {
901         _rtld_error("Shared object \"%s\" not found", name);
902     }
903     return NULL;
904 }
905
906 /*
907  * Given a symbol number in a referencing object, find the corresponding
908  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
909  * no definition was found.  Returns a pointer to the Obj_Entry of the
910  * defining object via the reference parameter DEFOBJ_OUT.
911  */
912 const Elf_Sym *
913 find_symdef(unsigned long symnum, const Obj_Entry *refobj,
914     const Obj_Entry **defobj_out, bool in_plt, SymCache *cache)
915 {
916     const Elf_Sym *ref;
917     const Elf_Sym *def;
918     const Obj_Entry *defobj;
919     const char *name;
920     unsigned long hash;
921
922     /*
923      * If we have already found this symbol, get the information from
924      * the cache.
925      */
926     if (symnum >= refobj->nchains)
927         return NULL;    /* Bad object */
928     if (cache != NULL && cache[symnum].sym != NULL) {
929         *defobj_out = cache[symnum].obj;
930         return cache[symnum].sym;
931     }
932
933     ref = refobj->symtab + symnum;
934     name = refobj->strtab + ref->st_name;
935     hash = elf_hash(name);
936     defobj = NULL;
937
938     def = symlook_default(name, hash, refobj, &defobj, in_plt);
939
940     /*
941      * If we found no definition and the reference is weak, treat the
942      * symbol as having the value zero.
943      */
944     if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
945         def = &sym_zero;
946         defobj = obj_main;
947     }
948
949     if (def != NULL) {
950         *defobj_out = defobj;
951         /* Record the information in the cache to avoid subsequent lookups. */
952         if (cache != NULL) {
953             cache[symnum].sym = def;
954             cache[symnum].obj = defobj;
955         }
956     } else
957         _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
958     return def;
959 }
960
961 /*
962  * Return the search path from the ldconfig hints file, reading it if
963  * necessary.  Returns NULL if there are problems with the hints file,
964  * or if the search path there is empty.
965  */
966 static const char *
967 gethints(void)
968 {
969     static char *hints;
970
971     if (hints == NULL) {
972         int fd;
973         struct elfhints_hdr hdr;
974         char *p;
975
976         /* Keep from trying again in case the hints file is bad. */
977         hints = "";
978
979         if ((fd = open(_PATH_ELF_HINTS, O_RDONLY)) == -1)
980             return NULL;
981         if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
982           hdr.magic != ELFHINTS_MAGIC ||
983           hdr.version != 1) {
984             close(fd);
985             return NULL;
986         }
987         p = xmalloc(hdr.dirlistlen + 1);
988         if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 ||
989           read(fd, p, hdr.dirlistlen + 1) != hdr.dirlistlen + 1) {
990             free(p);
991             close(fd);
992             return NULL;
993         }
994         hints = p;
995         close(fd);
996     }
997     return hints[0] != '\0' ? hints : NULL;
998 }
999
1000 static void
1001 init_dag(Obj_Entry *root)
1002 {
1003     DoneList donelist;
1004
1005     donelist_init(&donelist);
1006     init_dag1(root, root, &donelist);
1007 }
1008
1009 static void
1010 init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *dlp)
1011 {
1012     const Needed_Entry *needed;
1013
1014     if (donelist_check(dlp, obj))
1015         return;
1016     objlist_push_tail(&obj->dldags, root);
1017     objlist_push_tail(&root->dagmembers, obj);
1018     for (needed = obj->needed;  needed != NULL;  needed = needed->next)
1019         if (needed->obj != NULL)
1020             init_dag1(root, needed->obj, dlp);
1021 }
1022
1023 /*
1024  * Initialize the dynamic linker.  The argument is the address at which
1025  * the dynamic linker has been mapped into memory.  The primary task of
1026  * this function is to relocate the dynamic linker.
1027  */
1028 static void
1029 init_rtld(caddr_t mapbase)
1030 {
1031     /*
1032      * Conjure up an Obj_Entry structure for the dynamic linker.
1033      *
1034      * The "path" member is supposed to be dynamically-allocated, but we
1035      * aren't yet initialized sufficiently to do that.  Below we will
1036      * replace the static version with a dynamically-allocated copy.
1037      */
1038     obj_rtld.path = PATH_RTLD;
1039     obj_rtld.rtld = true;
1040     obj_rtld.mapbase = mapbase;
1041 #ifdef PIC
1042     obj_rtld.relocbase = mapbase;
1043 #endif
1044     if (&_DYNAMIC != 0) {
1045         obj_rtld.dynamic = rtld_dynamic(&obj_rtld);
1046         digest_dynamic(&obj_rtld);
1047         assert(obj_rtld.needed == NULL);
1048         assert(!obj_rtld.textrel);
1049
1050         /*
1051          * Temporarily put the dynamic linker entry into the object list, so
1052          * that symbols can be found.
1053          */
1054         obj_list = &obj_rtld;
1055         obj_tail = &obj_rtld.next;
1056         obj_count = 1;
1057
1058         relocate_objects(&obj_rtld, true);
1059     }
1060
1061     /* Make the object list empty again. */
1062     obj_list = NULL;
1063     obj_tail = &obj_list;
1064     obj_count = 0;
1065
1066     /* Replace the path with a dynamically allocated copy. */
1067     obj_rtld.path = xstrdup(obj_rtld.path);
1068
1069     r_debug.r_brk = r_debug_state;
1070     r_debug.r_state = RT_CONSISTENT;
1071 }
1072
1073 /*
1074  * Add the init functions from a needed object list (and its recursive
1075  * needed objects) to "list".  This is not used directly; it is a helper
1076  * function for initlist_add_objects().  The write lock must be held
1077  * when this function is called.
1078  */
1079 static void
1080 initlist_add_neededs(Needed_Entry *needed, Objlist *list)
1081 {
1082     /* Recursively process the successor needed objects. */
1083     if (needed->next != NULL)
1084         initlist_add_neededs(needed->next, list);
1085
1086     /* Process the current needed object. */
1087     if (needed->obj != NULL)
1088         initlist_add_objects(needed->obj, &needed->obj->next, list);
1089 }
1090
1091 /*
1092  * Scan all of the DAGs rooted in the range of objects from "obj" to
1093  * "tail" and add their init functions to "list".  This recurses over
1094  * the DAGs and ensure the proper init ordering such that each object's
1095  * needed libraries are initialized before the object itself.  At the
1096  * same time, this function adds the objects to the global finalization
1097  * list "list_fini" in the opposite order.  The write lock must be
1098  * held when this function is called.
1099  */
1100 static void
1101 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list)
1102 {
1103     if (obj->init_done)
1104         return;
1105     obj->init_done = true;
1106
1107     /* Recursively process the successor objects. */
1108     if (&obj->next != tail)
1109         initlist_add_objects(obj->next, tail, list);
1110
1111     /* Recursively process the needed objects. */
1112     if (obj->needed != NULL)
1113         initlist_add_neededs(obj->needed, list);
1114
1115     /* Add the object to the init list. */
1116     if (obj->init != NULL)
1117         objlist_push_tail(list, obj);
1118
1119     /* Add the object to the global fini list in the reverse order. */
1120     if (obj->fini != NULL)
1121         objlist_push_head(&list_fini, obj);
1122 }
1123
1124 static bool
1125 is_exported(const Elf_Sym *def)
1126 {
1127     func_ptr_type value;
1128     const func_ptr_type *p;
1129
1130     value = (func_ptr_type)(obj_rtld.relocbase + def->st_value);
1131     for (p = exports;  *p != NULL;  p++)
1132         if (*p == value)
1133             return true;
1134     return false;
1135 }
1136
1137 /*
1138  * Given a shared object, traverse its list of needed objects, and load
1139  * each of them.  Returns 0 on success.  Generates an error message and
1140  * returns -1 on failure.
1141  */
1142 static int
1143 load_needed_objects(Obj_Entry *first)
1144 {
1145     Obj_Entry *obj;
1146
1147     for (obj = first;  obj != NULL;  obj = obj->next) {
1148         Needed_Entry *needed;
1149
1150         for (needed = obj->needed;  needed != NULL;  needed = needed->next) {
1151             const char *name = obj->strtab + needed->name;
1152             char *path = find_library(name, obj);
1153
1154             needed->obj = NULL;
1155             if (path == NULL && !ld_tracing)
1156                 return -1;
1157
1158             if (path) {
1159                 needed->obj = load_object(path);
1160                 if (needed->obj == NULL && !ld_tracing)
1161                     return -1;          /* XXX - cleanup */
1162             }
1163         }
1164     }
1165
1166     return 0;
1167 }
1168
1169 static int
1170 load_preload_objects(void)
1171 {
1172     char *p = ld_preload;
1173     static const char delim[] = " \t:;";
1174
1175     if (p == NULL)
1176         return NULL;
1177
1178     p += strspn(p, delim);
1179     while (*p != '\0') {
1180         size_t len = strcspn(p, delim);
1181         char *path;
1182         char savech;
1183
1184         savech = p[len];
1185         p[len] = '\0';
1186         if ((path = find_library(p, NULL)) == NULL)
1187             return -1;
1188         if (load_object(path) == NULL)
1189             return -1;  /* XXX - cleanup */
1190         p[len] = savech;
1191         p += len;
1192         p += strspn(p, delim);
1193     }
1194     return 0;
1195 }
1196
1197 /*
1198  * Returns a point to the Obj_Entry for the object with the given path.
1199  * Returns NULL if no matching object was found.
1200  */
1201 static Obj_Entry *
1202 find_object(const char *path)
1203 {
1204     Obj_Entry *obj;
1205
1206     for (obj = obj_list->next;  obj != NULL;  obj = obj->next) {
1207         if (strcmp(obj->path, path) == 0)
1208             return(obj);
1209     }
1210     return(NULL);
1211 }
1212
1213 /*
1214  * Load a shared object into memory, if it is not already loaded.  The
1215  * argument must be a string allocated on the heap.  This function assumes
1216  * responsibility for freeing it when necessary.
1217  *
1218  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
1219  * on failure.
1220  */
1221 static Obj_Entry *
1222 load_object(char *path)
1223 {
1224     Obj_Entry *obj;
1225     int fd = -1;
1226     struct stat sb;
1227
1228     obj = find_object(path);
1229
1230     /*
1231      * If we didn't find a match by pathname, open the file and check
1232      * again by device and inode.  This avoids false mismatches caused
1233      * by multiple links or ".." in pathnames.
1234      *
1235      * To avoid a race, we open the file and use fstat() rather than
1236      * using stat().
1237      */
1238     if (obj == NULL) {
1239         if ((fd = open(path, O_RDONLY)) == -1) {
1240             _rtld_error("Cannot open \"%s\"", path);
1241             return NULL;
1242         }
1243         if (fstat(fd, &sb) == -1) {
1244             _rtld_error("Cannot fstat \"%s\"", path);
1245             close(fd);
1246             return NULL;
1247         }
1248         for (obj = obj_list->next;  obj != NULL;  obj = obj->next) {
1249             if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
1250                 close(fd);
1251                 break;
1252             }
1253         }
1254     }
1255
1256     if (obj == NULL) {  /* First use of this object, so we must map it in */
1257         dbg("loading \"%s\"", path);
1258         obj = map_object(fd, path, &sb);
1259         close(fd);
1260         if (obj == NULL) {
1261             free(path);
1262             return NULL;
1263         }
1264
1265         obj->path = path;
1266         digest_dynamic(obj);
1267
1268         *obj_tail = obj;
1269         obj_tail = &obj->next;
1270         obj_count++;
1271         linkmap_add(obj);       /* for GDB & dlinfo() */
1272
1273         dbg("  %p .. %p: %s", obj->mapbase,
1274           obj->mapbase + obj->mapsize - 1, obj->path);
1275         if (obj->textrel)
1276             dbg("  WARNING: %s has impure text", obj->path);
1277     } else
1278         free(path);
1279
1280     obj->refcount++;
1281     return obj;
1282 }
1283
1284 /*
1285  * Check for locking violations and die if one is found.
1286  */
1287 static void
1288 lock_check(void)
1289 {
1290     int rcount, wcount;
1291
1292     rcount = lockinfo.rcount;
1293     wcount = lockinfo.wcount;
1294     assert(rcount >= 0);
1295     assert(wcount >= 0);
1296     if (wcount > 1 || (wcount != 0 && rcount != 0)) {
1297         _rtld_error("Application locking error: %d readers and %d writers"
1298           " in dynamic linker.  See DLLOCKINIT(3) in manual pages.",
1299           rcount, wcount);
1300         die();
1301     }
1302 }
1303
1304 static Obj_Entry *
1305 obj_from_addr(const void *addr)
1306 {
1307     Obj_Entry *obj;
1308
1309     for (obj = obj_list;  obj != NULL;  obj = obj->next) {
1310         if (addr < (void *) obj->mapbase)
1311             continue;
1312         if (addr < (void *) (obj->mapbase + obj->mapsize))
1313             return obj;
1314     }
1315     return NULL;
1316 }
1317
1318 /*
1319  * Call the finalization functions for each of the objects in "list"
1320  * which are unreferenced.  All of the objects are expected to have
1321  * non-NULL fini functions.
1322  */
1323 static void
1324 objlist_call_fini(Objlist *list)
1325 {
1326     Objlist_Entry *elm;
1327     char *saved_msg;
1328
1329     /*
1330      * Preserve the current error message since a fini function might
1331      * call into the dynamic linker and overwrite it.
1332      */
1333     saved_msg = errmsg_save();
1334     STAILQ_FOREACH(elm, list, link) {
1335         if (elm->obj->refcount == 0) {
1336             dbg("calling fini function for %s", elm->obj->path);
1337             (*elm->obj->fini)();
1338         }
1339     }
1340     errmsg_restore(saved_msg);
1341 }
1342
1343 /*
1344  * Call the initialization functions for each of the objects in
1345  * "list".  All of the objects are expected to have non-NULL init
1346  * functions.
1347  */
1348 static void
1349 objlist_call_init(Objlist *list)
1350 {
1351     Objlist_Entry *elm;
1352     char *saved_msg;
1353
1354     /*
1355      * Preserve the current error message since an init function might
1356      * call into the dynamic linker and overwrite it.
1357      */
1358     saved_msg = errmsg_save();
1359     STAILQ_FOREACH(elm, list, link) {
1360         dbg("calling init function for %s", elm->obj->path);
1361         (*elm->obj->init)();
1362     }
1363     errmsg_restore(saved_msg);
1364 }
1365
1366 static void
1367 objlist_clear(Objlist *list)
1368 {
1369     Objlist_Entry *elm;
1370
1371     while (!STAILQ_EMPTY(list)) {
1372         elm = STAILQ_FIRST(list);
1373         STAILQ_REMOVE_HEAD(list, link);
1374         free(elm);
1375     }
1376 }
1377
1378 static Objlist_Entry *
1379 objlist_find(Objlist *list, const Obj_Entry *obj)
1380 {
1381     Objlist_Entry *elm;
1382
1383     STAILQ_FOREACH(elm, list, link)
1384         if (elm->obj == obj)
1385             return elm;
1386     return NULL;
1387 }
1388
1389 static void
1390 objlist_init(Objlist *list)
1391 {
1392     STAILQ_INIT(list);
1393 }
1394
1395 static void
1396 objlist_push_head(Objlist *list, Obj_Entry *obj)
1397 {
1398     Objlist_Entry *elm;
1399
1400     elm = NEW(Objlist_Entry);
1401     elm->obj = obj;
1402     STAILQ_INSERT_HEAD(list, elm, link);
1403 }
1404
1405 static void
1406 objlist_push_tail(Objlist *list, Obj_Entry *obj)
1407 {
1408     Objlist_Entry *elm;
1409
1410     elm = NEW(Objlist_Entry);
1411     elm->obj = obj;
1412     STAILQ_INSERT_TAIL(list, elm, link);
1413 }
1414
1415 static void
1416 objlist_remove(Objlist *list, Obj_Entry *obj)
1417 {
1418     Objlist_Entry *elm;
1419
1420     if ((elm = objlist_find(list, obj)) != NULL) {
1421         STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1422         free(elm);
1423     }
1424 }
1425
1426 /*
1427  * Remove all of the unreferenced objects from "list".
1428  */
1429 static void
1430 objlist_remove_unref(Objlist *list)
1431 {
1432     Objlist newlist;
1433     Objlist_Entry *elm;
1434
1435     STAILQ_INIT(&newlist);
1436     while (!STAILQ_EMPTY(list)) {
1437         elm = STAILQ_FIRST(list);
1438         STAILQ_REMOVE_HEAD(list, link);
1439         if (elm->obj->refcount == 0)
1440             free(elm);
1441         else
1442             STAILQ_INSERT_TAIL(&newlist, elm, link);
1443     }
1444     *list = newlist;
1445 }
1446
1447 /*
1448  * Relocate newly-loaded shared objects.  The argument is a pointer to
1449  * the Obj_Entry for the first such object.  All objects from the first
1450  * to the end of the list of objects are relocated.  Returns 0 on success,
1451  * or -1 on failure.
1452  */
1453 static int
1454 relocate_objects(Obj_Entry *first, bool bind_now)
1455 {
1456     Obj_Entry *obj;
1457
1458     for (obj = first;  obj != NULL;  obj = obj->next) {
1459         if (obj != &obj_rtld)
1460             dbg("relocating \"%s\"", obj->path);
1461         if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL ||
1462             obj->symtab == NULL || obj->strtab == NULL) {
1463             _rtld_error("%s: Shared object has no run-time symbol table",
1464               obj->path);
1465             return -1;
1466         }
1467
1468         if (obj->textrel) {
1469             /* There are relocations to the write-protected text segment. */
1470             if (mprotect(obj->mapbase, obj->textsize,
1471               PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
1472                 _rtld_error("%s: Cannot write-enable text segment: %s",
1473                   obj->path, strerror(errno));
1474                 return -1;
1475             }
1476         }
1477
1478         /* Process the non-PLT relocations. */
1479         if (reloc_non_plt(obj, &obj_rtld))
1480                 return -1;
1481
1482         /*
1483          * Reprotect the text segment.  Make sure it is included in the
1484          * core dump since we modified it.  This unfortunately causes the
1485          * entire text segment to core-out but we don't have much of a
1486          * choice.  We could try to only reenable core dumps on pages
1487          * in which relocations occured but that is likely most of the text
1488          * pages anyway, and even that would not work because the rest of
1489          * the text pages would wind up as a read-only OBJT_DEFAULT object
1490          * (created due to our modifications) backed by the original OBJT_VNODE
1491          * object, and the ELF coredump code is currently only able to dump
1492          * vnode records for pure vnode-backed mappings, not vnode backings
1493          * to memory objects.
1494          */
1495         if (obj->textrel) {
1496             madvise(obj->mapbase, obj->textsize, MADV_CORE);
1497             if (mprotect(obj->mapbase, obj->textsize,
1498               PROT_READ|PROT_EXEC) == -1) {
1499                 _rtld_error("%s: Cannot write-protect text segment: %s",
1500                   obj->path, strerror(errno));
1501                 return -1;
1502             }
1503         }
1504
1505         /* Process the PLT relocations. */
1506         if (reloc_plt(obj) == -1)
1507             return -1;
1508         /* Relocate the jump slots if we are doing immediate binding. */
1509         if (obj->bind_now || bind_now)
1510             if (reloc_jmpslots(obj) == -1)
1511                 return -1;
1512
1513
1514         /*
1515          * Set up the magic number and version in the Obj_Entry.  These
1516          * were checked in the crt1.o from the original ElfKit, so we
1517          * set them for backward compatibility.
1518          */
1519         obj->magic = RTLD_MAGIC;
1520         obj->version = RTLD_VERSION;
1521
1522         /* Set the special PLT or GOT entries. */
1523         init_pltgot(obj);
1524     }
1525
1526     return 0;
1527 }
1528
1529 /*
1530  * Cleanup procedure.  It will be called (by the atexit mechanism) just
1531  * before the process exits.
1532  */
1533 static void
1534 rtld_exit(void)
1535 {
1536     Obj_Entry *obj;
1537
1538     dbg("rtld_exit()");
1539     /* Clear all the reference counts so the fini functions will be called. */
1540     for (obj = obj_list;  obj != NULL;  obj = obj->next)
1541         obj->refcount = 0;
1542     objlist_call_fini(&list_fini);
1543     /* No need to remove the items from the list, since we are exiting. */
1544 }
1545
1546 static void *
1547 path_enumerate(const char *path, path_enum_proc callback, void *arg)
1548 {
1549     if (path == NULL)
1550         return (NULL);
1551
1552     path += strspn(path, ":;");
1553     while (*path != '\0') {
1554         size_t len;
1555         char  *res;
1556
1557         len = strcspn(path, ":;");
1558         res = callback(path, len, arg);
1559
1560         if (res != NULL)
1561             return (res);
1562
1563         path += len;
1564         path += strspn(path, ":;");
1565     }
1566
1567     return (NULL);
1568 }
1569
1570 struct try_library_args {
1571     const char  *name;
1572     size_t       namelen;
1573     char        *buffer;
1574     size_t       buflen;
1575 };
1576
1577 static void *
1578 try_library_path(const char *dir, size_t dirlen, void *param)
1579 {
1580     struct try_library_args *arg;
1581
1582     arg = param;
1583     if (*dir == '/' || trust) {
1584         char *pathname;
1585
1586         if (dirlen + 1 + arg->namelen + 1 > arg->buflen)
1587                 return (NULL);
1588
1589         pathname = arg->buffer;
1590         strncpy(pathname, dir, dirlen);
1591         pathname[dirlen] = '/';
1592         strcpy(pathname + dirlen + 1, arg->name);
1593
1594         dbg("  Trying \"%s\"", pathname);
1595         if (access(pathname, F_OK) == 0) {              /* We found it */
1596             pathname = xmalloc(dirlen + 1 + arg->namelen + 1);
1597             strcpy(pathname, arg->buffer);
1598             return (pathname);
1599         }
1600     }
1601     return (NULL);
1602 }
1603
1604 static char *
1605 search_library_path(const char *name, const char *path)
1606 {
1607     char *p;
1608     struct try_library_args arg;
1609
1610     if (path == NULL)
1611         return NULL;
1612
1613     arg.name = name;
1614     arg.namelen = strlen(name);
1615     arg.buffer = xmalloc(PATH_MAX);
1616     arg.buflen = PATH_MAX;
1617
1618     p = path_enumerate(path, try_library_path, &arg);
1619
1620     free(arg.buffer);
1621
1622     return (p);
1623 }
1624
1625 int
1626 dlclose(void *handle)
1627 {
1628     Obj_Entry *root;
1629
1630     wlock_acquire();
1631     root = dlcheck(handle);
1632     if (root == NULL) {
1633         wlock_release();
1634         return -1;
1635     }
1636
1637     /* Unreference the object and its dependencies. */
1638     root->dl_refcount--;
1639     unref_dag(root);
1640
1641     if (root->refcount == 0) {
1642         /*
1643          * The object is no longer referenced, so we must unload it.
1644          * First, call the fini functions with no locks held.
1645          */
1646         wlock_release();
1647         objlist_call_fini(&list_fini);
1648         wlock_acquire();
1649         objlist_remove_unref(&list_fini);
1650
1651         /* Finish cleaning up the newly-unreferenced objects. */
1652         GDB_STATE(RT_DELETE,&root->linkmap);
1653         unload_object(root);
1654         GDB_STATE(RT_CONSISTENT,NULL);
1655     }
1656     wlock_release();
1657     return 0;
1658 }
1659
1660 const char *
1661 dlerror(void)
1662 {
1663     char *msg = error_message;
1664     error_message = NULL;
1665     return msg;
1666 }
1667
1668 /*
1669  * This function is deprecated and has no effect.
1670  */
1671 void
1672 dllockinit(void *context,
1673            void *(*lock_create)(void *context),
1674            void (*rlock_acquire)(void *lock),
1675            void (*wlock_acquire)(void *lock),
1676            void (*lock_release)(void *lock),
1677            void (*lock_destroy)(void *lock),
1678            void (*context_destroy)(void *context))
1679 {
1680     static void *cur_context;
1681     static void (*cur_context_destroy)(void *);
1682
1683     /* Just destroy the context from the previous call, if necessary. */
1684     if (cur_context_destroy != NULL)
1685         cur_context_destroy(cur_context);
1686     cur_context = context;
1687     cur_context_destroy = context_destroy;
1688 }
1689
1690 void *
1691 dlopen(const char *name, int mode)
1692 {
1693     Obj_Entry **old_obj_tail;
1694     Obj_Entry *obj;
1695     Objlist initlist;
1696     int result;
1697
1698     ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1";
1699     if (ld_tracing != NULL)
1700         environ = (char **)*get_program_var_addr("environ");
1701
1702     objlist_init(&initlist);
1703
1704     wlock_acquire();
1705     GDB_STATE(RT_ADD,NULL);
1706
1707     old_obj_tail = obj_tail;
1708     obj = NULL;
1709     if (name == NULL) {
1710         obj = obj_main;
1711         obj->refcount++;
1712     } else {
1713         char *path = find_library(name, obj_main);
1714         if (path != NULL)
1715             obj = load_object(path);
1716     }
1717
1718     if (obj) {
1719         obj->dl_refcount++;
1720         if ((mode & RTLD_GLOBAL) && objlist_find(&list_global, obj) == NULL)
1721             objlist_push_tail(&list_global, obj);
1722         mode &= RTLD_MODEMASK;
1723         if (*old_obj_tail != NULL) {            /* We loaded something new. */
1724             assert(*old_obj_tail == obj);
1725
1726             result = load_needed_objects(obj);
1727             if (result != -1 && ld_tracing)
1728                 goto trace;
1729
1730             if (result == -1 ||
1731               (init_dag(obj), relocate_objects(obj, mode == RTLD_NOW)) == -1) {
1732                 obj->dl_refcount--;
1733                 unref_dag(obj);
1734                 if (obj->refcount == 0)
1735                     unload_object(obj);
1736                 obj = NULL;
1737             } else {
1738                 /* Make list of init functions to call. */
1739                 initlist_add_objects(obj, &obj->next, &initlist);
1740             }
1741         } else if (ld_tracing)
1742             goto trace;
1743     }
1744
1745     GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL);
1746
1747     /* Call the init functions with no locks held. */
1748     wlock_release();
1749     objlist_call_init(&initlist);
1750     wlock_acquire();
1751     objlist_clear(&initlist);
1752     wlock_release();
1753     return obj;
1754 trace:
1755     trace_loaded_objects(obj);
1756     wlock_release();
1757     exit(0);
1758 }
1759
1760 void *
1761 dlsym(void *handle, const char *name)
1762 {
1763     const Obj_Entry *obj;
1764     unsigned long hash;
1765     const Elf_Sym *def;
1766     const Obj_Entry *defobj;
1767
1768     hash = elf_hash(name);
1769     def = NULL;
1770     defobj = NULL;
1771
1772     rlock_acquire();
1773     if (handle == NULL || handle == RTLD_NEXT ||
1774         handle == RTLD_DEFAULT || handle == RTLD_SELF) {
1775         void *retaddr;
1776
1777         retaddr = __builtin_return_address(0);  /* __GNUC__ only */
1778         if ((obj = obj_from_addr(retaddr)) == NULL) {
1779             _rtld_error("Cannot determine caller's shared object");
1780             rlock_release();
1781             return NULL;
1782         }
1783         if (handle == NULL) {   /* Just the caller's shared object. */
1784             def = symlook_obj(name, hash, obj, true);
1785             defobj = obj;
1786         } else if (handle == RTLD_NEXT || /* Objects after caller's */
1787                    handle == RTLD_SELF) { /* ... caller included */
1788             if (handle == RTLD_NEXT)
1789                 obj = obj->next;
1790             for (; obj != NULL; obj = obj->next) {
1791                 if ((def = symlook_obj(name, hash, obj, true)) != NULL) {
1792                     defobj = obj;
1793                     break;
1794                 }
1795             }
1796         } else {
1797             assert(handle == RTLD_DEFAULT);
1798             def = symlook_default(name, hash, obj, &defobj, true);
1799         }
1800     } else {
1801         if ((obj = dlcheck(handle)) == NULL) {
1802             rlock_release();
1803             return NULL;
1804         }
1805
1806         if (obj->mainprog) {
1807             DoneList donelist;
1808
1809             /* Search main program and all libraries loaded by it. */
1810             donelist_init(&donelist);
1811             def = symlook_list(name, hash, &list_main, &defobj, true,
1812               &donelist);
1813         } else {
1814             /*
1815              * XXX - This isn't correct.  The search should include the whole
1816              * DAG rooted at the given object.
1817              */
1818             def = symlook_obj(name, hash, obj, true);
1819             defobj = obj;
1820         }
1821     }
1822
1823     if (def != NULL) {
1824         rlock_release();
1825         return defobj->relocbase + def->st_value;
1826     }
1827
1828     _rtld_error("Undefined symbol \"%s\"", name);
1829     rlock_release();
1830     return NULL;
1831 }
1832
1833 int
1834 dladdr(const void *addr, Dl_info *info)
1835 {
1836     const Obj_Entry *obj;
1837     const Elf_Sym *def;
1838     void *symbol_addr;
1839     unsigned long symoffset;
1840  
1841     rlock_acquire();
1842     obj = obj_from_addr(addr);
1843     if (obj == NULL) {
1844         _rtld_error("No shared object contains address");
1845         rlock_release();
1846         return 0;
1847     }
1848     info->dli_fname = obj->path;
1849     info->dli_fbase = obj->mapbase;
1850     info->dli_saddr = (void *)0;
1851     info->dli_sname = NULL;
1852
1853     /*
1854      * Walk the symbol list looking for the symbol whose address is
1855      * closest to the address sent in.
1856      */
1857     for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1858         def = obj->symtab + symoffset;
1859
1860         /*
1861          * For skip the symbol if st_shndx is either SHN_UNDEF or
1862          * SHN_COMMON.
1863          */
1864         if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1865             continue;
1866
1867         /*
1868          * If the symbol is greater than the specified address, or if it
1869          * is further away from addr than the current nearest symbol,
1870          * then reject it.
1871          */
1872         symbol_addr = obj->relocbase + def->st_value;
1873         if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1874             continue;
1875
1876         /* Update our idea of the nearest symbol. */
1877         info->dli_sname = obj->strtab + def->st_name;
1878         info->dli_saddr = symbol_addr;
1879
1880         /* Exact match? */
1881         if (info->dli_saddr == addr)
1882             break;
1883     }
1884     rlock_release();
1885     return 1;
1886 }
1887
1888 int
1889 dlinfo(void *handle, int request, void *p)
1890 {
1891     const Obj_Entry *obj;
1892     int error;
1893
1894     rlock_acquire();
1895
1896     if (handle == NULL || handle == RTLD_SELF) {
1897         void *retaddr;
1898
1899         retaddr = __builtin_return_address(0);  /* __GNUC__ only */
1900         if ((obj = obj_from_addr(retaddr)) == NULL)
1901             _rtld_error("Cannot determine caller's shared object");
1902     } else
1903         obj = dlcheck(handle);
1904
1905     if (obj == NULL) {
1906         rlock_release();
1907         return (-1);
1908     }
1909
1910     error = 0;
1911     switch (request) {
1912     case RTLD_DI_LINKMAP:
1913         *((struct link_map const **)p) = &obj->linkmap;
1914         break;
1915     case RTLD_DI_ORIGIN:
1916         error = rtld_dirname(obj->path, p);
1917         break;
1918
1919     case RTLD_DI_SERINFOSIZE:
1920     case RTLD_DI_SERINFO:
1921         error = do_search_info(obj, request, (struct dl_serinfo *)p);
1922         break;
1923
1924     default:
1925         _rtld_error("Invalid request %d passed to dlinfo()", request);
1926         error = -1;
1927     }
1928
1929     rlock_release();
1930
1931     return (error);
1932 }
1933
1934 struct fill_search_info_args {
1935     int          request;
1936     unsigned int flags;
1937     Dl_serinfo  *serinfo;
1938     Dl_serpath  *serpath;
1939     char        *strspace;
1940 };
1941
1942 static void *
1943 fill_search_info(const char *dir, size_t dirlen, void *param)
1944 {
1945     struct fill_search_info_args *arg;
1946
1947     arg = param;
1948
1949     if (arg->request == RTLD_DI_SERINFOSIZE) {
1950         arg->serinfo->dls_cnt ++;
1951         arg->serinfo->dls_size += dirlen + 1;
1952     } else {
1953         struct dl_serpath *s_entry;
1954
1955         s_entry = arg->serpath;
1956         s_entry->dls_name  = arg->strspace;
1957         s_entry->dls_flags = arg->flags;
1958
1959         strncpy(arg->strspace, dir, dirlen);
1960         arg->strspace[dirlen] = '\0';
1961
1962         arg->strspace += dirlen + 1;
1963         arg->serpath++;
1964     }
1965
1966     return (NULL);
1967 }
1968
1969 static int
1970 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
1971 {
1972     struct dl_serinfo _info;
1973     struct fill_search_info_args args;
1974
1975     args.request = RTLD_DI_SERINFOSIZE;
1976     args.serinfo = &_info;
1977
1978     _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
1979     _info.dls_cnt  = 0;
1980
1981     path_enumerate(ld_library_path, fill_search_info, &args);
1982     path_enumerate(obj->rpath, fill_search_info, &args);
1983     path_enumerate(gethints(), fill_search_info, &args);
1984     path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args);
1985
1986
1987     if (request == RTLD_DI_SERINFOSIZE) {
1988         info->dls_size = _info.dls_size;
1989         info->dls_cnt = _info.dls_cnt;
1990         return (0);
1991     }
1992
1993     if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
1994         _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
1995         return (-1);
1996     }
1997
1998     args.request  = RTLD_DI_SERINFO;
1999     args.serinfo  = info;
2000     args.serpath  = &info->dls_serpath[0];
2001     args.strspace = (char *)&info->dls_serpath[_info.dls_cnt];
2002
2003     args.flags = LA_SER_LIBPATH;
2004     if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
2005         return (-1);
2006
2007     args.flags = LA_SER_RUNPATH;
2008     if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
2009         return (-1);
2010
2011     args.flags = LA_SER_CONFIG;
2012     if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
2013         return (-1);
2014
2015     args.flags = LA_SER_DEFAULT;
2016     if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
2017         return (-1);
2018     return (0);
2019 }
2020
2021 static int
2022 rtld_dirname(const char *path, char *bname)
2023 {
2024     const char *endp;
2025
2026     /* Empty or NULL string gets treated as "." */
2027     if (path == NULL || *path == '\0') {
2028         bname[0] = '.';
2029         bname[1] = '\0';
2030         return (0);
2031     }
2032
2033     /* Strip trailing slashes */
2034     endp = path + strlen(path) - 1;
2035     while (endp > path && *endp == '/')
2036         endp--;
2037
2038     /* Find the start of the dir */
2039     while (endp > path && *endp != '/')
2040         endp--;
2041
2042     /* Either the dir is "/" or there are no slashes */
2043     if (endp == path) {
2044         bname[0] = *endp == '/' ? '/' : '.';
2045         bname[1] = '\0';
2046         return (0);
2047     } else {
2048         do {
2049             endp--;
2050         } while (endp > path && *endp == '/');
2051     }
2052
2053     if (endp - path + 2 > PATH_MAX)
2054     {
2055         _rtld_error("Filename is too long: %s", path);
2056         return(-1);
2057     }
2058
2059     strncpy(bname, path, endp - path + 1);
2060     bname[endp - path + 1] = '\0';
2061     return (0);
2062 }
2063
2064 static void
2065 linkmap_add(Obj_Entry *obj)
2066 {
2067     struct link_map *l = &obj->linkmap;
2068     struct link_map *prev;
2069
2070     obj->linkmap.l_name = obj->path;
2071     obj->linkmap.l_addr = obj->mapbase;
2072     obj->linkmap.l_ld = obj->dynamic;
2073 #ifdef __mips__
2074     /* GDB needs load offset on MIPS to use the symbols */
2075     obj->linkmap.l_offs = obj->relocbase;
2076 #endif
2077
2078     if (r_debug.r_map == NULL) {
2079         r_debug.r_map = l;
2080         return;
2081     }
2082
2083     /*
2084      * Scan to the end of the list, but not past the entry for the
2085      * dynamic linker, which we want to keep at the very end.
2086      */
2087     for (prev = r_debug.r_map;
2088       prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
2089       prev = prev->l_next)
2090         ;
2091
2092     /* Link in the new entry. */
2093     l->l_prev = prev;
2094     l->l_next = prev->l_next;
2095     if (l->l_next != NULL)
2096         l->l_next->l_prev = l;
2097     prev->l_next = l;
2098 }
2099
2100 static void
2101 linkmap_delete(Obj_Entry *obj)
2102 {
2103     struct link_map *l = &obj->linkmap;
2104
2105     if (l->l_prev == NULL) {
2106         if ((r_debug.r_map = l->l_next) != NULL)
2107             l->l_next->l_prev = NULL;
2108         return;
2109     }
2110
2111     if ((l->l_prev->l_next = l->l_next) != NULL)
2112         l->l_next->l_prev = l->l_prev;
2113 }
2114
2115 /*
2116  * Function for the debugger to set a breakpoint on to gain control.
2117  *
2118  * The two parameters allow the debugger to easily find and determine
2119  * what the runtime loader is doing and to whom it is doing it.
2120  *
2121  * When the loadhook trap is hit (r_debug_state, set at program
2122  * initialization), the arguments can be found on the stack:
2123  *
2124  *  +8   struct link_map *m
2125  *  +4   struct r_debug  *rd
2126  *  +0   RetAddr
2127  */
2128 void
2129 r_debug_state(struct r_debug* rd, struct link_map *m)
2130 {
2131 }
2132
2133 /*
2134  * Get address of the pointer variable in the main program.
2135  */
2136 static const void **
2137 get_program_var_addr(const char *name)
2138 {
2139     const Obj_Entry *obj;
2140     unsigned long hash;
2141
2142     hash = elf_hash(name);
2143     for (obj = obj_main;  obj != NULL;  obj = obj->next) {
2144         const Elf_Sym *def;
2145
2146         if ((def = symlook_obj(name, hash, obj, false)) != NULL) {
2147             const void **addr;
2148
2149             addr = (const void **)(obj->relocbase + def->st_value);
2150             return addr;
2151         }
2152     }
2153     return NULL;
2154 }
2155
2156 /*
2157  * Set a pointer variable in the main program to the given value.  This
2158  * is used to set key variables such as "environ" before any of the
2159  * init functions are called.
2160  */
2161 static void
2162 set_program_var(const char *name, const void *value)
2163 {
2164     const void **addr;
2165
2166     if ((addr = get_program_var_addr(name)) != NULL) {
2167         dbg("\"%s\": *%p <-- %p", name, addr, value);
2168         *addr = value;
2169     }
2170 }
2171
2172 /*
2173  * This is a special version of getenv which is far more efficient
2174  * at finding LD_ environment vars.
2175  */
2176 static
2177 const char *
2178 _getenv_ld(const char *id)
2179 {
2180     const char *envp;
2181     int i, j;
2182     int idlen = strlen(id);
2183
2184     if (ld_index == LD_ARY_CACHE)
2185         return(getenv(id));
2186     if (ld_index == 0) {
2187         for (i = j = 0; (envp = environ[i]) != NULL && j < LD_ARY_CACHE; ++i) {
2188             if (envp[0] == 'L' && envp[1] == 'D' && envp[2] == '_')
2189                 ld_ary[j++] = envp;
2190         }
2191         if (j == 0)
2192                 ld_ary[j++] = "";
2193         ld_index = j;
2194     }
2195     for (i = ld_index - 1; i >= 0; --i) {
2196         if (strncmp(ld_ary[i], id, idlen) == 0 && ld_ary[i][idlen] == '=')
2197             return(ld_ary[i] + idlen + 1);
2198     }
2199     return(NULL);
2200 }
2201
2202 /*
2203  * Given a symbol name in a referencing object, find the corresponding
2204  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
2205  * no definition was found.  Returns a pointer to the Obj_Entry of the
2206  * defining object via the reference parameter DEFOBJ_OUT.
2207  */
2208 static const Elf_Sym *
2209 symlook_default(const char *name, unsigned long hash,
2210     const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
2211 {
2212     DoneList donelist;
2213     const Elf_Sym *def;
2214     const Elf_Sym *symp;
2215     const Obj_Entry *obj;
2216     const Obj_Entry *defobj;
2217     const Objlist_Entry *elm;
2218     def = NULL;
2219     defobj = NULL;
2220     donelist_init(&donelist);
2221
2222     /* Look first in the referencing object if linked symbolically. */
2223     if (refobj->symbolic && !donelist_check(&donelist, refobj)) {
2224         symp = symlook_obj(name, hash, refobj, in_plt);
2225         if (symp != NULL) {
2226             def = symp;
2227             defobj = refobj;
2228         }
2229     }
2230
2231     /* Search all objects loaded at program start up. */
2232     if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2233         symp = symlook_list(name, hash, &list_main, &obj, in_plt, &donelist);
2234         if (symp != NULL &&
2235           (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2236             def = symp;
2237             defobj = obj;
2238         }
2239     }
2240
2241     /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2242     STAILQ_FOREACH(elm, &list_global, link) {
2243        if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2244            break;
2245        symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2246          &donelist);
2247         if (symp != NULL &&
2248           (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2249             def = symp;
2250             defobj = obj;
2251         }
2252     }
2253
2254     /* Search all dlopened DAGs containing the referencing object. */
2255     STAILQ_FOREACH(elm, &refobj->dldags, link) {
2256         if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2257             break;
2258         symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2259           &donelist);
2260         if (symp != NULL &&
2261           (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2262             def = symp;
2263             defobj = obj;
2264         }
2265     }
2266
2267     /*
2268      * Search the dynamic linker itself, and possibly resolve the
2269      * symbol from there.  This is how the application links to
2270      * dynamic linker services such as dlopen.  Only the values listed
2271      * in the "exports" array can be resolved from the dynamic linker.
2272      */
2273     if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2274         symp = symlook_obj(name, hash, &obj_rtld, in_plt);
2275         if (symp != NULL && is_exported(symp)) {
2276             def = symp;
2277             defobj = &obj_rtld;
2278         }
2279     }
2280
2281     if (def != NULL)
2282         *defobj_out = defobj;
2283     return def;
2284 }
2285
2286 static const Elf_Sym *
2287 symlook_list(const char *name, unsigned long hash, Objlist *objlist,
2288   const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
2289 {
2290     const Elf_Sym *symp;
2291     const Elf_Sym *def;
2292     const Obj_Entry *defobj;
2293     const Objlist_Entry *elm;
2294
2295     def = NULL;
2296     defobj = NULL;
2297     STAILQ_FOREACH(elm, objlist, link) {
2298         if (donelist_check(dlp, elm->obj))
2299             continue;
2300         if ((symp = symlook_obj(name, hash, elm->obj, in_plt)) != NULL) {
2301             if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
2302                 def = symp;
2303                 defobj = elm->obj;
2304                 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2305                     break;
2306             }
2307         }
2308     }
2309     if (def != NULL)
2310         *defobj_out = defobj;
2311     return def;
2312 }
2313
2314 /*
2315  * Search the symbol table of a single shared object for a symbol of
2316  * the given name.  Returns a pointer to the symbol, or NULL if no
2317  * definition was found.
2318  *
2319  * The symbol's hash value is passed in for efficiency reasons; that
2320  * eliminates many recomputations of the hash value.
2321  */
2322 const Elf_Sym *
2323 symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
2324   bool in_plt)
2325 {
2326     if (obj->buckets != NULL) {
2327         unsigned long symnum = obj->buckets[hash % obj->nbuckets];
2328
2329         while (symnum != STN_UNDEF) {
2330             const Elf_Sym *symp;
2331             const char *strp;
2332
2333             if (symnum >= obj->nchains)
2334                 return NULL;    /* Bad object */
2335             symp = obj->symtab + symnum;
2336             strp = obj->strtab + symp->st_name;
2337
2338             if (name[0] == strp[0] && strcmp(name, strp) == 0)
2339                 return symp->st_shndx != SHN_UNDEF ||
2340                   (!in_plt && symp->st_value != 0 &&
2341                   ELF_ST_TYPE(symp->st_info) == STT_FUNC) ? symp : NULL;
2342
2343             symnum = obj->chains[symnum];
2344         }
2345     }
2346     return NULL;
2347 }
2348
2349 static void
2350 trace_loaded_objects(Obj_Entry *obj)
2351 {
2352     const char *fmt1, *fmt2, *fmt, *main_local;
2353     int         c;
2354
2355     if ((main_local = _getenv_ld("LD_TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
2356         main_local = "";
2357
2358     if ((fmt1 = _getenv_ld("LD_TRACE_LOADED_OBJECTS_FMT1")) == NULL)
2359         fmt1 = "\t%o => %p (%x)\n";
2360
2361     if ((fmt2 = _getenv_ld("LD_TRACE_LOADED_OBJECTS_FMT2")) == NULL)
2362         fmt2 = "\t%o (%x)\n";
2363
2364     for (; obj; obj = obj->next) {
2365         Needed_Entry            *needed;
2366         char                    *name, *path;
2367         bool                    is_lib;
2368
2369         for (needed = obj->needed; needed; needed = needed->next) {
2370             if (needed->obj != NULL) {
2371                 if (needed->obj->traced)
2372                     continue;
2373                 needed->obj->traced = true;
2374                 path = needed->obj->path;
2375             } else
2376                 path = "not found";
2377
2378             name = (char *)obj->strtab + needed->name;
2379             is_lib = strncmp(name, "lib", 3) == 0;      /* XXX - bogus */
2380
2381             fmt = is_lib ? fmt1 : fmt2;
2382             while ((c = *fmt++) != '\0') {
2383                 switch (c) {
2384                 default:
2385                     putchar(c);
2386                     continue;
2387                 case '\\':
2388                     switch (c = *fmt) {
2389                     case '\0':
2390                         continue;
2391                     case 'n':
2392                         putchar('\n');
2393                         break;
2394                     case 't':
2395                         putchar('\t');
2396                         break;
2397                     }
2398                     break;
2399                 case '%':
2400                     switch (c = *fmt) {
2401                     case '\0':
2402                         continue;
2403                     case '%':
2404                     default:
2405                         putchar(c);
2406                         break;
2407                     case 'A':
2408                         printf("%s", main_local);
2409                         break;
2410                     case 'a':
2411                         printf("%s", obj_main->path);
2412                         break;
2413                     case 'o':
2414                         printf("%s", name);
2415                         break;
2416 #if 0
2417                     case 'm':
2418                         printf("%d", sodp->sod_major);
2419                         break;
2420                     case 'n':
2421                         printf("%d", sodp->sod_minor);
2422                         break;
2423 #endif
2424                     case 'p':
2425                         printf("%s", path);
2426                         break;
2427                     case 'x':
2428                         printf("%p", needed->obj ? needed->obj->mapbase : 0);
2429                         break;
2430                     }
2431                     break;
2432                 }
2433                 ++fmt;
2434             }
2435         }
2436     }
2437 }
2438
2439 /*
2440  * Unload a dlopened object and its dependencies from memory and from
2441  * our data structures.  It is assumed that the DAG rooted in the
2442  * object has already been unreferenced, and that the object has a
2443  * reference count of 0.
2444  */
2445 static void
2446 unload_object(Obj_Entry *root)
2447 {
2448     Obj_Entry *obj;
2449     Obj_Entry **linkp;
2450
2451     assert(root->refcount == 0);
2452
2453     /*
2454      * Pass over the DAG removing unreferenced objects from
2455      * appropriate lists.
2456      */ 
2457     unlink_object(root);
2458
2459     /* Unmap all objects that are no longer referenced. */
2460     linkp = &obj_list->next;
2461     while ((obj = *linkp) != NULL) {
2462         if (obj->refcount == 0) {
2463             dbg("unloading \"%s\"", obj->path);
2464             munmap(obj->mapbase, obj->mapsize);
2465             linkmap_delete(obj);
2466             *linkp = obj->next;
2467             obj_count--;
2468             obj_free(obj);
2469         } else
2470             linkp = &obj->next;
2471     }
2472     obj_tail = linkp;
2473 }
2474
2475 static void
2476 unlink_object(Obj_Entry *root)
2477 {
2478     const Needed_Entry *needed;
2479     Objlist_Entry *elm;
2480
2481     if (root->refcount == 0) {
2482         /* Remove the object from the RTLD_GLOBAL list. */
2483         objlist_remove(&list_global, root);
2484
2485         /* Remove the object from all objects' DAG lists. */
2486         STAILQ_FOREACH(elm, &root->dagmembers , link)
2487             objlist_remove(&elm->obj->dldags, root);
2488     }
2489
2490     for (needed = root->needed;  needed != NULL;  needed = needed->next)
2491         if (needed->obj != NULL)
2492             unlink_object(needed->obj);
2493 }
2494
2495 static void
2496 unref_dag(Obj_Entry *root)
2497 {
2498     const Needed_Entry *needed;
2499
2500     if (root->refcount == 0)
2501         return;
2502     root->refcount--;
2503     if (root->refcount == 0)
2504         for (needed = root->needed;  needed != NULL;  needed = needed->next)
2505             if (needed->obj != NULL)
2506                 unref_dag(needed->obj);
2507 }