remove freeing of dynamic linker data when dlopen/dlsym are not used
[musl] / src / ldso / dynlink.c
index c733dc5..8ff8e69 100644 (file)
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -16,6 +17,9 @@
 #include <pthread.h>
 #include <ctype.h>
 #include <dlfcn.h>
+#include "pthread_impl.h"
+#include "libc.h"
+#undef libc
 
 static int errflag;
 static char errbuf[128];
@@ -63,6 +67,8 @@ struct dso {
        char relocated;
        char constructed;
        struct dso **deps;
+       void *tls_image;
+       size_t tls_len, tls_size, tls_align, tls_id;
        char *shortname;
        char buf[];
 };
@@ -70,10 +76,10 @@ struct dso {
 #include "reloc.h"
 
 void __init_ssp(size_t *);
+void *__install_initial_tls(void *);
 
 static struct dso *head, *tail, *libc;
 static char *env_path, *sys_path, *r_path;
-static int rtld_used;
 static int ssp_used;
 static int runtime;
 static int ldd_mode;
@@ -82,6 +88,7 @@ static jmp_buf rtld_fail;
 static pthread_rwlock_t lock;
 static struct debug debug;
 static size_t *auxv;
+static size_t tls_cnt, tls_size;
 
 struct debug *_dl_debug_addr = &debug;
 
@@ -174,13 +181,9 @@ static void *find_sym(struct dso *dso, const char *s, int need_def)
        void *def = 0;
        if (dso->ghashtab) {
                gh = gnu_hash(s);
-               if (gh == 0xf9040207 && !strcmp(s, "dlopen")) rtld_used = 1;
-               if (gh == 0xf4dc4ae && !strcmp(s, "dlsym")) rtld_used = 1;
                if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
        } else {
                h = sysv_hash(s);
-               if (h == 0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
-               if (h == 0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
                if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
        }
        for (; dso; dso=dso->next) {
@@ -274,7 +277,7 @@ static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phc
        }
 }
 
-static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
+static void *map_library(int fd, struct dso *dso)
 {
        Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
        size_t phsize;
@@ -286,6 +289,7 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
        unsigned prot;
        unsigned char *map, *base;
        size_t dyn;
+       size_t tls_image=0;
        size_t i;
 
        ssize_t l = read(fd, buf, sizeof buf);
@@ -302,6 +306,12 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
        for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
                if (ph->p_type == PT_DYNAMIC)
                        dyn = ph->p_vaddr;
+               if (ph->p_type == PT_TLS) {
+                       tls_image = ph->p_vaddr;
+                       dso->tls_align = ph->p_align;
+                       dso->tls_len = ph->p_filesz;
+                       dso->tls_size = ph->p_memsz;
+               }
                if (ph->p_type != PT_LOAD) continue;
                if (ph->p_vaddr < addr_min) {
                        addr_min = ph->p_vaddr;
@@ -356,9 +366,11 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
                }
        if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
                eh->e_phentsize, eh->e_phnum);
-       *lenp = map_len;
-       *basep = base;
-       *dynp = dyn;
+       dso->map = map;
+       dso->map_len = map_len;
+       dso->base = base;
+       dso->dynv = (void *)(base+dyn);
+       if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
        return map;
 error:
        munmap(map, map_len);
@@ -375,7 +387,7 @@ static int path_open(const char *name, const char *search, char *buf, size_t buf
                z = strchr(s, ':');
                l = z ? z-s : strlen(s);
                snprintf(buf, buf_size, "%.*s/%s", l, s, name);
-               if ((fd = open(buf, O_RDONLY))>=0) return fd;
+               if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
                s += l;
        }
 }
@@ -398,7 +410,7 @@ static struct dso *load_library(const char *name)
        const char *pathname;
        unsigned char *base, *map;
        size_t dyno, map_len;
-       struct dso *p;
+       struct dso *p, temp_dso = {0};
        int fd;
        struct stat st;
 
@@ -422,7 +434,7 @@ static struct dso *load_library(const char *name)
        }
        if (strchr(name, '/')) {
                pathname = name;
-               fd = open(name, O_RDONLY);
+               fd = open(name, O_RDONLY|O_CLOEXEC);
        } else {
                /* Search for the name to see if it's already loaded */
                for (p=head->next; p; p=p->next) {
@@ -437,7 +449,7 @@ static struct dso *load_library(const char *name)
                if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
                if (fd < 0) {
                        if (!sys_path) {
-                               FILE *f = fopen(ETC_LDSO_PATH, "r");
+                               FILE *f = fopen(ETC_LDSO_PATH, "rbe");
                                if (f) {
                                        if (getline(&sys_path, (size_t[1]){0}, f) > 0)
                                                sys_path[strlen(sys_path)-1]=0;
@@ -465,21 +477,21 @@ static struct dso *load_library(const char *name)
                        return p;
                }
        }
-       map = map_library(fd, &map_len, &base, &dyno);
+       map = map_library(fd, &temp_dso);
        close(fd);
        if (!map) return 0;
-       p = calloc(1, sizeof *p + strlen(pathname) + 1);
+       p = malloc(sizeof *p + strlen(pathname) + 1);
        if (!p) {
                munmap(map, map_len);
                return 0;
        }
-
-       p->map = map;
-       p->map_len = map_len;
-       p->base = base;
-       p->dynv = (void *)(base + dyno);
+       memcpy(p, &temp_dso, sizeof temp_dso);
        decode_dyn(p);
-
+       if (p->tls_image) {
+               p->tls_id = ++tls_cnt;
+               tls_size += p->tls_size + p->tls_align + 8*sizeof(size_t) - 1
+                       & -4*sizeof(size_t);
+       }
        p->dev = st.st_dev;
        p->ino = st.st_ino;
        p->refcnt = 1;
@@ -571,7 +583,7 @@ static void free_all(struct dso *p)
        struct dso *n;
        while (p) {
                n = p->next;
-               if (p->map) free(p);
+               if (p->map && p!=libc && p!=head) free(p);
                p = n;
        }
 }
@@ -584,6 +596,22 @@ static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
        return 0;
 }
 
+static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
+{
+       size_t min_addr = -1, max_addr = 0;
+       for (; cnt--; ph = (void *)((char *)ph + stride)) {
+               if (ph->p_type != PT_LOAD) continue;
+               if (ph->p_vaddr < min_addr)
+                       min_addr = ph->p_vaddr;
+               if (ph->p_vaddr+ph->p_memsz > max_addr)
+                       max_addr = ph->p_vaddr+ph->p_memsz;
+       }
+       min_addr &= -PAGE_SIZE;
+       max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
+       p->map = p->base + min_addr;
+       p->map_len = max_addr - min_addr;
+}
+
 static void do_init_fini(struct dso *p)
 {
        size_t dyn[DYN_CNT] = {0};
@@ -602,6 +630,35 @@ void _dl_debug_state(void)
 {
 }
 
+void *__copy_tls(unsigned char *mem, size_t cnt)
+{
+       struct dso *p;
+       void **dtv = (void *)mem;
+       dtv[0] = (void *)cnt;
+       mem = (void *)(dtv + cnt + 1);
+       for (p=tail; p; p=p->prev) {
+               if (p->tls_id-1 >= cnt) continue;
+               mem += -p->tls_len & (4*sizeof(size_t)-1);
+               mem += ((uintptr_t)p->tls_image - (uintptr_t)mem)
+                       & (p->tls_align-1);
+               dtv[p->tls_id] = mem;
+               memcpy(mem, p->tls_image, p->tls_len);
+               mem += p->tls_size;
+       }
+       ((pthread_t)mem)->dtv = dtv;
+       return mem;
+}
+
+void *__tls_get_addr(size_t *p)
+{
+       pthread_t self = __pthread_self();
+       if ((size_t)self->dtv[0] < p[0]) {
+               // FIXME: obtain new DTV and TLS from the DSO
+               a_crash();
+       }
+       return (char *)self->dtv[p[0]] + p[1];
+}
+
 void *__dynlink(int argc, char **argv)
 {
        size_t aux[AUX_CNT] = {0};
@@ -647,6 +704,8 @@ void *__dynlink(int argc, char **argv)
        lib->name = lib->shortname = "libc.so";
        lib->global = 1;
        ehdr = (void *)lib->base;
+       find_map_range((void *)(aux[AT_BASE]+ehdr->e_phoff),
+               ehdr->e_phnum, ehdr->e_phentsize, lib);
        lib->dynv = (void *)(lib->base + find_dyn(
                (void *)(aux[AT_BASE]+ehdr->e_phoff),
                ehdr->e_phnum, ehdr->e_phentsize));
@@ -654,6 +713,7 @@ void *__dynlink(int argc, char **argv)
 
        if (aux[AT_PHDR]) {
                size_t interp_off = 0;
+               size_t tls_image = 0;
                /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
                phdr = (void *)aux[AT_PHDR];
                for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
@@ -661,11 +721,20 @@ void *__dynlink(int argc, char **argv)
                                app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
                        else if (phdr->p_type == PT_INTERP)
                                interp_off = (size_t)phdr->p_vaddr;
+                       else if (phdr->p_type == PT_TLS) {
+                               tls_image = phdr->p_vaddr;
+                               app->tls_len = phdr->p_filesz;
+                               app->tls_size = phdr->p_memsz;
+                               app->tls_align = phdr->p_align;
+                       }
                }
+               if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
                if (interp_off) lib->name = (char *)app->base + interp_off;
                app->name = argv[0];
                app->dynv = (void *)(app->base + find_dyn(
                        (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
+               find_map_range((void *)aux[AT_PHDR],
+                       aux[AT_PHNUM], aux[AT_PHENT], app);
        } else {
                int fd;
                char *ldname = argv[0];
@@ -685,7 +754,7 @@ void *__dynlink(int argc, char **argv)
                        _exit(1);
                }
                runtime = 1;
-               ehdr = (void *)map_library(fd, &app->map_len, &app->base, &dyno);
+               ehdr = (void *)map_library(fd, app);
                if (!ehdr) {
                        dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
                        _exit(1);
@@ -694,9 +763,13 @@ void *__dynlink(int argc, char **argv)
                close(fd);
                lib->name = ldname;
                app->name = argv[0];
-               app->dynv = (void *)(app->base + dyno);
                aux[AT_ENTRY] = ehdr->e_entry;
        }
+       if (app->tls_size) {
+               app->tls_id = ++tls_cnt;
+               tls_size += app->tls_size+app->tls_align + 8*sizeof(size_t)-1
+                       & -4*sizeof(size_t);
+       }
        app->global = 1;
        app->constructed = 1;
        decode_dyn(app);
@@ -767,16 +840,23 @@ void *__dynlink(int argc, char **argv)
        debug.state = 0;
        _dl_debug_state();
 
+       tls_size += sizeof(struct pthread) + 4*sizeof(size_t);
+       __libc.tls_size = tls_size;
+       __libc.tls_cnt = tls_cnt;
+       if (tls_cnt) {
+               void *mem = mmap(0, __libc.tls_size, PROT_READ|PROT_WRITE,
+                       MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+               if (mem==MAP_FAILED ||
+                   !__install_initial_tls(__copy_tls(mem, tls_cnt))) {
+                       dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
+                               argv[0], tls_size);
+                       _exit(127);
+               }
+       }
        if (ssp_used) __init_ssp(auxv);
 
        do_init_fini(tail);
 
-       if (!rtld_used) {
-               free_all(head);
-               free(sys_path);
-               reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
-       }
-
        errno = 0;
        return (void *)aux[AT_ENTRY];
 }
@@ -891,7 +971,68 @@ failed:
        return 0;
 }
 
-void *__dlsym(void *p, const char *s, void *ra)
+int __dladdr(void *addr, Dl_info *info)
+{
+       struct dso *p;
+       Sym *sym;
+       uint32_t nsym;
+       char *strings;
+       size_t i;
+       void *best = 0;
+       char *bestname;
+
+       pthread_rwlock_rdlock(&lock);
+       for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
+       pthread_rwlock_unlock(&lock);
+
+       if (!p) return 0;
+
+       sym = p->syms;
+       strings = p->strings;
+       if (p->hashtab) {
+               nsym = p->hashtab[1];
+       } else {
+               uint32_t *buckets;
+               uint32_t *hashval;
+               buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
+               sym += p->ghashtab[1];
+               for (i = 0; i < p->ghashtab[0]; i++) {
+                       if (buckets[i] > nsym)
+                               nsym = buckets[i];
+               }
+               if (nsym) {
+                       nsym -= p->ghashtab[1];
+                       hashval = buckets + p->ghashtab[0] + nsym;
+                       do nsym++;
+                       while (!(*hashval++ & 1));
+               }
+       }
+
+       for (; nsym; nsym--, sym++) {
+               if (sym->st_shndx && sym->st_value
+                && (1<<(sym->st_info&0xf) & OK_TYPES)
+                && (1<<(sym->st_info>>4) & OK_BINDS)) {
+                       void *symaddr = p->base + sym->st_value;
+                       if (symaddr > addr || symaddr < best)
+                               continue;
+                       best = symaddr;
+                       bestname = strings + sym->st_name;
+                       if (addr == symaddr)
+                               break;
+               }
+       }
+
+       if (!best) return 0;
+
+       info->dli_fname = p->name;
+       info->dli_fbase = p->base;
+       info->dli_sname = bestname;
+       info->dli_saddr = best;
+
+       return 1;
+}
+
+void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
 {
        void *res;
        pthread_rwlock_rdlock(&lock);
@@ -904,7 +1045,11 @@ void *dlopen(const char *file, int mode)
 {
        return 0;
 }
-void *__dlsym(void *p, const char *s, void *ra)
+void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
+{
+       return 0;
+}
+int __dladdr (void *addr, Dl_info *info)
 {
        return 0;
 }