Expose libc_shared_globals to libc.so with symbol

Previously, the address of the global variable was communicated from the
dynamic linker to libc.so using a field of KernelArgumentBlock, which is
communicated using the TLS_SLOT_BIONIC_PREINIT slot.

As long as this function isn't called during relocations (i.e. while
executing an ifunc), it always return a non-NULL value. If it's called
before its PLT entry is relocated, I expect a crash.

I removed the __libc_init_shared_globals function. It's currently empty,
and I don't think there's one point in libc's initialization where
shared globals should be initialized.

Bug: http://b/25751302
Test: bionic unit tests
Change-Id: I614d25e7ef5e0d2ccc40d5c821dee10f1ec61c2e
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 45ae6ed..5ae7b9b 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -38,6 +38,7 @@
 #include <android/api-level.h>
 
 #include <bionic/pthread_internal.h>
+#include "private/bionic_globals.h"
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
 
@@ -86,6 +87,7 @@
                       const void* caller_addr) __LINKER_PUBLIC__;
 void __loader_add_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
 void __loader_remove_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
+libc_shared_globals* __loader_shared_globals() __LINKER_PUBLIC__;
 #if defined(__arm__)
 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) __LINKER_PUBLIC__;
 #endif
@@ -299,6 +301,10 @@
   decrement_dso_handle_reference_counter(dso_handle);
 }
 
+libc_shared_globals* __loader_shared_globals() {
+  return __libc_shared_globals();
+}
+
 static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
 static soinfo* __libdl_info = nullptr;
 
diff --git a/linker/ld_android.cpp b/linker/ld_android.cpp
index 0528cd8..152c2e2 100644
--- a/linker/ld_android.cpp
+++ b/linker/ld_android.cpp
@@ -53,6 +53,7 @@
 __strong_alias(__loader_dlvsym, __internal_linker_error);
 __strong_alias(__loader_add_thread_local_dtor, __internal_linker_error);
 __strong_alias(__loader_remove_thread_local_dtor, __internal_linker_error);
+__strong_alias(__loader_shared_globals, __internal_linker_error);
 #if defined(__arm__)
 __strong_alias(__loader_dl_unwind_find_exidx, __internal_linker_error);
 #endif
diff --git a/linker/linker.arm.map b/linker/linker.arm.map
index a58e7c8..be438ca 100644
--- a/linker/linker.arm.map
+++ b/linker/linker.arm.map
@@ -22,6 +22,7 @@
     __loader_dl_unwind_find_exidx;
     __loader_add_thread_local_dtor;
     __loader_remove_thread_local_dtor;
+    __loader_shared_globals;
     rtld_db_dlactivity;
   local:
     *;
diff --git a/linker/linker.generic.map b/linker/linker.generic.map
index 45bb0b5..f3c01c0 100644
--- a/linker/linker.generic.map
+++ b/linker/linker.generic.map
@@ -21,6 +21,7 @@
     __loader_android_get_exported_namespace;
     __loader_add_thread_local_dtor;
     __loader_remove_thread_local_dtor;
+    __loader_shared_globals;
     rtld_db_dlactivity;
   local:
     *;
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 66766fe..963e82c 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -622,12 +622,6 @@
   // couldn't make system calls on x86 at that point, but we can now...
   if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
 
-  // Initialize the linker/libc.so shared global inside the linker.
-  static libc_shared_globals shared_globals;
-  __libc_shared_globals = &shared_globals;
-  __libc_init_shared_globals(&shared_globals);
-  args.shared_globals = __libc_shared_globals;
-
   // Initialize the linker's static libc's globals
   __libc_init_globals(args);
 
@@ -655,12 +649,12 @@
       exit(0);
     }
     exe_to_load = args.argv[1];
-    __libc_shared_globals->initial_linker_arg_count = 1;
+    __libc_shared_globals()->initial_linker_arg_count = 1;
   }
 
   // store argc/argv/envp to use them for calling constructors
-  g_argc = args.argc - __libc_shared_globals->initial_linker_arg_count;
-  g_argv = args.argv + __libc_shared_globals->initial_linker_arg_count;
+  g_argc = args.argc - __libc_shared_globals()->initial_linker_arg_count;
+  g_argv = args.argv + __libc_shared_globals()->initial_linker_arg_count;
   g_envp = args.envp;
 
   // Initialize static variables. Note that in order to