Clean up TLS_SLOT_BIONIC_PREINIT usage a bit

 - It is only needed for dynamic executables, so move the initialization
   out of __libc_init_main_thread and just before the solib constructor
   calls. For static executables, the slot was initialized, then never
   used or cleared. Instead, leave it clear.

 - For static executables, __libc_init_main_thread already initialized the
   stack guard, so remove the redundant __init_thread_stack_guard call.

 - Simplify the slot access/clearing a bit in __libc_preinit.

 - Remove the "__libc_init_common() will change the TLS area so the old one
   won't be accessible anyway." comment. AFAICT, it's incorrect -- the
   main thread's TLS area in a dynamic executable is initialized to a
   static pthread_internal_t object in the linker, then reused by libc.so.

Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
Change-Id: Ie2da6f5be3ad563fa65b38eaadf8ba6ecc6a64b6
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 0b68280..fb086c8 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -85,14 +85,11 @@
 // to run before any others (such as the jemalloc constructor), and lower
 // is better (http://b/68046352).
 __attribute__((constructor(1))) static void __libc_preinit() {
-  // Read the kernel argument block pointer from TLS.
+  // Read the kernel argument block pointer from TLS, then clear the slot so no
+  // other initializer sees its value.
   void** tls = __get_tls();
-  KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT];
-  KernelArgumentBlock* args = *args_slot;
-
-  // Clear the slot so no other initializer sees its value.
-  // __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
-  *args_slot = NULL;
+  KernelArgumentBlock* args = static_cast<KernelArgumentBlock*>(tls[TLS_SLOT_BIONIC_PREINIT]);
+  tls[TLS_SLOT_BIONIC_PREINIT] = nullptr;
 
   // The linker has initialized its copy of the global stack_chk_guard, and filled in the main
   // thread's TLS slot with that value. Initialize the local global stack guard with its value.
@@ -102,9 +99,8 @@
 }
 
 // This function is called from the executable's _start entry point
-// (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
-// called by the dynamic linker after it has loaded all shared
-// libraries the executable depends on.
+// (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic
+// linker after it has loaded all shared libraries the executable depends on.
 //
 // Note that the dynamic linker has also run all constructors in the
 // executable at this point.