fix errno value when fdopendir is given an invalid file descriptor
[musl] / src / ldso / dynlink.c
index 031e1f0..33937e7 100644 (file)
@@ -53,7 +53,7 @@ struct dso
 };
 
 static struct dso *head, *tail, *libc;
-static char *env_path, *sys_path;
+static char *env_path, *sys_path, *r_path;
 static int rtld_used;
 static int runtime;
 static jmp_buf rtld_fail;
@@ -248,6 +248,11 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
                        }
                }
        }
+       for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
+               if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
+                       mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
+                       break;
+               }
        if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
                eh->e_phentsize, eh->e_phnum);
        *lenp = map_len;
@@ -311,7 +316,8 @@ static struct dso *load_library(const char *name)
        } else {
                if (strlen(name) > NAME_MAX) return 0;
                fd = -1;
-               if (env_path) fd = path_open(name, env_path);
+               if (r_path) fd = path_open(name, r_path);
+               if (fd < 0 && env_path) fd = path_open(name, env_path);
                if (fd < 0) {
                        if (!sys_path) {
                                FILE *f = fopen(ETC_LDSO_PATH, "r");
@@ -373,6 +379,10 @@ static void load_deps(struct dso *p)
        size_t i, ndeps=0;
        struct dso ***deps = &p->deps, **tmp, *dep;
        for (; p; p=p->next) {
+               for (i=0; p->dynv[i]; i+=2) {
+                       if (p->dynv[i] != DT_RPATH) continue;
+                       r_path = (void *)(p->strings + p->dynv[i+1]);
+               }
                for (i=0; p->dynv[i]; i+=2) {
                        if (p->dynv[i] != DT_NEEDED) continue;
                        dep = load_library(p->strings + p->dynv[i+1]);
@@ -390,6 +400,7 @@ static void load_deps(struct dso *p)
                                *deps = tmp;
                        }
                }
+               r_path = 0;
        }
 }
 
@@ -525,7 +536,7 @@ void *__dynlink(int argc, char **argv, size_t *got)
 
 void *dlopen(const char *file, int mode)
 {
-       struct dso *p, *orig_tail = tail, *next;
+       struct dso *volatile p, *orig_tail = tail, *next;
        size_t i;
 
        if (!file) return head;