Replace TLS_SLOT_BIONIC_PREINIT w/ shared globals

Instead of passing the address of a KernelArgumentBlock to libc.so for
initialization, use __loader_shared_globals() to initialize globals.

Most of the work happened in the previous CLs. This CL switches a few
KernelArgumentBlock::getauxval calls to [__bionic_]getauxval and stops
routing the KernelArgumentBlock address through the libc init functions.

Bug: none
Test: bionic unit tests
Change-Id: I96c7b02c21d55c454558b7a5a9243c682782f2dd
Merged-In: I96c7b02c21d55c454558b7a5a9243c682782f2dd
(cherry picked from commit 746ad15912cfa82271424747e94d8125acc43d8c)
diff --git a/libc/arch-x86/bionic/__libc_init_sysinfo.cpp b/libc/arch-x86/bionic/__libc_init_sysinfo.cpp
index 1bb7ab0..849d253 100644
--- a/libc/arch-x86/bionic/__libc_init_sysinfo.cpp
+++ b/libc/arch-x86/bionic/__libc_init_sysinfo.cpp
@@ -26,14 +26,15 @@
  * SUCH DAMAGE.
  */
 
-#include "private/KernelArgumentBlock.h"
+#include "private/bionic_auxv.h"
 #include "private/bionic_globals.h"
 
 // This file is compiled without stack protection, because it runs before TLS
 // has been set up.
 
-__LIBC_HIDDEN__ void __libc_init_sysinfo(KernelArgumentBlock& args) {
-  __libc_sysinfo = reinterpret_cast<void*>(args.getauxval(AT_SYSINFO));
+__LIBC_HIDDEN__ void __libc_init_sysinfo() {
+  bool dummy;
+  __libc_sysinfo = reinterpret_cast<void*>(__bionic_getauxval(AT_SYSINFO, dummy));
 }
 
 // TODO: lose this function and just access __libc_sysinfo directly.
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index e6e0c56..5abdc07 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -72,7 +72,7 @@
 void __libc_init_main_thread_early(KernelArgumentBlock& args) {
   __libc_shared_globals()->auxv = args.auxv;
 #if defined(__i386__)
-  __libc_init_sysinfo(args);
+  __libc_init_sysinfo();
 #endif
   __set_tls(main_thread.tls);
   __init_tls(&main_thread);
@@ -82,7 +82,7 @@
 
 // Finish initializing the main thread.
 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
-void __libc_init_main_thread_late(KernelArgumentBlock& args) {
+void __libc_init_main_thread_late() {
   main_thread.bionic_tls = __allocate_bionic_tls();
   if (main_thread.bionic_tls == nullptr) {
     // Avoid strerror because it might need bionic_tls.
@@ -109,7 +109,7 @@
   // The TLS stack guard is set from the global, so ensure that we've initialized the global
   // 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_safe_arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard), args);
+  __libc_safe_arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard));
   __init_tls_stack_guard(&main_thread);
 
   __init_thread(&main_thread);
diff --git a/libc/bionic/bionic_arc4random.cpp b/libc/bionic/bionic_arc4random.cpp
index fa2617f..74ac43c 100644
--- a/libc/bionic/bionic_arc4random.cpp
+++ b/libc/bionic/bionic_arc4random.cpp
@@ -35,9 +35,7 @@
 
 #include <async_safe/log.h>
 
-#include "private/KernelArgumentBlock.h"
-
-void __libc_safe_arc4random_buf(void* buf, size_t n, KernelArgumentBlock& args) {
+void __libc_safe_arc4random_buf(void* buf, size_t n) {
   // Only call arc4random_buf once we have `/dev/urandom` because getentropy(3)
   // will fall back to using `/dev/urandom` if getrandom(2) fails, and abort if
   // if can't use `/dev/urandom`.
@@ -53,7 +51,7 @@
                      16 - at_random_bytes_consumed, n);
   }
 
-  memcpy(buf, reinterpret_cast<char*>(args.getauxval(AT_RANDOM)) + at_random_bytes_consumed, n);
+  memcpy(buf, reinterpret_cast<char*>(getauxval(AT_RANDOM)) + at_random_bytes_consumed, n);
   at_random_bytes_consumed += n;
   return;
 }
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index fca628b..f1fbfa9 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -43,7 +43,6 @@
 
 #include <async_safe/log.h>
 
-#include "private/KernelArgumentBlock.h"
 #include "private/WriteProtected.h"
 #include "private/bionic_defs.h"
 #include "private/bionic_globals.h"
@@ -58,14 +57,14 @@
 // Not public, but well-known in the BSDs.
 const char* __progname;
 
-void __libc_init_globals(KernelArgumentBlock& args) {
+void __libc_init_globals() {
   // Initialize libc globals that are needed in both the linker and in libc.
   // In dynamic binaries, this is run at least twice for different copies of the
   // globals, once for the linker's copy and once for the one in libc.so.
   __libc_globals.initialize();
-  __libc_globals.mutate([&args](libc_globals* globals) {
-    __libc_init_vdso(globals, args);
-    __libc_init_setjmp_cookie(globals, args);
+  __libc_globals.mutate([](libc_globals* globals) {
+    __libc_init_vdso(globals);
+    __libc_init_setjmp_cookie(globals);
   });
 }
 
diff --git a/libc/bionic/libc_init_common.h b/libc/bionic/libc_init_common.h
index 84b59ca..73f5817 100644
--- a/libc/bionic/libc_init_common.h
+++ b/libc/bionic/libc_init_common.h
@@ -50,9 +50,7 @@
 
 #if defined(__cplusplus)
 
-class KernelArgumentBlock;
-
-__LIBC_HIDDEN__ void __libc_init_globals(KernelArgumentBlock& args);
+__LIBC_HIDDEN__ void __libc_init_globals();
 
 __LIBC_HIDDEN__ void __libc_init_common();
 
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 2ec1556..af1b847 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -77,12 +77,12 @@
 // after __stack_chk_guard is initialized and therefore can safely have a stack
 // protector.
 __attribute__((noinline))
-static void __libc_preinit_impl(KernelArgumentBlock& args) {
+static void __libc_preinit_impl() {
 #if defined(__i386__)
-  __libc_init_sysinfo(args);
+  __libc_init_sysinfo();
 #endif
 
-  __libc_init_globals(args);
+  __libc_init_globals();
   __libc_init_common();
 
   // Hooks for various libraries to let them know that we're starting up.
@@ -98,17 +98,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, then clear the slot so no
-  // other initializer sees its value.
-  void** tls = __get_tls();
-  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.
-  __stack_chk_guard = reinterpret_cast<uintptr_t>(tls[TLS_SLOT_STACK_GUARD]);
+  __stack_chk_guard = reinterpret_cast<uintptr_t>(__get_tls()[TLS_SLOT_STACK_GUARD]);
 
-  __libc_preinit_impl(*args);
+  __libc_preinit_impl();
 }
 
 // This function is called from the executable's _start entry point
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index e79d3b3..289b4a3 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -98,8 +98,8 @@
   // Initialize TLS early so system calls and errno work.
   KernelArgumentBlock args(raw_args);
   __libc_init_main_thread_early(args);
-  __libc_init_main_thread_late(args);
-  __libc_init_globals(args);
+  __libc_init_main_thread_late();
+  __libc_init_globals();
   __libc_shared_globals()->init_progname = args.argv[0];
   __libc_init_AT_SECURE(args.envp);
   __libc_init_common();
diff --git a/libc/bionic/setjmp_cookie.cpp b/libc/bionic/setjmp_cookie.cpp
index 41a439f..e2a3fc0 100644
--- a/libc/bionic/setjmp_cookie.cpp
+++ b/libc/bionic/setjmp_cookie.cpp
@@ -38,11 +38,10 @@
 
 #include "private/bionic_arc4random.h"
 #include "private/bionic_globals.h"
-#include "private/KernelArgumentBlock.h"
 
-void __libc_init_setjmp_cookie(libc_globals* globals, KernelArgumentBlock& args) {
+void __libc_init_setjmp_cookie(libc_globals* globals) {
   long value;
-  __libc_safe_arc4random_buf(&value, sizeof(value), args);
+  __libc_safe_arc4random_buf(&value, sizeof(value));
 
   // Mask off the last bit to store the signal flag.
   globals->setjmp_cookie = value & ~1;
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
index c926a58..dbca9c0 100644
--- a/libc/bionic/vdso.cpp
+++ b/libc/bionic/vdso.cpp
@@ -20,11 +20,11 @@
 #include <limits.h>
 #include <link.h>
 #include <string.h>
+#include <sys/auxv.h>
 #include <sys/cdefs.h>
 #include <sys/time.h>
 #include <time.h>
 #include <unistd.h>
-#include "private/KernelArgumentBlock.h"
 
 static inline int vdso_return(int result) {
   if (__predict_true(result == 0)) return 0;
@@ -73,7 +73,7 @@
   return tv.tv_sec;
 }
 
-void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
+void __libc_init_vdso(libc_globals* globals) {
   auto&& vdso = globals->vdso;
   vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, nullptr };
   vdso[VDSO_CLOCK_GETRES] = { VDSO_CLOCK_GETRES_SYMBOL, nullptr };
@@ -81,7 +81,7 @@
   vdso[VDSO_TIME] = { VDSO_TIME_SYMBOL, nullptr };
 
   // Do we have a vdso?
-  uintptr_t vdso_ehdr_addr = args.getauxval(AT_SYSINFO_EHDR);
+  uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
   ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
   if (vdso_ehdr == nullptr) {
     return;
diff --git a/libc/private/bionic_arc4random.h b/libc/private/bionic_arc4random.h
index 0e9376e..cdc9b6d 100644
--- a/libc/private/bionic_arc4random.h
+++ b/libc/private/bionic_arc4random.h
@@ -31,13 +31,11 @@
 
 #include <stddef.h>
 
-#include "private/KernelArgumentBlock.h"
-
 // arc4random(3) aborts if it's unable to fetch entropy, which is always
 // the case for init on devices. GCE kernels have a workaround to ensure
 // sufficient entropy during early boot, but no device kernels do. This
 // wrapper falls back to AT_RANDOM if the kernel doesn't have enough
 // entropy for getrandom(2) or /dev/urandom.
-void __libc_safe_arc4random_buf(void* buf, size_t n, KernelArgumentBlock& args);
+void __libc_safe_arc4random_buf(void* buf, size_t n);
 
 #endif
diff --git a/libc/private/bionic_globals.h b/libc/private/bionic_globals.h
index 9c74d92..ba510b1 100644
--- a/libc/private/bionic_globals.h
+++ b/libc/private/bionic_globals.h
@@ -70,14 +70,13 @@
 __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals();
 __LIBC_HIDDEN__ void __libc_init_fdsan();
 
-class KernelArgumentBlock;
 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals);
-__LIBC_HIDDEN__ void __libc_init_setjmp_cookie(libc_globals* globals, KernelArgumentBlock& args);
-__LIBC_HIDDEN__ void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args);
+__LIBC_HIDDEN__ void __libc_init_setjmp_cookie(libc_globals* globals);
+__LIBC_HIDDEN__ void __libc_init_vdso(libc_globals* globals);
 
 #if defined(__i386__)
 __LIBC_HIDDEN__ extern void* __libc_sysinfo;
-__LIBC_HIDDEN__ void __libc_init_sysinfo(KernelArgumentBlock& args);
+__LIBC_HIDDEN__ void __libc_init_sysinfo();
 #endif
 
 #endif
diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h
index f65de34..80dc9bc 100644
--- a/libc/private/bionic_tls.h
+++ b/libc/private/bionic_tls.h
@@ -63,12 +63,6 @@
   TLS_SLOT_OPENGL_API = 3,
   TLS_SLOT_OPENGL = 4,
 
-  // This slot is only used to pass information from the dynamic linker to
-  // libc.so when the C library is loaded in to memory. The C runtime init
-  // function will then clear it. Since its use is extremely temporary,
-  // we reuse an existing location that isn't needed during libc startup.
-  TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API,
-
   TLS_SLOT_STACK_GUARD = 5, // GCC requires this specific slot for x86.
   TLS_SLOT_DLERROR,
 
@@ -133,7 +127,7 @@
 #if defined(__cplusplus)
 class KernelArgumentBlock;
 extern void __libc_init_main_thread_early(KernelArgumentBlock& args);
-extern void __libc_init_main_thread_late(KernelArgumentBlock& args);
+extern void __libc_init_main_thread_late();
 #endif
 
 #endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */