Use shared globals so getauxval works earlier

Make getauxval() work in .preinit_array. It still won't be usable for
ifuncs unless we can guarantee that the __loader_shared_globals relocation
is resolved before the ifunc calls [__bionic_]getauxval.

Define __bionic_getauxval for use in replacing calls to
KernelArgumentBlock::getauxval, which doesn't (and sometimes isn't allowed
to) access TLS variables like errno.

Bug: http://b/25751302
Test: bionic unit tests
Change-Id: I461feeaed7f43cfa2a2b6c34147194f0df82b516
Merged-In: I461feeaed7f43cfa2a2b6c34147194f0df82b516
(cherry picked from commit bdab4a2b97c53af0205788875342ec08e6901376)
diff --git a/libc/bionic/getauxval.cpp b/libc/bionic/getauxval.cpp
index 607e89c..c8f867b 100644
--- a/libc/bionic/getauxval.cpp
+++ b/libc/bionic/getauxval.cpp
@@ -30,17 +30,27 @@
 #include <sys/cdefs.h>
 #include <sys/auxv.h>
 #include <private/bionic_auxv.h>
+#include <private/bionic_globals.h>
 #include <elf.h>
 #include <errno.h>
 
-__LIBC_HIDDEN__ ElfW(auxv_t)* __libc_auxv = nullptr;
-
-extern "C" unsigned long int getauxval(unsigned long int type) {
-  for (ElfW(auxv_t)* v = __libc_auxv; v->a_type != AT_NULL; ++v) {
+// This function needs to be safe to call before TLS is set up, so it can't
+// access errno or the stack protector.
+__attribute__((no_stack_protector))
+__LIBC_HIDDEN__ unsigned long __bionic_getauxval(unsigned long type, bool& exists) {
+  for (ElfW(auxv_t)* v = __libc_shared_globals()->auxv; v->a_type != AT_NULL; ++v) {
     if (v->a_type == type) {
+      exists = true;
       return v->a_un.a_val;
     }
   }
-  errno = ENOENT;
+  exists = false;
   return 0;
 }
+
+extern "C" unsigned long getauxval(unsigned long type) {
+  bool exists;
+  unsigned long result = __bionic_getauxval(type, exists);
+  if (!exists) errno = ENOENT;
+  return result;
+}