diff --git a/.gitmodules b/.gitmodules index ac526ae946c..d41ed910026 100644 --- a/.gitmodules +++ b/.gitmodules @@ -6,14 +6,10 @@ path = external/debugger-libs url = https://github.com/mono/debugger-libs branch = master -[submodule "external/dlfcn-win32"] - path = external/dlfcn-win32 - url = https://github.com/dlfcn-win32/dlfcn-win32.git - branch = v1.1.1 [submodule "external/Java.Interop"] path = external/Java.Interop - url = https://github.com/xamarin/java.interop.git - branch = master + url = https://github.com/jonpryor/java.interop.git + branch = jonp-loadlib-search-order [submodule "external/lz4"] path = external/lz4 url = https://github.com/lz4/lz4.git diff --git a/build-tools/xaprepare/xaprepare/Steps/Step_BuildMingwDependencies.cs b/build-tools/xaprepare/xaprepare/Steps/Step_BuildMingwDependencies.cs index 44b3e92b644..0e9b30be0f9 100644 --- a/build-tools/xaprepare/xaprepare/Steps/Step_BuildMingwDependencies.cs +++ b/build-tools/xaprepare/xaprepare/Steps/Step_BuildMingwDependencies.cs @@ -8,7 +8,6 @@ namespace Xamarin.Android.Prepare class Step_BuildMingwDependencies : Step { static readonly SortedDictionary dependencies = new SortedDictionary (StringComparer.Ordinal) { - { "dlfcn-win32", (description: "libdl for Windows", libraryName: "libdl.a") }, { "mman-win32", (description: "mmap for Windows", libraryName: "libmman.a") }, }; diff --git a/build-tools/xaprepare/xaprepare/ThirdPartyNotices/dlfcn.cs b/build-tools/xaprepare/xaprepare/ThirdPartyNotices/dlfcn.cs deleted file mode 100644 index 07ddf0b1c57..00000000000 --- a/build-tools/xaprepare/xaprepare/ThirdPartyNotices/dlfcn.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace Xamarin.Android.Prepare -{ - [TPN] - class dlfcn_TPN : ThirdPartyNotice - { - static readonly Uri url = new Uri ("https://github.com/dlfcn-win32/dlfcn-win32/"); - static readonly string licenseFile = Path.Combine (Configurables.Paths.ExternalDir, "dlfcn-win32", "COPYING"); - - - public override string LicenseFile => licenseFile; - public override string Name => "dlfcn-win32/dlfcn-win32"; - public override Uri SourceUrl => url; - public override string LicenseText => ""; - - public override bool Include (bool includeExternalDeps, bool includeBuildDeps) => includeExternalDeps; - } -} diff --git a/build-tools/xaprepare/xaprepare/xaprepare.csproj b/build-tools/xaprepare/xaprepare/xaprepare.csproj index 417fec407c8..1a5fcd5a07c 100644 --- a/build-tools/xaprepare/xaprepare/xaprepare.csproj +++ b/build-tools/xaprepare/xaprepare/xaprepare.csproj @@ -157,7 +157,6 @@ - diff --git a/external/Java.Interop b/external/Java.Interop index 007b35b4891..f0bb80f3e3d 160000 --- a/external/Java.Interop +++ b/external/Java.Interop @@ -1 +1 @@ -Subproject commit 007b35b489173010de038c95cb2a0d3ec514f30d +Subproject commit f0bb80f3e3d98c086d09e52e6e96ee91f717daf9 diff --git a/external/dlfcn-win32 b/external/dlfcn-win32 deleted file mode 160000 index ef7e412d3fa..00000000000 --- a/external/dlfcn-win32 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ef7e412d3fa2b511e660342e237536c235e51ee4 diff --git a/src/monodroid/CMakeLists.txt b/src/monodroid/CMakeLists.txt index 191a4e2f55f..f777f00b3d7 100644 --- a/src/monodroid/CMakeLists.txt +++ b/src/monodroid/CMakeLists.txt @@ -196,7 +196,7 @@ else() if(WIN32 OR MINGW) message(STATUS "Win32 or MinGW") set(EXTRA_COMPILER_FLAGS "${EXTRA_COMPILER_FLAGS} -DWINDOWS -DNTDDI_VERSION=NTDDI_VISTA -D_WIN32_WINNT=_WIN32_WINNT_VISTA -fomit-frame-pointer") - set(EXTRA_LINKER_FLAGS "${EXTRA_LINKER_FLAGS} -ldl -lmman -static -pthread -dynamic -lmincore -lmswsock -lwsock32 -lshlwapi -lpsapi -lwinmm") + set(EXTRA_LINKER_FLAGS "${EXTRA_LINKER_FLAGS} -lmman -static -pthread -dynamic -lmincore -lmswsock -lwsock32 -lshlwapi -lpsapi -lwinmm") if (MINGW_TARGET_32) set(ANDROID_ABI "host-mxe-Win32") @@ -298,6 +298,7 @@ set(MONODROID_SOURCES ${JAVA_INTEROP_SRC_PATH}/java-interop.cc ${JAVA_INTEROP_SRC_PATH}/java-interop-mono.cc ${JAVA_INTEROP_SRC_PATH}/java-interop-util.cc + ${JAVA_INTEROP_SRC_PATH}/java-interop-dlfcn.cc ) set(XA_INTERNAL_API_SOURCES @@ -392,11 +393,13 @@ else() set(LINK_LIBS "-lmonosgen-2.0 -lz ${EXTRA_LINKER_FLAGS}") endif() -set(DEBUG_HELPER_LINK_LIBS "-ldl") +if(NOT MINGW AND NOT WIN32) + set(DEBUG_HELPER_LINK_LIBS "-ldl") +endif() if(ENABLE_NDK) set(LINK_LIBS "${LINK_LIBS} -llog") set(DEBUG_HELPER_LINK_LIBS "${DEBUG_HELPER_LINK_LIBS} -llog") -elseif(NOT ANDROID) +elseif(NOT ANDROID AND NOT MINGW AND NOT WIN32) set(LINK_LIBS "-pthread ${LINK_LIBS} -ldl") endif() diff --git a/src/monodroid/jni/android-system.cc b/src/monodroid/jni/android-system.cc index 99e8bf8398f..9a4384acbb5 100644 --- a/src/monodroid/jni/android-system.cc +++ b/src/monodroid/jni/android-system.cc @@ -4,7 +4,6 @@ #include #include #include -#include #include #ifdef ANDROID @@ -27,6 +26,8 @@ #include "jni-wrappers.hh" #include "xamarin-app.hh" #include "cpp-util.hh" +#include "java-interop-dlfcn.h" +#include "java-interop.h" #if defined (DEBUG) || !defined (ANDROID) namespace xamarin::android::internal { @@ -358,9 +359,11 @@ AndroidSystem::load_dso (const char *path, int dl_flags, bool skip_exists_check) return nullptr; } - void *handle = dlopen (path, dl_flags); + char *error = nullptr; + void *handle = java_interop_load_library (path, 0, &error); if (handle == nullptr && utils.should_log (LOG_ASSEMBLY)) - log_info_nocheck (LOG_ASSEMBLY, "Failed to load shared library '%s'. %s", path, dlerror ()); + log_info_nocheck (LOG_ASSEMBLY, "Failed to load shared library '%s'. %s", path, error); + java_interop_free (error); return handle; } diff --git a/src/monodroid/jni/debug.cc b/src/monodroid/jni/debug.cc index 293d3ab8b6d..d76ed815819 100644 --- a/src/monodroid/jni/debug.cc +++ b/src/monodroid/jni/debug.cc @@ -27,7 +27,6 @@ #include #include #include -#include #include #ifdef ANDROID @@ -42,6 +41,8 @@ #include "globals.hh" #include "cpp-util.hh" +#include "java-interop-dlfcn.h" + using namespace xamarin::android; // @@ -79,15 +80,14 @@ Debug::monodroid_profiler_load (const char *libmono_path, const char *desc, cons } simple_pointer_guard mname (mname_ptr); - int dlopen_flags = RTLD_LAZY; simple_pointer_guard libname (utils.string_concat ("libmono-profiler-", mname.get (), ".so")); bool found = false; - void *handle = androidSystem.load_dso_from_any_directories (libname, dlopen_flags); + void *handle = androidSystem.load_dso_from_any_directories (libname, 0); found = load_profiler_from_handle (handle, desc, mname); if (!found && libmono_path != nullptr) { simple_pointer_guard full_path (utils.path_combine (libmono_path, libname)); - handle = androidSystem.load_dso (full_path, dlopen_flags, FALSE); + handle = androidSystem.load_dso (full_path, 0, FALSE); found = load_profiler_from_handle (handle, desc, mname); } @@ -108,7 +108,7 @@ typedef void (*ProfilerInitializer) (const char*); bool Debug::load_profiler (void *handle, const char *desc, const char *symbol) { - ProfilerInitializer func = reinterpret_cast (dlsym (handle, symbol)); + ProfilerInitializer func = reinterpret_cast (java_interop_get_symbol_address (handle, symbol, nullptr)); log_warn (LOG_DEFAULT, "Looking for profiler init symbol '%s'? %p", symbol, func); if (func != nullptr) { @@ -129,7 +129,7 @@ Debug::load_profiler_from_handle (void *dso_handle, const char *desc, const char if (result) return true; - dlclose (dso_handle); + java_interop_close_library (dso_handle, nullptr); return false; } diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc index 75e5003f2a0..2d0a024eab5 100644 --- a/src/monodroid/jni/monodroid-glue.cc +++ b/src/monodroid/jni/monodroid-glue.cc @@ -10,7 +10,10 @@ #include #include +#if defined (APPLE_OS_X) #include +#endif // def APPLE_OX_X + #include #include #include @@ -82,6 +85,8 @@ #include "cpp-util.hh" +#include "java-interop-dlfcn.h" + using namespace xamarin::android; using namespace xamarin::android::internal; @@ -151,12 +156,11 @@ MonodroidRuntime::setup_bundled_app (const char *dso_name) if (!application_config.is_a_bundled_app) return; - static int dlopen_flags = RTLD_LAZY; void *libapp = nullptr; if (androidSystem.is_embedded_dso_mode_enabled ()) { log_info (LOG_DEFAULT, "bundle app: embedded DSO mode"); - libapp = androidSystem.load_dso_from_any_directories (dso_name, dlopen_flags); + libapp = androidSystem.load_dso_from_any_directories (dso_name, 0); } else { bool needs_free = false; log_info (LOG_DEFAULT, "bundle app: normal mode"); @@ -165,7 +169,7 @@ MonodroidRuntime::setup_bundled_app (const char *dso_name) if (bundle_path == nullptr) return; log_info (LOG_BUNDLE, "Attempting to load bundled app from %s", bundle_path); - libapp = androidSystem.load_dso (bundle_path, dlopen_flags, true); + libapp = androidSystem.load_dso (bundle_path, 0, true); if (needs_free) delete[] bundle_path; } @@ -181,11 +185,11 @@ MonodroidRuntime::setup_bundled_app (const char *dso_name) } } - mono_mkbundle_initialize_mono_api = reinterpret_cast (dlsym (libapp, "initialize_mono_api")); + mono_mkbundle_initialize_mono_api = reinterpret_cast (java_interop_get_symbol_address (libapp, "initialize_mono_api", nullptr)); if (!mono_mkbundle_initialize_mono_api) log_error (LOG_BUNDLE, "Missing initialize_mono_api in the application"); - mono_mkbundle_init = reinterpret_cast (dlsym (libapp, "mono_mkbundle_init")); + mono_mkbundle_init = reinterpret_cast (java_interop_get_symbol_address (libapp, "mono_mkbundle_init", nullptr)); if (!mono_mkbundle_init) log_error (LOG_BUNDLE, "Missing mono_mkbundle_init in the application"); log_info (LOG_BUNDLE, "Bundled app loaded: %s", dso_name); @@ -1067,6 +1071,7 @@ setup_gc_logging (void) force_inline int MonodroidRuntime::convert_dl_flags (int flags) { +#if 0 int lflags = flags & static_cast (MONO_DL_LOCAL) ? 0: RTLD_GLOBAL; if (flags & static_cast (MONO_DL_LAZY)) @@ -1074,6 +1079,9 @@ MonodroidRuntime::convert_dl_flags (int flags) else lflags |= RTLD_NOW; return lflags; +#else + return 0; +#endif } force_inline void @@ -1099,7 +1107,7 @@ MonodroidRuntime::init_internal_api_dso (void *handle) std::lock_guard lock (api_init_lock); if (api_dso_handle != nullptr) { - auto api_shutdown = reinterpret_cast (dlsym (api_dso_handle, MonoAndroidInternalCalls::SHUTDOWN_FUNCTION_NAME)); + auto api_shutdown = reinterpret_cast (java_interop_get_symbol_address (api_dso_handle, MonoAndroidInternalCalls::SHUTDOWN_FUNCTION_NAME, nullptr)); if (api_shutdown == nullptr) { // We COULD ignore this situation, but if the function is missing it means we messed something up and thus // it *is* a fatal error. @@ -1111,7 +1119,7 @@ MonodroidRuntime::init_internal_api_dso (void *handle) api_dso_handle = handle; auto api = new MonoAndroidInternalCalls_Impl (); - auto api_init = reinterpret_cast(dlsym (handle, MonoAndroidInternalCalls::INIT_FUNCTION_NAME)); + auto api_init = reinterpret_cast(java_interop_get_symbol_address (handle, MonoAndroidInternalCalls::INIT_FUNCTION_NAME, nullptr)); if (api_init == nullptr) { log_fatal (LOG_DEFAULT, "Unable to initialize Internal API library, init function '%s' not found in the module", MonoAndroidInternalCalls::INIT_FUNCTION_NAME); exit (FATAL_EXIT_MONO_MISSING_SYMBOLS); @@ -1190,11 +1198,15 @@ void* MonodroidRuntime::monodroid_dlsym (void *handle, const char *name, char **err, [[maybe_unused]] void *user_data) { void *s; + char *e; - s = dlsym (handle, name); + s = java_interop_get_symbol_address (handle, name, &e); if (!s && err) { - *err = utils.monodroid_strdup_printf ("Could not find symbol '%s'.", name); + *err = utils.monodroid_strdup_printf ("Could not find symbol '%s': %s", name, e); + } + if (e) { + java_interop_free (e); } return s; @@ -1370,9 +1382,9 @@ MonodroidRuntime::disable_external_signal_handlers (void) if (!androidSystem.is_mono_llvm_enabled ()) return; - void *llvm = androidSystem.load_dso ("libLLVM.so", RTLD_LAZY, TRUE); + void *llvm = androidSystem.load_dso ("libLLVM.so", 0, TRUE); if (llvm) { - bool *disable_signals = reinterpret_cast (dlsym (llvm, "_ZN4llvm23DisablePrettyStackTraceE")); + bool *disable_signals = reinterpret_cast (java_interop_get_symbol_address (llvm, "_ZN4llvm23DisablePrettyStackTraceE", nullptr)); if (disable_signals) { *disable_signals = true; log_info (LOG_DEFAULT, "Disabled LLVM signal trapping"); @@ -1605,7 +1617,7 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl if (my_location != nullptr) { simple_pointer_guard dso_path (utils.path_combine (my_location, API_DSO_NAME)); log_info (LOG_DEFAULT, "Attempting to load %s", dso_path.get ()); - api_dso_handle = dlopen (dso_path.get (), RTLD_NOW); + api_dso_handle = java_interop_load_library (dso_path.get (), 0, nullptr); #if defined (APPLE_OS_X) delete[] my_location; #else // !defined(APPLE_OS_X) @@ -1615,11 +1627,11 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl if (api_dso_handle == nullptr) { log_info (LOG_DEFAULT, "Attempting to load %s with \"bare\" dlopen", API_DSO_NAME); - api_dso_handle = dlopen (API_DSO_NAME, RTLD_NOW); + api_dso_handle = java_interop_load_library (API_DSO_NAME, 0, nullptr); } #endif // defined(WINDOWS) || defined(APPLE_OS_X) if (api_dso_handle == nullptr) - api_dso_handle = androidSystem.load_dso_from_any_directories (API_DSO_NAME, RTLD_NOW); + api_dso_handle = androidSystem.load_dso_from_any_directories (API_DSO_NAME, 0); init_internal_api_dso (api_dso_handle); mono_dl_fallback_register (monodroid_dlopen, monodroid_dlsym, nullptr, nullptr);