Improve detection of already loaded libraries

Linker is now able to resolve symlinked libraries correctly.

soinfo is extended to save the graph of dependencies during
load/unload. Dependencies are used only in CallConstructor.

Bug: 9741592
Change-Id: Id9c48a74c46aa89bcdf3d54ec2f8ba3d398130b1
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 86204de..c5006e0 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -68,13 +68,11 @@
 static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
 static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
 
-// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
-// maps, each a single page in size. The pages are broken up into as many struct soinfo
-// objects as will fit.
 static LinkerAllocator<soinfo> g_soinfo_allocator;
+static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
 
-static soinfo* solist = &libdl_info;
-static soinfo* sonext = &libdl_info;
+static soinfo* solist;
+static soinfo* sonext;
 static soinfo* somain; /* main process, always the one after libdl_info */
 
 static const char* const kDefaultLdPaths[] = {
@@ -263,7 +261,20 @@
   rtld_db_dlactivity();
 }
 
-static soinfo* soinfo_alloc(const char* name) {
+LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
+  return g_soinfo_links_allocator.alloc();
+}
+
+void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
+  g_soinfo_links_allocator.free(entry);
+}
+
+static void protect_data(int protection) {
+  g_soinfo_allocator.protect_all(protection);
+  g_soinfo_links_allocator.protect_all(protection);
+}
+
+static soinfo* soinfo_alloc(const char* name, struct stat* file_stat) {
   if (strlen(name) >= SOINFO_NAME_LEN) {
     DL_ERR("library name \"%s\" too long", name);
     return NULL;
@@ -274,6 +285,13 @@
   // Initialize the new element.
   memset(si, 0, sizeof(soinfo));
   strlcpy(si->name, name, sizeof(si->name));
+  si->flags = FLAG_NEW_SOINFO;
+
+  if (file_stat != NULL) {
+    si->set_st_dev(file_stat->st_dev);
+    si->set_st_ino(file_stat->st_ino);
+  }
+
   sonext->next = si;
   sonext = si;
 
@@ -286,6 +304,10 @@
         return;
     }
 
+    if (si->base != 0 && si->size != 0) {
+      munmap(reinterpret_cast<void*>(si->base), si->size);
+    }
+
     soinfo *prev = NULL, *trav;
 
     TRACE("name %s: freeing soinfo @ %p", si->name, si);
@@ -301,6 +323,9 @@
         return;
     }
 
+    // clear links to/from si
+    si->remove_all_links();
+
     /* prev will never be NULL, because the first entry in solist is
        always the static libdl_info.
     */
@@ -651,25 +676,52 @@
         return NULL;
     }
 
-    // Read the ELF header and load the segments.
     ElfReader elf_reader(name, fd);
+
+    struct stat file_stat;
+    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
+      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
+      return NULL;
+    }
+
+    // Check for symlink and other situations where
+    // file can have different names.
+    for (soinfo* si = solist; si != NULL; si = si->next) {
+      if (si->get_st_dev() != 0 &&
+          si->get_st_ino() != 0 &&
+          si->get_st_dev() == file_stat.st_dev &&
+          si->get_st_ino() == file_stat.st_ino) {
+        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
+        return si;
+      }
+    }
+
+    // Read the ELF header and load the segments.
     if (!elf_reader.Load(extinfo)) {
         return NULL;
     }
 
     const char* bname = strrchr(name, '/');
-    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
+    soinfo* si = soinfo_alloc(bname ? bname + 1 : name, &file_stat);
     if (si == NULL) {
         return NULL;
     }
     si->base = elf_reader.load_start();
     si->size = elf_reader.load_size();
     si->load_bias = elf_reader.load_bias();
-    si->flags = 0;
-    si->entry = 0;
-    si->dynamic = NULL;
     si->phnum = elf_reader.phdr_count();
     si->phdr = elf_reader.loaded_phdr();
+
+    // At this point we know that whatever is loaded @ base is a valid ELF
+    // shared library whose segments are properly mapped in.
+    TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
+          reinterpret_cast<void*>(si->base), si->size, si->name);
+
+    if (!soinfo_link_image(si, extinfo)) {
+      soinfo_free(si);
+      return NULL;
+    }
+
     return si;
 }
 
@@ -703,23 +755,7 @@
   }
 
   TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
-  si = load_library(name, extinfo);
-  if (si == NULL) {
-    return NULL;
-  }
-
-  // At this point we know that whatever is loaded @ base is a valid ELF
-  // shared library whose segments are properly mapped in.
-  TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
-        reinterpret_cast<void*>(si->base), si->size, si->name);
-
-  if (!soinfo_link_image(si, extinfo)) {
-    munmap(reinterpret_cast<void*>(si->base), si->size);
-    soinfo_free(si);
-    return NULL;
-  }
-
-  return si;
+  return load_library(name, extinfo);
 }
 
 static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
@@ -735,15 +771,21 @@
     TRACE("unloading '%s'", si->name);
     si->CallDestructors();
 
-    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
-      if (d->d_tag == DT_NEEDED) {
-        const char* library_name = si->strtab + d->d_un.d_val;
-        TRACE("%s needs to unload %s", si->name, library_name);
-        soinfo_unload(find_loaded_library(library_name));
+    if ((si->flags | FLAG_NEW_SOINFO) != 0) {
+      si->get_children().for_each([&] (soinfo* child) {
+        TRACE("%s needs to unload %s", si->name, child->name);
+        soinfo_unload(child);
+      });
+    } else {
+      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
+        if (d->d_tag == DT_NEEDED) {
+          const char* library_name = si->strtab + d->d_un.d_val;
+          TRACE("%s needs to unload %s", si->name, library_name);
+          soinfo_unload(find_loaded_library(library_name));
+        }
       }
     }
 
-    munmap(reinterpret_cast<void*>(si->base), si->size);
     notify_gdb_of_unload(si);
     si->ref_count = 0;
     soinfo_free(si);
@@ -773,19 +815,20 @@
     DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
     return NULL;
   }
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
   soinfo* si = find_library(name, extinfo);
   if (si != NULL) {
     si->CallConstructors();
+    somain->add_child(si);
   }
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
   return si;
 }
 
 int do_dlclose(soinfo* si) {
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
   int result = soinfo_unload(si);
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
   return result;
 }
 
@@ -1333,7 +1376,7 @@
 
   // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
   // are still writable. This happens with our debug malloc (see http://b/7941716).
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
 }
 
 void soinfo::CallPreInitConstructors() {
@@ -1365,15 +1408,9 @@
           name, preinit_array_count);
   }
 
-  if (dynamic != NULL) {
-    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
-      if (d->d_tag == DT_NEEDED) {
-        const char* library_name = strtab + d->d_un.d_val;
-        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
-        find_loaded_library(library_name)->CallConstructors();
-      }
-    }
-  }
+  get_children().for_each([] (soinfo* si) {
+    si->CallConstructors();
+  });
 
   TRACE("\"%s\": calling constructors", name);
 
@@ -1392,6 +1429,82 @@
   CallFunction("DT_FINI", fini_func);
 }
 
+void soinfo::add_child(soinfo* child) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  this->children.push_front(child);
+  child->parents.push_front(this);
+}
+
+void soinfo::remove_all_links() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  // 1. Untie connected soinfos from 'this'.
+  children.for_each([&] (soinfo* child) {
+    child->parents.remove_if([&] (const soinfo* parent) {
+      return parent == this;
+    });
+  });
+
+  parents.for_each([&] (soinfo* parent) {
+    parent->children.for_each([&] (const soinfo* child) {
+      return child == this;
+    });
+  });
+
+  // 2. Once everything untied - clear local lists.
+  parents.clear();
+  children.clear();
+}
+
+void soinfo::set_st_dev(dev_t dev) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  st_dev = dev;
+}
+
+void soinfo::set_st_ino(ino_t ino) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  st_ino = ino;
+}
+
+dev_t soinfo::get_st_dev() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return 0;
+  }
+
+  return st_dev;
+};
+
+ino_t soinfo::get_st_ino() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return 0;
+  }
+
+  return st_ino;
+}
+
+// This is a return on get_children() in case
+// 'this->flags' does not have FLAG_NEW_SOINFO set.
+static soinfo::soinfo_list_t g_empty_list;
+
+soinfo::soinfo_list_t& soinfo::get_children() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return g_empty_list;
+  }
+
+  return this->children;
+}
+
 /* Force any of the closed stdin, stdout and stderr to be associated with
    /dev/null. */
 static int nullify_closed_stdio() {
@@ -1715,6 +1828,8 @@
                        library_name, si->name, tmp_err_buf);
                 return false;
             }
+
+            si->add_child(lsi);
             *pneeded++ = lsi;
         }
     }
@@ -1824,13 +1939,12 @@
     return;
   }
 
-  soinfo* si = soinfo_alloc("[vdso]");
+  soinfo* si = soinfo_alloc("[vdso]", NULL);
 
   si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
   si->phnum = ehdr_vdso->e_phnum;
   si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
   si->size = phdr_table_get_load_size(si->phdr, si->phnum);
-  si->flags = 0;
   si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 
   soinfo_link_image(si, NULL);
@@ -1838,6 +1952,40 @@
 }
 
 /*
+ * This is linker soinfo for GDB. See details below.
+ */
+static soinfo linker_soinfo_for_gdb;
+
+/* gdb expects the linker to be in the debug shared object list.
+ * Without this, gdb has trouble locating the linker's ".text"
+ * and ".plt" sections. Gdb could also potentially use this to
+ * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
+ * Don't use soinfo_alloc(), because the linker shouldn't
+ * be on the soinfo list.
+ */
+static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
+#if defined(__LP64__)
+  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name));
+#else
+  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name));
+#endif
+  linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO;
+  linker_soinfo_for_gdb.base = linker_base;
+
+  /*
+   * Set the dynamic field in the link map otherwise gdb will complain with
+   * the following:
+   *   warning: .dynamic section for "/system/bin/linker" is not at the
+   *   expected address (wrong library or version mismatch?)
+   */
+  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
+  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
+  phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
+                                 &linker_soinfo_for_gdb.dynamic, NULL, NULL);
+  insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
+}
+
+/*
  * This code is called after the linker has linked itself and
  * fixed it's own GOT. It is safe to make references to externs
  * and other non-local data at this point.
@@ -1886,12 +2034,13 @@
 
     // Linker does not call constructors for its own
     // global variables so we need to initialize
-    // the allocator explicitly.
+    // the allocators explicitly.
     g_soinfo_allocator.init();
+    g_soinfo_links_allocator.init();
 
     INFO("[ android linker & debugger ]");
 
-    soinfo* si = soinfo_alloc(args.argv[0]);
+    soinfo* si = soinfo_alloc(args.argv[0], NULL);
     if (si == NULL) {
         exit(EXIT_FAILURE);
     }
@@ -1908,35 +2057,7 @@
     _r_debug.r_map = map;
     r_debug_tail = map;
 
-    /* gdb expects the linker to be in the debug shared object list.
-     * Without this, gdb has trouble locating the linker's ".text"
-     * and ".plt" sections. Gdb could also potentially use this to
-     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
-     * Don't use soinfo_alloc(), because the linker shouldn't
-     * be on the soinfo list.
-     */
-    {
-        static soinfo linker_soinfo;
-#if defined(__LP64__)
-        strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
-#else
-        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
-#endif
-        linker_soinfo.flags = 0;
-        linker_soinfo.base = linker_base;
-
-        /*
-         * Set the dynamic field in the link map otherwise gdb will complain with
-         * the following:
-         *   warning: .dynamic section for "/system/bin/linker" is not at the
-         *   expected address (wrong library or version mismatch?)
-         */
-        ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
-        ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
-        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
-                                       &linker_soinfo.dynamic, NULL, NULL);
-        insert_soinfo_into_debug_map(&linker_soinfo);
-    }
+    init_linker_info_for_gdb(linker_base);
 
     // Extract information passed from the kernel.
     si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
@@ -2071,6 +2192,10 @@
  * function, or other GOT reference will generate a segfault.
  */
 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
+  // Initialize static variables.
+  solist = get_libdl_info();
+  sonext = get_libdl_info();
+
   KernelArgumentBlock args(raw_args);
 
   ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
@@ -2106,7 +2231,7 @@
   args.abort_message_ptr = &g_abort_message;
   ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
 
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
 
   // Return the address that the calling assembly stub should jump to.
   return start_address;