void __libc_init_globals(KernelArgumentBlock& args) { // 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_init_global_stack_chk_guard(args); __libc_auxv = args.auxv; __libc_globals.initialize(); __libc_globals.mutate([&args](libc_globals* globals) { __libc_init_vdso(globals, args); __libc_init_setjmp_cookie(globals, args); }); }
void __libc_init_main_thread(KernelArgumentBlock& args) { __libc_auxv = args.auxv; #if defined(__i386__) __libc_init_sysinfo(args); #endif static pthread_internal_t main_thread; // 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). main_thread.tid = __set_tid_address(&main_thread.tid); main_thread.set_cached_pid(main_thread.tid); // We don't want to free the main thread's stack even when the main thread exits // because things like environment variables with global scope live on it. // We also can't free the pthread_internal_t itself, since that lives on the main // thread's stack rather than on the heap. // The main thread has no mmap allocated space for stack or pthread_internal_t. main_thread.mmap_size = 0; pthread_attr_init(&main_thread.attr); main_thread.attr.guard_size = 0; // The main thread has no guard page. main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked. // TODO: the main thread's sched_policy and sched_priority need to be queried. // 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_init_global_stack_chk_guard(args); __init_thread_stack_guard(&main_thread); __init_thread(&main_thread); // Store a pointer to the kernel argument block in a TLS slot to be // picked up by the libc constructor. main_thread.tls[TLS_SLOT_BIONIC_PREINIT] = &args; __init_alternate_signal_stack(&main_thread); }