diff --git a/native/src/base/Android.mk b/native/src/base/Android.mk index ab46e2092..32491495b 100644 --- a/native/src/base/Android.mk +++ b/native/src/base/Android.mk @@ -28,12 +28,10 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libcompat -# Add "hacky" libc.a missing symbols back -# All symbols in this library are weak, so a vanilla NDK should still link properly LOCAL_SRC_FILES := compat/compat.cpp # Fix static variables' ctor/dtor when using LTO # See: https://github.com/android/ndk/issues/1461 -LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds +LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds -Wl,--wrap=rename -Wl,--wrap=renameat # For some reason, using the hacky libc.a with x86 will trigger stack protection violation # when mixing Rust and C++ code. Disable stack protector to bypass this issue. ifeq ($(TARGET_ARCH), x86) diff --git a/native/src/base/compat/compat.cpp b/native/src/base/compat/compat.cpp index 9efc58628..0a689e0ad 100644 --- a/native/src/base/compat/compat.cpp +++ b/native/src/base/compat/compat.cpp @@ -1,8 +1,6 @@ // This file implements all missing symbols that should exist in normal API 23 // libc.a but missing in our extremely lean libc.a replacements. -#if !defined(__LP64__) - #include #include #include @@ -12,6 +10,11 @@ extern "C" { +#if !defined(__LP64__) + +// Add "hacky" libc.a missing symbols back +// All symbols in this file are weak, so a vanilla NDK should still link properly + #include "fortify.hpp" // Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c @@ -111,5 +114,20 @@ extern FILE __sF[]; [[gnu::weak]] FILE* stdout = &__sF[1]; [[gnu::weak]] FILE* stderr = &__sF[2]; +#endif // !defined(__LP64__) + +[[maybe_unused]] +int __wrap_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path) { + long out = syscall(__NR_renameat, old_dir_fd, old_path, new_dir_fd, new_path); + if (out == -1 && errno == ENOSYS) { + out = syscall(__NR_renameat2, old_dir_fd, old_path, new_dir_fd, new_path, 0); + } + return static_cast(out); +} + +[[maybe_unused]] +int __wrap_rename(const char *old_path, const char *new_path) { + return __wrap_renameat(AT_FDCWD, old_path, AT_FDCWD, new_path); +} + } // extern "C" -#endif