Skip to content

Commit 498ae9a

Browse files
committed
Add riscv64 support
1 parent c9e11c6 commit 498ae9a

File tree

10 files changed

+69
-10
lines changed

10 files changed

+69
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ apache/local/generated
99
.DS_Store
1010
/build
1111
/captures
12+
/apache/build

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
[submodule "external/lsplt"]
2020
path = external/lsplt
2121
url = https://github.com/JingMatrix/LSPlt
22+
[submodule "external/riscv64-inline-hook"]
23+
path = external/riscv64-inline-hook
24+
url = https://github.com/eirv/riscv64-inline-hook

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ You can contribute translation [here](https://crowdin.com/project/lsposed_jingma
5858
- [XposedBridge](https://github.com/rovo89/XposedBridge): the OG Xposed framework APIs
5959
- [LSPlant](https://github.com/JingMatrix/LSPlant): the core ART hooking framework
6060
- [Dobby](https://github.com/JingMatrix/Dobby): inline hooker for `LSPlant` and `native_api` implement
61+
- [riscv64-inline-hook](https://github.com/eirv/riscv64-inline-hook): inline hooker for `RISC-V 64`
6162
- [EdXposed](https://github.com/ElderDrivers/EdXposed): fork source
6263
- [xz-embedded](https://github.com/tukaani-project/xz-embedded): decompress `.gnu_debugdata` header section of stripped `libart.so`
6364
- ~~[Riru](https://github.com/RikkaApps/Riru): provides a way to inject code into zygote process~~

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ cmaker {
4545
)
4646
cFlags.addAll(flags)
4747
cppFlags.addAll(flags)
48-
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
48+
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64", "riscv64")
4949
}
5050
buildTypes {
5151
if (it.name == "release") {

core/src/main/jni/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ set(IGNORED_WARNINGS
1717
-Wno-gnu-flexible-array-initializer
1818
-Wno-variadic-macros
1919
-Wno-zero-length-array)
20+
set(${PROJECT_NAME}_LINK_LIBS lsplant_static xz_static log fmt-header-only)
21+
22+
if (ANDROID_ABI STREQUAL "riscv64")
23+
set(${PROJECT_NAME}_LINK_LIBS ${${PROJECT_NAME}_LINK_LIBS} rv64hook-static)
24+
else ()
25+
set(${PROJECT_NAME}_LINK_LIBS ${${PROJECT_NAME}_LINK_LIBS} dobby_static)
26+
endif ()
2027

2128
target_include_directories(${PROJECT_NAME} PUBLIC include)
2229
target_include_directories(${PROJECT_NAME} PRIVATE src ${EXTERNAL_ROOT}/xz-embedded/linux/include)
2330
target_compile_options(${PROJECT_NAME} PRIVATE -Wpedantic ${IGNORED_WARNINGS})
2431

25-
target_link_libraries(${PROJECT_NAME} PUBLIC dobby_static lsplant_static xz_static log fmt-header-only)
32+
target_link_libraries(${PROJECT_NAME} PUBLIC ${${PROJECT_NAME}_LINK_LIBS})
2633
target_link_libraries(${PROJECT_NAME} PRIVATE dex_builder_static)

core/src/main/jni/src/native_api.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
#include <cstdint>
2929
#include <dlfcn.h>
3030
#include <string>
31+
#ifdef __riscv
32+
#include <rv64hook.h>
33+
#else
3134
#include <dobby.h>
35+
#endif
3236

3337
#include "config.h"
3438
#include "utils/hook_helper.hpp"
@@ -56,24 +60,35 @@ namespace lspd {
5660
if constexpr (isDebug) {
5761
Dl_info info;
5862
if (dladdr(original, &info))
59-
LOGD("Dobby hooking {} ({}) from {} ({})",
63+
LOGD("Inline hooking {} ({}) from {} ({})",
6064
info.dli_sname ? info.dli_sname : "(unknown symbol)",
6165
info.dli_saddr ? info.dli_saddr : original,
6266
info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase);
6367
}
68+
#ifdef __riscv
69+
rv64hook::ScopedRWXMemory rwx(original);
70+
return rv64hook::InlineHook(original, replace, backup) != nullptr ? 0 : -1;
71+
#else
6472
return DobbyHook(original, reinterpret_cast<dobby_dummy_func_t>(replace), reinterpret_cast<dobby_dummy_func_t *>(backup));
73+
#endif
6574
}
6675

6776
inline int UnhookInline(void *original) {
6877
if constexpr (isDebug) {
6978
Dl_info info;
7079
if (dladdr(original, &info))
71-
LOGD("Dobby unhooking {} ({}) from {} ({})",
80+
LOGD("Inline unhooking {} ({}) from {} ({})",
7281
info.dli_sname ? info.dli_sname : "(unknown symbol)",
7382
info.dli_saddr ? info.dli_saddr : original,
7483
info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase);
7584
}
85+
#ifdef __riscv
86+
rv64hook::ScopedRWXMemory rwx(original);
87+
rv64hook::UnhookFunction(original);
88+
return 0;
89+
#else
7690
return DobbyDestroy(original);
91+
#endif
7792
}
7893
}
7994

external/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
project(external)
22

3+
if (ANDROID_ABI STREQUAL "riscv64")
4+
set(ANDROID_PLATFORM "android-35")
5+
endif ()
6+
37
macro(SET_OPTION option value)
48
set(${option} ${value} CACHE INTERNAL "" FORCE)
59
endmacro()
@@ -20,7 +24,11 @@ target_compile_options(xz_static PRIVATE -DXZ_USE_CRC64)
2024
target_include_directories(xz_static PRIVATE ${XZ_INCLUDES})
2125

2226
OPTION(LSPLANT_BUILD_SHARED OFF)
23-
add_subdirectory(dobby)
27+
if (ANDROID_ABI STREQUAL "riscv64")
28+
add_subdirectory(riscv64-inline-hook)
29+
else ()
30+
add_subdirectory(dobby)
31+
endif ()
2432
add_subdirectory(fmt)
2533
add_subdirectory(lsplant/lsplant/src/main/jni)
2634
target_compile_definitions(fmt-header-only INTERFACE FMT_USE_LOCALE=0 FMT_USE_FLOAT=0 FMT_USE_DOUBLE=0 FMT_USE_LONG_DOUBLE=0 FMT_USE_BITINT=0)

external/riscv64-inline-hook

Submodule riscv64-inline-hook added at 9887178

magisk-loader/magisk_module/customize.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ check_incompatible_module
6262
enforce_install_from_magisk_app
6363

6464
# Check architecture
65-
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then
65+
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ] && [ "$ARCH" != "riscv64" ]; then
6666
abort "! Unsupported platform: $ARCH"
6767
else
6868
ui_print "- Device platform: $ARCH"
@@ -115,6 +115,11 @@ if [ "$FLAVOR" == "zygisk" ]; then
115115
mv "$MODPATH/zygisk/liblspd.so" "$MODPATH/zygisk/x86_64.so"
116116
fi
117117
fi
118+
119+
if [ "$ARCH" = "riscv64" ]; then
120+
extract "$ZIPFILE" "lib/riscv64/liblspd.so" "$MODPATH/zygisk" true
121+
mv "$MODPATH/zygisk/liblspd.so" "$MODPATH/zygisk/riscv64.so"
122+
fi
118123
fi
119124

120125
if [ "$API" -ge 29 ]; then
@@ -145,12 +150,19 @@ if [ "$API" -ge 29 ]; then
145150
mv "$MODPATH/bin/dex2oat" "$MODPATH/bin/dex2oat64"
146151
mv "$MODPATH/bin/liboat_hook.so" "$MODPATH/bin/liboat_hook64.so"
147152
fi
153+
elif [ "$ARCH" == "riscv64" ]; then
154+
extract "$ZIPFILE" "bin/riscv64/dex2oat" "$MODPATH/bin" true
155+
extract "$ZIPFILE" "bin/riscv64/liboat_hook.so" "$MODPATH/bin" true
156+
mv "$MODPATH/bin/dex2oat" "$MODPATH/bin/dex2oat64"
157+
mv "$MODPATH/bin/liboat_hook.so" "$MODPATH/bin/liboat_hook64.so"
148158
fi
149159

150160
ui_print "- Patching binaries"
151161
DEV_PATH=$(tr -dc 'a-z0-9' < /dev/urandom | head -c 32)
152162
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/g" "$MODPATH/daemon.apk"
153-
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat32"
163+
if [ "$ARCH" != "riscv64" ]; then
164+
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat32"
165+
fi
154166
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat64"
155167
else
156168
extract "$ZIPFILE" 'system.prop' "$MODPATH"

magisk-loader/magisk_module/util_functions.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,28 @@ check_magisk_version() {
3535

3636
require_new_android() {
3737
ui_print "*********************************************************"
38-
ui_print "! Unsupported Android version ${1} (below Oreo MR1)"
38+
ui_print "! Unsupported Android version ${1} (below ${2})"
3939
ui_print "! Learn more from our GitHub"
4040
[ "$BOOTMODE" == "true" ] && am start -a android.intent.action.VIEW -d https://github.com/JingMatrix/LSPosed/#supported-versions
4141
abort "*********************************************************"
4242
}
4343

4444
check_android_version() {
45-
if [ "$API" -ge 27 ]; then
45+
local required_version
46+
local min_api
47+
48+
if [ "$ARCH" == "riscv64" ]; then
49+
required_version="Vanilla Ice Cream"
50+
min_api=35
51+
else
52+
required_version="Oreo MR1"
53+
min_api=27
54+
fi
55+
56+
if [ "$API" -ge "$min_api" ]; then
4657
ui_print "- Android SDK version: $API"
4758
else
48-
require_new_android "$API"
59+
require_new_android "$API" "$required_version"
4960
fi
5061
}
5162

0 commit comments

Comments
 (0)