Wrap rename and renameat

This commit is contained in:
LoveSy 2023-06-16 01:40:50 +08:00 committed by John Wu
parent 2dbb812126
commit c072b4254d
2 changed files with 22 additions and 6 deletions

View File

@ -28,12 +28,10 @@ include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := libcompat 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 LOCAL_SRC_FILES := compat/compat.cpp
# Fix static variables' ctor/dtor when using LTO # Fix static variables' ctor/dtor when using LTO
# See: https://github.com/android/ndk/issues/1461 # 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 # 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. # when mixing Rust and C++ code. Disable stack protector to bypass this issue.
ifeq ($(TARGET_ARCH), x86) ifeq ($(TARGET_ARCH), x86)

View File

@ -1,8 +1,6 @@
// This file implements all missing symbols that should exist in normal API 23 // This file implements all missing symbols that should exist in normal API 23
// libc.a but missing in our extremely lean libc.a replacements. // libc.a but missing in our extremely lean libc.a replacements.
#if !defined(__LP64__)
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -12,6 +10,11 @@
extern "C" { 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" #include "fortify.hpp"
// Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c // 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* stdout = &__sF[1];
[[gnu::weak]] FILE* stderr = &__sF[2]; [[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<int>(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" } // extern "C"
#endif