diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp index bd716286a..3ca6c0d5e 100644 --- a/libc/bionic/libc_init_common.cpp +++ b/libc/bionic/libc_init_common.cpp @@ -64,18 +64,15 @@ char** environ; // Declared in "private/bionic_ssp.h". uintptr_t __stack_chk_guard = 0; -/* 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. - * - * Note: this function creates a pthread_internal_t for the initial thread and - * stores the pointer in TLS, but does not add it to pthread's thread list. This - * has to be done later from libc itself (see __libc_init_common). - * - * This function also stores a pointer to the kernel argument block in a TLS slot to be - * picked up by the libc constructor. - */ -void __libc_init_tls(KernelArgumentBlock& args) { +// Setup for the main thread. For dynamic executables, this is called by the +// linker _before_ libc is mapped in memory. This means that all writes to +// globals from this function will apply to linker-private copies and will not +// be visible from libc later on. +// +// Note: this function creates a pthread_internal_t for the initial thread and +// stores the pointer in TLS, but does not add it to pthread's thread list. This +// has to be done later from libc itself (see __libc_init_common). +void __libc_init_main_thread(KernelArgumentBlock& args) { __libc_auxv = args.auxv; static pthread_internal_t main_thread; @@ -99,6 +96,9 @@ void __libc_init_tls(KernelArgumentBlock& args) { __init_thread(&main_thread); __init_tls(&main_thread); __set_tls(main_thread.tls); + + // 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); diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp index 7794fbe76..ef57af650 100644 --- a/libc/bionic/libc_init_static.cpp +++ b/libc/bionic/libc_init_static.cpp @@ -90,7 +90,7 @@ __noreturn void __libc_init(void* raw_args, int (*slingshot)(int, char**, char**), structors_array_t const * const structors) { KernelArgumentBlock args(raw_args); - __libc_init_tls(args); + __libc_init_main_thread(args); __libc_init_AT_SECURE(args); __libc_init_common(args); diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h index 30dc0eb3e..d92d9d9a5 100644 --- a/libc/private/bionic_tls.h +++ b/libc/private/bionic_tls.h @@ -114,7 +114,7 @@ __END_DECLS #if defined(__cplusplus) class KernelArgumentBlock; -extern __LIBC_HIDDEN__ void __libc_init_tls(KernelArgumentBlock& args); +extern __LIBC_HIDDEN__ void __libc_init_main_thread(KernelArgumentBlock& args); #endif #endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */ diff --git a/linker/linker.cpp b/linker/linker.cpp index 73eeba17b..0718efcd7 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -3465,7 +3465,7 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) { _exit(EXIT_FAILURE); } - __libc_init_tls(args); + __libc_init_main_thread(args); // Initialize the linker's own global variables linker_so.call_constructors();