Initialize main thread TLS before the global stack guard.

The GCE breakage caused by 78a52f19bb207d1c736f1d5362e01f338d78645 was
due to TLS_SLOT_SELF being uninitialized before the use of errno by
syscall. Separate stack guard initialization from TLS initialization so
that stack guard initialization can make syscalls.

Bug: http://b/29622562
Bug: http://b/31251721
Change-Id: Id0e4379e0efb7194a2df7bd16211ff11c6598033
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index 2643eee..b5a83f4 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -69,7 +69,9 @@
 
   // The -fstack-protector implementation uses TLS, so make sure that's
   // set up before we call any function that might get a stack check inserted.
+  // TLS also needs to be set up before errno (and therefore syscalls) can be used.
   __set_tls(main_thread.tls);
+  __init_tls(&main_thread);
 
   // Tell the kernel to clear our tid field when we exit, so we're like any other pthread.
   // As a side-effect, this tells us our pid (which is the same as the main thread's tid).
@@ -91,9 +93,9 @@
   // before we initialize the TLS. Dynamic executables will initialize their copy of the global
   // stack protector from the one in the main thread's TLS.
   __libc_init_global_stack_chk_guard(args);
+  __init_thread_stack_guard(&main_thread);
 
   __init_thread(&main_thread);
-  __init_tls(&main_thread);
 
   // Store a pointer to the kernel argument block in a TLS slot to be
   // picked up by the libc constructor.