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