diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 982174d5d..2806f0e7a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -27,7 +27,7 @@ android { versionName = Config.version versionCode = Config.versionCode ndk { - abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64") + abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64", "riscv64") debugSymbolLevel = "FULL" } } diff --git a/build.py b/build.py index 42dcfc2f1..93579cc3e 100755 --- a/build.py +++ b/build.py @@ -68,12 +68,13 @@ if shutil.which("ccache") is not None: cpu_count = multiprocessing.cpu_count() os_name = platform.system().lower() -archs = ["armeabi-v7a", "x86", "arm64-v8a", "x86_64"] +archs = ["armeabi-v7a", "x86", "arm64-v8a", "x86_64", "riscv64"] triples = [ "armv7a-linux-androideabi", "i686-linux-android", "aarch64-linux-android", "x86_64-linux-android", + "riscv64-linux-android", ] default_targets = ["magisk", "magiskinit", "magiskboot", "magiskpolicy", "busybox"] support_targets = default_targets + ["resetprop"] diff --git a/buildSrc/src/main/java/Setup.kt b/buildSrc/src/main/java/Setup.kt index 61f564d61..06e1a712f 100644 --- a/buildSrc/src/main/java/Setup.kt +++ b/buildSrc/src/main/java/Setup.kt @@ -229,32 +229,16 @@ fun Project.setupApp() { val syncLibs by tasks.registering(Sync::class) { into("src/main/jniLibs") - into("armeabi-v7a") { - from(rootProject.file("native/out/armeabi-v7a")) { - include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { "lib$it.so" } - } - } - into("x86") { - from(rootProject.file("native/out/x86")) { - include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { "lib$it.so" } - } - } - into("arm64-v8a") { - from(rootProject.file("native/out/arm64-v8a")) { - include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { "lib$it.so" } - } - } - into("x86_64") { - from(rootProject.file("native/out/x86_64")) { - include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { "lib$it.so" } + for (abi in arrayOf("armeabi-v7a", "x86", "arm64-v8a", "x86_64", "riscv64")) { + into(abi) { + from(rootProject.file("native/out/$abi")) { + include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") + rename { "lib$it.so" } + } } } onlyIf { - if (inputs.sourceFiles.files.size != 20) + if (inputs.sourceFiles.files.size != 25) throw StopExecutionException("Please build binaries first! (./build.py binary)") true } diff --git a/native/build.gradle.kts b/native/build.gradle.kts index 33494048e..d31983004 100644 --- a/native/build.gradle.kts +++ b/native/build.gradle.kts @@ -25,6 +25,7 @@ android { "B_MAGISK=1", "B_INIT=1", "B_BOOT=1", "B_POLICY=1", "B_PRELOAD=1", "B_PROP=1", "B_CRT0=1" ) + abiFilters("armeabi-v7a", "arm64-v8a", "x86", "x86_64", "riscv64") } } } diff --git a/native/src/Application.mk b/native/src/Application.mk index 72db816fd..0c3c9eed0 100644 --- a/native/src/Application.mk +++ b/native/src/Application.mk @@ -1,5 +1,5 @@ APP_BUILD_SCRIPT := src/Android.mk -APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 +APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 riscv64 APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -flto APP_LDFLAGS := -flto -Wl,--icf=all APP_CPPFLAGS := -std=c++23 diff --git a/native/src/base/include/embed.hpp b/native/src/base/include/embed.hpp index c34394a55..6575af25d 100644 --- a/native/src/base/include/embed.hpp +++ b/native/src/base/include/embed.hpp @@ -6,6 +6,8 @@ #include #elif defined(__x86_64__) #include +#elif defined(__riscv) +#include #else #error Unsupported ABI #endif diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index 2f5113866..dfa31da83 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -418,6 +418,9 @@ static void collect_modules(bool open_zygisk) { #elif defined(__x86_64__) info.z32 = openat(modfd, "zygisk/x86.so", O_RDONLY | O_CLOEXEC); info.z64 = openat(modfd, "zygisk/x86_64.so", O_RDONLY | O_CLOEXEC); +#elif defined(__riscv) + info.z32 = -1; + info.z64 = openat(modfd, "zygisk/riscv64.so", O_RDONLY | O_CLOEXEC); #else #error Unsupported ABI #endif diff --git a/native/src/core/zygisk/hook.cpp b/native/src/core/zygisk/hook.cpp index 151ea93b2..7ab9b5fde 100644 --- a/native/src/core/zygisk/hook.cpp +++ b/native/src/core/zygisk/hook.cpp @@ -313,6 +313,14 @@ static const NativeBridgeRuntimeCallbacks* find_runtime_callbacks(struct _Unwind if (val >= start && val < end) return reinterpret_cast(val); } +#elif defined(__riscv) + // x8-x9, x18-x27 callee-saved registers + for (int i : {8, 9, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27}) { + auto val = static_cast(_Unwind_GetGR(ctx, i)); + ZLOGV("x%d = %p\n", i, reinterpret_cast(val)); + if (val >= start && val < end) + return reinterpret_cast(val); + } #else #error "Unsupported architecture" #endif diff --git a/native/src/external/Android.mk b/native/src/external/Android.mk index 06f4d7356..b2b1597e8 100644 --- a/native/src/external/Android.mk +++ b/native/src/external/Android.mk @@ -215,6 +215,9 @@ LOCAL_SRC_FILES := \ selinux/libsepol/cil/src/cil_write_ast.c LOCAL_CFLAGS := -Wno-unused-but-set-variable +ifeq ($(TARGET_ARCH),riscv64) +LOCAL_CFLAGS += -DHAVE_REALLOCARRAY +endif include $(BUILD_STATIC_LIBRARY) # libselinux.a diff --git a/scripts/avd_magisk.sh b/scripts/avd_magisk.sh index 422a03eb7..325999f56 100755 --- a/scripts/avd_magisk.sh +++ b/scripts/avd_magisk.sh @@ -58,7 +58,7 @@ for file in lib*.so; do mv "$file" "${file:3:${#file}-6}" done -if $IS64BIT; then +if [ -e "/system/bin/linker" ]; then unzip -oj magisk.apk "lib/$ABI32/libmagisk.so" mv libmagisk.so magisk32 chmod 755 magisk32 diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index a1e96f785..923a15b6c 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -494,11 +494,7 @@ remove_system_su() { api_level_arch_detect() { API=$(grep_get_prop ro.build.version.sdk) ABI=$(grep_get_prop ro.product.cpu.abi) - if [ "$ABI" = "x86" ]; then - ARCH=x86 - ABI32=x86 - IS64BIT=false - elif [ "$ABI" = "arm64-v8a" ]; then + if [ "$ABI" = "arm64-v8a" ]; then ARCH=arm64 ABI32=armeabi-v7a IS64BIT=true @@ -506,11 +502,18 @@ api_level_arch_detect() { ARCH=x64 ABI32=x86 IS64BIT=true - else + elif [ "$ABI" = "armeabi-v7a" ]; then ARCH=arm - ABI=armeabi-v7a ABI32=armeabi-v7a IS64BIT=false + elif [ "$ABI" = "x86" ]; then + ARCH=x86 + ABI32=x86 + IS64BIT=false + elif [ "$ABI" = "riscv64" ]; then + ARCH=riscv64 + ABI32=riscv32 + IS64BIT=true fi }