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
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_ */
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index b384ce4..3318c2c 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -148,8 +148,8 @@
 // An empty list of soinfos
 static soinfo_list_t g_empty_list;
 
-static void add_vdso(KernelArgumentBlock& args) {
-  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
+static void add_vdso() {
+  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR));
   if (ehdr_vdso == nullptr) {
     return;
   }
@@ -191,7 +191,7 @@
   ElfW(Addr) entry_point;
 };
 
-static ExecutableInfo get_executable_info(KernelArgumentBlock& args) {
+static ExecutableInfo get_executable_info() {
   ExecutableInfo result = {};
 
   if (is_first_stage_init()) {
@@ -222,9 +222,9 @@
     result.path = std::string(path, path_len);
   }
 
-  result.phdr = reinterpret_cast<const ElfW(Phdr)*>(args.getauxval(AT_PHDR));
-  result.phdr_count = args.getauxval(AT_PHNUM);
-  result.entry_point = args.getauxval(AT_ENTRY);
+  result.phdr = reinterpret_cast<const ElfW(Phdr)*>(getauxval(AT_PHDR));
+  result.phdr_count = getauxval(AT_PHNUM);
+  result.entry_point = getauxval(AT_ENTRY);
   return result;
 }
 
@@ -344,7 +344,7 @@
   }
 
   const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
-                                                get_executable_info(args);
+                                                get_executable_info();
 
   // Assign to a static variable for the sake of the debug map, which needs
   // a C-style string to last until the program exits.
@@ -375,7 +375,7 @@
   insert_link_map_into_debug_map(&si->link_map_head);
   insert_link_map_into_debug_map(&solinker->link_map_head);
 
-  add_vdso(args);
+  add_vdso();
 
   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
 
@@ -452,10 +452,6 @@
 
   if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]);
 
-  // Store a pointer to the kernel argument block in a TLS slot to be
-  // picked up by the libc constructor.
-  __get_tls()[TLS_SLOT_BIONIC_PREINIT] = &args;
-
   si->call_pre_init_constructors();
   si->call_constructors();
 
@@ -565,14 +561,14 @@
 
   // When the linker is run by itself (rather than as an interpreter for
   // another program), AT_BASE is 0.
-  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
+  ElfW(Addr) linker_addr = getauxval(AT_BASE);
   if (linker_addr == 0) {
     // Detect an attempt to run the linker on itself (e.g.
     // `linker64 /system/bin/linker64`). If the kernel loaded this instance of
     // the linker, then AT_ENTRY will refer to &_start. If it doesn't, then
     // something else must have loaded this instance of the linker. It's
     // simpler if we only allow one copy of the linker to be loaded at a time.
-    if (args.getauxval(AT_ENTRY) != reinterpret_cast<uintptr_t>(&_start)) {
+    if (getauxval(AT_ENTRY) != reinterpret_cast<uintptr_t>(&_start)) {
       // The first linker already relocated this one and set up TLS, so we don't
       // need further libc initialization.
       __linker_error("error: linker cannot load itself\n");
@@ -581,7 +577,7 @@
     // instance, so use the phdr to find the linker's base address.
     ElfW(Addr) load_bias;
     get_elf_base_from_phdr(
-      reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)), args.getauxval(AT_PHNUM),
+      reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)), getauxval(AT_PHNUM),
       &linker_addr, &load_bias);
   }
 
@@ -621,14 +617,14 @@
 static ElfW(Addr) __attribute__((noinline))
 __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) {
   // Finish initializing the main thread.
-  __libc_init_main_thread_late(args);
+  __libc_init_main_thread_late();
 
   // We didn't protect the linker's RELRO pages in link_image because we
   // couldn't make system calls on x86 at that point, but we can now...
   if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
 
   // Initialize the linker's static libc's globals
-  __libc_init_globals(args);
+  __libc_init_globals();
 
   // Initialize the linker's own global variables
   tmp_linker_so.call_constructors();
@@ -638,7 +634,7 @@
   // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the
   // linker's _start.
   const char* exe_to_load = nullptr;
-  if (args.getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
+  if (getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
     if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) {
       async_safe_format_fd(STDOUT_FILENO,
          "Usage: %s program [arguments...]\n"