This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
jni_lib_merge.h
89 lines (82 loc) · 2.89 KB
/
jni_lib_merge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <stddef.h>
#include <jni.h>
#ifdef JNI_MERGE_PRINT_ONLOAD
# ifdef __ANDROID__
# include <android/log.h>
# define JNI_MERGE_PRINT(...) \
__android_log_print(ANDROID_LOG_DEBUG, "jni_lib_merge", __VA_ARGS__)
# else
# include <stdio.h>
# define JNI_MERGE_PRINT(...) fprintf(stderr, __VA_ARGS__);
# endif
#else
# define JNI_MERGE_PRINT(...) do{}while(0)
#endif
#ifdef __cplusplus
#define JNI_MERGE_GET_JAVA_VM(env, vm) (env->GetJavaVM(&vm))
#else
#define JNI_MERGE_GET_JAVA_VM(env, vm) ((*env)->GetJavaVM(env, &vm))
#endif
struct pre_merge_jni_library {
const char* name;
int (*onload_func)(JNIEnv*, jclass);
};
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL JNI_OnLoad_Weak(JavaVM* vm, void* reserved);
#ifdef __cplusplus
}
#endif
#define JNI_OnLoad \
/* Need to make JNI_OnLoad weak, which requires splitting it into */ \
/* a declaration and definition, which is a little risky. */ \
/* Hopefully they didn't use 'extern "C"'. */ \
JNI_OnLoad_Weak(JavaVM* vm, void* reserved) __attribute__ ((weak)); \
\
/* We rename the declared JNI_OnLoad to this so we can call it */ \
/* from either our weak JNI_OnLoad or our merge-friendly init function. */ \
static jint pre_merge_original_JNI_OnLoad(JavaVM* vm, void* reserved); \
\
/* Merge-friendly wrapper for the original JNI_OnLoad, called by JNI. */ \
/* Return non-zero to indicate failure. */ \
static inline jint pre_merge_jni_library_wrapper_for_JNI_OnLoad(JNIEnv* env, jclass clazz) { \
(void)clazz; \
JNI_MERGE_PRINT("In JNI_OnLoad wrapper for %s", ORIGINAL_SONAME); \
/* Note: relying on SoLoader's synchronization for thread-safety. */ \
static char already_loaded = 0; \
if (already_loaded) { return 0; } \
already_loaded = 1; \
JavaVM* vm; \
jint ret = JNI_MERGE_GET_JAVA_VM(env, vm); \
if (ret < 0) { \
/* Exception already thrown. */ \
return -1; \
} \
JNI_MERGE_PRINT("Calling original JNI_OnLoad for %s", ORIGINAL_SONAME); \
ret = pre_merge_original_JNI_OnLoad(vm, NULL); \
if (!(ret == JNI_VERSION_1_2 || ret == JNI_VERSION_1_4 || ret == JNI_VERSION_1_6)) { \
return -1; \
} \
return 0; \
} \
\
jint JNI_OnLoad_Weak(JavaVM* vm, void* reserved) { \
/* This path will be taken if we're not merged. */ \
/* Just call the original JNI_OnLoad. */ \
return pre_merge_original_JNI_OnLoad(vm, reserved); \
} \
\
/* Register our name and wrapper in the proper section. */ \
static struct pre_merge_jni_library pre_merge_jni_library_register_object \
__attribute__ ((__section__("pre_merge_jni_libraries"))) \
__attribute__ ((__used__)) \
= { \
.name = ORIGINAL_SONAME, \
.onload_func = pre_merge_jni_library_wrapper_for_JNI_OnLoad, \
}; \
\
/* Re-start the JNI_OnLoad prototype to capture the body. */ \
static jint pre_merge_original_JNI_OnLoad