Initialize TLS before any application code is run.

Since e19d702b8e33, dlsym and friends use recursive mutexes that
require the current thread id, which is not available before the libc
constructor. This prevents us from using dlsym() in .preinit_array.

This change moves TLS initialization from libc constructor to the earliest
possible point - immediately after linker itself is relocated. As a result,
pthread_internal_t for the initial thread is available from the start.

As a bonus, values stored in TLS in .preinit_array are not lost when libc is
initialized.

Change-Id: Iee5a710ee000173bff63e924adeb4a4c600c1e2d
diff --git a/libc/bionic/libc_init_static.c b/libc/bionic/libc_init_static.c
index a2c11a9..f97961d 100644
--- a/libc/bionic/libc_init_static.c
+++ b/libc/bionic/libc_init_static.c
@@ -65,6 +65,11 @@
     int  argc;
     char **argv, **envp;
 
+    __libc_init_tls(NULL);
+
+    /* get the initial thread from TLS and add it to gThreadList */
+    _pthread_internal_add(__get_thread());
+
     /* Initialize the C runtime environment */
     __libc_init_common(elfdata);