early stage ldso: remove symbolic references via error handling function
[musl] / ldso / dynlink.c
index f9ac010..cc67795 100644 (file)
@@ -1,6 +1,5 @@
 #define _GNU_SOURCE
 #define SYSCALL_NO_TLS 1
-#include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <semaphore.h>
 #include <sys/membarrier.h>
 #include "pthread_impl.h"
+#include "fork_impl.h"
 #include "libc.h"
 #include "dynlink.h"
 
-static void error(const char *, ...);
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc __libc_realloc
+#define free __libc_free
+
+static void error_impl(const char *, ...);
+static void error_noop(const char *, ...);
+static void (*error)(const char *, ...) = error_noop;
 
 #define MAXP2(a,b) (-(-(a)&-(b)))
 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
@@ -556,10 +563,25 @@ static void reclaim_gaps(struct dso *dso)
        }
 }
 
+static ssize_t read_loop(int fd, void *p, size_t n)
+{
+       for (size_t i=0; i<n; ) {
+               ssize_t l = read(fd, (char *)p+i, n-i);
+               if (l<0) {
+                       if (errno==EINTR) continue;
+                       else return -1;
+               }
+               if (l==0) return i;
+               i += l;
+       }
+       return n;
+}
+
 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
 {
        static int no_map_fixed;
        char *q;
+       if (!n) return p;
        if (!no_map_fixed) {
                q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
                if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
@@ -1060,13 +1082,17 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
                                snprintf(etc_ldso_path, sizeof etc_ldso_path,
                                        "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
                                        (int)prefix_len, prefix);
-                               FILE *f = fopen(etc_ldso_path, "rbe");
-                               if (f) {
-                                       if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
+                               fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC);
+                               if (fd>=0) {
+                                       size_t n = 0;
+                                       if (!fstat(fd, &st)) n = st.st_size;
+                                       if ((sys_path = malloc(n+1)))
+                                               sys_path[n] = 0;
+                                       if (!sys_path || read_loop(fd, sys_path, n)<0) {
                                                free(sys_path);
                                                sys_path = "";
                                        }
-                                       fclose(f);
+                                       close(fd);
                                } else if (errno != ENOENT) {
                                        sys_path = "";
                                }
@@ -1332,12 +1358,14 @@ static void reloc_all(struct dso *p)
                do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
                do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
 
-               if (head != &ldso && p->relro_start != p->relro_end &&
-                   mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
-                   && errno != ENOSYS) {
-                       error("Error relocating %s: RELRO protection failed: %m",
-                               p->name);
-                       if (runtime) longjmp(*rtld_fail, 1);
+               if (head != &ldso && p->relro_start != p->relro_end) {
+                       long ret = __syscall(SYS_mprotect, laddr(p, p->relro_start),
+                               p->relro_end-p->relro_start, PROT_READ);
+                       if (ret != 0 && ret != -ENOSYS) {
+                               error("Error relocating %s: RELRO protection failed: %m",
+                                       p->name);
+                               if (runtime) longjmp(*rtld_fail, 1);
+                       }
                }
 
                p->relocated = 1;
@@ -1404,6 +1432,17 @@ void __libc_exit_fini()
        }
 }
 
+void __ldso_atfork(int who)
+{
+       if (who<0) {
+               pthread_rwlock_wrlock(&lock);
+               pthread_mutex_lock(&init_fini_lock);
+       } else {
+               pthread_mutex_unlock(&init_fini_lock);
+               pthread_rwlock_unlock(&lock);
+       }
+}
+
 static struct dso **queue_ctors(struct dso *dso)
 {
        size_t cnt, qpos, spos, i;
@@ -1462,6 +1501,13 @@ static struct dso **queue_ctors(struct dso *dso)
        }
        queue[qpos] = 0;
        for (i=0; i<qpos; i++) queue[i]->mark = 0;
+       for (i=0; i<qpos; i++)
+               if (queue[i]->ctor_visitor && queue[i]->ctor_visitor->tid < 0) {
+                       error("State of %s is inconsistent due to multithreaded fork\n",
+                               queue[i]->name);
+                       free(queue);
+                       if (runtime) longjmp(*rtld_fail, 1);
+               }
 
        return queue;
 }
@@ -1714,6 +1760,9 @@ void __dls3(size_t *sp, size_t *auxv)
                env_preload = getenv("LD_PRELOAD");
        }
 
+       /* Activate error handler function */
+       error = error_impl;
+
        /* If the main program was already loaded by the kernel,
         * AT_PHDR will point to some location other than the dynamic
         * linker's program headers. */
@@ -1789,7 +1838,7 @@ void __dls3(size_t *sp, size_t *auxv)
                        dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
                        _exit(1);
                }
-               Ehdr *ehdr = (void *)map_library(fd, &app);
+               Ehdr *ehdr = map_library(fd, &app);
                if (!ehdr) {
                        dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
                        _exit(1);
@@ -2289,7 +2338,8 @@ int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void
                info.dlpi_adds      = gencnt;
                info.dlpi_subs      = 0;
                info.dlpi_tls_modid = current->tls_id;
-               info.dlpi_tls_data  = current->tls.image;
+               info.dlpi_tls_data = !current->tls_id ? 0 :
+                       __tls_get_addr((tls_mod_off_t[]){current->tls_id,0});
 
                ret = (callback)(&info, sizeof (info), data);
 
@@ -2302,7 +2352,7 @@ int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void
        return ret;
 }
 
-static void error(const char *fmt, ...)
+static void error_impl(const char *fmt, ...)
 {
        va_list ap;
        va_start(ap, fmt);
@@ -2316,3 +2366,7 @@ static void error(const char *fmt, ...)
        __dl_vseterr(fmt, ap);
        va_end(ap);
 }
+
+static void error_noop(const char *fmt, ...)
+{
+}