make dlerror produce informative results
[musl] / src / ldso / dynlink.c
index eda0046..0533bbb 100644 (file)
@@ -1,4 +1,3 @@
-#ifdef __PIC__
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <dlfcn.h>
 
+static int errflag;
+static char errbuf[128];
+
+#ifdef __PIC__
+
 #include "reloc.h"
 
 #if ULONG_MAX == 0xffffffff
@@ -139,8 +143,11 @@ static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t
                        ctx = IS_COPY(type) ? dso->next : dso;
                        sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
                        if (!sym_val && sym->st_info>>4 != STB_WEAK) {
+                               snprintf(errbuf, sizeof errbuf,
+                                       "Error relocating %s: %s: symbol not found",
+                                       dso->name, name);
                                if (runtime) longjmp(rtld_fail, 1);
-                               dprintf(2, "%s: symbol not found\n", name);
+                               dprintf(2, "%s\n", errbuf);
                                _exit(127);
                        }
                        sym_size = sym->st_size;
@@ -402,9 +409,11 @@ static void load_deps(struct dso *p)
                        if (p->dynv[i] != DT_NEEDED) continue;
                        dep = load_library(p->strings + p->dynv[i+1]);
                        if (!dep) {
-                               if (runtime) longjmp(rtld_fail, 1);
-                               dprintf(2, "%s: %m (needed by %s)\n",
+                               snprintf(errbuf, sizeof errbuf,
+                                       "Error loading shared library %s: %m (needed by %s)",
                                        p->strings + p->dynv[i+1], p->name);
+                               if (runtime) longjmp(rtld_fail, 1);
+                               dprintf(2, "%s\n", errbuf);
                                _exit(127);
                        }
                        if (runtime) {
@@ -610,9 +619,11 @@ void *dlopen(const char *file, int mode)
 {
        struct dso *volatile p, *orig_tail = tail, *next;
        size_t i;
+       int cs;
 
        if (!file) return head;
 
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
        pthread_rwlock_wrlock(&lock);
 
        if (setjmp(rtld_fail)) {
@@ -628,12 +639,17 @@ void *dlopen(const char *file, int mode)
                }
                tail = orig_tail;
                tail->next = 0;
-               pthread_rwlock_unlock(&lock);
-               return 0;
-       }
+               p = 0;
+               errflag = 1;
+               goto end;
+       } else p = load_library(file);
 
-       p = load_library(file);
-       if (!p) goto end;
+       if (!p) {
+               snprintf(errbuf, sizeof errbuf,
+                       "Error loading shared library %s: %m", file);
+               errflag = 1;
+               goto end;
+       }
 
        /* First load handling */
        if (!p->deps) {
@@ -658,6 +674,7 @@ void *dlopen(const char *file, int mode)
        do_init_fini(tail);
 end:
        pthread_rwlock_unlock(&lock);
+       pthread_setcancelstate(cs, 0);
        return p;
 }
 
@@ -671,8 +688,11 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra)
                if (!p) p=head;
                p=p->next;
        }
-       if (p == head || p == RTLD_DEFAULT)
-               return find_sym(head, s, 0);
+       if (p == head || p == RTLD_DEFAULT) {
+               void *res = find_sym(head, s, 0);
+               if (!res) errflag = 1;
+               return res;
+       }
        h = hash(s);
        sym = lookup(s, h, p->syms, p->hashtab, p->strings);
        if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
@@ -683,6 +703,8 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra)
                if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
                        return p->deps[i]->base + sym->st_value;
        }
+       errflag = 1;
+       snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
        return 0;
 }
 
@@ -707,7 +729,9 @@ void *__dlsym(void *p, const char *s, void *ra)
 
 char *dlerror()
 {
-       return "unknown error";
+       if (!errflag) return 0;
+       errflag = 0;
+       return errbuf;
 }
 
 int dlclose(void *p)