diff --git a/Makefile b/Makefile index ad5428759..fcbe44196 100644 --- a/Makefile +++ b/Makefile @@ -274,6 +274,7 @@ endif # pthreads functions (possibly stub) for either thread model LIBC_TOP_HALF_MUSL_SOURCES += \ $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \ + env/__init_tls.c \ thread/default_attr.c \ thread/pthread_attr_destroy.c \ thread/pthread_attr_get.c \ @@ -293,6 +294,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \ thread/pthread_condattr_setclock.c \ thread/pthread_condattr_setpshared.c \ thread/pthread_equal.c \ + thread/pthread_getattr_np.c \ thread/pthread_getspecific.c \ thread/pthread_key_create.c \ thread/pthread_mutex_destroy.c \ @@ -320,7 +322,6 @@ ifeq ($(THREAD_MODEL), posix) # pthreads functions needed for actual thread support LIBC_TOP_HALF_MUSL_SOURCES += \ $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \ - env/__init_tls.c \ stdio/__lockfile.c \ stdio/flockfile.c \ stdio/ftrylockfile.c \ @@ -339,7 +340,6 @@ LIBC_TOP_HALF_MUSL_SOURCES += \ thread/pthread_cond_wait.c \ thread/pthread_create.c \ thread/pthread_detach.c \ - thread/pthread_getattr_np.c \ thread/pthread_join.c \ thread/pthread_mutex_consistent.c \ thread/pthread_mutex_getprioceiling.c \ @@ -384,7 +384,6 @@ LIBC_TOP_HALF_MUSL_SOURCES += \ pthread_cond_wait.c \ pthread_create.c \ pthread_detach.c \ - pthread_getattr_np.c \ pthread_join.c \ pthread_mutex_consistent.c \ pthread_mutex_getprioceiling.c \ diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index 5ec699a95..7df367e27 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -87,6 +87,7 @@ __gmtime_r __hwcap __inet_aton __init_ssp +__init_tp __intscan __invtrigl_R __isalnum_l @@ -311,6 +312,7 @@ __wasi_fd_seek __wasi_fd_sync __wasi_fd_tell __wasi_fd_write +__wasi_init_tp __wasi_path_create_directory __wasi_path_filestat_get __wasi_path_filestat_set_times diff --git a/expected/wasm32-wasip1/undefined-symbols.txt b/expected/wasm32-wasip1/undefined-symbols.txt index bdcb0c786..967798f18 100644 --- a/expected/wasm32-wasip1/undefined-symbols.txt +++ b/expected/wasm32-wasip1/undefined-symbols.txt @@ -1,4 +1,5 @@ __addtf3 +__data_end __divtf3 __eqtf2 __extenddftf2 @@ -9,6 +10,7 @@ __fixunstfsi __floatsitf __floatunsitf __getf2 +__global_base __gttf2 __heap_base __heap_end diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index 10e55033c..e27d04b43 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -90,6 +90,7 @@ __gmtime_r __hwcap __inet_aton __init_ssp +__init_tp __intscan __invtrigl_R __isalnum_l @@ -314,6 +315,7 @@ __wasi_fd_seek __wasi_fd_sync __wasi_fd_tell __wasi_fd_write +__wasi_init_tp __wasi_path_create_directory __wasi_path_filestat_get __wasi_path_filestat_set_times diff --git a/expected/wasm32-wasip2/undefined-symbols.txt b/expected/wasm32-wasip2/undefined-symbols.txt index b98dc7113..a27149b73 100644 --- a/expected/wasm32-wasip2/undefined-symbols.txt +++ b/expected/wasm32-wasip2/undefined-symbols.txt @@ -1,4 +1,5 @@ __addtf3 +__data_end __divtf3 __eqtf2 __extenddftf2 @@ -9,6 +10,7 @@ __fixunstfsi __floatsitf __floatunsitf __getf2 +__global_base __gttf2 __heap_base __heap_end diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index 1639dec13..ad9ab5a7a 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -1,7 +1,7 @@ #ifdef _REENTRANT #include -extern void __wasi_init_tp(void); #endif +extern void __wasi_init_tp(void); #ifdef __wasilibc_use_wasip2 #include #else @@ -34,9 +34,7 @@ void _start(void) { started = 1; #endif -#ifdef _REENTRANT __wasi_init_tp(); -#endif // The linker synthesizes this to call constructors. __wasm_call_ctors(); diff --git a/libc-bottom-half/crt/crt1-reactor.c b/libc-bottom-half/crt/crt1-reactor.c index ea4a84f8d..3e9a27b98 100644 --- a/libc-bottom-half/crt/crt1-reactor.c +++ b/libc-bottom-half/crt/crt1-reactor.c @@ -1,7 +1,7 @@ #if defined(_REENTRANT) #include -extern void __wasi_init_tp(void); #endif +extern void __wasi_init_tp(void); extern void __wasm_call_ctors(void); __attribute__((export_name("_initialize"))) @@ -12,8 +12,6 @@ void _initialize(void) { if (!atomic_compare_exchange_strong(&initialized, &expected, 1)) { __builtin_trap(); } - - __wasi_init_tp(); #else static volatile int initialized = 0; if (initialized != 0) { @@ -21,6 +19,7 @@ void _initialize(void) { } initialized = 1; #endif + __wasi_init_tp(); // The linker synthesizes this to call constructors. __wasm_call_ctors(); diff --git a/libc-top-half/musl/src/env/__init_tls.c b/libc-top-half/musl/src/env/__init_tls.c index 4f4c22171..7f0d92931 100644 --- a/libc-top-half/musl/src/env/__init_tls.c +++ b/libc-top-half/musl/src/env/__init_tls.c @@ -13,7 +13,9 @@ #include "atomic.h" #include "syscall.h" +#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) volatile int __thread_list_lock; +#endif #ifndef __wasilibc_unmodified_upstream @@ -25,9 +27,11 @@ volatile int __thread_list_lock; * TODO: remove usage of __heap_base/__data_end for stack size calculation * once we drop support for LLVM v15 and older. */ +#if !defined(__pic__) extern unsigned char __heap_base; extern unsigned char __data_end; extern unsigned char __global_base; +#endif extern weak unsigned char __stack_high; extern weak unsigned char __stack_low; @@ -44,6 +48,10 @@ static inline struct stack_bounds get_stack_bounds() bounds.base = &__stack_high; bounds.size = &__stack_high - &__stack_low; } else { + /* For non-pic, make a guess using the knowledge about + * how wasm-ld lays out things. For pic, just give up. + */ +#if !defined(__pic__) unsigned char *sp; __asm__( ".globaltype __stack_pointer, i32\n" @@ -57,6 +65,10 @@ static inline struct stack_bounds get_stack_bounds() bounds.base = &__global_base; bounds.size = (size_t)&__global_base; } +#else + bounds.base = 0; + bounds.size = 0; +#endif } return bounds; @@ -82,10 +94,11 @@ int __init_tp(void *p) __default_stacksize = bounds.size < DEFAULT_STACK_MAX ? bounds.size : DEFAULT_STACK_MAX; - td->detach_state = DT_JOINABLE; td->stack = bounds.base; td->stack_size = bounds.size; td->guard_size = 0; +#ifdef _REENTRANT + td->detach_state = DT_JOINABLE; /* * Initialize the TID to a value which doesn't conflict with * host-allocated TIDs, so that TID-based locks can work. @@ -98,8 +111,11 @@ int __init_tp(void *p) */ td->tid = 0x3fffffff; #endif +#endif +#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) td->locale = &libc.global_locale; td->robust_list.head = &td->robust_list.head; +#endif td->sysinfo = __sysinfo; td->next = td->prev = td; return 0; @@ -121,6 +137,7 @@ static struct tls_module main_tls; extern void __wasm_init_tls(void*); #endif +#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) void *__copy_tls(unsigned char *mem) { #ifdef __wasilibc_unmodified_upstream @@ -167,6 +184,7 @@ void *__copy_tls(unsigned char *mem) return mem; #endif } +#endif /* defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) */ #ifdef __wasilibc_unmodified_upstream #if ULONG_MAX == 0xffffffff diff --git a/thread-stub/pthread_getattr_np.c b/thread-stub/pthread_getattr_np.c deleted file mode 100644 index d463de143..000000000 --- a/thread-stub/pthread_getattr_np.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "pthread_impl.h" - -int pthread_getattr_np(pthread_t t, pthread_attr_t *a) -{ - *a = (pthread_attr_t){0}; - /* Can't join main thread. */ - a->_a_detach = PTHREAD_CREATE_DETACHED; - /* WASI doesn't have memory protection required for stack guards. */ - a->_a_guardsize = 0; - return 0; -}