remove unused refcnt field for shared libraries
[musl] / ldso / dynlink.c
index f8db1f8..178fe27 100644 (file)
@@ -52,7 +52,6 @@ struct dso {
        Phdr *phdr;
        int phnum;
        size_t phentsize;
-       int refcnt;
        Sym *syms;
        Elf_Symndx *hashtab;
        uint32_t *ghashtab;
@@ -971,7 +970,6 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
                /* Search for the name to see if it's already loaded */
                for (p=head->next; p; p=p->next) {
                        if (p->shortname && !strcmp(p->shortname, name)) {
-                               p->refcnt++;
                                return p;
                        }
                }
@@ -1034,7 +1032,6 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
                        if (!p->shortname && pathname != name)
                                p->shortname = strrchr(p->name, '/')+1;
                        close(fd);
-                       p->refcnt++;
                        return p;
                }
        }
@@ -1042,6 +1039,17 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
        close(fd);
        if (!map) return 0;
 
+       /* Avoid the danger of getting two versions of libc mapped into the
+        * same process when an absolute pathname was used. The symbols
+        * checked are chosen to catch both musl and glibc, and to avoid
+        * false positives from interposition-hack libraries. */
+       decode_dyn(&temp_dso);
+       if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
+           find_sym(&temp_dso, "stdin", 1).sym) {
+               unmap_library(&temp_dso);
+               return load_library("libc.so", needed_by);
+       }
+
        /* Allocate storage for the new DSO. When there is TLS, this
         * storage must include a reservation for all pre-existing
         * threads to obtain copies of both the new TLS, and an
@@ -1061,10 +1069,8 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
                return 0;
        }
        memcpy(p, &temp_dso, sizeof temp_dso);
-       decode_dyn(p);
        p->dev = st.st_dev;
        p->ino = st.st_ino;
-       p->refcnt = 1;
        p->needed_by = needed_by;
        p->name = p->buf;
        strcpy(p->name, pathname);