From e4af881bdbae26ebb9b5bfd3b0417be9b1a8115d Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Thu, 17 Aug 2017 12:30:36 -0700 Subject: [PATCH] Do not define JNI_OnLoad when not dynamic Motivation: Due to an oversight (by myself), linking two JNI modules with duplicate symbols fails in linking. This only seems to happen some of the time (the behavior seems to be different between GCC and Clang toolchains). For instance, including both netty tcnative and netty epoll fails to link because of duplicate JNI_OnLoad symobols. Modification: Do not define the JNI_OnLoad and JNI_OnUnload symbols when compiling for static linkage, as indicated by the NETTY_BUILD_STATIC preprocessor define. They are never directly called when statically linked. Result: Able to statically compile epoll and tcnative code into a single binary. --- transport-native-epoll/src/main/c/netty_epoll_native.c | 8 ++++++-- transport-native-kqueue/src/main/c/netty_kqueue_native.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/transport-native-epoll/src/main/c/netty_epoll_native.c b/transport-native-epoll/src/main/c/netty_epoll_native.c index f1e8cb87bc..f3b7def978 100644 --- a/transport-native-epoll/src/main/c/netty_epoll_native.c +++ b/transport-native-epoll/src/main/c/netty_epoll_native.c @@ -595,7 +595,7 @@ jint JNI_OnLoad_netty_transport_native_epoll(JavaVM* vm, void* reserved) { return JNI_ERR; } char* packagePrefix = NULL; -#ifndef NETTY_NOT_DYNAMIC +#ifndef NETTY_BUILD_STATIC Dl_info dlinfo; jint status = 0; // We need to use an address of a function that is uniquely part of this library, so choose a static @@ -609,7 +609,7 @@ jint JNI_OnLoad_netty_transport_native_epoll(JavaVM* vm, void* reserved) { fprintf(stderr, "FATAL: transport-native-epoll JNI encountered unexpected dlinfo.dli_fname: %s\n", dlinfo.dli_fname); return JNI_ERR; } -#endif /* NETTY_NOT_DYNAMIC */ +#endif /* NETTY_BUILD_STATIC */ jint ret = netty_epoll_native_JNI_OnLoad(env, packagePrefix); if (packagePrefix != NULL) { @@ -620,9 +620,11 @@ jint JNI_OnLoad_netty_transport_native_epoll(JavaVM* vm, void* reserved) { return ret; } +#ifndef NETTY_BUILD_STATIC jint JNI_OnLoad(JavaVM* vm, void* reserved) { return JNI_OnLoad_netty_transport_native_epoll(vm, reserved); } +#endif /* NETTY_BUILD_STATIC */ // Invoked by the JVM when statically linked void JNI_OnUnload_netty_transport_native_epoll(JavaVM* vm, void* reserved) { @@ -634,6 +636,8 @@ void JNI_OnUnload_netty_transport_native_epoll(JavaVM* vm, void* reserved) { netty_epoll_native_JNI_OnUnLoad(env); } +#ifndef NETTY_BUILD_STATIC void JNI_OnUnload(JavaVM* vm, void* reserved) { JNI_OnUnload_netty_transport_native_epoll(vm, reserved); } +#endif /* NETTY_BUILD_STATIC */ diff --git a/transport-native-kqueue/src/main/c/netty_kqueue_native.c b/transport-native-kqueue/src/main/c/netty_kqueue_native.c index 1ae15834a1..b9ff0f6f67 100644 --- a/transport-native-kqueue/src/main/c/netty_kqueue_native.c +++ b/transport-native-kqueue/src/main/c/netty_kqueue_native.c @@ -278,7 +278,7 @@ jint JNI_OnLoad_netty_transport_native_kqueue(JavaVM* vm, void* reserved) { } char* packagePrefix = NULL; -#ifndef NETTY_NOT_DYNAMIC +#ifndef NETTY_BUILD_STATIC Dl_info dlinfo; jint status = 0; // We need to use an address of a function that is uniquely part of this library, so choose a static @@ -292,7 +292,7 @@ jint JNI_OnLoad_netty_transport_native_kqueue(JavaVM* vm, void* reserved) { fprintf(stderr, "FATAL: transport-native-kqueue JNI encountered unexpected dlinfo.dli_fname: %s\n", dlinfo.dli_fname); return JNI_ERR; } -#endif /* NETTY_NOT_DYNAMIC */ +#endif /* NETTY_BUILD_STATIC */ jint ret = netty_kqueue_native_JNI_OnLoad(env, packagePrefix); if (packagePrefix != NULL) { @@ -303,9 +303,11 @@ jint JNI_OnLoad_netty_transport_native_kqueue(JavaVM* vm, void* reserved) { return ret; } +#ifndef NETTY_BUILD_STATIC jint JNI_OnLoad(JavaVM* vm, void* reserved) { return JNI_OnLoad_netty_transport_native_kqueue(vm, reserved); } +#endif /* NETTY_BUILD_STATIC */ // Invoked by the JVM when statically linked void JNI_OnUnload_netty_transport_native_kqueue(JavaVM* vm, void* reserved) { @@ -317,6 +319,8 @@ void JNI_OnUnload_netty_transport_native_kqueue(JavaVM* vm, void* reserved) { netty_kqueue_native_JNI_OnUnLoad(env); } +#ifndef NETTY_BUILD_STATIC void JNI_OnUnload(JavaVM* vm, void* reserved) { return JNI_OnUnload_netty_transport_native_kqueue(vm, reserved); } +#endif /* NETTY_BUILD_STATIC */