Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add riscv64 support #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ apache/local/generated
.DS_Store
/build
/captures
/apache/build
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "external/lsplt"]
path = external/lsplt
url = https://github.com/JingMatrix/LSPlt
[submodule "external/riscv64-inline-hook"]
path = external/riscv64-inline-hook
url = https://github.com/eirv/riscv64-inline-hook
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can contribute translation [here](https://crowdin.com/project/lsposed_jingma
- [XposedBridge](https://github.com/rovo89/XposedBridge): the OG Xposed framework APIs
- [LSPlant](https://github.com/JingMatrix/LSPlant): the core ART hooking framework
- [Dobby](https://github.com/JingMatrix/Dobby): inline hooker for `LSPlant` and `native_api` implement
- [riscv64-inline-hook](https://github.com/eirv/riscv64-inline-hook): inline hooker for `RISC-V 64`
- [EdXposed](https://github.com/ElderDrivers/EdXposed): fork source
- [xz-embedded](https://github.com/tukaani-project/xz-embedded): decompress `.gnu_debugdata` header section of stripped `libart.so`
- ~~[Riru](https://github.com/RikkaApps/Riru): provides a way to inject code into zygote process~~
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cmaker {
)
cFlags.addAll(flags)
cppFlags.addAll(flags)
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64", "riscv64")
}
buildTypes {
if (it.name == "release") {
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ set(IGNORED_WARNINGS
-Wno-gnu-flexible-array-initializer
-Wno-variadic-macros
-Wno-zero-length-array)
set(${PROJECT_NAME}_LINK_LIBS lsplant_static xz_static log fmt-header-only)

if (ANDROID_ABI STREQUAL "riscv64")
set(${PROJECT_NAME}_LINK_LIBS ${${PROJECT_NAME}_LINK_LIBS} rv64hook-static)
else ()
set(${PROJECT_NAME}_LINK_LIBS ${${PROJECT_NAME}_LINK_LIBS} dobby_static)
endif ()

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

target_link_libraries(${PROJECT_NAME} PUBLIC dobby_static lsplant_static xz_static log fmt-header-only)
target_link_libraries(${PROJECT_NAME} PUBLIC ${${PROJECT_NAME}_LINK_LIBS})
target_link_libraries(${PROJECT_NAME} PRIVATE dex_builder_static)
19 changes: 17 additions & 2 deletions core/src/main/jni/src/native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
#include <cstdint>
#include <dlfcn.h>
#include <string>
#ifdef __riscv
#include <rv64hook.h>
#else
#include <dobby.h>
#endif

#include "config.h"
#include "utils/hook_helper.hpp"
Expand Down Expand Up @@ -56,24 +60,35 @@ namespace lspd {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
LOGD("Dobby hooking {} ({}) from {} ({})",
LOGD("Inline hooking {} ({}) from {} ({})",
info.dli_sname ? info.dli_sname : "(unknown symbol)",
info.dli_saddr ? info.dli_saddr : original,
info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase);
}
#ifdef __riscv
rv64hook::ScopedRWXMemory rwx(original);
return rv64hook::InlineHook(original, replace, backup) != nullptr ? 0 : -1;
#else
return DobbyHook(original, reinterpret_cast<dobby_dummy_func_t>(replace), reinterpret_cast<dobby_dummy_func_t *>(backup));
#endif
}

inline int UnhookInline(void *original) {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
LOGD("Dobby unhooking {} ({}) from {} ({})",
LOGD("Inline unhooking {} ({}) from {} ({})",
info.dli_sname ? info.dli_sname : "(unknown symbol)",
info.dli_saddr ? info.dli_saddr : original,
info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase);
}
#ifdef __riscv
rv64hook::ScopedRWXMemory rwx(original);
rv64hook::UnhookFunction(original);
return 0;
#else
return DobbyDestroy(original);
#endif
}
}

Expand Down
10 changes: 9 additions & 1 deletion external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
project(external)

if (ANDROID_ABI STREQUAL "riscv64")
set(ANDROID_PLATFORM "android-35")
endif ()

macro(SET_OPTION option value)
set(${option} ${value} CACHE INTERNAL "" FORCE)
endmacro()
Expand All @@ -20,7 +24,11 @@ target_compile_options(xz_static PRIVATE -DXZ_USE_CRC64)
target_include_directories(xz_static PRIVATE ${XZ_INCLUDES})

OPTION(LSPLANT_BUILD_SHARED OFF)
add_subdirectory(dobby)
if (ANDROID_ABI STREQUAL "riscv64")
add_subdirectory(riscv64-inline-hook)
else ()
add_subdirectory(dobby)
endif ()
add_subdirectory(fmt)
add_subdirectory(lsplant/lsplant/src/main/jni)
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)
1 change: 1 addition & 0 deletions external/riscv64-inline-hook
Submodule riscv64-inline-hook added at 988717
16 changes: 14 additions & 2 deletions magisk-loader/magisk_module/customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ check_incompatible_module
enforce_install_from_magisk_app

# Check architecture
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ] && [ "$ARCH" != "riscv64" ]; then
abort "! Unsupported platform: $ARCH"
else
ui_print "- Device platform: $ARCH"
Expand Down Expand Up @@ -115,6 +115,11 @@ if [ "$FLAVOR" == "zygisk" ]; then
mv "$MODPATH/zygisk/liblspd.so" "$MODPATH/zygisk/x86_64.so"
fi
fi

if [ "$ARCH" = "riscv64" ]; then
extract "$ZIPFILE" "lib/riscv64/liblspd.so" "$MODPATH/zygisk" true
mv "$MODPATH/zygisk/liblspd.so" "$MODPATH/zygisk/riscv64.so"
fi
fi

if [ "$API" -ge 29 ]; then
Expand Down Expand Up @@ -145,12 +150,19 @@ if [ "$API" -ge 29 ]; then
mv "$MODPATH/bin/dex2oat" "$MODPATH/bin/dex2oat64"
mv "$MODPATH/bin/liboat_hook.so" "$MODPATH/bin/liboat_hook64.so"
fi
elif [ "$ARCH" == "riscv64" ]; then
extract "$ZIPFILE" "bin/riscv64/dex2oat" "$MODPATH/bin" true
extract "$ZIPFILE" "bin/riscv64/liboat_hook.so" "$MODPATH/bin" true
mv "$MODPATH/bin/dex2oat" "$MODPATH/bin/dex2oat64"
mv "$MODPATH/bin/liboat_hook.so" "$MODPATH/bin/liboat_hook64.so"
fi

ui_print "- Patching binaries"
DEV_PATH=$(tr -dc 'a-z0-9' < /dev/urandom | head -c 32)
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/g" "$MODPATH/daemon.apk"
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat32"
if [ "$ARCH" != "riscv64" ]; then
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat32"
fi
sed -i "s/5291374ceda0aef7c5d86cd2a4f6a3ac/$DEV_PATH/" "$MODPATH/bin/dex2oat64"
else
extract "$ZIPFILE" 'system.prop' "$MODPATH"
Expand Down
17 changes: 14 additions & 3 deletions magisk-loader/magisk_module/util_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,28 @@ check_magisk_version() {

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

check_android_version() {
if [ "$API" -ge 27 ]; then
local required_version
local min_api

if [ "$ARCH" == "riscv64" ]; then
required_version="Vanilla Ice Cream"
min_api=35
else
required_version="Oreo MR1"
min_api=27
fi

if [ "$API" -ge "$min_api" ]; then
ui_print "- Android SDK version: $API"
else
require_new_android "$API"
require_new_android "$API" "$required_version"
fi
}

Expand Down
Loading