From 47e6dd286d2ad49431b353a5382dfc50f04737d7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Feb 2024 22:00:09 -0800 Subject: [PATCH] Minor fixes --- native/src/crt0/misc.c | 34 ++++++++++++++++++++++++++-------- native/src/crt0/syscall.c | 18 +++++++++++++++++- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/native/src/crt0/misc.c b/native/src/crt0/misc.c index 6c38eab7e..3a00d06e9 100644 --- a/native/src/crt0/misc.c +++ b/native/src/crt0/misc.c @@ -3,6 +3,7 @@ #include #include #include +#include // Source: bionic/libc/upstream-openbsd/lib/libc/stdlib/getenv.c static char *__findenv(const char *name, int len, int *offset) { @@ -162,17 +163,34 @@ int __cxa_atexit(void (*func) (void *), void * arg, void * dso_handle) { return 0; } -// Dummy function symbols +// Emulate pthread functions -long dummy() { return 0; } +static pthread_key_t g_counter = 0; +static void **g_key_values = NULL; -#define DUMMY_SYMBOL(name) \ -__asm__(".global " #name " \n " #name " = dummy") +int pthread_key_create(pthread_key_t *key_ptr, void (*dtor)(void*)) { + *key_ptr = g_counter++; + g_key_values = realloc(g_key_values, g_counter * sizeof(void*)); + return 0; +} -DUMMY_SYMBOL(pthread_setspecific); -DUMMY_SYMBOL(pthread_key_create); -DUMMY_SYMBOL(pthread_key_delete); -DUMMY_SYMBOL(pthread_getspecific); +int pthread_key_delete(pthread_key_t key) { + if (key < g_counter) { + g_key_values[key] = NULL; + } + return 0; +} + +void *pthread_getspecific(pthread_key_t key) { + return key < g_counter ? g_key_values[key] : NULL; +} + +int pthread_setspecific(pthread_key_t key, const void *value) { + if (key < g_counter) { + g_key_values[key] = (void *) value; + } + return 0; +} // Workaround LTO bug: https://github.com/llvm/llvm-project/issues/61101 #if defined(__i386__) diff --git a/native/src/crt0/syscall.c b/native/src/crt0/syscall.c index 2f493ff9b..1f10a502b 100644 --- a/native/src/crt0/syscall.c +++ b/native/src/crt0/syscall.c @@ -28,7 +28,6 @@ __asm__(".global " #from " \n " #from " = " #to) SYMBOL_ALIAS(name, sys_##name) EXPORT_SYMBOL(_exit); -EXPORT_SYMBOL(open); EXPORT_SYMBOL(openat); EXPORT_SYMBOL(close); EXPORT_SYMBOL(read); @@ -203,3 +202,20 @@ int faccessat(int dirfd, const char *pathname, int mode, int flags) { return sys_faccessat(dirfd, pathname, mode); } + +int open(const char *pathname, int flags, ...) { + int mode = 0; + + if (((flags & O_CREAT) == O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) { + va_list args; + va_start(args, flags); + mode = va_arg(args, int); + va_end(args); + } + +#if !defined(__LP64__) + flags |= O_LARGEFILE; +#endif + + return sys_openat(AT_FDCWD, pathname, flags, mode); +}