├── project ├── example │ ├── app │ │ ├── .gitignore │ │ ├── src │ │ │ ├── main │ │ │ │ ├── res │ │ │ │ │ ├── values │ │ │ │ │ │ ├── strings.xml │ │ │ │ │ │ ├── colors.xml │ │ │ │ │ │ ├── dimens.xml │ │ │ │ │ │ └── styles.xml │ │ │ │ │ ├── mipmap-hdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-mdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── values-w820dp │ │ │ │ │ │ └── dimens.xml │ │ │ │ │ └── layout │ │ │ │ │ │ └── activity_main.xml │ │ │ │ ├── java │ │ │ │ │ └── com │ │ │ │ │ │ └── tbd │ │ │ │ │ │ └── example │ │ │ │ │ │ ├── MainActivity.java │ │ │ │ │ │ └── MyApplication.java │ │ │ │ └── AndroidManifest.xml │ │ │ ├── test │ │ │ │ └── java │ │ │ │ │ └── com │ │ │ │ │ └── tbd │ │ │ │ │ └── example │ │ │ │ │ └── ExampleUnitTest.java │ │ │ └── androidTest │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── tbd │ │ │ │ └── example │ │ │ │ └── ApplicationTest.java │ │ ├── proguard-rules.pro │ │ └── build.gradle │ ├── turbodex │ │ ├── .gitignore │ │ ├── libs │ │ │ ├── x86 │ │ │ │ └── libturbo-dex.so │ │ │ ├── armeabi │ │ │ │ └── libturbo-dex.so │ │ │ ├── x86_64 │ │ │ │ └── libturbo-dex.so │ │ │ ├── arm64-v8a │ │ │ │ └── libturbo-dex.so │ │ │ └── armeabi-v7a │ │ │ │ └── libturbo-dex.so │ │ ├── gradle.properties │ │ ├── src │ │ │ ├── main │ │ │ │ ├── AndroidManifest.xml │ │ │ │ └── java │ │ │ │ │ └── com │ │ │ │ │ └── lody │ │ │ │ │ └── turbodex │ │ │ │ │ └── TurboDex.java │ │ │ ├── test │ │ │ │ └── java │ │ │ │ │ └── com │ │ │ │ │ └── lody │ │ │ │ │ └── turbodex │ │ │ │ │ └── ExampleUnitTest.java │ │ │ └── androidTest │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lody │ │ │ │ └── turbodex │ │ │ │ └── ApplicationTest.java │ │ ├── proguard-rules.pro │ │ └── build.gradle │ ├── settings.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── .gitignore │ ├── build.gradle │ └── gradle.properties ├── turbodex │ ├── app │ │ ├── .gitignore │ │ ├── src │ │ │ ├── main │ │ │ │ ├── res │ │ │ │ │ ├── values │ │ │ │ │ │ ├── strings.xml │ │ │ │ │ │ ├── colors.xml │ │ │ │ │ │ ├── dimens.xml │ │ │ │ │ │ └── styles.xml │ │ │ │ │ ├── mipmap-hdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-mdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── values-w820dp │ │ │ │ │ │ └── dimens.xml │ │ │ │ │ └── layout │ │ │ │ │ │ └── activity_main.xml │ │ │ │ ├── java │ │ │ │ │ └── com │ │ │ │ │ │ └── tbd │ │ │ │ │ │ └── app │ │ │ │ │ │ ├── MainActivity.java │ │ │ │ │ │ └── Application.java │ │ │ │ └── AndroidManifest.xml │ │ │ ├── test │ │ │ │ └── java │ │ │ │ │ └── com │ │ │ │ │ └── tbd │ │ │ │ │ └── app │ │ │ │ │ └── ExampleUnitTest.java │ │ │ └── androidTest │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── tbd │ │ │ │ └── app │ │ │ │ └── ApplicationTest.java │ │ ├── proguard-rules.pro │ │ └── build.gradle │ ├── turbodex │ │ ├── .gitignore │ │ ├── src │ │ │ └── main │ │ │ │ ├── jni │ │ │ │ ├── MSHook │ │ │ │ │ ├── x86.h │ │ │ │ │ ├── Thumb.h │ │ │ │ │ ├── x86_64.h │ │ │ │ │ ├── PosixMemory.h │ │ │ │ │ ├── SubstrateStruct.h │ │ │ │ │ ├── util.h │ │ │ │ │ ├── CydiaSubstrate.h │ │ │ │ │ ├── hook.h │ │ │ │ │ ├── Hooker.h │ │ │ │ │ ├── hook.cpp │ │ │ │ │ ├── Debug.h │ │ │ │ │ ├── Hooker.cpp │ │ │ │ │ ├── PosixMemory.cpp │ │ │ │ │ ├── Log.h │ │ │ │ │ ├── ARM.h │ │ │ │ │ ├── Debug.cpp │ │ │ │ │ ├── hde64.h │ │ │ │ │ ├── ARM.cpp │ │ │ │ │ ├── x86.cpp │ │ │ │ │ ├── util.cpp │ │ │ │ │ ├── x86_64.cpp │ │ │ │ │ └── Thumb.cpp │ │ │ │ ├── core │ │ │ │ │ ├── FastLoadDex.h │ │ │ │ │ ├── HookCore.h │ │ │ │ │ └── FastLoadDex.cpp │ │ │ │ ├── Application.mk │ │ │ │ ├── Logger.h │ │ │ │ ├── XJniHook.cpp │ │ │ │ ├── XJniHook.h │ │ │ │ └── Android.mk │ │ │ │ ├── AndroidManifest.xml │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lody │ │ │ │ └── turbodex │ │ │ │ └── TurboDex.java │ │ ├── proguard-rules.pro │ │ └── build.gradle │ ├── settings.gradle │ ├── .gitignore │ ├── build.gradle │ └── gradle.properties └── README.md ├── .gitignore ├── CHINESE.md ├── README.md └── LICENSE /project/example/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /project/turbodex/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /project/example/turbodex/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /project/example/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':turbodex' 2 | -------------------------------------------------------------------------------- /project/turbodex/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':turbodex', ':app' 2 | -------------------------------------------------------------------------------- /project/example/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Example 3 | 4 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | App 3 | 4 | -------------------------------------------------------------------------------- /project/example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /project/example/turbodex/libs/x86/libturbo-dex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/turbodex/libs/x86/libturbo-dex.so -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/x86.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/turbodex/src/main/jni/MSHook/x86.h -------------------------------------------------------------------------------- /project/example/turbodex/libs/armeabi/libturbo-dex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/turbodex/libs/armeabi/libturbo-dex.so -------------------------------------------------------------------------------- /project/example/turbodex/libs/x86_64/libturbo-dex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/turbodex/libs/x86_64/libturbo-dex.so -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Thumb.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/turbodex/src/main/jni/MSHook/Thumb.h -------------------------------------------------------------------------------- /project/example/turbodex/libs/arm64-v8a/libturbo-dex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/turbodex/libs/arm64-v8a/libturbo-dex.so -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/x86_64.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/turbodex/src/main/jni/MSHook/x86_64.h -------------------------------------------------------------------------------- /project/example/turbodex/libs/armeabi-v7a/libturbo-dex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/turbodex/libs/armeabi-v7a/libturbo-dex.so -------------------------------------------------------------------------------- /project/example/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/example/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/example/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/PosixMemory.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/turbodex/src/main/jni/MSHook/PosixMemory.h -------------------------------------------------------------------------------- /project/example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/SubstrateStruct.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/turbodex/src/main/jni/MSHook/SubstrateStruct.h -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asLody/TurboDex/HEAD/project/turbodex/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /project/example/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .idea 10 | gradlew.bat 11 | gradlew -------------------------------------------------------------------------------- /project/turbodex/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .idea 10 | gradlew.bat 11 | gradlew -------------------------------------------------------------------------------- /project/example/turbodex/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_BINTRAY_NAME=com.github.asLody:turbodex 2 | POM_DESCRIPTION=Fast load Dex file in runtime 3 | POM_ARTIFACT_ID=turbodex 4 | 5 | VERSION_NAME=1.1.0 6 | VERSION_CODE=1604221845 -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/core/FastLoadDex.h: -------------------------------------------------------------------------------- 1 | #ifndef ____FastLoadDex__ 2 | #define ____FastLoadDex__ 3 | 4 | void enableFastLoadDex(); 5 | 6 | void closeFastLoadDex(); 7 | 8 | 9 | #endif /* defined(____FastLoadDex__) */ -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/Application.mk: -------------------------------------------------------------------------------- 1 | NDK_TOOLCHAIN_VERSION=4.8 2 | APP_ABI := arm64-v8a armeabi armeabi-v7a x86 x86_64 3 | APP_STL=gnustl_static 4 | APP_CPPFLAGS := -std=c++11 -fexceptions -frtti 5 | APP_CPPFLAGS +=-fpermissive -------------------------------------------------------------------------------- /project/example/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /project/example/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/util.h: -------------------------------------------------------------------------------- 1 | #ifndef HOOK_UTIL_H_ 2 | #define HOOK_UTIL_H_ 3 | extern int find_name(pid_t pid, const char *name,const char *libn, unsigned long *addr); 4 | extern int find_libbase(pid_t pid, char *libn, unsigned long *addr); 5 | #endif 6 | -------------------------------------------------------------------------------- /project/example/turbodex/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /project/example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/core/HookCore.h: -------------------------------------------------------------------------------- 1 | #ifndef ____HookCore__ 2 | #define ____HookCore__ 3 | 4 | #include "../MSHook/Hooker.h" 5 | #define LIBC_SO "libc.so" 6 | 7 | #define HOOK(func) Cydia::elfHookFunction("libc.so", #func, (void*) my_##func, (void**) &org_##func) 8 | 9 | 10 | #endif /* defined(____HookCore__) */ -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/Logger.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define LOG_TAG "Native-X" 5 | 6 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) 7 | 8 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) 9 | 10 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/XJniHook.cpp: -------------------------------------------------------------------------------- 1 | #include "XJniHook.h" 2 | #include "Logger.h" 3 | #include "core/FastLoadDex.h" 4 | 5 | 6 | void Java_com_lody_turbodex_TurboDex_nativeEnableTurboDex(JNIEnv* env,jobject thiz) { 7 | enableFastLoadDex(); 8 | } 9 | 10 | void Java_com_lody_turbodex_TurboDex_nativeDisableTurboDex(JNIEnv* env,jobject thiz) { 11 | closeFastLoadDex(); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /project/example/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /project/turbodex/app/src/test/java/com/tbd/app/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.tbd.app; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /project/example/app/src/test/java/com/tbd/example/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.tbd.example; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /project/example/turbodex/src/test/java/com/lody/turbodex/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.lody.turbodex; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /project/turbodex/app/src/main/java/com/tbd/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tbd.app; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /project/example/app/src/main/java/com/tbd/example/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tbd.example; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /project/turbodex/app/src/androidTest/java/com/tbd/app/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.tbd.app; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/XJniHook.h: -------------------------------------------------------------------------------- 1 | #ifndef __PLUGIN_HOOK_H_ 2 | #define __PLUGIN_HOOK_H_ 3 | #include 4 | 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | void Java_com_lody_turbodex_TurboDex_nativeEnableTurboDex(JNIEnv* env,jobject thiz); 11 | 12 | void Java_com_lody_turbodex_TurboDex_nativeDisableTurboDex(JNIEnv* env,jobject thiz); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | 18 | #endif //__PLUGIN_HOOK_H_ 19 | -------------------------------------------------------------------------------- /project/example/app/src/androidTest/java/com/tbd/example/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.tbd.example; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/CydiaSubstrate.h: -------------------------------------------------------------------------------- 1 | #ifndef CYDIASUBSTRATE_H_ 2 | #define CYDIASUBSTRATE_H_ 3 | 4 | #include 5 | #include 6 | 7 | #define _finline \ 8 | inline __attribute__((__always_inline__)) 9 | #define _disused \ 10 | __attribute__((__unused__)) 11 | #define _extern \ 12 | extern "C" __attribute__((__visibility__("default"))) 13 | 14 | #include "SubstrateStruct.h" 15 | #endif /* CYDIASUBSTRATE_H_ */ 16 | -------------------------------------------------------------------------------- /project/example/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /project/example/turbodex/src/androidTest/java/com/lody/turbodex/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.lody.turbodex; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /project/turbodex/app/src/main/java/com/tbd/app/Application.java: -------------------------------------------------------------------------------- 1 | package com.tbd.app; 2 | 3 | import android.content.Context; 4 | 5 | import com.lody.turbodex.TurboDex; 6 | 7 | /** 8 | * Created by qiujuer 9 | * on 16/4/22. 10 | */ 11 | public class Application extends android.app.Application { 12 | @Override 13 | protected void attachBaseContext(Context base) { 14 | TurboDex.enableTurboDex(); 15 | super.attachBaseContext(base); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/hook.h: -------------------------------------------------------------------------------- 1 | #ifndef LIBHOOK_H_ 2 | #define LIBHOOK_H_ 3 | 4 | #define HOOK_FAILED -1 5 | #define HOOK_SUCCESS 0 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | extern int elfHook(const char *soname, const char *symbol, void *replace_func, void **old_func); 12 | extern int elfHookDirect(unsigned int addr, void *replace_func,void **old_func); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | #endif /* LIBHOOK_HOOK2_H_ */ 18 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | include $(CLEAR_VARS) 3 | LOCAL_MODULE := turbo-dex 4 | LOCAL_SRC_FILES := \ 5 | MSHook/hook.cpp \ 6 | MSHook/ARM.cpp \ 7 | MSHook/Thumb.cpp \ 8 | MSHook/x86.cpp \ 9 | MSHook/x86_64.cpp \ 10 | MSHook/Debug.cpp \ 11 | MSHook/Hooker.cpp \ 12 | MSHook/PosixMemory.cpp \ 13 | MSHook/util.cpp \ 14 | core/FastLoadDex.cpp \ 15 | XJniHook.cpp 16 | LOCAL_LDLIBS := -llog 17 | include $(BUILD_SHARED_LIBRARY) 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | -------------------------------------------------------------------------------- /project/README.md: -------------------------------------------------------------------------------- 1 | ## Example 2 | 3 | 这是实例项目,在这个项目中你可以找到**TurboDex.java**和***.so**文件。 4 | 5 | 6 | #### Android Studio: 7 | 8 | `Import project` -> `project/example` -> `ok` 9 | 10 | 注意: 导入当前项目时 gradle 本地库应该 >= gradle-2.10 11 | 12 | 13 | ## TurboDex 14 | 15 | 这是项目核心代码, 在这个项目中你可以找到**jni**文件和**TurboDex.java**文件. 16 | 17 | 18 | #### Android Studio: 19 | 20 | `Import project` -> `project/example` -> `Gradle Sync` -> `Cancel` -> `Gradle home: gradle-2.8` -> `ok` 21 | 22 | 注意: 导入当前项目时提示**Gradle Sync**时选择**Cancel**, 然后选择**gradle-2.8**版本, 否则无法正常编译jni文件. -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Hooker.h: -------------------------------------------------------------------------------- 1 | #ifndef HOOKER_H_ 2 | #define HOOKER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Debug.h" 10 | #include "Log.h" 11 | #include "PosixMemory.h" 12 | #include "CydiaSubstrate.h" 13 | 14 | namespace Cydia{ 15 | 16 | _extern void elfHookFunction(const char *soname, const char *symbol, void *replace_func, void **old_func); 17 | _extern void MSHookFunction(void *symbol, void *replace, void **result); 18 | } 19 | #endif /* HOOKER_H_ */ -------------------------------------------------------------------------------- /project/turbodex/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /project/example/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/qiujuer/Tools/Env/SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /project/example/turbodex/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/qiujuer/Tools/Env/SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /project/turbodex/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/qiujuer/Tools/Env/SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/core/FastLoadDex.cpp: -------------------------------------------------------------------------------- 1 | #include "FastLoadDex.h" 2 | #include "HookCore.h" 3 | 4 | #define DEX2OAT_BIN "/system/bin/dex2oat" 5 | 6 | #define DEBUG 1 7 | 8 | static bool hooked = false; 9 | static bool enable = false; 10 | 11 | int (*org_execv)(const char *name, char **argv); 12 | 13 | int my_execv(const char *name, char **argv) { 14 | #ifdef DEBUG 15 | LOGD("#execv %s.", name); 16 | #endif 17 | 18 | if(enable && strcmp(name, DEX2OAT_BIN) == 0) { 19 | exit(0); 20 | } 21 | 22 | return org_execv(name, argv); 23 | } 24 | 25 | void enableFastLoadDex() { 26 | if(!hooked) { 27 | HOOK(execv); 28 | hooked = true; 29 | } 30 | enable = true; 31 | } 32 | 33 | void closeFastLoadDex() { 34 | enable = false; 35 | } 36 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/qiujuer/Tools/Env/SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /project/example/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.0-beta3' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | } 21 | } 22 | 23 | task clean(type: Delete) { 24 | delete rootProject.buildDir 25 | } 26 | -------------------------------------------------------------------------------- /project/example/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.tbd.example" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.3.0' 26 | compile project(':turbodex') 27 | } 28 | -------------------------------------------------------------------------------- /project/turbodex/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.tbd.app" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.3.0' 26 | compile project(':turbodex') 27 | } 28 | -------------------------------------------------------------------------------- /project/example/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /project/turbodex/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | minSdkVersion 9 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | ndk { 14 | moduleName "turbo-dex" 15 | cFlags "-std=c++11 -fexceptions -frtti -fpermissive" 16 | ldLibs "log" 17 | stl "gnustl_static" 18 | abiFilters "arm64-v8a", "armeabi", "armeabi-v7a", "x86", "x86_64" 19 | } 20 | } 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /project/example/turbodex/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | minSdkVersion 9 9 | targetSdkVersion 23 10 | versionCode 1604221845 11 | versionName "1.1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | sourceSets { 20 | main { 21 | jniLibs.srcDirs = ['libs'] 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | testCompile 'junit:junit:4.12' 29 | } 30 | 31 | apply from: 'https://raw.github.com/qiujuer/BeFoot/master/blog/gradle-jcenter-push/gradle-jcenter-push.gradle' 32 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/hook.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "util.h" 7 | #include "log.h" 8 | 9 | #include "Hooker.h" 10 | #include "hook.h" 11 | 12 | int elfHook(const char *soname, const char *symbol, void *replace_func, 13 | void **old_func) { 14 | int ret = -1; 15 | void *addr = NULL; 16 | if (find_name(getpid(), symbol, soname, (unsigned long *) &addr) < 0) { 17 | LOGW("Not find: %s\n", symbol); 18 | return -1; 19 | } 20 | Cydia::MSHookFunction(addr, replace_func, old_func); 21 | ret = 0; 22 | return ret; 23 | } 24 | 25 | int elfHookDirect(unsigned int addr, void *replace_func, void **old_func) { 26 | if (addr == 0) { 27 | LOGW("hook direct addr:%p error!", (void* )addr); 28 | return -1; 29 | } 30 | Cydia::MSHookFunction((void*) addr, replace_func, old_func); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /project/example/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /project/example/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_GROUP_ID=com.github.asLody 2 | POM_DESCRIPTION=Fast load Dex file in runtime 3 | POM_URL=https://github.com/asLody/TurboDex 4 | POM_SCM_URL=https://github.com/asLody/TurboDex 5 | POM_SCM_CONNECTION=scm:git@github.com:asLody/TurboDex.git 6 | POM_SCM_DEV_CONNECTION=scm:git@github.com:asLody/TurboDex.git 7 | POM_LICENCE_COMMENTS= If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. 8 | POM_LICENCE_NAME=GPL3.0 9 | POM_LICENCE_URL=https://opensource.org/licenses/gpl-3.0.html 10 | POM_LICENCE_DIST=repo 11 | POM_DEVELOPER_ID=lody 12 | POM_DEVELOPER_NAME=Lody 13 | POM_DEVELOPER_EMAIL=imlody@foxmail.com 14 | POM_DEVELOPER_URL=https://github.com/asLody 15 | POM_ISSUE_MANAGEMENT_SYSTEM=Github 16 | POM_ISSUE_MANAGEMENT_URL=https://github.com/asLody/TurboDex/issues 17 | POM_INCEPTION_YEAR=2016 18 | POM_BINTRAY_PRPO=maven 19 | POM_PACKAGING=aar 20 | POM_NAME=TurboDex 21 | POM_GIT_URL=https://github.com/asLody/TurboDex.git 22 | POM_LICENCE_ALL=["GPL-3.0"] 23 | 24 | android.useDeprecatedNdk=true 25 | 26 | -------------------------------------------------------------------------------- /project/example/app/src/main/java/com/tbd/example/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.tbd.example; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import com.lody.turbodex.TurboDex; 7 | 8 | import dalvik.system.DexClassLoader; 9 | 10 | /** 11 | * @author Lody 12 | * @version 1.0 13 | */ 14 | public class MyApplication extends Application { 15 | 16 | @Override 17 | protected void attachBaseContext(Context base) { 18 | TurboDex.enableTurboDex(); 19 | super.attachBaseContext(base); 20 | } 21 | 22 | @Override 23 | public void onCreate() { 24 | super.onCreate(); 25 | 26 | String optDir = getDir("sec-dex", MODE_PRIVATE).getPath(); 27 | DexClassLoader dl = new DexClassLoader( 28 | "/sdcard/classes2.dex", //classes.dex 29 | optDir, //Opt dir 30 | null, //Lib dir 31 | ClassLoader.getSystemClassLoader().getParent()); //Parent ClassLoader 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /project/turbodex/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_GROUP_ID=com.github.asLody 2 | POM_DESCRIPTION=Fast load Dex file in runtime 3 | POM_URL=https://github.com/asLody/TurboDex 4 | POM_SCM_URL=https://github.com/asLody/TurboDex 5 | POM_SCM_CONNECTION=scm:git@github.com:asLody/TurboDex.git 6 | POM_SCM_DEV_CONNECTION=scm:git@github.com:asLody/TurboDex.git 7 | POM_LICENCE_COMMENTS= If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. 8 | POM_LICENCE_NAME=GPL3.0 9 | POM_LICENCE_URL=https://opensource.org/licenses/gpl-3.0.html 10 | POM_LICENCE_DIST=repo 11 | POM_DEVELOPER_ID=lody 12 | POM_DEVELOPER_NAME=Lody 13 | POM_DEVELOPER_EMAIL=imlody@foxmail.com 14 | POM_DEVELOPER_URL=https://github.com/asLody 15 | POM_ISSUE_MANAGEMENT_SYSTEM=Github 16 | POM_ISSUE_MANAGEMENT_URL=https://github.com/asLody/TurboDex/issues 17 | POM_INCEPTION_YEAR=2016 18 | POM_BINTRAY_PRPO=maven 19 | POM_PACKAGING=aar 20 | POM_NAME=TurboDex 21 | POM_GIT_URL=https://github.com/asLody/TurboDex.git 22 | POM_LICENCE_ALL=["GPL-3.0"] 23 | 24 | android.useDeprecatedNdk=true 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /project/example/turbodex/src/main/java/com/lody/turbodex/TurboDex.java: -------------------------------------------------------------------------------- 1 | package com.lody.turbodex; 2 | 3 | /** 4 | * @author Lody 5 | * @version 1.3 6 | */ 7 | public class TurboDex { 8 | 9 | static { 10 | System.loadLibrary("turbo-dex"); 11 | } 12 | 13 | static native void nativeEnableTurboDex(); 14 | 15 | static native void nativeDisableTurboDex(); 16 | 17 | public static boolean enableTurboDex() { 18 | if (isArtMode()) { 19 | try { 20 | nativeEnableTurboDex(); 21 | return true; 22 | } catch (Throwable e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | return false; 27 | } 28 | 29 | public static void disableTurboDex() { 30 | try { 31 | nativeDisableTurboDex(); 32 | } catch (Throwable e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | 38 | /** 39 | * In current version, only enable TBD at ART mode. 40 | * 41 | * @return current Environment is ART mode 42 | */ 43 | private static boolean isArtMode() { 44 | return System.getProperty("java.vm.version", "").startsWith("2"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/java/com/lody/turbodex/TurboDex.java: -------------------------------------------------------------------------------- 1 | package com.lody.turbodex; 2 | 3 | /** 4 | * @author Lody 5 | * @version 1.3 6 | */ 7 | public class TurboDex { 8 | 9 | static { 10 | System.loadLibrary("turbo-dex"); 11 | } 12 | 13 | static native void nativeEnableTurboDex(); 14 | 15 | static native void nativeDisableTurboDex(); 16 | 17 | public static boolean enableTurboDex() { 18 | if (isArtMode()) { 19 | try { 20 | nativeEnableTurboDex(); 21 | return true; 22 | } catch (Throwable e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | return false; 27 | } 28 | 29 | public static void disableTurboDex() { 30 | try { 31 | nativeDisableTurboDex(); 32 | } catch (Throwable e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | 38 | /** 39 | * In current version, only enable TBD at ART mode. 40 | * 41 | * @return current Environment is ART mode 42 | */ 43 | private static boolean isArtMode() { 44 | return System.getProperty("java.vm.version", "").startsWith("2"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Debug.h: -------------------------------------------------------------------------------- 1 | /* Cydia Substrate - Powerful Code Insertion Platform 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* 7 | * Substrate is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or (at your 10 | * option) any later version. 11 | * 12 | * Substrate is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 | * License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Substrate. If not, see . 19 | **/ 20 | /* }}} */ 21 | 22 | #ifndef SUBSTRATE_DEBUG_HPP 23 | #define SUBSTRATE_DEBUG_HPP 24 | 25 | #include "Log.h" 26 | #include 27 | #define lprintf(format, ...) \ 28 | MSLog(MSLogLevelNotice, format, ## __VA_ARGS__) 29 | 30 | extern "C" bool MSDebug; 31 | void MSLogHexEx(const void *vdata, size_t size, size_t stride, const char *mark = 0); 32 | void MSLogHex(const void *vdata, size_t size, const char *mark = 0); 33 | 34 | #endif//SUBSTRATE_DEBUG_HPP 35 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Hooker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Hooker.h" 3 | #include "util.h" 4 | #include "ARM.h" 5 | #include "Thumb.h" 6 | #include "x86.h" 7 | 8 | _extern void Cydia::MSHookFunction(void *symbol, void *replace, void **result) { 9 | 10 | SubstrateProcessRef process = NULL; 11 | if (MSDebug){ 12 | MSLog(MSLogLevelNotice, "SubstrateHookFunction(process:%p, symbol:%p, replace:%p, result:%p)", process, symbol, replace, result); 13 | } 14 | #if defined(__arm__) || defined(__thumb__) 15 | if ((reinterpret_cast(symbol) & 0x1) == 0){ 16 | return ARM::SubstrateHookFunctionARM(process, symbol, replace, result); 17 | }else{ 18 | return Thumb::SubstrateHookFunctionThumb(process, reinterpret_cast(reinterpret_cast(symbol) & ~0x1), replace, result); 19 | } 20 | #endif 21 | 22 | 23 | #if defined(__i386__) || defined(__x86_64__) 24 | return x86::SubstrateHookFunctionx86(process, symbol, replace, result); 25 | #endif 26 | } 27 | 28 | _extern void Cydia::elfHookFunction(const char *soname, const char *symbol, void *replace_func, void **old_func) { 29 | void *addr = NULL; 30 | if (find_name(getpid(), symbol, soname, (unsigned long *)&addr) < 0) { 31 | MSLog(MSLogLevelError, "Not found %s.", symbol); 32 | return; 33 | } 34 | Cydia::MSHookFunction(addr, replace_func, old_func); 35 | } -------------------------------------------------------------------------------- /CHINESE.md: -------------------------------------------------------------------------------- 1 | TurboDex: 在Android瞬间加载Dex 2 | -------- 3 | 4 | 众所周知,Android中在Runtime加载一个 **未优化的Dex文件** (尤其在 **ART 模式**)需要花费 **很长的时间**. 5 | 当你在App中使用 **插件化框架** 的时候, 首次加载插件就需要耗费很长的时间. 6 | 7 | TurboDex 就是为了解决这一问题而生, 就像是给AndroidVM开启了上帝模式, 8 | 在引入TurboDex后, 无论你加载了多大的Dex文件,都可以在毫秒级别内完成. 9 | 10 | # Quick Start Guide 11 | 12 | ## Building TurboDex 13 | TurboDex的 **pre-compiled** 版本在 **/Prebuilt** 目录下, 14 | 如果你想要构建自己的TurboDex, 你需要安装 **Android-NDK**. 15 | 16 | ``` 17 | ✘ lody@MacBook-Pro  ~/TurboDex/TurboDex/jni> ndk-build 18 | SharedLibrary : libturbo-dex.so 19 | Install : libturbo-dex.so => libs/armeabi/libturbo-dex.so 20 | SharedLibrary : libturbo-dex.so 21 | Install : libturbo-dex.so => libs/x86/libturbo-dex.so 22 | ``` 23 | 24 | 25 | 26 | ## Config 27 | 28 | #### Maven 29 | 30 | ```xml 31 | 32 | com.github.asLody 33 | turbodex 34 | 1.1.0 35 | pom 36 | 37 | ``` 38 | 39 | #### Gradle 40 | 41 | ```groovy 42 | compile 'com.github.asLody:turbodex:1.1.0' 43 | ``` 44 | 45 | 46 | 47 | ## Usage 48 | 使用TurboDex, 你需要将**library** 添加到你的项目中, 49 | 在 **Application** 中写入以下代码: 50 | 51 | ```java 52 | 53 | @Override 54 | protected void attachBaseContext(Context base) { 55 | TurboDex.enableTurboDex(); 56 | super.attachBaseContext(base); 57 | } 58 | 59 | 60 | ``` 61 | 62 | **开启** TurboDex后, 下列调用都不再成为拖慢你App运行的元凶: 63 | ``` 64 | new DexClassLoader(...): 65 | 66 | DexFile.loadDex(...); 67 | ``` 68 | ## 其它的分析和评论 69 | http://note.youdao.com/share/?id=28e62692d218a1f1faef98e4e7724f22&type=note#/ 70 | 71 | 然而,不知道这篇笔记的作者为什么会认为Hook模块是我实现的, 72 | 我并没有给Substrate那部分的模块自己命名,而是采用了原名:MSHook, 73 | 而且, 74 | 所有的Cydia源码我也保留了头部的协议申明,你知道源码的出处,却没有意识到这一点? 75 | 76 | ## Remark 77 | QQ Group: **535498571** 78 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/PosixMemory.cpp: -------------------------------------------------------------------------------- 1 | /* Cydia Substrate - Powerful Code Insertion Platform 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* 7 | * Substrate is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or (at your 10 | * option) any later version. 11 | * 12 | * Substrate is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 | * License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Substrate. If not, see . 19 | **/ 20 | /* }}} */ 21 | 22 | #include "CydiaSubstrate.h" 23 | #include "PosixMemory.h" 24 | #include "Log.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | extern "C" SubstrateMemoryRef SubstrateMemoryCreate( 32 | SubstrateAllocatorRef allocator, SubstrateProcessRef process, 33 | void *data, size_t size) { 34 | if (allocator != NULL) { 35 | MSLog(MSLogLevelError, "MS:Error:allocator != NULL"); 36 | return NULL; 37 | } 38 | 39 | if (size == 0) 40 | return NULL; 41 | 42 | int page(getpagesize()); 43 | 44 | uintptr_t base(reinterpret_cast(data) / page * page); 45 | size_t width( 46 | ((reinterpret_cast(data) + size - 1) / page + 1) * page 47 | - base); 48 | void *address(reinterpret_cast(base)); 49 | 50 | if (mprotect(address, width, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 51 | MSLog(MSLogLevelError, "MS:Error:mprotect() = %d", errno); 52 | return NULL; 53 | } 54 | 55 | return new SubstrateMemory(address, width); 56 | } 57 | 58 | extern "C" void SubstrateMemoryRelease(SubstrateMemoryRef memory) { 59 | if (mprotect(memory->address_, memory->width_, 60 | PROT_READ | PROT_WRITE | PROT_EXEC) == -1) 61 | MSLog(MSLogLevelError, "MS:Error:mprotect() = %d", errno); 62 | 63 | __clear_cache(reinterpret_cast(memory->address_), 64 | reinterpret_cast(memory->address_) + memory->width_); 65 | 66 | delete memory; 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TurboDex: Fast load dex in memory 2 | -------- 3 | 4 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-TurboDex-green.svg?style=true)](https://android-arsenal.com/details/1/3456) 5 | 6 | [中文](CHINESE.md "中文") 7 | 8 | It is generally known that load an **unoptimized Dex file** at runtime 9 | in Android (especially in **ART mode**) would take a **long time**. 10 | When your App is using **MultiDex or PluginFramework**, 11 | You will find that this problem is hard to bear. 12 | 13 | TurboDex was born to solve this problem, Like to **opens the god mode** for AndroidVM, 14 | after using TurboDex, no matter how much Dex file your need to load, 15 | it will be finished in **a very short time**. 16 | 17 | # Quick Start Guide 18 | 19 | ## Building TurboDex 20 | TurboDex has given you a **pre-compiled** version in **/Prebuilt**, 21 | if you need to custom the TurboDex, you need to install **Android-NDK**. 22 | 23 | ``` 24 | ✘ lody@MacBook-Pro ~/TurboDex/TurboDex/jni> ndk-build 25 | SharedLibrary : libturbo-dex.so 26 | Install : libturbo-dex.so => libs/armeabi/libturbo-dex.so 27 | SharedLibrary : libturbo-dex.so 28 | Install : libturbo-dex.so => libs/x86/libturbo-dex.so 29 | ``` 30 | 31 | 32 | 33 | ## Config 34 | 35 | #### Maven 36 | 37 | ```xml 38 | 39 | com.github.asLody 40 | turbodex 41 | 1.1.0 42 | pom 43 | 44 | ``` 45 | 46 | #### Gradle 47 | 48 | ```groovy 49 | compile 'com.github.asLody:turbodex:1.1.0' 50 | ``` 51 | 52 | 53 | 54 | ## Usage 55 | To use TurboDex, first add **library** to your project, 56 | Then write the following code in your **Application**: 57 | 58 | ```java 59 | 60 | @Override 61 | protected void attachBaseContext(Context base) { 62 | TurboDex.enableTurboDex(); 63 | super.attachBaseContext(base); 64 | } 65 | 66 | 67 | ``` 68 | 69 | After your **enable** the TurboDex, No matter where you load the Dex, it will return quickly. 70 | Example: 71 | ``` 72 | new DexClassLoader(...): 73 | 74 | DexFile.loadDex(...); 75 | ``` 76 | 77 | ## Others analysis and comment 78 | http://note.youdao.com/share/?id=28e62692d218a1f1faef98e4e7724f22&type=note#/ 79 | 80 | 然而,不知道这篇笔记的作者为什么会认为Hook模块是我实现的, 81 | 我并没有给Substrate那部分的模块自己命名,而是采用了原名:MSHook, 82 | 而且, 83 | 所有的Cydia源码我也保留了头部的协议申明,你知道源码的出处,却没有意识到这一点? 84 | 85 | ## Remark 86 | QQ Group: **535498571** 87 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Log.h: -------------------------------------------------------------------------------- 1 | /* Cydia Substrate - Powerful Code Insertion Platform 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* 7 | * Substrate is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or (at your 10 | * option) any later version. 11 | * 12 | * Substrate is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 | * License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Substrate. If not, see . 19 | **/ 20 | /* }}} */ 21 | 22 | #ifndef SUBSTRATE_LOG_HPP 23 | #define SUBSTRATE_LOG_HPP 24 | 25 | #include 26 | 27 | #define MSLogLevelNotice ANDROID_LOG_INFO 28 | #define MSLogLevelWarning ANDROID_LOG_WARN 29 | #define MSLogLevelError ANDROID_LOG_ERROR 30 | 31 | #define DEBUG 1 32 | #define EXE_PRINTF 0 33 | #ifndef LOG_TAG 34 | # define LOG_TAG "Native_X" 35 | #endif 36 | 37 | #if DEBUG 38 | #ifdef EXE_PRINTF 39 | #define LOGD(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 40 | #define LOGI(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 41 | #define LOGV(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 42 | #define LOGW(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 43 | #define LOGE(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 44 | #define LOGF(fmt,...) printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__) 45 | 46 | #else 47 | #define LOGD(fmt,...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 48 | #define LOGI(fmt,...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 49 | #define LOGV(fmt,...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 50 | #define LOGW(fmt,...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 51 | #define LOGE(fmt,...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 52 | #define LOGF(fmt,...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__) 53 | #endif 54 | #else 55 | #define LOGD(...) while(0){} 56 | #define LOGI(...) while(0){} 57 | #define LOGV(...) while(0){} 58 | #define LOGW(...) while(0){} 59 | #define LOGE(...) while(0){} 60 | #define LOGW(...) while(0){} 61 | #endif 62 | 63 | #define MSLog(level, fmt,...) do { \ 64 | printf("[%12s] " fmt "\n", __FUNCTION__,##__VA_ARGS__); \ 65 | __android_log_print(level, LOG_TAG, "[%s]" fmt, __FUNCTION__,##__VA_ARGS__); \ 66 | } while (false) 67 | #endif//SUBSTRATE_LOG_HPP 68 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/ARM.h: -------------------------------------------------------------------------------- 1 | /* Cydia Substrate - Powerful Code Insertion Platform 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* 7 | * Substrate is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or (at your 10 | * option) any later version. 11 | * 12 | * Substrate is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 | * License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Substrate. If not, see . 19 | **/ 20 | /* }}} */ 21 | 22 | #ifndef SUBSTRATE_ARM_HPP 23 | #define SUBSTRATE_ARM_HPP 24 | 25 | #include "CydiaSubstrate.h" 26 | #include "Log.h" 27 | #include "Debug.h" 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | enum A$r { 35 | A$r0, A$r1, A$r2, A$r3, 36 | A$r4, A$r5, A$r6, A$r7, 37 | A$r8, A$r9, A$r10, A$r11, 38 | A$r12, A$r13, A$r14, A$r15, 39 | A$sp = A$r13, 40 | A$lr = A$r14, 41 | A$pc = A$r15 42 | }; 43 | 44 | enum A$c { 45 | A$eq, A$ne, A$cs, A$cc, 46 | A$mi, A$pl, A$vs, A$vc, 47 | A$hi, A$ls, A$ge, A$lt, 48 | A$gt, A$le, A$al, 49 | A$hs = A$cs, 50 | A$lo = A$cc 51 | }; 52 | 53 | #define A$mrs_rm_cpsr(rd) /* mrs rd, cpsr */ \ 54 | (0xe10f0000 | ((rd) << 12)) 55 | #define A$msr_cpsr_f_rm(rm) /* msr cpsr_f, rm */ \ 56 | (0xe128f000 | (rm)) 57 | #define A$ldr_rd_$rn_im$(rd, rn, im) /* ldr rd, [rn, #im] */ \ 58 | (0xe5100000 | ((im) < 0 ? 0 : 1 << 23) | ((rn) << 16) | ((rd) << 12) | abs(im)) 59 | #define A$str_rd_$rn_im$(rd, rn, im) /* sr rd, [rn, #im] */ \ 60 | (0xe5000000 | ((im) < 0 ? 0 : 1 << 23) | ((rn) << 16) | ((rd) << 12) | abs(im)) 61 | #define A$sub_rd_rn_$im(rd, rn, im) /* sub, rd, rn, #im */ \ 62 | (0xe2400000 | ((rn) << 16) | ((rd) << 12) | (im & 0xff)) 63 | #define A$blx_rm(rm) /* blx rm */ \ 64 | (0xe12fff30 | (rm)) 65 | #define A$mov_rd_rm(rd, rm) /* mov rd, rm */ \ 66 | (0xe1a00000 | ((rd) << 12) | (rm)) 67 | #define A$ldmia_sp$_$rs$(rs) /* ldmia sp!, {rs} */ \ 68 | (0xe8b00000 | (A$sp << 16) | (rs)) 69 | #define A$stmdb_sp$_$rs$(rs) /* stmdb sp!, {rs} */ \ 70 | (0xe9200000 | (A$sp << 16) | (rs)) 71 | #define A$stmia_sp$_$r0$ 0xe8ad0001 /* stmia sp!, {r0} */ 72 | #define A$bx_r0 0xe12fff10 /* bx r0 */ 73 | 74 | static inline bool A$pcrel$r(uint32_t ic) { 75 | return (ic & 0x0c000000) == 0x04000000 && (ic & 0xf0000000) != 0xf0000000 && (ic & 0x000f0000) == 0x000f0000; 76 | } 77 | 78 | namespace ARM{ 79 | extern "C" void SubstrateHookFunctionARM(SubstrateProcessRef process, void *symbol, void *replace, void **result); 80 | } 81 | #endif//SUBSTRATE_ARM_HPP 82 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Debug.cpp: -------------------------------------------------------------------------------- 1 | /* Cydia Substrate - Powerful Code Insertion Platform 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* 7 | * Substrate is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or (at your 10 | * option) any later version. 11 | * 12 | * Substrate is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 | * License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Substrate. If not, see . 19 | **/ 20 | /* }}} */ 21 | 22 | #include "CydiaSubstrate.h" 23 | #include "Debug.h" 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | _extern bool MSDebug; 30 | bool MSDebug = true; 31 | 32 | static char _MSHexChar(uint8_t value) { 33 | return value < 0x20 || value >= 0x80 ? '.' : value; 34 | } 35 | 36 | #define HexWidth_ 16 37 | #define HexDepth_ 4 38 | 39 | void MSLogHexEx(const void *vdata, size_t size, size_t stride, const char *mark) { 40 | const uint8_t *data((const uint8_t *) vdata); 41 | 42 | size_t i(0), j; 43 | 44 | char d[256]; 45 | size_t b(0); 46 | d[0] = '\0'; 47 | 48 | while (i != size) { 49 | if (i % HexWidth_ == 0) { 50 | if (mark != NULL) 51 | b += sprintf(d + b, "[%s] ", mark); 52 | b += sprintf(d + b, "0x%.3zx:", i); 53 | } 54 | 55 | b += sprintf(d + b, " "); 56 | 57 | for (size_t q(0); q != stride; ++q) 58 | b += sprintf(d + b, "%.2x", data[i + stride - q - 1]); 59 | 60 | i += stride; 61 | 62 | for (size_t q(1); q != stride; ++q) 63 | b += sprintf(d + b, " "); 64 | 65 | if (i % HexDepth_ == 0) 66 | b += sprintf(d + b, " "); 67 | 68 | if (i % HexWidth_ == 0) { 69 | b += sprintf(d + b, " "); 70 | for (j = i - HexWidth_; j != i; ++j) 71 | b += sprintf(d + b, "%c", _MSHexChar(data[j])); 72 | 73 | lprintf("%s", d); 74 | b = 0; 75 | d[0] = '\0'; 76 | } 77 | } 78 | 79 | if (i % HexWidth_ != 0) { 80 | for (j = i % HexWidth_; j != HexWidth_; ++j) 81 | b += sprintf(d + b, " "); 82 | for (j = 0; j != (HexWidth_ - i % HexWidth_ + HexDepth_ - 1) / HexDepth_; ++j) 83 | b += sprintf(d + b, " "); 84 | b += sprintf(d + b, " "); 85 | for (j = i / HexWidth_ * HexWidth_; j != i; ++j) 86 | b += sprintf(d + b, "%c", _MSHexChar(data[j])); 87 | 88 | lprintf("%s", d); 89 | b = 0; 90 | d[0] = '\0'; 91 | } 92 | } 93 | 94 | void MSLogHex(const void *vdata, size_t size, const char *mark) { 95 | return MSLogHexEx(vdata, size, 1, mark); 96 | } 97 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/hde64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 64 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | * hde64.h: C/C++ header file 7 | * 8 | */ 9 | 10 | #ifndef _HDE64_H_ 11 | #define _HDE64_H_ 12 | 13 | /* stdint.h - C99 standard header 14 | * http://en.wikipedia.org/wiki/stdint.h 15 | * 16 | * if your compiler doesn't contain "stdint.h" header (for 17 | * example, Microsoft Visual C++), you can download file: 18 | * http://www.azillionmonkeys.com/qed/pstdint.h 19 | * and change next line to: 20 | * #include "pstdint.h" 21 | */ 22 | #include 23 | 24 | #define F_MODRM 0x00000001 25 | #define F_SIB 0x00000002 26 | #define F_IMM8 0x00000004 27 | #define F_IMM16 0x00000008 28 | #define F_IMM32 0x00000010 29 | #define F_IMM64 0x00000020 30 | #define F_DISP8 0x00000040 31 | #define F_DISP16 0x00000080 32 | #define F_DISP32 0x00000100 33 | #define F_RELATIVE 0x00000200 34 | #define F_ERROR 0x00001000 35 | #define F_ERROR_OPCODE 0x00002000 36 | #define F_ERROR_LENGTH 0x00004000 37 | #define F_ERROR_LOCK 0x00008000 38 | #define F_ERROR_OPERAND 0x00010000 39 | #define F_PREFIX_REPNZ 0x01000000 40 | #define F_PREFIX_REPX 0x02000000 41 | #define F_PREFIX_REP 0x03000000 42 | #define F_PREFIX_66 0x04000000 43 | #define F_PREFIX_67 0x08000000 44 | #define F_PREFIX_LOCK 0x10000000 45 | #define F_PREFIX_SEG 0x20000000 46 | #define F_PREFIX_REX 0x40000000 47 | #define F_PREFIX_ANY 0x7f000000 48 | 49 | #define PREFIX_SEGMENT_CS 0x2e 50 | #define PREFIX_SEGMENT_SS 0x36 51 | #define PREFIX_SEGMENT_DS 0x3e 52 | #define PREFIX_SEGMENT_ES 0x26 53 | #define PREFIX_SEGMENT_FS 0x64 54 | #define PREFIX_SEGMENT_GS 0x65 55 | #define PREFIX_LOCK 0xf0 56 | #define PREFIX_REPNZ 0xf2 57 | #define PREFIX_REPX 0xf3 58 | #define PREFIX_OPERAND_SIZE 0x66 59 | #define PREFIX_ADDRESS_SIZE 0x67 60 | 61 | #pragma pack(push,1) 62 | 63 | typedef struct { 64 | uint8_t len; 65 | uint8_t p_rep; 66 | uint8_t p_lock; 67 | uint8_t p_seg; 68 | uint8_t p_66; 69 | uint8_t p_67; 70 | uint8_t rex; 71 | uint8_t rex_w; 72 | uint8_t rex_r; 73 | uint8_t rex_x; 74 | uint8_t rex_b; 75 | uint8_t opcode; 76 | uint8_t opcode2; 77 | uint8_t modrm; 78 | uint8_t modrm_mod; 79 | uint8_t modrm_reg; 80 | uint8_t modrm_rm; 81 | uint8_t sib; 82 | uint8_t sib_scale; 83 | uint8_t sib_index; 84 | uint8_t sib_base; 85 | union { 86 | uint8_t imm8; 87 | uint16_t imm16; 88 | uint32_t imm32; 89 | uint64_t imm64; 90 | } imm; 91 | union { 92 | uint8_t disp8; 93 | uint16_t disp16; 94 | uint32_t disp32; 95 | } disp; 96 | uint32_t flags; 97 | } hde64s; 98 | 99 | #pragma pack(pop) 100 | 101 | #ifdef __cplusplus 102 | extern "C" { 103 | #endif 104 | 105 | /* __cdecl */ 106 | unsigned int hde64_disasm(const void *code, hde64s *hs); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif /* _HDE64_H_ */ 113 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/ARM.cpp: -------------------------------------------------------------------------------- 1 | #include "ARM.h" 2 | #include "PosixMemory.h" 3 | 4 | void ARM::SubstrateHookFunctionARM(SubstrateProcessRef process, void *symbol, void *replace, void **result) { 5 | if (symbol == NULL) 6 | return; 7 | 8 | uint32_t *area(reinterpret_cast(symbol)); 9 | uint32_t *arm(area); 10 | 11 | const size_t used(8); 12 | 13 | uint32_t backup[used / sizeof(uint32_t)] = {arm[0], arm[1]}; 14 | 15 | if (MSDebug) { 16 | char name[16]; 17 | sprintf(name, "%p", area); 18 | MSLogHexEx(area, used + sizeof(uint32_t), 4, name); 19 | } 20 | 21 | if (result != NULL) { 22 | 23 | if (backup[0] == A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8)) { 24 | *result = reinterpret_cast(backup[1]); 25 | return; 26 | } 27 | 28 | size_t length(used); 29 | for (unsigned offset(0); offset != used / sizeof(uint32_t); ++offset) 30 | if (A$pcrel$r(backup[offset])) { 31 | if ((backup[offset] & 0x02000000) == 0 || (backup[offset] & 0x0000f000 >> 12) != (backup[offset] & 0x0000000f)) 32 | length += 2 * sizeof(uint32_t); 33 | else 34 | length += 4 * sizeof(uint32_t); 35 | } 36 | 37 | length += 2 * sizeof(uint32_t); 38 | 39 | uint32_t *buffer(reinterpret_cast(mmap( 40 | NULL, length, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 41 | ))); 42 | 43 | if (buffer == MAP_FAILED) { 44 | MSLog(MSLogLevelError, "MS:Error:mmap() = %d", errno); 45 | *result = NULL; 46 | return; 47 | } 48 | 49 | if (false) fail: { 50 | munmap(buffer, length); 51 | *result = NULL; 52 | return; 53 | } 54 | 55 | size_t start(0), end(length / sizeof(uint32_t)); 56 | uint32_t *trailer(reinterpret_cast(buffer + end)); 57 | for (unsigned offset(0); offset != used / sizeof(uint32_t); ++offset) 58 | if (A$pcrel$r(backup[offset])) { 59 | union { 60 | uint32_t value; 61 | 62 | struct { 63 | uint32_t rm : 4; 64 | uint32_t : 1; 65 | uint32_t shift : 2; 66 | uint32_t shiftamount : 5; 67 | uint32_t rd : 4; 68 | uint32_t rn : 4; 69 | uint32_t l : 1; 70 | uint32_t w : 1; 71 | uint32_t b : 1; 72 | uint32_t u : 1; 73 | uint32_t p : 1; 74 | uint32_t mode : 1; 75 | uint32_t type : 2; 76 | uint32_t cond : 4; 77 | }; 78 | } bits = {backup[offset+0]}, copy(bits); 79 | 80 | bool guard; 81 | if (bits.mode == 0 || bits.rd != bits.rm) { 82 | copy.rn = bits.rd; 83 | guard = false; 84 | } else { 85 | copy.rn = bits.rm != A$r0 ? A$r0 : A$r1; 86 | guard = true; 87 | } 88 | 89 | if (guard) 90 | buffer[start++] = A$stmdb_sp$_$rs$((1 << copy.rn)); 91 | 92 | buffer[start+0] = A$ldr_rd_$rn_im$(copy.rn, A$pc, (end-1 - (start+0)) * 4 - 8); 93 | buffer[start+1] = copy.value; 94 | 95 | start += 2; 96 | 97 | if (guard) 98 | buffer[start++] = A$ldmia_sp$_$rs$((1 << copy.rn)); 99 | 100 | *--trailer = reinterpret_cast(area + offset) + 8; 101 | end -= 1; 102 | } else 103 | buffer[start++] = backup[offset]; 104 | 105 | buffer[start+0] = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 106 | buffer[start+1] = reinterpret_cast(area + used / sizeof(uint32_t)); 107 | 108 | if (mprotect(buffer, length, PROT_READ | PROT_EXEC) == -1) { 109 | MSLog(MSLogLevelError, "MS:Error:mprotect():%d", errno); 110 | goto fail; 111 | } 112 | 113 | *result = buffer; 114 | 115 | if (MSDebug) { 116 | char name[16]; 117 | sprintf(name, "%p", *result); 118 | MSLogHexEx(buffer, length, 4, name); 119 | } 120 | 121 | } 122 | 123 | { 124 | SubstrateHookMemory code(process, symbol, used); 125 | 126 | arm[0] = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 127 | arm[1] = reinterpret_cast(replace); 128 | } 129 | 130 | if (MSDebug) { 131 | char name[16]; 132 | sprintf(name, "%p", area); 133 | MSLogHexEx(area, used + sizeof(uint32_t), 4, name); 134 | } 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/x86.cpp: -------------------------------------------------------------------------------- 1 | #include "x86.h" 2 | #include "x86_64.h" 3 | 4 | static size_t MSGetInstructionWidthIntel(void *start) { 5 | hde64s decode; 6 | return hde64_disasm(start, &decode); 7 | } 8 | 9 | void x86::SubstrateHookFunctionx86(SubstrateProcessRef process, void *symbol, void *replace, void **result){ 10 | if (MSDebug) 11 | MSLog(MSLogLevelNotice, "SubstrateHookFunctionx86(process:%p, symbol:%p, replace:%p, result:%p)", process, symbol, replace, result); 12 | if (symbol == NULL) 13 | return; 14 | 15 | uintptr_t source(reinterpret_cast(symbol)); 16 | uintptr_t target(reinterpret_cast(replace)); 17 | 18 | uint8_t *area(reinterpret_cast(symbol)); 19 | 20 | size_t required(MSSizeOfJump(target, source)); 21 | 22 | if (MSDebug) { 23 | char name[16]; 24 | sprintf(name, "%p", area); 25 | MSLogHex(area, 32, name); 26 | } 27 | 28 | size_t used(0); 29 | while (used < required) { 30 | size_t width(MSGetInstructionWidthIntel(area + used)); 31 | if (width == 0) { 32 | MSLog(MSLogLevelError, "MS:Error:MSGetInstructionWidthIntel(%p) == 0", area + used); 33 | return; 34 | } 35 | 36 | used += width; 37 | } 38 | 39 | size_t blank(used - required); 40 | 41 | if (MSDebug) { 42 | char name[16]; 43 | sprintf(name, "%p", area); 44 | MSLogHex(area, used + sizeof(uint16_t), name); 45 | } 46 | 47 | uint8_t backup[used]; 48 | memcpy(backup, area, used); 49 | 50 | if (result != NULL) { 51 | 52 | if (backup[0] == 0xe9) { 53 | *result = reinterpret_cast(source + 5 + *reinterpret_cast(backup + 1)); 54 | return; 55 | } 56 | 57 | if (!ia32 && backup[0] == 0xff && backup[1] == 0x25) { 58 | *result = *reinterpret_cast(source + 6 + *reinterpret_cast(backup + 2)); 59 | return; 60 | } 61 | 62 | size_t length(used + MSSizeOfJump(source + used)); 63 | 64 | for (size_t offset(0), width; offset != used; offset += width) { 65 | hde64s decode; 66 | hde64_disasm(backup + offset, &decode); 67 | width = decode.len; 68 | //_assert(width != 0 && offset + width <= used); 69 | 70 | #ifdef __LP64__ 71 | if ((decode.modrm & 0xc7) == 0x05) { 72 | if (decode.opcode == 0x8b) { 73 | void *destiny(area + offset + width + int32_t(decode.disp.disp32)); 74 | uint8_t reg(decode.rex_r << 3 | decode.modrm_reg); 75 | length -= decode.len; 76 | length += MSSizeOfPushPointer(destiny); 77 | length += MSSizeOfPop(reg); 78 | length += MSSizeOfMove64(); 79 | } else { 80 | MSLog(MSLogLevelError, "MS:Error: Unknown RIP-Relative (%.2x %.2x)", decode.opcode, decode.opcode2); 81 | continue; 82 | } 83 | } else 84 | #endif 85 | 86 | if (backup[offset] == 0xe8) { 87 | int32_t relative(*reinterpret_cast(backup + offset + 1)); 88 | void *destiny(area + offset + decode.len + relative); 89 | 90 | if (relative == 0) { 91 | length -= decode.len; 92 | length += MSSizeOfPushPointer(destiny); 93 | } else { 94 | length += MSSizeOfSkip(); 95 | length += MSSizeOfJump(destiny); 96 | } 97 | } else if (backup[offset] == 0xeb) { 98 | length -= decode.len; 99 | length += MSSizeOfJump(area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 100 | } else if (backup[offset] == 0xe9) { 101 | length -= decode.len; 102 | length += MSSizeOfJump(area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 103 | } else if ( 104 | backup[offset] == 0xe3 || 105 | (backup[offset] & 0xf0) == 0x70 106 | // XXX: opcode2 & 0xf0 is 0x80? 107 | ) { 108 | length += decode.len; 109 | length += MSSizeOfJump(area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 110 | } 111 | } 112 | 113 | uint8_t *buffer(reinterpret_cast(mmap( 114 | NULL, length, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 115 | ))); 116 | 117 | if (buffer == MAP_FAILED) { 118 | MSLog(MSLogLevelError, "MS:Error:mmap() = %d", errno); 119 | *result = NULL; 120 | return; 121 | } 122 | 123 | if (false) fail: { 124 | munmap(buffer, length); 125 | *result = NULL; 126 | return; 127 | } 128 | 129 | { 130 | uint8_t *current(buffer); 131 | 132 | for (size_t offset(0), width; offset != used; offset += width) { 133 | hde64s decode; 134 | hde64_disasm(backup + offset, &decode); 135 | width = decode.len; 136 | //_assert(width != 0 && offset + width <= used); 137 | 138 | #ifdef __LP64__ 139 | if ((decode.modrm & 0xc7) == 0x05) { 140 | if (decode.opcode == 0x8b) { 141 | void *destiny(area + offset + width + int32_t(decode.disp.disp32)); 142 | uint8_t reg(decode.rex_r << 3 | decode.modrm_reg); 143 | MSPushPointer(current, destiny); 144 | MSWritePop(current, reg); 145 | MSWriteMove64(current, reg, reg); 146 | } else { 147 | MSLog(MSLogLevelError, "MS:Error: Unknown RIP-Relative (%.2x %.2x)", decode.opcode, decode.opcode2); 148 | goto copy; 149 | } 150 | } else 151 | #endif 152 | 153 | if (backup[offset] == 0xe8) { 154 | int32_t relative(*reinterpret_cast(backup + offset + 1)); 155 | if (relative == 0) 156 | MSPushPointer(current, area + offset + decode.len); 157 | else { 158 | MSWrite(current, 0xe8); 159 | MSWrite(current, MSSizeOfSkip()); 160 | void *destiny(area + offset + decode.len + relative); 161 | MSWriteSkip(current, MSSizeOfJump(destiny, current + MSSizeOfSkip())); 162 | MSWriteJump(current, destiny); 163 | } 164 | } else if (backup[offset] == 0xeb) 165 | MSWriteJump(current, area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 166 | else if (backup[offset] == 0xe9) 167 | MSWriteJump(current, area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 168 | else if ( 169 | backup[offset] == 0xe3 || 170 | (backup[offset] & 0xf0) == 0x70 171 | ) { 172 | MSWrite(current, backup[offset]); 173 | MSWrite(current, 2); 174 | MSWrite(current, 0xeb); 175 | void *destiny(area + offset + decode.len + *reinterpret_cast(backup + offset + 1)); 176 | MSWrite(current, MSSizeOfJump(destiny, current + 1)); 177 | MSWriteJump(current, destiny); 178 | } else 179 | #ifdef __LP64__ 180 | copy: 181 | #endif 182 | { 183 | MSWrite(current, backup + offset, width); 184 | } 185 | } 186 | 187 | MSWriteJump(current, area + used); 188 | } 189 | 190 | if (mprotect(buffer, length, PROT_READ | PROT_EXEC) == -1) { 191 | MSLog(MSLogLevelError, "MS:Error:mprotect():%d", errno); 192 | goto fail; 193 | } 194 | 195 | *result = buffer; 196 | 197 | if (MSDebug) { 198 | char name[16]; 199 | sprintf(name, "%p", *result); 200 | MSLogHex(buffer, length, name); 201 | } 202 | 203 | } 204 | 205 | { 206 | SubstrateHookMemory code(process, area, used); 207 | 208 | uint8_t *current(area); 209 | MSWriteJump(current, target); 210 | for (unsigned offset(0); offset != blank; ++offset) 211 | MSWrite(current, 0x90); 212 | } 213 | 214 | if (MSDebug) { 215 | char name[16]; 216 | sprintf(name, "%p", area); 217 | MSLogHex(area, used + sizeof(uint16_t), name); 218 | } 219 | } 220 | 221 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/util.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "Log.h" 18 | 19 | /* memory map for libraries */ 20 | #define MAX_NAME_LEN 256 21 | #define MEMORY_ONLY "[memory]" 22 | struct mm { 23 | char name[MAX_NAME_LEN]; 24 | unsigned long start, end; 25 | }; 26 | 27 | typedef struct symtab *symtab_t; 28 | struct symlist { 29 | Elf32_Sym *sym; /* symbols */ 30 | char *str; /* symbol strings */ 31 | unsigned num; /* number of symbols */ 32 | }; 33 | struct symtab { 34 | struct symlist *st; /* "static" symbols */ 35 | struct symlist *dyn; /* dynamic symbols */ 36 | }; 37 | 38 | static void* xmalloc(size_t size) { 39 | void *p; 40 | p = malloc(size); 41 | if (!p) { 42 | printf("Out of memory\n"); 43 | exit(1); 44 | } 45 | return p; 46 | } 47 | 48 | static int my_pread(int fd, void *buf, size_t count, off_t offset) { 49 | lseek(fd, offset, SEEK_SET); 50 | return read(fd, buf, count); 51 | } 52 | 53 | static struct symlist* get_syms(int fd, Elf32_Shdr *symh, Elf32_Shdr *strh) { 54 | struct symlist *sl, *ret; 55 | int rv; 56 | 57 | ret = NULL; 58 | sl = (struct symlist *) xmalloc(sizeof(struct symlist)); 59 | sl->str = NULL; 60 | sl->sym = NULL; 61 | 62 | /* sanity */ 63 | if (symh->sh_size % sizeof(Elf32_Sym)) { 64 | //printf("elf_error\n"); 65 | goto out; 66 | } 67 | 68 | /* symbol table */ 69 | sl->num = symh->sh_size / sizeof(Elf32_Sym); 70 | sl->sym = (Elf32_Sym *) xmalloc(symh->sh_size); 71 | rv = my_pread(fd, sl->sym, symh->sh_size, symh->sh_offset); 72 | if (0 > rv) { 73 | //perror("read"); 74 | goto out; 75 | } 76 | if (rv != symh->sh_size) { 77 | //printf("elf error\n"); 78 | goto out; 79 | } 80 | 81 | /* string table */ 82 | sl->str = (char *) xmalloc(strh->sh_size); 83 | rv = my_pread(fd, sl->str, strh->sh_size, strh->sh_offset); 84 | if (0 > rv) { 85 | //perror("read"); 86 | goto out; 87 | } 88 | if (rv != strh->sh_size) { 89 | //printf("elf error"); 90 | goto out; 91 | } 92 | 93 | ret = sl; 94 | out: return ret; 95 | } 96 | 97 | static int do_load(int fd, symtab_t symtab) { 98 | int rv; 99 | size_t size; 100 | Elf32_Ehdr ehdr; 101 | Elf32_Shdr *shdr = NULL, *p; 102 | Elf32_Shdr *dynsymh, *dynstrh; 103 | Elf32_Shdr *symh, *strh; 104 | char *shstrtab = NULL; 105 | int i; 106 | int ret = -1; 107 | 108 | /* elf header */ 109 | rv = read(fd, &ehdr, sizeof(ehdr)); 110 | if (0 > rv) { 111 | LOGD("read\n"); 112 | goto out; 113 | } 114 | if (rv != sizeof(ehdr)) { 115 | LOGD("elf error 1\n"); 116 | goto out; 117 | } 118 | if (strncmp((const char *) ELFMAG, (const char *) ehdr.e_ident, SELFMAG)) { /* sanity */ 119 | LOGD("not an elf\n"); 120 | goto out; 121 | } 122 | if (sizeof(Elf32_Shdr) != ehdr.e_shentsize) { /* sanity */ 123 | LOGD("elf error 2\n"); 124 | goto out; 125 | } 126 | 127 | /* section header table */ 128 | size = ehdr.e_shentsize * ehdr.e_shnum; 129 | shdr = (Elf32_Shdr *) xmalloc(size); 130 | rv = my_pread(fd, shdr, size, ehdr.e_shoff); 131 | if (0 > rv) { 132 | LOGD("read\n"); 133 | goto out; 134 | } 135 | if (rv != size) { 136 | LOGD("elf error 3 %d %d\n", rv, size); 137 | goto out; 138 | } 139 | 140 | /* section header string table */ 141 | size = shdr[ehdr.e_shstrndx].sh_size; 142 | shstrtab = (char *) xmalloc(size); 143 | rv = my_pread(fd, shstrtab, size, shdr[ehdr.e_shstrndx].sh_offset); 144 | if (0 > rv) { 145 | LOGD("read\n"); 146 | goto out; 147 | } 148 | if (rv != size) { 149 | LOGD("elf error 4 %d %d\n", rv, size); 150 | goto out; 151 | } 152 | 153 | /* symbol table headers */ 154 | symh = dynsymh = NULL; 155 | strh = dynstrh = NULL; 156 | for (i = 0, p = shdr; i < ehdr.e_shnum; i++, p++) 157 | if (SHT_SYMTAB == p->sh_type) { 158 | if (symh) { 159 | LOGD("too many symbol tables\n"); 160 | goto out; 161 | } 162 | symh = p; 163 | } else if (SHT_DYNSYM == p->sh_type) { 164 | if (dynsymh) { 165 | LOGD("too many symbol tables\n"); 166 | goto out; 167 | } 168 | dynsymh = p; 169 | } else if (SHT_STRTAB == p->sh_type 170 | && !strncmp(shstrtab + p->sh_name, ".strtab", 7)) { 171 | if (strh) { 172 | LOGD("too many string tables\n"); 173 | goto out; 174 | } 175 | strh = p; 176 | } else if (SHT_STRTAB == p->sh_type 177 | && !strncmp(shstrtab + p->sh_name, ".dynstr", 7)) { 178 | if (dynstrh) { 179 | LOGD("too many string tables\n"); 180 | goto out; 181 | } 182 | dynstrh = p; 183 | } 184 | /* sanity checks */ 185 | if ((!dynsymh && dynstrh) || (dynsymh && !dynstrh)) { 186 | LOGD("bad dynamic symbol table\n"); 187 | goto out; 188 | } 189 | if ((!symh && strh) || (symh && !strh)) { 190 | LOGD("bad symbol table\n"); 191 | goto out; 192 | } 193 | if (!dynsymh && !symh) { 194 | LOGD("no symbol table\n"); 195 | goto out; 196 | } 197 | 198 | /* symbol tables */ 199 | if (dynsymh) 200 | symtab->dyn = get_syms(fd, dynsymh, dynstrh); 201 | if (symh) 202 | symtab->st = get_syms(fd, symh, strh); 203 | ret = 0; 204 | out: free(shstrtab); 205 | free(shdr); 206 | return ret; 207 | } 208 | 209 | static symtab_t load_symtab(char *filename) { 210 | int fd; 211 | symtab_t symtab; 212 | 213 | symtab = (symtab_t) xmalloc(sizeof(*symtab)); 214 | memset(symtab, 0, sizeof(*symtab)); 215 | 216 | fd = open(filename, O_RDONLY); 217 | if (0 > fd) { 218 | LOGE("%s open\n", __func__); 219 | return NULL; 220 | } 221 | if (0 > do_load(fd, symtab)) { 222 | LOGE("Error ELF parsing %s\n", filename); 223 | free(symtab); 224 | symtab = NULL; 225 | } 226 | close(fd); 227 | return symtab; 228 | } 229 | 230 | static int load_memmap(pid_t pid, struct mm *mm, int *nmmp) { 231 | size_t buf_size = 0x40000; 232 | char *p_buf = (char *) malloc(buf_size); // increase this if needed for larger "maps" 233 | char name[MAX_NAME_LEN] = { 0 }; 234 | char *p; 235 | unsigned long start, end; 236 | struct mm *m; 237 | int nmm = 0; 238 | int fd, rv; 239 | int i; 240 | 241 | sprintf(p_buf, "/proc/%d/maps", pid); 242 | fd = open(p_buf, O_RDONLY); 243 | if (0 > fd) { 244 | LOGE("Can't open %s for reading\n", p_buf); 245 | free(p_buf); 246 | return -1; 247 | } 248 | 249 | /* Zero to ensure data is null terminated */ 250 | memset(p_buf, 0, buf_size); 251 | 252 | p = p_buf; 253 | while (1) { 254 | rv = read(fd, p, buf_size - (p - p_buf)); 255 | if (0 > rv) { 256 | LOGE("%s read", __FUNCTION__); 257 | free(p_buf); 258 | return -1; 259 | } 260 | if (0 == rv) 261 | break; 262 | p += rv; 263 | if (p - p_buf >= buf_size) { 264 | LOGE("Too many memory mapping\n"); 265 | free(p_buf); 266 | return -1; 267 | } 268 | } 269 | close(fd); 270 | 271 | p = strtok(p_buf, "\n"); 272 | m = mm; 273 | while (p) { 274 | /* parse current map line */ 275 | rv = sscanf(p, "%08lx-%08lx %*s %*s %*s %*s %s\n", &start, &end, name); 276 | 277 | p = strtok(NULL, "\n"); 278 | 279 | if (rv == 2) { 280 | m = &mm[nmm++]; 281 | m->start = start; 282 | m->end = end; 283 | memcpy(m->name, MEMORY_ONLY, sizeof(MEMORY_ONLY)); 284 | continue; 285 | } 286 | 287 | /* search backward for other mapping with same name */ 288 | for (i = nmm - 1; i >= 0; i--) { 289 | m = &mm[i]; 290 | if (!strcmp(m->name, name)) 291 | break; 292 | } 293 | 294 | if (i >= 0) { 295 | if (start < m->start) 296 | m->start = start; 297 | if (end > m->end) 298 | m->end = end; 299 | } else { 300 | /* new entry */ 301 | m = &mm[nmm++]; 302 | m->start = start; 303 | m->end = end; 304 | memcpy(m->name, name, strlen(name)); 305 | } 306 | } 307 | 308 | *nmmp = nmm; 309 | free(p_buf); 310 | return 0; 311 | } 312 | 313 | /* Find libc in MM, storing no more than LEN-1 chars of 314 | its name in NAME and set START to its starting 315 | address. If libc cannot be found return -1 and 316 | leave NAME and START untouched. Otherwise return 0 317 | and null-terminated NAME. */ 318 | static int find_libname(char *libn, char *name, int len, unsigned long *start, 319 | struct mm *mm, int nmm) { 320 | int i; 321 | struct mm *m; 322 | char *p; 323 | for (i = 0, m = mm; i < nmm; i++, m++) { 324 | if (!strcmp(m->name, MEMORY_ONLY)) 325 | continue; 326 | p = strrchr(m->name, '/'); 327 | if (!p) 328 | continue; 329 | p++; 330 | if (strncmp(libn, p, strlen(libn))) 331 | continue; 332 | p += strlen(libn); 333 | 334 | /* here comes our crude test -> 'libc.so' or 'libc-[0-9]' */ 335 | if (!strncmp("so", p, 2) || 1) // || (p[0] == '-' && isdigit(p[1]))) 336 | break; 337 | } 338 | if (i >= nmm) 339 | /* not found */ 340 | return -1; 341 | 342 | *start = m->start; 343 | strncpy(name, m->name, len); 344 | if (strlen(m->name) >= len) 345 | name[len - 1] = '\0'; 346 | 347 | mprotect((void*) m->start, m->end - m->start, 348 | PROT_READ | PROT_WRITE | PROT_EXEC); 349 | return 0; 350 | } 351 | 352 | static int lookup2(struct symlist *sl, unsigned char type, char *name, 353 | unsigned long *val) { 354 | Elf32_Sym *p; 355 | int len; 356 | int i; 357 | 358 | len = strlen(name); 359 | for (i = 0, p = sl->sym; i < sl->num; i++, p++) { 360 | //LOGD("name: %s %x\n", sl->str+p->st_name, p->st_value) 361 | if (!strncmp(sl->str + p->st_name, name, len) 362 | && *(sl->str + p->st_name + len) == 0 363 | && ELF32_ST_TYPE(p->st_info) == type) { 364 | //if (p->st_value != 0) { 365 | *val = p->st_value; 366 | return 0; 367 | //} 368 | } 369 | } 370 | return -1; 371 | } 372 | 373 | static int lookup_sym(symtab_t s, unsigned char type, char *name, 374 | unsigned long *val) { 375 | if (s->dyn && !lookup2(s->dyn, type, name, val)) 376 | return 0; 377 | if (s->st && !lookup2(s->st, type, name, val)) 378 | return 0; 379 | return -1; 380 | } 381 | 382 | static int lookup_func_sym(symtab_t s, char *name, unsigned long *val) { 383 | return lookup_sym(s, STT_FUNC, name, val); 384 | } 385 | 386 | int find_name(pid_t pid, const char *name, const char *libn, 387 | unsigned long *addr) { 388 | struct mm mm[1000] = { 0 }; 389 | unsigned long libcaddr; 390 | int nmm; 391 | char libc[1024] = { 0 }; 392 | symtab_t s; 393 | 394 | if (0 > load_memmap(pid, mm, &nmm)) { 395 | LOGD("cannot read memory map\n"); 396 | return -1; 397 | } 398 | if (0 399 | > find_libname((char *) libn, (char *) libc, sizeof(libc), 400 | &libcaddr, mm, nmm)) { 401 | LOGD("cannot find lib: %s\n", libn); 402 | return -1; 403 | } 404 | //LOGD("lib: >%s<\n", libc) 405 | s = load_symtab(libc); 406 | if (!s) { 407 | LOGD("cannot read symbol table\n"); 408 | return -1; 409 | } 410 | if (0 > lookup_func_sym(s, (char *) name, addr)) { 411 | LOGD("cannot find function: %s\n", name); 412 | return -1; 413 | } 414 | *addr += libcaddr; 415 | return 0; 416 | } 417 | 418 | int find_libbase(pid_t pid, char *libn, unsigned long *addr) { 419 | struct mm mm[1000] = { 0 }; 420 | unsigned long libcaddr; 421 | int nmm; 422 | char libc[1024] = { 0 }; 423 | symtab_t s; 424 | 425 | if (0 > load_memmap(pid, mm, &nmm)) { 426 | LOGD("cannot read memory map\n"); 427 | return -1; 428 | } 429 | if (0 > find_libname(libn, libc, sizeof(libc), &libcaddr, mm, nmm)) { 430 | LOGD("cannot find lib\n"); 431 | return -1; 432 | } 433 | *addr = libcaddr; 434 | return 0; 435 | } 436 | 437 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/x86_64.cpp: -------------------------------------------------------------------------------- 1 | #ifndef X86_64_CPP_ 2 | #define X86_64_CPP_ 3 | 4 | #include 5 | #include 6 | #include "x86_64.h" 7 | 8 | unsigned char hde64_table[] = { 9 | 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5, 10 | 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1, 11 | 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea, 12 | 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0, 13 | 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab, 14 | 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92, 15 | 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90, 16 | 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b, 17 | 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b, 18 | 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc, 19 | 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20, 20 | 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff, 21 | 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00, 22 | 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01, 23 | 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10, 24 | 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00, 25 | 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00, 26 | 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00, 27 | 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00, 28 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, 29 | 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00, 30 | 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40, 31 | 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43, 32 | 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, 33 | 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40, 34 | 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06, 35 | 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07, 36 | 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, 37 | 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10, 38 | 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00, 39 | 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb, 40 | 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff, 41 | 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09, 42 | 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff, 43 | 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08, 44 | 0x00,0xf0,0x02,0x00 45 | }; 46 | 47 | /* 48 | * Hacker Disassembler Engine 64 C 49 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 50 | * All rights reserved. 51 | * 52 | */ 53 | unsigned int hde64_disasm(const void *code, hde64s *hs) 54 | { 55 | uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; 56 | uint8_t *ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0; 57 | uint8_t op64 = 0; 58 | 59 | memset(hs,0,sizeof(hde64s)); 60 | 61 | for (x = 16; x; x--) 62 | switch (c = *p++) { 63 | case 0xf3: 64 | hs->p_rep = c; 65 | pref |= PRE_F3; 66 | break; 67 | case 0xf2: 68 | hs->p_rep = c; 69 | pref |= PRE_F2; 70 | break; 71 | case 0xf0: 72 | hs->p_lock = c; 73 | pref |= PRE_LOCK; 74 | break; 75 | case 0x26: case 0x2e: case 0x36: 76 | case 0x3e: case 0x64: case 0x65: 77 | hs->p_seg = c; 78 | pref |= PRE_SEG; 79 | break; 80 | case 0x66: 81 | hs->p_66 = c; 82 | pref |= PRE_66; 83 | break; 84 | case 0x67: 85 | hs->p_67 = c; 86 | pref |= PRE_67; 87 | break; 88 | default: 89 | goto pref_done; 90 | } 91 | pref_done: 92 | 93 | hs->flags = (uint32_t)pref << 23; 94 | 95 | if (!pref) 96 | pref |= PRE_NONE; 97 | 98 | if ((c & 0xf0) == 0x40) { 99 | hs->flags |= F_PREFIX_REX; 100 | if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8) 101 | op64++; 102 | hs->rex_r = (c & 7) >> 2; 103 | hs->rex_x = (c & 3) >> 1; 104 | hs->rex_b = c & 1; 105 | if (((c = *p++) & 0xf0) == 0x40) { 106 | opcode = c; 107 | goto error_opcode; 108 | } 109 | } 110 | 111 | if ((hs->opcode = c) == 0x0f) { 112 | hs->opcode2 = c = *p++; 113 | ht += DELTA_OPCODES; 114 | } else if (c >= 0xa0 && c <= 0xa3) { 115 | op64++; 116 | if (pref & PRE_67) 117 | pref |= PRE_66; 118 | else 119 | pref &= ~PRE_66; 120 | } 121 | 122 | opcode = c; 123 | cflags = ht[ht[opcode / 4] + (opcode % 4)]; 124 | 125 | if (cflags == C_ERROR) { 126 | error_opcode: 127 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 128 | cflags = 0; 129 | if ((opcode & -3) == 0x24) 130 | cflags++; 131 | } 132 | 133 | x = 0; 134 | if (cflags & C_GROUP) { 135 | uint16_t t; 136 | t = *(uint16_t *)(ht + (cflags & 0x7f)); 137 | cflags = (uint8_t)t; 138 | x = (uint8_t)(t >> 8); 139 | } 140 | 141 | if (hs->opcode2) { 142 | ht = hde64_table + DELTA_PREFIXES; 143 | if (ht[ht[opcode / 4] + (opcode % 4)] & pref) 144 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 145 | } 146 | 147 | if (cflags & C_MODRM) { 148 | hs->flags |= F_MODRM; 149 | hs->modrm = c = *p++; 150 | hs->modrm_mod = m_mod = c >> 6; 151 | hs->modrm_rm = m_rm = c & 7; 152 | hs->modrm_reg = m_reg = (c & 0x3f) >> 3; 153 | 154 | if (x && ((x << m_reg) & 0x80)) 155 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 156 | 157 | if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { 158 | uint8_t t = opcode - 0xd9; 159 | if (m_mod == 3) { 160 | ht = hde64_table + DELTA_FPU_MODRM + t*8; 161 | t = ht[m_reg] << m_rm; 162 | } else { 163 | ht = hde64_table + DELTA_FPU_REG; 164 | t = ht[t] << m_reg; 165 | } 166 | if (t & 0x80) 167 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 168 | } 169 | 170 | if (pref & PRE_LOCK) { 171 | if (m_mod == 3) { 172 | hs->flags |= F_ERROR | F_ERROR_LOCK; 173 | } else { 174 | uint8_t *table_end, op = opcode; 175 | if (hs->opcode2) { 176 | ht = hde64_table + DELTA_OP2_LOCK_OK; 177 | table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; 178 | } else { 179 | ht = hde64_table + DELTA_OP_LOCK_OK; 180 | table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; 181 | op &= -2; 182 | } 183 | for (; ht != table_end; ht++) 184 | if (*ht++ == op) { 185 | if (!((*ht << m_reg) & 0x80)) 186 | goto no_lock_error; 187 | else 188 | break; 189 | } 190 | hs->flags |= F_ERROR | F_ERROR_LOCK; 191 | no_lock_error: 192 | ; 193 | } 194 | } 195 | 196 | if (hs->opcode2) { 197 | switch (opcode) { 198 | case 0x20: case 0x22: 199 | m_mod = 3; 200 | if (m_reg > 4 || m_reg == 1) 201 | goto error_operand; 202 | else 203 | goto no_error_operand; 204 | case 0x21: case 0x23: 205 | m_mod = 3; 206 | if (m_reg == 4 || m_reg == 5) 207 | goto error_operand; 208 | else 209 | goto no_error_operand; 210 | } 211 | } else { 212 | switch (opcode) { 213 | case 0x8c: 214 | if (m_reg > 5) 215 | goto error_operand; 216 | else 217 | goto no_error_operand; 218 | case 0x8e: 219 | if (m_reg == 1 || m_reg > 5) 220 | goto error_operand; 221 | else 222 | goto no_error_operand; 223 | } 224 | } 225 | 226 | if (m_mod == 3) { 227 | uint8_t *table_end; 228 | if (hs->opcode2) { 229 | ht = hde64_table + DELTA_OP2_ONLY_MEM; 230 | table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM; 231 | } else { 232 | ht = hde64_table + DELTA_OP_ONLY_MEM; 233 | table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; 234 | } 235 | for (; ht != table_end; ht += 2) 236 | if (*ht++ == opcode) { 237 | if (*ht++ & pref && !((*ht << m_reg) & 0x80)) 238 | goto error_operand; 239 | else 240 | break; 241 | } 242 | goto no_error_operand; 243 | } else if (hs->opcode2) { 244 | switch (opcode) { 245 | case 0x50: case 0xd7: case 0xf7: 246 | if (pref & (PRE_NONE | PRE_66)) 247 | goto error_operand; 248 | break; 249 | case 0xd6: 250 | if (pref & (PRE_F2 | PRE_F3)) 251 | goto error_operand; 252 | break; 253 | case 0xc5: 254 | goto error_operand; 255 | } 256 | goto no_error_operand; 257 | } else 258 | goto no_error_operand; 259 | 260 | error_operand: 261 | hs->flags |= F_ERROR | F_ERROR_OPERAND; 262 | no_error_operand: 263 | 264 | c = *p++; 265 | if (m_reg <= 1) { 266 | if (opcode == 0xf6) 267 | cflags |= C_IMM8; 268 | else if (opcode == 0xf7) 269 | cflags |= C_IMM_P66; 270 | } 271 | 272 | switch (m_mod) { 273 | case 0: 274 | if (pref & PRE_67) { 275 | if (m_rm == 6) 276 | disp_size = 2; 277 | } else 278 | if (m_rm == 5) 279 | disp_size = 4; 280 | break; 281 | case 1: 282 | disp_size = 1; 283 | break; 284 | case 2: 285 | disp_size = 2; 286 | if (!(pref & PRE_67)) 287 | disp_size <<= 1; 288 | } 289 | 290 | if (m_mod != 3 && m_rm == 4) { 291 | hs->flags |= F_SIB; 292 | p++; 293 | hs->sib = c; 294 | hs->sib_scale = c >> 6; 295 | hs->sib_index = (c & 0x3f) >> 3; 296 | if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) 297 | disp_size = 4; 298 | } 299 | 300 | p--; 301 | switch (disp_size) { 302 | case 1: 303 | hs->flags |= F_DISP8; 304 | hs->disp.disp8 = *p; 305 | break; 306 | case 2: 307 | hs->flags |= F_DISP16; 308 | hs->disp.disp16 = *(uint16_t *)p; 309 | break; 310 | case 4: 311 | hs->flags |= F_DISP32; 312 | hs->disp.disp32 = *(uint32_t *)p; 313 | } 314 | p += disp_size; 315 | } else if (pref & PRE_LOCK) 316 | hs->flags |= F_ERROR | F_ERROR_LOCK; 317 | 318 | if (cflags & C_IMM_P66) { 319 | if (cflags & C_REL32) { 320 | if (pref & PRE_66) { 321 | hs->flags |= F_IMM16 | F_RELATIVE; 322 | hs->imm.imm16 = *(uint16_t *)p; 323 | p += 2; 324 | goto disasm_done; 325 | } 326 | goto rel32_ok; 327 | } 328 | if (op64) { 329 | hs->flags |= F_IMM64; 330 | hs->imm.imm64 = *(uint64_t *)p; 331 | p += 8; 332 | } else if (!(pref & PRE_66)) { 333 | hs->flags |= F_IMM32; 334 | hs->imm.imm32 = *(uint32_t *)p; 335 | p += 4; 336 | } else 337 | goto imm16_ok; 338 | } 339 | 340 | 341 | if (cflags & C_IMM16) { 342 | imm16_ok: 343 | hs->flags |= F_IMM16; 344 | hs->imm.imm16 = *(uint16_t *)p; 345 | p += 2; 346 | } 347 | if (cflags & C_IMM8) { 348 | hs->flags |= F_IMM8; 349 | hs->imm.imm8 = *p++; 350 | } 351 | 352 | if (cflags & C_REL32) { 353 | rel32_ok: 354 | hs->flags |= F_IMM32 | F_RELATIVE; 355 | hs->imm.imm32 = *(uint32_t *)p; 356 | p += 4; 357 | } else if (cflags & C_REL8) { 358 | hs->flags |= F_IMM8 | F_RELATIVE; 359 | hs->imm.imm8 = *p++; 360 | } 361 | 362 | disasm_done: 363 | 364 | if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) { 365 | hs->flags |= F_ERROR | F_ERROR_LENGTH; 366 | hs->len = 15; 367 | } 368 | 369 | return (unsigned int)hs->len; 370 | } 371 | 372 | 373 | 374 | #endif /* X86_64_CPP_ */ 375 | -------------------------------------------------------------------------------- /project/turbodex/turbodex/src/main/jni/MSHook/Thumb.cpp: -------------------------------------------------------------------------------- 1 | #include "ARM.h" 2 | #include "Thumb.h" 3 | 4 | static size_t Thumb::MSGetInstructionWidth(void *start) { 5 | if ((reinterpret_cast(start) & 0x1) == 0) 6 | return MSGetInstructionWidthARM(start); 7 | else 8 | return MSGetInstructionWidthThumb(reinterpret_cast(reinterpret_cast(start) & ~0x1)); 9 | } 10 | 11 | void Thumb::SubstrateHookFunctionThumb(SubstrateProcessRef process, void *symbol, void *replace, void **result){ 12 | if (symbol == NULL) 13 | return; 14 | 15 | uint16_t *area(reinterpret_cast(symbol)); 16 | 17 | unsigned align((reinterpret_cast(area) & 0x2) == 0 ? 0 : 1); 18 | uint16_t *thumb(area + align); 19 | 20 | uint32_t *arm(reinterpret_cast(thumb + 2)); 21 | uint16_t *trail(reinterpret_cast(arm + 2)); 22 | 23 | if ( 24 | (align == 0 || area[0] == T$nop) && 25 | thumb[0] == T$bx(A$pc) && 26 | thumb[1] == T$nop && 27 | arm[0] == A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8) 28 | ) { 29 | if (result != NULL) 30 | *result = reinterpret_cast(arm[1]); 31 | 32 | SubstrateHookMemory code(process, arm + 1, sizeof(uint32_t) * 1); 33 | 34 | arm[1] = reinterpret_cast(replace); 35 | 36 | return; 37 | } 38 | 39 | size_t required((trail - area) * sizeof(uint16_t)); 40 | 41 | size_t used(0); 42 | while (used < required) 43 | used += MSGetInstructionWidthThumb(reinterpret_cast(area) + used); 44 | used = (used + sizeof(uint16_t) - 1) / sizeof(uint16_t) * sizeof(uint16_t); 45 | 46 | size_t blank((used - required) / sizeof(uint16_t)); 47 | 48 | uint16_t backup[used / sizeof(uint16_t)]; 49 | memcpy(backup, area, used); 50 | 51 | if (MSDebug) { 52 | char name[16]; 53 | sprintf(name, "%p", area); 54 | MSLogHexEx(area, used + sizeof(uint16_t), 2, name); 55 | } 56 | 57 | if (result != NULL) { 58 | 59 | size_t length(used); 60 | for (unsigned offset(0); offset != used / sizeof(uint16_t); ++offset) 61 | if (T$pcrel$ldr(backup[offset])) 62 | length += 3 * sizeof(uint16_t); 63 | else if (T$pcrel$b(backup[offset])) 64 | length += 6 * sizeof(uint16_t); 65 | else if (T2$pcrel$b(backup + offset)) { 66 | length += 5 * sizeof(uint16_t); 67 | ++offset; 68 | } else if (T$pcrel$bl(backup + offset)) { 69 | length += 5 * sizeof(uint16_t); 70 | ++offset; 71 | } else if (T$pcrel$cbz(backup[offset])) { 72 | length += 16 * sizeof(uint16_t); 73 | } else if (T$pcrel$ldrw(backup[offset])) { 74 | length += 4 * sizeof(uint16_t); 75 | ++offset; 76 | } else if (T$pcrel$add(backup[offset])) 77 | length += 6 * sizeof(uint16_t); 78 | else if (T$32bit$i(backup[offset])) 79 | ++offset; 80 | 81 | unsigned pad((length & 0x2) == 0 ? 0 : 1); 82 | length += (pad + 2) * sizeof(uint16_t) + 2 * sizeof(uint32_t); 83 | 84 | uint16_t *buffer(reinterpret_cast(mmap( 85 | NULL, length, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 86 | ))); 87 | 88 | if (buffer == MAP_FAILED) { 89 | MSLog(MSLogLevelError, "MS:Error:mmap() = %d", errno); 90 | *result = NULL; 91 | return; 92 | } 93 | 94 | if (false) fail: { 95 | munmap(buffer, length); 96 | *result = NULL; 97 | return; 98 | } 99 | 100 | size_t start(pad), end(length / sizeof(uint16_t)); 101 | uint32_t *trailer(reinterpret_cast(buffer + end)); 102 | for (unsigned offset(0); offset != used / sizeof(uint16_t); ++offset) { 103 | if (T$pcrel$ldr(backup[offset])) { 104 | union { 105 | uint16_t value; 106 | 107 | struct { 108 | uint16_t immediate : 8; 109 | uint16_t rd : 3; 110 | uint16_t : 5; 111 | }; 112 | } bits = {backup[offset+0]}; 113 | 114 | buffer[start+0] = T$ldr_rd_$pc_im_4$(bits.rd, T$Label(start+0, end-2) / 4); 115 | buffer[start+1] = T$ldr_rd_$rn_im_4$(bits.rd, bits.rd, 0); 116 | 117 | // XXX: this code "works", but is "wrong": the mechanism is more complex than this 118 | *--trailer = ((reinterpret_cast(area + offset) + 4) & ~0x2) + bits.immediate * 4; 119 | 120 | start += 2; 121 | end -= 2; 122 | } else if (T$pcrel$b(backup[offset])) { 123 | union { 124 | uint16_t value; 125 | 126 | struct { 127 | uint16_t imm8 : 8; 128 | uint16_t cond : 4; 129 | uint16_t /*1101*/ : 4; 130 | }; 131 | } bits = {backup[offset+0]}; 132 | 133 | intptr_t jump(bits.imm8 << 1); 134 | jump |= 1; 135 | jump <<= 23; 136 | jump >>= 23; 137 | 138 | buffer[start+0] = T$b$_$im(bits.cond, (end-6 - (start+0)) * 2 - 4); 139 | 140 | *--trailer = reinterpret_cast(area + offset) + 4 + jump; 141 | *--trailer = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 142 | *--trailer = T$nop << 16 | T$bx(A$pc); 143 | 144 | start += 1; 145 | end -= 6; 146 | } else if (T2$pcrel$b(backup + offset)) { 147 | union { 148 | uint16_t value; 149 | 150 | struct { 151 | uint16_t imm6 : 6; 152 | uint16_t cond : 4; 153 | uint16_t s : 1; 154 | uint16_t : 5; 155 | }; 156 | } bits = {backup[offset+0]}; 157 | 158 | union { 159 | uint16_t value; 160 | 161 | struct { 162 | uint16_t imm11 : 11; 163 | uint16_t j2 : 1; 164 | uint16_t a : 1; 165 | uint16_t j1 : 1; 166 | uint16_t : 2; 167 | }; 168 | } exts = {backup[offset+1]}; 169 | 170 | intptr_t jump(1); 171 | jump |= exts.imm11 << 1; 172 | jump |= bits.imm6 << 12; 173 | 174 | if (exts.a) { 175 | jump |= bits.s << 24; 176 | jump |= (~(bits.s ^ exts.j1) & 0x1) << 23; 177 | jump |= (~(bits.s ^ exts.j2) & 0x1) << 22; 178 | jump |= bits.cond << 18; 179 | jump <<= 7; 180 | jump >>= 7; 181 | } else { 182 | jump |= bits.s << 20; 183 | jump |= exts.j2 << 19; 184 | jump |= exts.j1 << 18; 185 | jump <<= 11; 186 | jump >>= 11; 187 | } 188 | 189 | buffer[start+0] = T$b$_$im(exts.a ? A$al : bits.cond, (end-6 - (start+0)) * 2 - 4); 190 | 191 | *--trailer = reinterpret_cast(area + offset) + 4 + jump; 192 | *--trailer = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 193 | *--trailer = T$nop << 16 | T$bx(A$pc); 194 | 195 | ++offset; 196 | start += 1; 197 | end -= 6; 198 | } else if (T$pcrel$bl(backup + offset)) { 199 | union { 200 | uint16_t value; 201 | 202 | struct { 203 | uint16_t immediate : 10; 204 | uint16_t s : 1; 205 | uint16_t : 5; 206 | }; 207 | } bits = {backup[offset+0]}; 208 | 209 | union { 210 | uint16_t value; 211 | 212 | struct { 213 | uint16_t immediate : 11; 214 | uint16_t j2 : 1; 215 | uint16_t x : 1; 216 | uint16_t j1 : 1; 217 | uint16_t : 2; 218 | }; 219 | } exts = {backup[offset+1]}; 220 | 221 | int32_t jump(0); 222 | jump |= bits.s << 24; 223 | jump |= (~(bits.s ^ exts.j1) & 0x1) << 23; 224 | jump |= (~(bits.s ^ exts.j2) & 0x1) << 22; 225 | jump |= bits.immediate << 12; 226 | jump |= exts.immediate << 1; 227 | jump |= exts.x; 228 | jump <<= 7; 229 | jump >>= 7; 230 | 231 | buffer[start+0] = T$push_r(1 << A$r7); 232 | buffer[start+1] = T$ldr_rd_$pc_im_4$(A$r7, ((end-2 - (start+1)) * 2 - 4 + 2) / 4); 233 | buffer[start+2] = T$mov_rd_rm(A$lr, A$r7); 234 | buffer[start+3] = T$pop_r(1 << A$r7); 235 | buffer[start+4] = T$blx(A$lr); 236 | 237 | *--trailer = reinterpret_cast(area + offset) + 4 + jump; 238 | 239 | ++offset; 240 | start += 5; 241 | end -= 2; 242 | } else if (T$pcrel$cbz(backup[offset])) { 243 | union { 244 | uint16_t value; 245 | 246 | struct { 247 | uint16_t rn : 3; 248 | uint16_t immediate : 5; 249 | uint16_t : 1; 250 | uint16_t i : 1; 251 | uint16_t : 1; 252 | uint16_t op : 1; 253 | uint16_t : 4; 254 | }; 255 | } bits = {backup[offset+0]}; 256 | 257 | intptr_t jump(1); 258 | jump |= bits.i << 6; 259 | jump |= bits.immediate << 1; 260 | 261 | //jump <<= 24; 262 | //jump >>= 24; 263 | 264 | unsigned rn(bits.rn); 265 | unsigned rt(rn == A$r7 ? A$r6 : A$r7); 266 | 267 | buffer[start+0] = T$push_r(1 << rt); 268 | buffer[start+1] = T1$mrs_rd_apsr(rt); 269 | buffer[start+2] = T2$mrs_rd_apsr(rt); 270 | buffer[start+3] = T$cbz$_rn_$im(bits.op, rn, (end-10 - (start+3)) * 2 - 4); 271 | buffer[start+4] = T1$msr_apsr_nzcvqg_rn(rt); 272 | buffer[start+5] = T2$msr_apsr_nzcvqg_rn(rt); 273 | buffer[start+6] = T$pop_r(1 << rt); 274 | 275 | *--trailer = reinterpret_cast(area + offset) + 4 + jump; 276 | *--trailer = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 277 | *--trailer = T$nop << 16 | T$bx(A$pc); 278 | *--trailer = T$nop << 16 | T$pop_r(1 << rt); 279 | *--trailer = T$msr_apsr_nzcvqg_rn(rt); 280 | 281 | #if 0 282 | if ((start & 0x1) == 0) 283 | buffer[start++] = T$nop; 284 | buffer[start++] = T$bx(A$pc); 285 | buffer[start++] = T$nop; 286 | 287 | uint32_t *arm(reinterpret_cast(buffer + start)); 288 | arm[0] = A$add(A$lr, A$pc, 1); 289 | arm[1] = A$ldr_rd_$rn_im$(A$pc, A$pc, (trailer - arm) * sizeof(uint32_t) - 8); 290 | #endif 291 | 292 | start += 7; 293 | end -= 10; 294 | } else if (T$pcrel$ldrw(backup[offset])) { 295 | union { 296 | uint16_t value; 297 | 298 | struct { 299 | uint16_t : 7; 300 | uint16_t u : 1; 301 | uint16_t : 8; 302 | }; 303 | } bits = {backup[offset+0]}; 304 | 305 | union { 306 | uint16_t value; 307 | 308 | struct { 309 | uint16_t immediate : 12; 310 | uint16_t rt : 4; 311 | }; 312 | } exts = {backup[offset+1]}; 313 | 314 | buffer[start+0] = T1$ldr_rt_$rn_im$(exts.rt, A$pc, T$Label(start+0, end-2)); 315 | buffer[start+1] = T2$ldr_rt_$rn_im$(exts.rt, A$pc, T$Label(start+0, end-2)); 316 | 317 | buffer[start+2] = T1$ldr_rt_$rn_im$(exts.rt, exts.rt, 0); 318 | buffer[start+3] = T2$ldr_rt_$rn_im$(exts.rt, exts.rt, 0); 319 | 320 | // XXX: this code "works", but is "wrong": the mechanism is more complex than this 321 | *--trailer = ((reinterpret_cast(area + offset) + 4) & ~0x2) + (bits.u == 0 ? -exts.immediate : exts.immediate); 322 | 323 | ++offset; 324 | start += 4; 325 | end -= 2; 326 | } else if (T$pcrel$add(backup[offset])) { 327 | union { 328 | uint16_t value; 329 | 330 | struct { 331 | uint16_t rd : 3; 332 | uint16_t rm : 3; 333 | uint16_t h2 : 1; 334 | uint16_t h1 : 1; 335 | uint16_t : 8; 336 | }; 337 | } bits = {backup[offset+0]}; 338 | 339 | if (bits.h1) { 340 | MSLog(MSLogLevelError, "MS:Error:pcrel(%u):add (rd > r7)", offset); 341 | goto fail; 342 | } 343 | 344 | unsigned rt(bits.rd == A$r7 ? A$r6 : A$r7); 345 | 346 | buffer[start+0] = T$push_r(1 << rt); 347 | buffer[start+1] = T$mov_rd_rm(rt, (bits.h1 << 3) | bits.rd); 348 | buffer[start+2] = T$ldr_rd_$pc_im_4$(bits.rd, T$Label(start+2, end-2) / 4); 349 | buffer[start+3] = T$add_rd_rm((bits.h1 << 3) | bits.rd, rt); 350 | buffer[start+4] = T$pop_r(1 << rt); 351 | *--trailer = reinterpret_cast(area + offset) + 4; 352 | 353 | start += 5; 354 | end -= 2; 355 | } else if (T$32bit$i(backup[offset])) { 356 | buffer[start++] = backup[offset]; 357 | buffer[start++] = backup[++offset]; 358 | } else { 359 | buffer[start++] = backup[offset]; 360 | } 361 | } 362 | 363 | buffer[start++] = T$bx(A$pc); 364 | buffer[start++] = T$nop; 365 | 366 | uint32_t *transfer = reinterpret_cast(buffer + start); 367 | transfer[0] = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 368 | transfer[1] = reinterpret_cast(area + used / sizeof(uint16_t)) + 1; 369 | 370 | if (mprotect(buffer, length, PROT_READ | PROT_EXEC) == -1) { 371 | MSLog(MSLogLevelError, "MS:Error:mprotect():%d", errno); 372 | return; 373 | } 374 | 375 | *result = reinterpret_cast(buffer + pad) + 1; 376 | 377 | if (MSDebug) { 378 | char name[16]; 379 | sprintf(name, "%p", *result); 380 | MSLogHexEx(buffer, length, 2, name); 381 | } 382 | 383 | } 384 | 385 | { 386 | SubstrateHookMemory code(process, area, used); 387 | 388 | if (align != 0) 389 | area[0] = T$nop; 390 | 391 | thumb[0] = T$bx(A$pc); 392 | thumb[1] = T$nop; 393 | 394 | arm[0] = A$ldr_rd_$rn_im$(A$pc, A$pc, 4 - 8); 395 | arm[1] = reinterpret_cast(replace); 396 | 397 | for (unsigned offset(0); offset != blank; ++offset) 398 | trail[offset] = T$nop; 399 | } 400 | 401 | if (MSDebug) { 402 | char name[16]; 403 | sprintf(name, "%p", area); 404 | MSLogHexEx(area, used + sizeof(uint16_t), 2, name); 405 | } 406 | } 407 | 408 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------