Commit in reactos/lib/ntdll/ldr on ELF_support
elf.c+2794added 1.1.2.1
src/libexec/rtld-elf/rtld.c (ELF runtime linker core) from FreeBSD. Initial commit, no code change yet

reactos/lib/ntdll/ldr
elf.c added at 1.1.2.1
diff -N elf.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf.c	2 Dec 2004 02:44:18 -0000	1.1.2.1
@@ -0,0 +1,2794 @@
+/* $Id: elf.c,v 1.1.2.1 2004/12/02 02:44:18 hyperion Exp $
+*/
+
+/*
+ * REACTOS ELF LOADER
+ *
+ * ELF run-time linker, ported from FreeBSD by KJK::Hyperion as part of the ELF
+ * support initiative. Original copyright, licensing and disclaimers follow
+ */
+
+/*-
+ * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
+ * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: src/libexec/rtld-elf/rtld.c,v 1.101 2004/11/02 09:42:21 ssouhlal Exp $
+ */
+
+/*
+ * Dynamic linker for ELF.
+ *
+ * John Polstra <jdp@polstra.com>.
+ */
+
+#ifndef __GNUC__
+#error "GCC is needed to compile this file"
+#endif
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <dlfcn.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "debug.h"
+#include "rtld.h"
+#include "libmap.h"
+#include "rtld_tls.h"
+
+#ifndef COMPAT_32BIT
+#define PATH_RTLD	"/libexec/ld-elf.so.1"
+#else
+#define PATH_RTLD	"/libexec/ld-elf32.so.1"
+#endif
+
+/* Types. */
+typedef void (*func_ptr_type)();
+typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
+
+/*
+ * This structure provides a reentrant way to keep a list of objects and
+ * check which ones have already been processed in some way.
+ */
+typedef struct Struct_DoneList {
+    const Obj_Entry **objs;		/* Array of object pointers */
+    unsigned int num_alloc;		/* Allocated size of the array */
+    unsigned int num_used;		/* Number of array slots used */
+} DoneList;
+
+/*
+ * Function declarations.
+ */
+static const char *basename(const char *);
+static void die(void);
+static void digest_dynamic(Obj_Entry *, int);
+static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
+static Obj_Entry *dlcheck(void *);
+static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
+static bool donelist_check(DoneList *, const Obj_Entry *);
+static void errmsg_restore(char *);
+static char *errmsg_save(void);
+static void *fill_search_info(const char *, size_t, void *);
+static char *find_library(const char *, const Obj_Entry *);
+static const char *gethints(void);
+static void init_dag(Obj_Entry *);
+static void init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *);
+static void init_rtld(caddr_t);
+static void initlist_add_neededs(Needed_Entry *needed, Objlist *list);
+static void initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail,
+  Objlist *list);
+static bool is_exported(const Elf_Sym *);
+static void linkmap_add(Obj_Entry *);
+static void linkmap_delete(Obj_Entry *);
+static int load_needed_objects(Obj_Entry *);
+static int load_preload_objects(void);
+static Obj_Entry *load_object(char *);
+static Obj_Entry *obj_from_addr(const void *);
+static void objlist_call_fini(Objlist *);
+static void objlist_call_init(Objlist *);
+static void objlist_clear(Objlist *);
+static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
+static void objlist_init(Objlist *);
+static void objlist_push_head(Objlist *, Obj_Entry *);
+static void objlist_push_tail(Objlist *, Obj_Entry *);
+static void objlist_remove(Objlist *, Obj_Entry *);
+static void objlist_remove_unref(Objlist *);
+static void *path_enumerate(const char *, path_enum_proc, void *);
+static int relocate_objects(Obj_Entry *, bool, Obj_Entry *);
+static int rtld_dirname(const char *, char *);
+static void rtld_exit(void);
+static char *search_library_path(const char *, const char *);
+static const void **get_program_var_addr(const char *name);
+static void set_program_var(const char *, const void *);
+static const Elf_Sym *symlook_default(const char *, unsigned long hash,
+  const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt);
+static const Elf_Sym *symlook_list(const char *, unsigned long,
+  Objlist *, const Obj_Entry **, bool in_plt, DoneList *);
+static void trace_loaded_objects(Obj_Entry *obj);
+static void unlink_object(Obj_Entry *);
+static void unload_object(Obj_Entry *);
+static void unref_dag(Obj_Entry *);
+static void ref_dag(Obj_Entry *);
+
+void r_debug_state(struct r_debug*, struct link_map*);
+
+/*
+ * Data declarations.
+ */
+static char *error_message;	/* Message for dlerror(), or NULL */
+struct r_debug r_debug;		/* for GDB; */
+static bool libmap_disable;	/* Disable libmap */
+static bool trust;		/* False for setuid and setgid programs */
+static char *ld_bind_now;	/* Environment variable for immediate binding */
+static char *ld_debug;		/* Environment variable for debugging */
+static char *ld_library_path;	/* Environment variable for search path */
+static char *ld_preload;	/* Environment variable for libraries to
+				   load first */
+static char *ld_tracing;	/* Called from ldd to print libs */
+static Obj_Entry *obj_list;	/* Head of linked list of shared objects */
+static Obj_Entry **obj_tail;	/* Link field of last object in list */
+static Obj_Entry *obj_main;	/* The main program shared object */
+static Obj_Entry obj_rtld;	/* The dynamic linker shared object */
+static unsigned int obj_count;	/* Number of objects in obj_list */
+
+static Objlist list_global =	/* Objects dlopened with RTLD_GLOBAL */
+  STAILQ_HEAD_INITIALIZER(list_global);
+static Objlist list_main =	/* Objects loaded at program startup */
+  STAILQ_HEAD_INITIALIZER(list_main);
+static Objlist list_fini =	/* Objects needing fini() calls */
+  STAILQ_HEAD_INITIALIZER(list_fini);
+
+static Elf_Sym sym_zero;	/* For resolving undefined weak refs. */
+
+#define GDB_STATE(s,m)	r_debug.r_state = s; r_debug_state(&r_debug,m);
+
+extern Elf_Dyn _DYNAMIC;
+#pragma weak _DYNAMIC
+#ifndef RTLD_IS_DYNAMIC
+#define	RTLD_IS_DYNAMIC()	(&_DYNAMIC != NULL)
+#endif
+
+/*
+ * These are the functions the dynamic linker exports to application
+ * programs.  They are the only symbols the dynamic linker is willing
+ * to export from itself.
+ */
+static func_ptr_type exports[] = {
+    (func_ptr_type) &_rtld_error,
+    (func_ptr_type) &dlclose,
+    (func_ptr_type) &dlerror,
+    (func_ptr_type) &dlopen,
+    (func_ptr_type) &dlsym,
+    (func_ptr_type) &dladdr,
+    (func_ptr_type) &dllockinit,
+    (func_ptr_type) &dlinfo,
+    (func_ptr_type) &_rtld_thread_init,
+#ifdef __i386__
+    (func_ptr_type) &___tls_get_addr,
+#endif
+    (func_ptr_type) &__tls_get_addr,
+    (func_ptr_type) &_rtld_allocate_tls,
+    (func_ptr_type) &_rtld_free_tls,
+    NULL
+};
+
+/*
+ * Global declarations normally provided by crt1.  The dynamic linker is
+ * not built with crt1, so we have to provide them ourselves.
+ */
+char *__progname;
+char **environ;
+
+/*
+ * Globals to control TLS allocation.
+ */
+size_t tls_last_offset;		/* Static TLS offset of last module */
+size_t tls_last_size;		/* Static TLS size of last module */
+size_t tls_static_space;	/* Static TLS space allocated */
+int tls_dtv_generation = 1;	/* Used to detect when dtv size changes  */
+int tls_max_index = 1;		/* Largest module index allocated */
+
+/*
+ * Fill in a DoneList with an allocation large enough to hold all of
+ * the currently-loaded objects.  Keep this as a macro since it calls
+ * alloca and we want that to occur within the scope of the caller.
+ */
+#define donelist_init(dlp)					\
+    ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]),	\
+    assert((dlp)->objs != NULL),				\
+    (dlp)->num_alloc = obj_count,				\
+    (dlp)->num_used = 0)
+
+/*
+ * Main entry point for dynamic linking.  The first argument is the
+ * stack pointer.  The stack is expected to be laid out as described
+ * in the SVR4 ABI specification, Intel 386 Processor Supplement.
+ * Specifically, the stack pointer points to a word containing
+ * ARGC.  Following that in the stack is a null-terminated sequence
+ * of pointers to argument strings.  Then comes a null-terminated
+ * sequence of pointers to environment strings.  Finally, there is a
+ * sequence of "auxiliary vector" entries.
+ *
+ * The second argument points to a place to store the dynamic linker's
+ * exit procedure pointer and the third to a place to store the main
+ * program's object.
+ *
+ * The return value is the main program's entry point.
+ */
+func_ptr_type
+_rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
+{
+    Elf_Auxinfo *aux_info[AT_COUNT];
+    int i;
+    int argc;
+    char **argv;
+    char **env;
+    Elf_Auxinfo *aux;
+    Elf_Auxinfo *auxp;
+    const char *argv0;
+    Objlist_Entry *entry;
+    Obj_Entry *obj;
+    Obj_Entry **preload_tail;
+    Objlist initlist;
+    int lockstate;
+
+    /*
+     * On entry, the dynamic linker itself has not been relocated yet.
+     * Be very careful not to reference any global data until after
+     * init_rtld has returned.  It is OK to reference file-scope statics
+     * and string constants, and to call static and global functions.
+     */
+
+    /* Find the auxiliary vector on the stack. */
+    argc = *sp++;
+    argv = (char **) sp;
+    sp += argc + 1;	/* Skip over arguments and NULL terminator */
+    env = (char **) sp;
+    while (*sp++ != 0)	/* Skip over environment, and NULL terminator */
+	;
+    aux = (Elf_Auxinfo *) sp;
+
+    /* Digest the auxiliary vector. */
+    for (i = 0;  i < AT_COUNT;  i++)
+	aux_info[i] = NULL;
+    for (auxp = aux;  auxp->a_type != AT_NULL;  auxp++) {
+	if (auxp->a_type < AT_COUNT)
+	    aux_info[auxp->a_type] = auxp;
+    }
+
+    /* Initialize and relocate ourselves. */
+    assert(aux_info[AT_BASE] != NULL);
+    init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
+
+    __progname = obj_rtld.path;
+    argv0 = argv[0] != NULL ? argv[0] : "(null)";
+    environ = env;
+
+    trust = !issetugid();
+
+    ld_bind_now = getenv(LD_ "BIND_NOW");
+    if (trust) {
+	ld_debug = getenv(LD_ "DEBUG");
+	libmap_disable = getenv(LD_ "LIBMAP_DISABLE") != NULL;
+	ld_library_path = getenv(LD_ "LIBRARY_PATH");
+	ld_preload = getenv(LD_ "PRELOAD");
+    }
+    ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS");
+
+    if (ld_debug != NULL && *ld_debug != '\0')
+	debug = 1;
+    dbg("%s is initialized, base address = %p", __progname,
+	(caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
+    dbg("RTLD dynamic = %p", obj_rtld.dynamic);
+    dbg("RTLD pltgot  = %p", obj_rtld.pltgot);
+
+    /*
+     * Load the main program, or process its program header if it is
+     * already loaded.
+     */
+    if (aux_info[AT_EXECFD] != NULL) {	/* Load the main program. */
+	int fd = aux_info[AT_EXECFD]->a_un.a_val;
+	dbg("loading main program");
+	obj_main = map_object(fd, argv0, NULL);
+	close(fd);
+	if (obj_main == NULL)
+	    die();
+    } else {				/* Main program already loaded. */
+	const Elf_Phdr *phdr;
+	int phnum;
+	caddr_t entry;
+
+	dbg("processing main program's program header");
+	assert(aux_info[AT_PHDR] != NULL);
+	phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
+	assert(aux_info[AT_PHNUM] != NULL);
+	phnum = aux_info[AT_PHNUM]->a_un.a_val;
+	assert(aux_info[AT_PHENT] != NULL);
+	assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
+	assert(aux_info[AT_ENTRY] != NULL);
+	entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
+	if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
+	    die();
+    }
+
+    obj_main->path = xstrdup(argv0);
+    obj_main->mainprog = true;
+
+    /*
+     * Get the actual dynamic linker pathname from the executable if
+     * possible.  (It should always be possible.)  That ensures that
+     * gdb will find the right dynamic linker even if a non-standard
+     * one is being used.
+     */
+    if (obj_main->interp != NULL &&
+      strcmp(obj_main->interp, obj_rtld.path) != 0) {
+	free(obj_rtld.path);
+	obj_rtld.path = xstrdup(obj_main->interp);
+        __progname = obj_rtld.path;
+    }
+
+    digest_dynamic(obj_main, 0);
+
+    linkmap_add(obj_main);
+    linkmap_add(&obj_rtld);
+
+    /* Link the main program into the list of objects. */
+    *obj_tail = obj_main;
+    obj_tail = &obj_main->next;
+    obj_count++;
+    /* Make sure we don't call the main program's init and fini functions. */
+    obj_main->init = obj_main->fini = (Elf_Addr)NULL;
+
+    /* Initialize a fake symbol for resolving undefined weak references. */
+    sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
+    sym_zero.st_shndx = SHN_UNDEF;
+
+    if (!libmap_disable)
+        libmap_disable = (bool)lm_init();
+
+    dbg("loading LD_PRELOAD libraries");
+    if (load_preload_objects() == -1)
+	die();
+    preload_tail = obj_tail;
+
+    dbg("loading needed objects");
+    if (load_needed_objects(obj_main) == -1)
+	die();
+
+    /* Make a list of all objects loaded at startup. */
+    for (obj = obj_list;  obj != NULL;  obj = obj->next) {
+	objlist_push_tail(&list_main, obj);
+    	obj->refcount++;
+    }
+
+    if (ld_tracing) {		/* We're done */
+	trace_loaded_objects(obj_main);
+	exit(0);
+    }
+
+    if (getenv(LD_ "DUMP_REL_PRE") != NULL) {
+       dump_relocations(obj_main);
+       exit (0);
+    }
+
+    /* setup TLS for main thread */
+    dbg("initializing initial thread local storage");
+    STAILQ_FOREACH(entry, &list_main, link) {
+	/*
+	 * Allocate all the initial objects out of the static TLS
+	 * block even if they didn't ask for it.
+	 */
+	allocate_tls_offset(entry->obj);
+    }
+    allocate_initial_tls(obj_list);
+
+    if (relocate_objects(obj_main,
+	ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld) == -1)
+	die();
+
+    dbg("doing copy relocations");
+    if (do_copy_relocations(obj_main) == -1)
+	die();
+
+    if (getenv(LD_ "DUMP_REL_POST") != NULL) {
+       dump_relocations(obj_main);
+       exit (0);
+    }
+
+    dbg("initializing key program variables");
+    set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
+    set_program_var("environ", env);
+
+    dbg("initializing thread locks");
+    lockdflt_init();
+
+    /* Make a list of init functions to call. */
+    objlist_init(&initlist);
+    initlist_add_objects(obj_list, preload_tail, &initlist);
+
+    r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
+
+    objlist_call_init(&initlist);
+    lockstate = wlock_acquire(rtld_bind_lock);
+    objlist_clear(&initlist);
+    wlock_release(rtld_bind_lock, lockstate);
+
+    dbg("transferring control to program entry point = %p", obj_main->entry);
+
+    /* Return the exit procedure and the program entry point. */
+    *exit_proc = rtld_exit;
+    *objp = obj_main;
+    return (func_ptr_type) obj_main->entry;
+}
+
+Elf_Addr
+_rtld_bind(Obj_Entry *obj, Elf_Word reloff)
+{
+    const Elf_Rel *rel;
+    const Elf_Sym *def;
+    const Obj_Entry *defobj;
+    Elf_Addr *where;
+    Elf_Addr target;
+    int lockstate;
+
+    lockstate = rlock_acquire(rtld_bind_lock);
+    if (obj->pltrel)
+	rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
+    else
+	rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
+
+    where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
+    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
+    if (def == NULL)
+	die();
+
+    target = (Elf_Addr)(defobj->relocbase + def->st_value);
+
+    dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
+      defobj->strtab + def->st_name, basename(obj->path),
+      (void *)target, basename(defobj->path));
+
+    /*
+     * Write the new contents for the jmpslot. Note that depending on
+     * architecture, the value which we need to return back to the
+     * lazy binding trampoline may or may not be the target
+     * address. The value returned from reloc_jmpslot() is the value
+     * that the trampoline needs.
+     */
+    target = reloc_jmpslot(where, target, defobj, obj, rel);
+    rlock_release(rtld_bind_lock, lockstate);
+    return target;
+}
+
+/*
+ * Error reporting function.  Use it like printf.  If formats the message
+ * into a buffer, and sets things up so that the next call to dlerror()
+ * will return the message.
+ */
+void
+_rtld_error(const char *fmt, ...)
+{
+    static char buf[512];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsnprintf(buf, sizeof buf, fmt, ap);
+    error_message = buf;
+    va_end(ap);
+}
+
+/*
+ * Return a dynamically-allocated copy of the current error message, if any.
+ */
+static char *
+errmsg_save(void)
+{
+    return error_message == NULL ? NULL : xstrdup(error_message);
+}
+
+/*
+ * Restore the current error message from a copy which was previously saved
+ * by errmsg_save().  The copy is freed.
+ */
+static void
+errmsg_restore(char *saved_msg)
+{
+    if (saved_msg == NULL)
+	error_message = NULL;
+    else {
+	_rtld_error("%s", saved_msg);
+	free(saved_msg);
+    }
+}
+
+static const char *
+basename(const char *name)
+{
+    const char *p = strrchr(name, '/');
+    return p != NULL ? p + 1 : name;
+}
+
+static void
+die(void)
+{
+    const char *msg = dlerror();
+
+    if (msg == NULL)
+	msg = "Fatal error";
+    errx(1, "%s", msg);
+}
+
+/*
+ * Process a shared object's DYNAMIC section, and save the important
+ * information in its Obj_Entry structure.
+ */
+static void
+digest_dynamic(Obj_Entry *obj, int early)
+{
+    const Elf_Dyn *dynp;
+    Needed_Entry **needed_tail = &obj->needed;
+    const Elf_Dyn *dyn_rpath = NULL;
+    int plttype = DT_REL;
+
+    obj->bind_now = false;
+    for (dynp = obj->dynamic;  dynp->d_tag != DT_NULL;  dynp++) {
+	switch (dynp->d_tag) {
+
+	case DT_REL:
+	    obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_RELSZ:
+	    obj->relsize = dynp->d_un.d_val;
+	    break;
+
+	case DT_RELENT:
+	    assert(dynp->d_un.d_val == sizeof(Elf_Rel));
+	    break;
+
+	case DT_JMPREL:
+	    obj->pltrel = (const Elf_Rel *)
+	      (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_PLTRELSZ:
+	    obj->pltrelsize = dynp->d_un.d_val;
+	    break;
+
+	case DT_RELA:
+	    obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_RELASZ:
+	    obj->relasize = dynp->d_un.d_val;
+	    break;
+
+	case DT_RELAENT:
+	    assert(dynp->d_un.d_val == sizeof(Elf_Rela));
+	    break;
+
+	case DT_PLTREL:
+	    plttype = dynp->d_un.d_val;
+	    assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
+	    break;
+
+	case DT_SYMTAB:
+	    obj->symtab = (const Elf_Sym *)
+	      (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_SYMENT:
+	    assert(dynp->d_un.d_val == sizeof(Elf_Sym));
+	    break;
+
+	case DT_STRTAB:
+	    obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_STRSZ:
+	    obj->strsize = dynp->d_un.d_val;
+	    break;
+
+	case DT_HASH:
+	    {
+		const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
+		  (obj->relocbase + dynp->d_un.d_ptr);
+		obj->nbuckets = hashtab[0];
+		obj->nchains = hashtab[1];
+		obj->buckets = hashtab + 2;
+		obj->chains = obj->buckets + obj->nbuckets;
+	    }
+	    break;
+
+	case DT_NEEDED:
+	    if (!obj->rtld) {
+		Needed_Entry *nep = NEW(Needed_Entry);
+		nep->name = dynp->d_un.d_val;
+		nep->obj = NULL;
+		nep->next = NULL;
+
+		*needed_tail = nep;
+		needed_tail = &nep->next;
+	    }
+	    break;
+
+	case DT_PLTGOT:
+	    obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_TEXTREL:
+	    obj->textrel = true;
+	    break;
+
+	case DT_SYMBOLIC:
+	    obj->symbolic = true;
+	    break;
+
+	case DT_RPATH:
+	case DT_RUNPATH:	/* XXX: process separately */
+	    /*
+	     * We have to wait until later to process this, because we
+	     * might not have gotten the address of the string table yet.
+	     */
+	    dyn_rpath = dynp;
+	    break;
+
+	case DT_SONAME:
+	    /* Not used by the dynamic linker. */
+	    break;
+
+	case DT_INIT:
+	    obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_FINI:
+	    obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
+	    break;
+
+	case DT_DEBUG:
+	    /* XXX - not implemented yet */
+	    if (!early)
+		dbg("Filling in DT_DEBUG entry");
+	    ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
+	    break;
+
+	case DT_FLAGS:
+		if (dynp->d_un.d_val & DF_ORIGIN) {
+		    obj->origin_path = xmalloc(PATH_MAX);
+		    if (rtld_dirname(obj->path, obj->origin_path) == -1)
+			die();
+		}
+		if (dynp->d_un.d_val & DF_SYMBOLIC)
+		    obj->symbolic = true;
+		if (dynp->d_un.d_val & DF_TEXTREL)
+		    obj->textrel = true;
+		if (dynp->d_un.d_val & DF_BIND_NOW)
+		    obj->bind_now = true;
+		if (dynp->d_un.d_val & DF_STATIC_TLS)
+		    ;
+	    break;
+
+	default:
+	    if (!early) {
+		dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag,
+		    (long)dynp->d_tag);
+	    }
+	    break;
+	}
+    }
+
+    obj->traced = false;
+
+    if (plttype == DT_RELA) {
+	obj->pltrela = (const Elf_Rela *) obj->pltrel;
+	obj->pltrel = NULL;
+	obj->pltrelasize = obj->pltrelsize;
+	obj->pltrelsize = 0;
+    }
+
+    if (dyn_rpath != NULL)
+	obj->rpath = obj->strtab + dyn_rpath->d_un.d_val;
+}
+
+/*
+ * Process a shared object's program header.  This is used only for the
+ * main program, when the kernel has already loaded the main program
+ * into memory before calling the dynamic linker.  It creates and
+ * returns an Obj_Entry structure.
+ */
+static Obj_Entry *
+digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
+{
+    Obj_Entry *obj;
+    const Elf_Phdr *phlimit = phdr + phnum;
+    const Elf_Phdr *ph;
+    int nsegs = 0;
+
+    obj = obj_new();
+    for (ph = phdr;  ph < phlimit;  ph++) {
+	switch (ph->p_type) {
+
+	case PT_PHDR:
+	    if ((const Elf_Phdr *)ph->p_vaddr != phdr) {
+		_rtld_error("%s: invalid PT_PHDR", path);
+		return NULL;
+	    }
+	    obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
+	    obj->phsize = ph->p_memsz;
+	    break;
+
+	case PT_INTERP:
+	    obj->interp = (const char *) ph->p_vaddr;
+	    break;
+
+	case PT_LOAD:
+	    if (nsegs == 0) {	/* First load segment */
+		obj->vaddrbase = trunc_page(ph->p_vaddr);
+		obj->mapbase = (caddr_t) obj->vaddrbase;
+		obj->relocbase = obj->mapbase - obj->vaddrbase;
+		obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
+		  obj->vaddrbase;
+	    } else {		/* Last load segment */
+		obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
+		  obj->vaddrbase;
+	    }
+	    nsegs++;
+	    break;
+
+	case PT_DYNAMIC:
+	    obj->dynamic = (const Elf_Dyn *) ph->p_vaddr;
+	    break;
+
+	case PT_TLS:
+	    obj->tlsindex = 1;
+	    obj->tlssize = ph->p_memsz;
+	    obj->tlsalign = ph->p_align;
+	    obj->tlsinitsize = ph->p_filesz;
+	    obj->tlsinit = (void*) ph->p_vaddr;
+	    break;
+	}
+    }
+    if (nsegs < 1) {
+	_rtld_error("%s: too few PT_LOAD segments", path);
+	return NULL;
+    }
+
+    obj->entry = entry;
+    return obj;
+}
+
+static Obj_Entry *
+dlcheck(void *handle)
+{
+    Obj_Entry *obj;
+
+    for (obj = obj_list;  obj != NULL;  obj = obj->next)
+	if (obj == (Obj_Entry *) handle)
+	    break;
+
+    if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
+	_rtld_error("Invalid shared object handle %p", handle);
+	return NULL;
+    }
+    return obj;
+}
+
+/*
+ * If the given object is already in the donelist, return true.  Otherwise
+ * add the object to the list and return false.
+ */
+static bool
+donelist_check(DoneList *dlp, const Obj_Entry *obj)
+{
+    unsigned int i;
+
+    for (i = 0;  i < dlp->num_used;  i++)
+	if (dlp->objs[i] == obj)
+	    return true;
+    /*
+     * Our donelist allocation should always be sufficient.  But if
+     * our threads locking isn't working properly, more shared objects
+     * could have been loaded since we allocated the list.  That should
+     * never happen, but we'll handle it properly just in case it does.
+     */
+    if (dlp->num_used < dlp->num_alloc)
+	dlp->objs[dlp->num_used++] = obj;
+    return false;
+}
+
+/*
+ * Hash function for symbol table lookup.  Don't even think about changing
+ * this.  It is specified by the System V ABI.
+ */
+unsigned long
+elf_hash(const char *name)
+{
+    const unsigned char *p = (const unsigned char *) name;
+    unsigned long h = 0;
+    unsigned long g;
+
+    while (*p != '\0') {
+	h = (h << 4) + *p++;
+	if ((g = h & 0xf0000000) != 0)
+	    h ^= g >> 24;
+	h &= ~g;
+    }
+    return h;
+}
+
+/*
+ * Find the library with the given name, and return its full pathname.
+ * The returned string is dynamically allocated.  Generates an error
+ * message and returns NULL if the library cannot be found.
+ *
+ * If the second argument is non-NULL, then it refers to an already-
+ * loaded shared object, whose library search path will be searched.
+ *
+ * The search order is:
+ *   LD_LIBRARY_PATH
+ *   rpath in the referencing file
+ *   ldconfig hints
+ *   /lib:/usr/lib
+ */
+static char *
+find_library(const char *xname, const Obj_Entry *refobj)
+{
+    char *pathname;
+    char *name;
+
+    if (strchr(xname, '/') != NULL) {	/* Hard coded pathname */
+	if (xname[0] != '/' && !trust) {
+	    _rtld_error("Absolute pathname required for shared object \"%s\"",
+	      xname);
+	    return NULL;
+	}
+	return xstrdup(xname);
+    }
+
+    if (libmap_disable || (refobj == NULL) ||
+	(name = lm_find(refobj->path, xname)) == NULL)
+	name = (char *)xname;
+
+    dbg(" Searching for \"%s\"", name);
+
+    if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
+      (refobj != NULL &&
+      (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
+      (pathname = search_library_path(name, gethints())) != NULL ||
+      (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
+	return pathname;
+
+    if(refobj != NULL && refobj->path != NULL) {
+	_rtld_error("Shared object \"%s\" not found, required by \"%s\"",
+	  name, basename(refobj->path));
+    } else {
+	_rtld_error("Shared object \"%s\" not found", name);
+    }
+    return NULL;
+}
+
+/*
+ * Given a symbol number in a referencing object, find the corresponding
+ * definition of the symbol.  Returns a pointer to the symbol, or NULL if
+ * no definition was found.  Returns a pointer to the Obj_Entry of the
+ * defining object via the reference parameter DEFOBJ_OUT.
+ */
+const Elf_Sym *
+find_symdef(unsigned long symnum, const Obj_Entry *refobj,
+    const Obj_Entry **defobj_out, bool in_plt, SymCache *cache)
+{
+    const Elf_Sym *ref;
+    const Elf_Sym *def;
+    const Obj_Entry *defobj;
+    const char *name;
+    unsigned long hash;
+
+    /*
+     * If we have already found this symbol, get the information from
+     * the cache.
+     */
+    if (symnum >= refobj->nchains)
+	return NULL;	/* Bad object */
+    if (cache != NULL && cache[symnum].sym != NULL) {
+	*defobj_out = cache[symnum].obj;
+	return cache[symnum].sym;
+    }
+
+    ref = refobj->symtab + symnum;
+    name = refobj->strtab + ref->st_name;
+    defobj = NULL;
+
+    /*
+     * We don't have to do a full scale lookup if the symbol is local.
+     * We know it will bind to the instance in this load module; to
+     * which we already have a pointer (ie ref). By not doing a lookup,
+     * we not only improve performance, but it also avoids unresolvable
+     * symbols when local symbols are not in the hash table. This has
+     * been seen with the ia64 toolchain.
+     */
+    if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
+	if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
+	    _rtld_error("%s: Bogus symbol table entry %lu", refobj->path,
+		symnum);
+	}
+	hash = elf_hash(name);
+	def = symlook_default(name, hash, refobj, &defobj, in_plt);
+    } else {
+	def = ref;
+	defobj = refobj;
+    }
+
+    /*
+     * If we found no definition and the reference is weak, treat the
+     * symbol as having the value zero.
+     */
+    if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
+	def = &sym_zero;
+	defobj = obj_main;
+    }
+
+    if (def != NULL) {
+	*defobj_out = defobj;
+	/* Record the information in the cache to avoid subsequent lookups. */
+	if (cache != NULL) {
+	    cache[symnum].sym = def;
+	    cache[symnum].obj = defobj;
+	}
+    } else {
+	if (refobj != &obj_rtld)
+	    _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
+    }
+    return def;
+}
+
+/*
+ * Return the search path from the ldconfig hints file, reading it if
+ * necessary.  Returns NULL if there are problems with the hints file,
+ * or if the search path there is empty.
+ */
+static const char *
+gethints(void)
+{
+    static char *hints;
+
+    if (hints == NULL) {
+	int fd;
+	struct elfhints_hdr hdr;
+	char *p;
+
+	/* Keep from trying again in case the hints file is bad. */
+	hints = "";
+
+	if ((fd = open(_PATH_ELF_HINTS, O_RDONLY)) == -1)
+	    return NULL;
+	if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
+	  hdr.magic != ELFHINTS_MAGIC ||
+	  hdr.version != 1) {
+	    close(fd);
[truncated at 1000 lines; 1798 more skipped]
CVSspam 0.2.8