]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blobdiff - linker/linker.cpp
Merge "Reinstate the x86 dynamic linker warning for text relocations."
[android-sdk/platform-bionic.git] / linker / linker.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 08231ea..4588948
@@ -34,7 +34,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/atomics.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -48,6 +47,7 @@
 #include "linker_debug.h"
 #include "linker_environ.h"
 #include "linker_phdr.h"
+#include "linker_allocator.h"
 
 /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
  *
  *   and NOEXEC
  */
 
+#if defined(__LP64__)
+#define SEARCH_NAME(x) x
+#else
+// Nvidia drivers are relying on the bug:
+// http://code.google.com/p/android/issues/detail?id=6670
+// so we continue to use base-name lookup for lp32
+static const char* get_base_name(const char* name) {
+  const char* bname = strrchr(name, '/');
+  return bname ? bname + 1 : name;
+}
+#define SEARCH_NAME(x) get_base_name(x)
+#endif
+
 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, and they're all threaded together on a free list.
-#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
-struct soinfo_pool_t {
-  soinfo_pool_t* next;
-  soinfo info[SOINFO_PER_POOL];
-};
-static struct soinfo_pool_t* gSoInfoPools = NULL;
-static soinfo* gSoInfoFreeList = NULL;
+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 gDefaultLdPaths[] = {
+static const char* const kDefaultLdPaths[] = {
 #if defined(__LP64__)
   "/vendor/lib64",
   "/system/lib64",
@@ -100,17 +105,17 @@ static const char* const gDefaultLdPaths[] = {
 #define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
 #define LDPRELOAD_MAX 8
 
-static char gLdPathsBuffer[LDPATH_BUFSIZE];
-static const char* gLdPaths[LDPATH_MAX + 1];
+static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
+static const char* g_ld_library_paths[LDPATH_MAX + 1];
 
-static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
-static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
+static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
+static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
 
-static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
+static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
 
-__LIBC_HIDDEN__ int gLdDebugVerbosity;
+__LIBC_HIDDEN__ int g_ld_debug_verbosity;
 
-__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
+__LIBC_HIDDEN__ abort_msg_t* g_abort_message = NULL; // For debuggerd.
 
 enum RelocationKind {
     kRelocAbsolute = 0,
@@ -185,11 +190,10 @@ size_t linker_get_error_buffer_size() {
  */
 extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
 
+static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
 static r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
 static link_map* r_debug_tail = 0;
 
-static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
-
 static void insert_soinfo_into_debug_map(soinfo* info) {
     // Copy the necessary fields into the debug structure.
     link_map* map = &(info->link_map_head);
@@ -235,7 +239,7 @@ static void notify_gdb_of_load(soinfo* info) {
         return;
     }
 
-    ScopedPthreadMutexLocker locker(&gDebugMutex);
+    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
 
     _r_debug.r_state = r_debug::RT_ADD;
     rtld_db_dlactivity();
@@ -252,7 +256,7 @@ static void notify_gdb_of_unload(soinfo* info) {
         return;
     }
 
-    ScopedPthreadMutexLocker locker(&gDebugMutex);
+    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
 
     _r_debug.r_state = r_debug::RT_DELETE;
     rtld_db_dlactivity();
@@ -270,60 +274,37 @@ void notify_gdb_of_libraries() {
   rtld_db_dlactivity();
 }
 
-static bool ensure_free_list_non_empty() {
-  if (gSoInfoFreeList != NULL) {
-    return true;
-  }
-
-  // Allocate a new pool.
-  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
-                                                              PROT_READ|PROT_WRITE,
-                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
-  if (pool == MAP_FAILED) {
-    return false;
-  }
-
-  // Add the pool to our list of pools.
-  pool->next = gSoInfoPools;
-  gSoInfoPools = pool;
-
-  // Chain the entries in the new pool onto the free list.
-  gSoInfoFreeList = &pool->info[0];
-  soinfo* next = NULL;
-  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
-    pool->info[i].next = next;
-    next = &pool->info[i];
-  }
+LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
+  return g_soinfo_links_allocator.alloc();
+}
 
-  return true;
+void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
+  g_soinfo_links_allocator.free(entry);
 }
 
-static void set_soinfo_pool_protection(int protection) {
-  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
-    if (mprotect(p, sizeof(*p), protection) == -1) {
-      abort(); // Can't happen.
-    }
-  }
+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) {
+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;
   }
 
-  if (!ensure_free_list_non_empty()) {
-    DL_ERR("out of memory when loading \"%s\"", name);
-    return NULL;
-  }
-
-  // Take the head element off the free list.
-  soinfo* si = gSoInfoFreeList;
-  gSoInfoFreeList = gSoInfoFreeList->next;
+  soinfo* si = g_soinfo_allocator.alloc();
 
   // 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;
 
@@ -336,6 +317,10 @@ static void soinfo_free(soinfo* si) {
         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);
@@ -351,6 +336,9 @@ static void soinfo_free(soinfo* si) {
         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.
     */
@@ -358,8 +346,8 @@ static void soinfo_free(soinfo* si) {
     if (si == sonext) {
         sonext = prev;
     }
-    si->next = gSoInfoFreeList;
-    gSoInfoFreeList = si;
+
+    g_soinfo_allocator.free(si);
 }
 
 
@@ -389,14 +377,14 @@ static void parse_path(const char* path, const char* delimiters,
 }
 
 static void parse_LD_LIBRARY_PATH(const char* path) {
-  parse_path(path, ":", gLdPaths,
-             gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
+  parse_path(path, ":", g_ld_library_paths,
+             g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
 }
 
 static void parse_LD_PRELOAD(const char* path) {
   // We have historically supported ':' as well as ' ' in LD_PRELOAD.
-  parse_path(path, " :", gLdPreloadNames,
-             gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
+  parse_path(path, " :", g_ld_preload_names,
+             g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
 }
 
 #if defined(__arm__)
@@ -554,10 +542,10 @@ static ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, s
     }
 
     /* Next, look for it in the preloads list */
-    for (int i = 0; gLdPreloads[i] != NULL; i++) {
-        s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
+    for (int i = 0; g_ld_preloads[i] != NULL; i++) {
+        s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
         if (s != NULL) {
-            *lsi = gLdPreloads[i];
+            *lsi = g_ld_preloads[i];
             goto done;
         }
     }
@@ -686,14 +674,14 @@ static int open_library(const char* name) {
   }
 
   // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
-  int fd = open_library_on_path(name, gLdPaths);
+  int fd = open_library_on_path(name, g_ld_library_paths);
   if (fd == -1) {
-    fd = open_library_on_path(name, gDefaultLdPaths);
+    fd = open_library_on_path(name, kDefaultLdPaths);
   }
   return fd;
 }
 
-static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) {
+static soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
     // Open the file.
     int fd = open_library(name);
     if (fd == -1) {
@@ -701,111 +689,136 @@ static soinfo* load_library(const char* name, const android_dlextinfo* extinfo)
         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;
+      }
+    }
+
+    if ((dlflags & RTLD_NOLOAD) != 0) {
+      return NULL;
+    }
+
+    // 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(SEARCH_NAME(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();
-    return si;
-}
 
-static soinfo *find_loaded_library(const char* name) {
-    // TODO: don't use basename only for determining libraries
-    // http://code.google.com/p/android/issues/detail?id=6670
+    // 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);
 
-    const char* bname = strrchr(name, '/');
-    bname = bname ? bname + 1 : name;
+    if (!soinfo_link_image(si, extinfo)) {
+      soinfo_free(si);
+      return NULL;
+    }
 
-    for (soinfo* si = solist; si != NULL; si = si->next) {
-        if (!strcmp(bname, si->name)) {
-            return si;
-        }
+    return si;
+}
+
+static soinfo *find_loaded_library_by_name(const char* name) {
+  const char* search_name = SEARCH_NAME(name);
+  for (soinfo* si = solist; si != NULL; si = si->next) {
+    if (!strcmp(search_name, si->name)) {
+      return si;
     }
-    return NULL;
+  }
+  return NULL;
 }
 
-static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) {
+static soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) {
   if (name == NULL) {
     return somain;
   }
 
-  soinfo* si = find_loaded_library(name);
-  if (si != NULL) {
-    if (si->flags & FLAG_LINKED) {
-      return si;
-    }
-    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
-    return NULL;
-  }
+  soinfo* si = find_loaded_library_by_name(name);
 
-  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
-  si = load_library(name, extinfo);
+  // Library might still be loaded, the accurate detection
+  // of this fact is done by load_library
   if (si == NULL) {
-    return NULL;
+    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
+    si = load_library(name, dlflags, extinfo);
   }
 
-  // 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);
+  if (si != NULL && (si->flags & FLAG_LINKED) == 0) {
+    DL_ERR("recursive link to \"%s\"", si->name);
     return NULL;
   }
 
   return si;
 }
 
-static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
-  soinfo* si = find_library_internal(name, extinfo);
+static soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
+  soinfo* si = find_library_internal(name, dlflags, extinfo);
   if (si != NULL) {
     si->ref_count++;
   }
   return si;
 }
 
-static int soinfo_unload(soinfo* si) {
+static void soinfo_unload(soinfo* si) {
   if (si->ref_count == 1) {
     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* needed = find_library(library_name, RTLD_NOLOAD, NULL);
+          if (needed != NULL) {
+            soinfo_unload(needed);
+          } else {
+            // Not found: for example if symlink was deleted between dlopen and dlclose
+            // Since we cannot really handle errors at this point - print and continue.
+            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
+          }
+        }
       }
     }
 
-    munmap(reinterpret_cast<void*>(si->base), si->size);
     notify_gdb_of_unload(si);
-    soinfo_free(si);
     si->ref_count = 0;
+    soinfo_free(si);
   } else {
     si->ref_count--;
     TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
   }
-  return 0;
 }
 
 void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
-  snprintf(buffer, buffer_size, "%s:%s", gDefaultLdPaths[0], gDefaultLdPaths[1]);
+  snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]);
 }
 
 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
@@ -815,7 +828,7 @@ void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
 }
 
 soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
-  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
+  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) {
     DL_ERR("invalid flags to dlopen: %x", flags);
     return NULL;
   }
@@ -823,20 +836,19 @@ soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo)
     DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
     return NULL;
   }
-  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
-  soinfo* si = find_library(name, extinfo);
+  protect_data(PROT_READ | PROT_WRITE);
+  soinfo* si = find_library(name, flags, extinfo);
   if (si != NULL) {
     si->CallConstructors();
   }
-  set_soinfo_pool_protection(PROT_READ);
+  protect_data(PROT_READ);
   return si;
 }
 
-int do_dlclose(soinfo* si) {
-  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
-  int result = soinfo_unload(si);
-  set_soinfo_pool_protection(PROT_READ);
-  return result;
+void do_dlclose(soinfo* si) {
+  protect_data(PROT_READ | PROT_WRITE);
+  soinfo_unload(si);
+  protect_data(PROT_READ);
 }
 
 #if defined(USE_RELA)
@@ -896,6 +908,7 @@ static int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo*
         case R_X86_64_JUMP_SLOT:
         case R_X86_64_GLOB_DAT:
         case R_X86_64_32:
+        case R_X86_64_64:
         case R_X86_64_RELATIVE:
           // No need to do anything.
           break;
@@ -1449,7 +1462,7 @@ void soinfo::CallFunction(const char* function_name __unused, linker_function_t
 
   // 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).
-  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
 }
 
 void soinfo::CallPreInitConstructors() {
@@ -1481,15 +1494,9 @@ void soinfo::CallConstructors() {
           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);
 
@@ -1506,6 +1513,86 @@ void soinfo::CallDestructors() {
 
   // DT_FINI should be called after DT_FINI_ARRAY if both are present.
   CallFunction("DT_FINI", fini_func);
+
+  // This is needed on second call to dlopen
+  // after library has been unloaded with RTLD_NODELETE
+  constructors_called = false;
+}
+
+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
@@ -1803,16 +1890,16 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
 
     // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
     if (si->flags & FLAG_EXE) {
-        memset(gLdPreloads, 0, sizeof(gLdPreloads));
+        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
         size_t preload_count = 0;
-        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
-            soinfo* lsi = find_library(gLdPreloadNames[i], NULL);
+        for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) {
+            soinfo* lsi = find_library(g_ld_preload_names[i], 0, NULL);
             if (lsi != NULL) {
-                gLdPreloads[preload_count++] = lsi;
+                g_ld_preloads[preload_count++] = lsi;
             } else {
                 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
                 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
-                        gLdPreloadNames[i], si->name, linker_get_error_buffer());
+                        g_ld_preload_names[i], si->name, linker_get_error_buffer());
             }
         }
     }
@@ -1824,13 +1911,15 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
         if (d->d_tag == DT_NEEDED) {
             const char* library_name = si->strtab + d->d_un.d_val;
             DEBUG("%s needs %s", si->name, library_name);
-            soinfo* lsi = find_library(library_name, NULL);
+            soinfo* lsi = find_library(library_name, 0, NULL);
             if (lsi == NULL) {
                 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
                 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
                        library_name, si->name, tmp_err_buf);
                 return false;
             }
+
+            si->add_child(lsi);
             *pneeded++ = lsi;
         }
     }
@@ -1840,10 +1929,8 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
     if (si->has_text_relocations) {
         // Make segments writable to allow text relocations to work properly. We will later call
         // phdr_table_protect_segments() after all of them are applied and all constructors are run.
-#if !defined(__i386__) // The platform itself has too many text relocations on x86.
         DL_WARN("%s has text relocations. This is wasting memory and prevents "
                 "security hardening. Please fix.", si->name);
-#endif
         if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
             DL_ERR("can't unprotect loadable segments for \"%s\": %s",
                    si->name, strerror(errno));
@@ -1940,19 +2027,52 @@ static void add_vdso(KernelArgumentBlock& args __unused) {
     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);
 #endif
 }
 
+/*
+ * 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
@@ -1988,7 +2108,7 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
     // Get a few environment variables.
     const char* LD_DEBUG = linker_env_get("LD_DEBUG");
     if (LD_DEBUG != NULL) {
-      gLdDebugVerbosity = atoi(LD_DEBUG);
+      g_ld_debug_verbosity = atoi(LD_DEBUG);
     }
 
     // Normally, these are cleaned by linker_env_init, but the test
@@ -2000,9 +2120,15 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
       ldpreload_env = linker_env_get("LD_PRELOAD");
     }
 
+    // Linker does not call constructors for its own
+    // global variables so we need to initialize
+    // 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);
     }
@@ -2019,35 +2145,7 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
     _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 +2169,14 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
     si->dynamic = NULL;
     si->ref_count = 1;
 
+#if defined(__LP64__)
+    ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
+    if (elf_hdr->e_type != ET_DYN) {
+        __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
+        exit(EXIT_FAILURE);
+    }
+#endif
+
     // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
     parse_LD_LIBRARY_PATH(ldpath_env);
     parse_LD_PRELOAD(ldpreload_env);
@@ -2086,8 +2192,8 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(
 
     si->CallPreInitConstructors();
 
-    for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
-        gLdPreloads[i]->CallConstructors();
+    for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) {
+        g_ld_preloads[i]->CallConstructors();
     }
 
     /* After the link_image, the si->load_bias is initialized.
@@ -2176,6 +2282,10 @@ static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
  * 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);
@@ -2208,10 +2318,10 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) {
 
   // We have successfully fixed our own relocations. It's safe to run
   // the main part of the linker now.
-  args.abort_message_ptr = &gAbortMessage;
+  args.abort_message_ptr = &g_abort_message;
   ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
 
-  set_soinfo_pool_protection(PROT_READ);
+  protect_data(PROT_READ);
 
   // Return the address that the calling assembly stub should jump to.
   return start_address;