diff --git a/build.py b/build.py index 6e34abeb0..34d32d02f 100755 --- a/build.py +++ b/build.py @@ -330,6 +330,12 @@ def build_binary(args): if 'test' in args.target: flag += ' B_TEST=1' + if flag: + run_ndk_build(flag + ' B_SHARED=1') + + if 'magisk' in args.target: + clean_elf() + if 'magiskinit' in args.target: dump_bin_header() flag += ' B_INIT=1' @@ -346,9 +352,6 @@ def build_binary(args): if flag: run_ndk_build(flag) - if 'magisk' in args.target: - clean_elf() - if 'busybox' in args.target: run_ndk_build('B_BB=1') @@ -445,19 +448,18 @@ def setup_ndk(args): mv(op.join(ndk_root, f'android-ndk-r{ndk_ver}'), ndk_path) header('* Patching static libs') - for api in ['16', '21']: - for target in ['aarch64-linux-android', 'arm-linux-androideabi', - 'i686-linux-android', 'x86_64-linux-android']: - arch = target.split('-')[0] - lib_dir = op.join( - ndk_path, 'toolchains', 'llvm', 'prebuilt', f'{os_name}-x86_64', - 'sysroot', 'usr', 'lib', f'{target}', api) - if not op.exists(lib_dir): - continue - src_dir = op.join('tools', 'ndk-bins', api, arch) - rm(op.join(src_dir, '.DS_Store')) - for path in copy_tree(src_dir, lib_dir): - vprint(f'Replaced {path}') + for target in ['aarch64-linux-android', 'arm-linux-androideabi', + 'i686-linux-android', 'x86_64-linux-android']: + arch = target.split('-')[0] + lib_dir = op.join( + ndk_path, 'toolchains', 'llvm', 'prebuilt', f'{os_name}-x86_64', + 'sysroot', 'usr', 'lib', f'{target}', '21') + if not op.exists(lib_dir): + continue + src_dir = op.join('tools', 'ndk-bins', '21', arch) + rm(op.join(src_dir, '.DS_Store')) + for path in copy_tree(src_dir, lib_dir): + vprint(f'Replaced {path}') def setup_avd(args): diff --git a/native/jni/Android.mk b/native/jni/Android.mk index afa006c6e..aff05ed14 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -8,7 +8,7 @@ ifdef B_MAGISK include $(CLEAR_VARS) LOCAL_MODULE := magisk -LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils libphmap libxhook +LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils-shared libphmap libxhook LOCAL_SRC_FILES := \ core/applets.cpp \ @@ -134,7 +134,7 @@ ifneq (,$(wildcard jni/test.cpp)) include $(CLEAR_VARS) LOCAL_MODULE := test -LOCAL_STATIC_LIBRARIES := libutils libphmap +LOCAL_STATIC_LIBRARIES := libutils-shared libphmap LOCAL_SRC_FILES := test.cpp include $(BUILD_EXECUTABLE) diff --git a/native/jni/Application.mk b/native/jni/Application.mk index a96b21827..c772cbe69 100644 --- a/native/jni/Application.mk +++ b/native/jni/Application.mk @@ -3,10 +3,19 @@ APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -flto APP_LDFLAGS := -flto APP_CPPFLAGS := -std=c++17 APP_STL := none -APP_PLATFORM := android-16 +APP_PLATFORM := android-21 APP_THIN_ARCHIVE := true APP_STRIP_MODE := --strip-all +ifneq ($(TARGET_ARCH),arm64) +ifneq ($(TARGET_ARCH),x86_64) +ifndef B_SHARED +# Disable fortify on static 32-bit targets +APP_CFLAGS += -D_FORTIFY_SOURCE=0 -Wno-macro-redefined +endif +endif +endif + # Busybox should use stock libc.a ifdef B_BB APP_PLATFORM := android-22 diff --git a/native/jni/external/systemproperties/include/private/hacks.h b/native/jni/external/systemproperties/include/private/hacks.h index 5ac34e195..eb839f93d 100644 --- a/native/jni/external/systemproperties/include/private/hacks.h +++ b/native/jni/external/systemproperties/include/private/hacks.h @@ -11,10 +11,6 @@ #define PR_SET_VMA_ANON_NAME 0 #endif -// Missing functions -#define getline compat_getline -ssize_t compat_getline(char **, size_t *, FILE *); - // Rename symbols #pragma redefine_extname __system_property_set _system_property_set2 #pragma redefine_extname __system_property_find _system_property_find2 diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index fac172748..f2387349b 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -1,3 +1,11 @@ +// Force using legacy_signal_inlines.h +#define __ANDROID_API_BACKUP__ __ANDROID_API__ +#undef __ANDROID_API__ +#define __ANDROID_API__ 20 +#include +#undef __ANDROID_API__ +#define __ANDROID_API__ __ANDROID_API_BACKUP__ + #include #include diff --git a/native/jni/utils/Android.mk b/native/jni/utils/Android.mk index 631915e95..1d5e083dc 100644 --- a/native/jni/utils/Android.mk +++ b/native/jni/utils/Android.mk @@ -2,14 +2,7 @@ LOCAL_PATH := $(call my-dir) # All Magisk common code lives here -include $(CLEAR_VARS) -LOCAL_MODULE:= libutils -LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated -LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) -LOCAL_EXPORT_STATIC_LIBRARIES := libcxx -LOCAL_STATIC_LIBRARIES := libcxx -LOCAL_SRC_FILES := \ - missing.cpp \ +UTILS_SRC_FILES := \ new.cpp \ files.cpp \ misc.cpp \ @@ -18,4 +11,20 @@ LOCAL_SRC_FILES := \ xwrap.cpp \ stream.cpp +include $(CLEAR_VARS) +LOCAL_MODULE:= libutils +LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) +LOCAL_EXPORT_STATIC_LIBRARIES := libcxx +LOCAL_STATIC_LIBRARIES := libcxx +LOCAL_SRC_FILES := $(UTILS_SRC_FILES) missing.cpp +include $(BUILD_STATIC_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE:= libutils-shared +LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) +LOCAL_EXPORT_STATIC_LIBRARIES := libcxx +LOCAL_STATIC_LIBRARIES := libcxx +LOCAL_SRC_FILES := $(UTILS_SRC_FILES) include $(BUILD_STATIC_LIBRARY) diff --git a/native/jni/utils/missing.cpp b/native/jni/utils/missing.cpp index 20ec85cc3..87ce2a333 100644 --- a/native/jni/utils/missing.cpp +++ b/native/jni/utils/missing.cpp @@ -1,19 +1,19 @@ -/* - * Host all missing/incomplete implementation in bionic - * Copied from various sources - * */ +// This file implements all missing symbols that should exist in normal API 21 +// libc.a but missing in our extremely lean libc.a replacements. -#include -#include -#include +#include +#include +#include #include +#include +#include -#include "missing.hpp" +extern "C" { /* Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c * License: BSD, full copyright notice please check original source */ -ssize_t compat_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) { +ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) { char *ptr, *eptr; if (*buf == nullptr || *bufsiz == 0) { @@ -49,14 +49,14 @@ ssize_t compat_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) { } } -ssize_t compat_getline(char **buf, size_t *bufsiz, FILE *fp) { +ssize_t getline(char **buf, size_t *bufsiz, FILE *fp) { return getdelim(buf, bufsiz, '\n', fp); } /* Original source: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/mntent.cpp * License: AOSP, full copyright notice please check original source */ -struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len) { +struct mntent *getmntent_r(FILE *fp, struct mntent *e, char *buf, int buf_len) { memset(e, 0, sizeof(*e)); while (fgets(buf, buf_len, fp) != nullptr) { // Entries look like "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0". @@ -79,31 +79,58 @@ struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf return nullptr; } -FILE *compat_setmntent(const char* path, const char* mode) { +FILE *setmntent(const char *path, const char *mode) { return fopen(path, mode); } -int compat_endmntent(FILE* fp) { +int endmntent(FILE *fp) { if (fp != nullptr) { fclose(fp); } return 1; } -char *compat_hasmntopt(const struct mntent* mnt, const char* opt) { - char* token = mnt->mnt_opts; - char* const end = mnt->mnt_opts + strlen(mnt->mnt_opts); - const size_t optLen = strlen(opt); - while (token) { - char* const tokenEnd = token + optLen; - if (tokenEnd > end) break; - if (memcmp(token, opt, optLen) == 0 && - (*tokenEnd == '\0' || *tokenEnd == ',' || *tokenEnd == '=')) { - return token; - } - token = strchr(token, ','); - if (token) token++; - } - return nullptr; +// Missing system call wrappers + +int setns(int fd, int nstype) { + return syscall(__NR_setns, fd, nstype); } +int unshare(int flags) { + return syscall(__NR_unshare, flags); +} + +int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { + return syscall(__NR_accept4, sockfd, addr, addrlen, flags); +} + +int dup3(int oldfd, int newfd, int flags) { + return syscall(__NR_dup3, oldfd, newfd, flags); +} + +ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { + return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz); +} + +int symlinkat(const char *target, int newdirfd, const char *linkpath) { + return syscall(__NR_symlinkat, target, newdirfd, linkpath); +} + +int linkat(int olddirfd, const char *oldpath, + int newdirfd, const char *newpath, int flags) { + return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags); +} + +int inotify_init1(int flags) { + return syscall(__NR_inotify_init1, flags); +} + +int faccessat(int dirfd, const char *pathname, int mode, int flags) { + return syscall(__NR_faccessat, dirfd, pathname, mode, flags); +} + +#if !defined(__LP64__) +void android_set_abort_message(const char *msg) {} +#endif + +} // extern "C" diff --git a/native/jni/utils/missing.hpp b/native/jni/utils/missing.hpp index afa6f2c5c..dfa9d9aa4 100644 --- a/native/jni/utils/missing.hpp +++ b/native/jni/utils/missing.hpp @@ -2,73 +2,8 @@ #include #include -#include -// Missing libc functions -#define getline compat_getline -#define getdelim compat_getdelim -#define getmntent_r compat_getmntent_r -#define setmntent compat_setmntent -#define endmntent compat_endmntent -#define hasmntopt compat_hasmntopt - -// Missing syscall wrappers -#define setns compat_setns -#define unshare compat_unshare -#define accept4 compat_accept4 -#define dup3 compat_dup3 -#define readlinkat compat_readlinkat -#define symlinkat compat_symlinkat -#define linkat compat_linkat -#define inotify_init1 compat_inotify_init1 -#define faccessat compat_faccessat -#define sigtimedwait compat_sigtimedwait - -ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream); -ssize_t compat_getdelim(char **lineptr, size_t *n, int delim, FILE *stream); -struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len); -FILE *compat_setmntent(const char* path, const char* mode); -int compat_endmntent(FILE* fp); -char *compat_hasmntopt(const struct mntent* mnt, const char* opt); - -static inline int compat_setns(int fd, int nstype) { - return syscall(__NR_setns, fd, nstype); -} - -static inline int compat_unshare(int flags) { - return syscall(__NR_unshare, flags); -} - -static inline int compat_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { - return syscall(__NR_accept4, sockfd, addr, addrlen, flags); -} - -static inline int compat_dup3(int oldfd, int newfd, int flags) { - return syscall(__NR_dup3, oldfd, newfd, flags); -} - -static inline ssize_t compat_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { - return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz); -} - -static inline int compat_symlinkat(const char *target, int newdirfd, const char *linkpath) { - return syscall(__NR_symlinkat, target, newdirfd, linkpath); -} - -static inline int compat_linkat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, int flags) { - return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags); -} - -static inline int compat_inotify_init1(int flags) { - return syscall(__NR_inotify_init1, flags); -} - -static inline int compat_faccessat(int dirfd, const char *pathname, int mode, int flags) { - return syscall(__NR_faccessat, dirfd, pathname, mode, flags); -} - -static inline int compat_sigtimedwait(const sigset_t* set, siginfo_t* info, const timespec* timeout) { +static inline int sigtimedwait(const sigset_t* set, siginfo_t* info, const timespec* timeout) { union { sigset_t set; sigset_t set64; diff --git a/native/jni/utils/selinux.cpp b/native/jni/utils/selinux.cpp index a5c035674..e625394c0 100644 --- a/native/jni/utils/selinux.cpp +++ b/native/jni/utils/selinux.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include diff --git a/native/jni/utils/stream.cpp b/native/jni/utils/stream.cpp index f27085188..e870c61ad 100644 --- a/native/jni/utils/stream.cpp +++ b/native/jni/utils/stream.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/native/jni/utils/xwrap.cpp b/native/jni/utils/xwrap.cpp index 565ea7b89..e1e2f166d 100644 --- a/native/jni/utils/xwrap.cpp +++ b/native/jni/utils/xwrap.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -198,26 +199,9 @@ int xlisten(int sockfd, int backlog) { return ret; } -static int accept4_compat(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { - int fd = accept(sockfd, addr, addrlen); - if (fd < 0) { - PLOGE("accept"); - } else { - if (flags & SOCK_CLOEXEC) - fcntl(fd, F_SETFD, FD_CLOEXEC); - if (flags & SOCK_NONBLOCK) { - int i = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, i | O_NONBLOCK); - } - } - return fd; -} - int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { int fd = accept4(sockfd, addr, addrlen, flags); if (fd < 0) { - if (errno == ENOSYS) - return accept4_compat(sockfd, addr, addrlen, flags); PLOGE("accept4"); } return fd; diff --git a/tools/ndk-bins/16/arm/crtbegin_dynamic.o b/tools/ndk-bins/16/arm/crtbegin_dynamic.o deleted file mode 100644 index 46556fb3f..000000000 Binary files a/tools/ndk-bins/16/arm/crtbegin_dynamic.o and /dev/null differ diff --git a/tools/ndk-bins/16/arm/crtbegin_so.o b/tools/ndk-bins/16/arm/crtbegin_so.o deleted file mode 100644 index 87062da77..000000000 Binary files a/tools/ndk-bins/16/arm/crtbegin_so.o and /dev/null differ diff --git a/tools/ndk-bins/16/arm/crtbegin_static.o b/tools/ndk-bins/16/arm/crtbegin_static.o deleted file mode 100644 index 6ff5ea907..000000000 Binary files a/tools/ndk-bins/16/arm/crtbegin_static.o and /dev/null differ diff --git a/tools/ndk-bins/16/arm/crtend_android.o b/tools/ndk-bins/16/arm/crtend_android.o deleted file mode 100644 index d2d7b4ad5..000000000 Binary files a/tools/ndk-bins/16/arm/crtend_android.o and /dev/null differ diff --git a/tools/ndk-bins/16/arm/crtend_so.o b/tools/ndk-bins/16/arm/crtend_so.o deleted file mode 100644 index e8a98e8ce..000000000 Binary files a/tools/ndk-bins/16/arm/crtend_so.o and /dev/null differ diff --git a/tools/ndk-bins/16/arm/libc.a b/tools/ndk-bins/16/arm/libc.a deleted file mode 100644 index 67ff8189e..000000000 Binary files a/tools/ndk-bins/16/arm/libc.a and /dev/null differ diff --git a/tools/ndk-bins/16/arm/libm.a b/tools/ndk-bins/16/arm/libm.a deleted file mode 100644 index 3e1ccb093..000000000 Binary files a/tools/ndk-bins/16/arm/libm.a and /dev/null differ diff --git a/tools/ndk-bins/16/arm/libstdc++.a b/tools/ndk-bins/16/arm/libstdc++.a deleted file mode 100644 index 8f495a5d0..000000000 Binary files a/tools/ndk-bins/16/arm/libstdc++.a and /dev/null differ diff --git a/tools/ndk-bins/16/i686/crtbegin_dynamic.o b/tools/ndk-bins/16/i686/crtbegin_dynamic.o deleted file mode 100644 index 53366dcb4..000000000 Binary files a/tools/ndk-bins/16/i686/crtbegin_dynamic.o and /dev/null differ diff --git a/tools/ndk-bins/16/i686/crtbegin_so.o b/tools/ndk-bins/16/i686/crtbegin_so.o deleted file mode 100644 index 1971da7fc..000000000 Binary files a/tools/ndk-bins/16/i686/crtbegin_so.o and /dev/null differ diff --git a/tools/ndk-bins/16/i686/crtbegin_static.o b/tools/ndk-bins/16/i686/crtbegin_static.o deleted file mode 100644 index 7f1a8d265..000000000 Binary files a/tools/ndk-bins/16/i686/crtbegin_static.o and /dev/null differ diff --git a/tools/ndk-bins/16/i686/crtend_android.o b/tools/ndk-bins/16/i686/crtend_android.o deleted file mode 100644 index 9c62fe09e..000000000 Binary files a/tools/ndk-bins/16/i686/crtend_android.o and /dev/null differ diff --git a/tools/ndk-bins/16/i686/crtend_so.o b/tools/ndk-bins/16/i686/crtend_so.o deleted file mode 100644 index 2c8850149..000000000 Binary files a/tools/ndk-bins/16/i686/crtend_so.o and /dev/null differ diff --git a/tools/ndk-bins/16/i686/libc.a b/tools/ndk-bins/16/i686/libc.a deleted file mode 100644 index 788b92e0d..000000000 Binary files a/tools/ndk-bins/16/i686/libc.a and /dev/null differ diff --git a/tools/ndk-bins/16/i686/libm.a b/tools/ndk-bins/16/i686/libm.a deleted file mode 100644 index 34f3c1f6d..000000000 Binary files a/tools/ndk-bins/16/i686/libm.a and /dev/null differ diff --git a/tools/ndk-bins/16/i686/libstdc++.a b/tools/ndk-bins/16/i686/libstdc++.a deleted file mode 100644 index 336bcfab8..000000000 Binary files a/tools/ndk-bins/16/i686/libstdc++.a and /dev/null differ diff --git a/tools/ndk-bins/16/i686/libz.a b/tools/ndk-bins/16/i686/libz.a deleted file mode 100644 index 5c91867c7..000000000 Binary files a/tools/ndk-bins/16/i686/libz.a and /dev/null differ diff --git a/tools/ndk-bins/21/arm/crtbegin_dynamic.o b/tools/ndk-bins/21/arm/crtbegin_dynamic.o index 41b89050c..46556fb3f 100644 Binary files a/tools/ndk-bins/21/arm/crtbegin_dynamic.o and b/tools/ndk-bins/21/arm/crtbegin_dynamic.o differ diff --git a/tools/ndk-bins/21/arm/crtbegin_static.o b/tools/ndk-bins/21/arm/crtbegin_static.o index 89d9a6744..6ff5ea907 100644 Binary files a/tools/ndk-bins/21/arm/crtbegin_static.o and b/tools/ndk-bins/21/arm/crtbegin_static.o differ diff --git a/tools/ndk-bins/21/arm/libc.a b/tools/ndk-bins/21/arm/libc.a index fb3ee8cdf..67ff8189e 100644 Binary files a/tools/ndk-bins/21/arm/libc.a and b/tools/ndk-bins/21/arm/libc.a differ diff --git a/tools/ndk-bins/21/arm/libm.a b/tools/ndk-bins/21/arm/libm.a index 9429fb527..3e1ccb093 100644 Binary files a/tools/ndk-bins/21/arm/libm.a and b/tools/ndk-bins/21/arm/libm.a differ diff --git a/tools/ndk-bins/21/arm/libstdc++.a b/tools/ndk-bins/21/arm/libstdc++.a index 698f3d61e..8f495a5d0 100644 Binary files a/tools/ndk-bins/21/arm/libstdc++.a and b/tools/ndk-bins/21/arm/libstdc++.a differ diff --git a/tools/ndk-bins/21/i686/crtbegin_dynamic.o b/tools/ndk-bins/21/i686/crtbegin_dynamic.o index de5826286..53366dcb4 100644 Binary files a/tools/ndk-bins/21/i686/crtbegin_dynamic.o and b/tools/ndk-bins/21/i686/crtbegin_dynamic.o differ diff --git a/tools/ndk-bins/21/i686/libc.a b/tools/ndk-bins/21/i686/libc.a index 94a33f3e0..788b92e0d 100644 Binary files a/tools/ndk-bins/21/i686/libc.a and b/tools/ndk-bins/21/i686/libc.a differ diff --git a/tools/ndk-bins/21/i686/libm.a b/tools/ndk-bins/21/i686/libm.a index a944a6a92..34f3c1f6d 100644 Binary files a/tools/ndk-bins/21/i686/libm.a and b/tools/ndk-bins/21/i686/libm.a differ diff --git a/tools/ndk-bins/21/i686/libstdc++.a b/tools/ndk-bins/21/i686/libstdc++.a index 4565d3f30..336bcfab8 100644 Binary files a/tools/ndk-bins/21/i686/libstdc++.a and b/tools/ndk-bins/21/i686/libstdc++.a differ diff --git a/tools/ndk-bins/21/i686/libz.a b/tools/ndk-bins/21/i686/libz.a index 6ea48c692..5c91867c7 100644 Binary files a/tools/ndk-bins/21/i686/libz.a and b/tools/ndk-bins/21/i686/libz.a differ