0x00
在前一篇文章Android SO逆向-基本数据类型及函数的工作原理中,我们介绍了ndk的使用,这篇文章直接列出C++源码及对应的汇编代码。
0x01
在java层主要是调用native方法,现在列出java层的代码:
Lesson1.java
package com.example.ndkreverse1;
public class Lesson1 {
static {
System.loadLibrary("lesson1");
}
public static native int getInt();
public native String getString();
public static native int getFor1(int n);
public static native String getIfElse(int n);
public static native int getWhile(int n);
public static native int getSwitch(int a,int b,int i);
public static native int getOperation(int a, int b);
}
MainActivity.java
package com.example.ndkreverse1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Lesson1 lesson1 = new Lesson1();
lesson1.getInt();
lesson1.getString();
lesson1.getFor1(5);
lesson1.getIfElse(20);
lesson1.getWhile(5);
lesson1.getSwitch(3, 4, 3);
lesson1.getOperation(9, 16);
}
}
在MainActivity中调用的native方法,是在native层实现的。
#include "com_example_ndkreverse1_Lesson1.h"
#include <android/log.h>
#define LOG_TAG "lesson1"
#define ALOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
JNIEXPORT jint JNICALL Java_com_example_ndkreverse1_Lesson1_getInt
(JNIEnv * env, jclass jclass) {
return 8;
}
JNIEXPORT jstring JNICALL Java_com_example_ndkreverse1_Lesson1_getString
(JNIEnv * env, jobject jobject) {
return env->NewStringUTF("method call");
}
JNIEXPORT jint JNICALL Java_com_example_ndkreverse1_Lesson1_getFor1
(JNIEnv * env, jclass jclass, jint n) {
int i = 0;
int s = 0;
for (i = 0; i < n; i++){
s += i * 2;
}
return s;
}
JNIEXPORT jstring JNICALL Java_com_example_ndkreverse1_Lesson1_getIfElse
(JNIEnv * env, jclass jclass, jint n) {
if(n < 16) {
return env->NewStringUTF("he is a boy");
} else if(n < 30){
return env->NewStringUTF("he is a young man");
} else if(n < 45){
return env->NewStringUTF("he is a strong man");
} else{
return env->NewStringUTF("he is an old man");
}
}
JNIEXPORT jint JNICALL Java_com_example_ndkreverse1_Lesson1_getWhile
(JNIEnv * env, jclass jclass, jint n) {
int i = 1;
int s = 0;
while(i <= n) {
s += i++;
}
return s;
}
JNIEXPORT jint JNICALL Java_com_example_ndkreverse1_Lesson1_getSwitch
(JNIEnv * env, jclass jclass, jint a, jint b, jint i) {
switch (i) {
case 1:
return a + b;
break;
case 2:
return a - b;
break;
case 3:
return a * b;
break;
case 4:
return a / b;
break;
default:
return a + b;
break;
}
}
JNIEXPORT jint JNICALL Java_com_example_ndkreverse1_Lesson1_getOperation
(JNIEnv * env, jclass jclass, jint a, jint b) {
if (a > 10 || !(b <=20 && b != 15)) {
return 8;
} else {
return