stack protector: use AT_RANDOM

Populate the stack canaries from the kernel supplied
AT_RANDOM value, which doesn't involve any system calls.
This is slightly faster (6 fewer syscalls) and avoids
unnecessarily reading /dev/urandom, which depletes entropy.

Bug: 7959813

Change-Id: If2b43100a2a9929666df3de56b6139fed969e0f1
diff --git a/libc/bionic/libc_init_common.c b/libc/bionic/libc_init_common.c
index 86e1eb5..e4ad2ee 100644
--- a/libc/bionic/libc_init_common.c
+++ b/libc/bionic/libc_init_common.c
@@ -53,6 +53,22 @@
 
 int __system_properties_init(void);
 
+static Elf32_auxv_t* get_aux_from_elfdata(uintptr_t* elf_data) {
+  int argc = *elf_data;
+  char** argv = (char**) (elf_data + 1);
+  char** envp = argv + argc + 1;
+
+  // The auxiliary vector is at the end of the environment block
+  while(*envp != NULL) {
+    envp++;
+  }
+  /* The end of the environment block is marked by two NULL pointers */
+  envp++;
+
+  return (Elf32_auxv_t*) envp;
+}
+
+
 /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
  * in memory. Beware: all writes to libc globals from this function will
  * apply to linker-private copies and will not be visible from libc later on.
@@ -64,7 +80,7 @@
  * This function also stores the elf_data argument in a specific TLS slot to be later
  * picked up by the libc constructor.
  */
-void __libc_init_tls(unsigned** elf_data) {
+void __libc_init_tls(uintptr_t* elf_data) {
   unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
   unsigned stack_size = 128 * 1024;
   unsigned stack_bottom = stack_top - stack_size;
@@ -77,6 +93,7 @@
   _init_thread(&thread, gettid(), &thread_attr, (void*) stack_bottom, false);
 
   static void* tls_area[BIONIC_TLS_SLOTS];
+  __libc_auxv = get_aux_from_elfdata(elf_data);
   __init_tls(tls_area, &thread);
   tls_area[TLS_SLOT_BIONIC_PREINIT] = elf_data;
 }
@@ -96,14 +113,7 @@
   __progname = argv[0] ? argv[0] : "<unknown>";
   environ = envp;
 
-  // The auxiliary vector is at the end of the environment block
-  while(*envp != NULL) {
-    envp++;
-  }
-  /* The end of the environment block is marked by two NULL pointers */
-  envp++;
-
-  __libc_auxv = (Elf32_auxv_t*) envp;
+  __libc_auxv = get_aux_from_elfdata(elf_data);
 
   __system_properties_init(); // Requires 'environ'.
 }
diff --git a/libc/bionic/libc_init_static.c b/libc/bionic/libc_init_static.c
index 24a4397..1cef632 100644
--- a/libc/bionic/libc_init_static.c
+++ b/libc/bionic/libc_init_static.c
@@ -96,7 +96,7 @@
     int  argc;
     char **argv, **envp;
 
-    __libc_init_tls(NULL);
+    __libc_init_tls(elfdata);
 
     /* Initialize the C runtime environment */
     __libc_init_common(elfdata);