|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <jni.h> |
| 4 | +#include <dlfcn.h> |
| 5 | +#include <android/dlext.h> |
| 6 | + |
| 7 | +#include <string_view> |
| 8 | + |
| 9 | +#include <runtime-base/android-system.hh> |
| 10 | +#include <runtime-base/util.hh> |
| 11 | +#include <shared/helpers.hh> |
| 12 | + |
| 13 | +namespace xamarin::android { |
| 14 | + class DsoLoader |
| 15 | + { |
| 16 | + public: |
| 17 | + [[gnu::flatten]] |
| 18 | + static void init (JNIEnv *env, jclass systemClass) |
| 19 | + { |
| 20 | + jni_env = env; |
| 21 | + systemKlass = systemClass; |
| 22 | + System_loadLibrary = env->GetMethodID (systemClass, "loadLibrary", "(Ljava/lang/String;)V"); |
| 23 | + if (System_loadLibrary == nullptr) [[unlikely]] { |
| 24 | + Helpers::abort_application ("Failed to look up the Java System.loadLibrary method."); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // Overload used to load libraries from the file system. |
| 29 | + template<bool SkipExistsCheck = false> |
| 30 | + [[gnu::always_inline, gnu::flatten]] |
| 31 | + static auto load (std::string_view const& path, int flags, bool is_jni) -> void* |
| 32 | + { |
| 33 | + if (is_jni) { |
| 34 | + return load_jni (path, true /* name_is_path */); |
| 35 | + } |
| 36 | + |
| 37 | + log_info (LOG_ASSEMBLY, "Trying to load shared library '{}'", path); |
| 38 | + if constexpr (!SkipExistsCheck) { |
| 39 | + if (!AndroidSystem::is_embedded_dso_mode_enabled () && !Util::file_exists (path)) { |
| 40 | + log_info (LOG_ASSEMBLY, "Shared library '{}' not found", path); |
| 41 | + return nullptr; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return log_and_return (dlopen (path.data (), flags), path); |
| 46 | + } |
| 47 | + |
| 48 | + // Overload used to load libraries from the APK. |
| 49 | + [[gnu::always_inline, gnu::flatten]] |
| 50 | + static auto load (int fd, off64_t offset, std::string_view const& name, int flags, bool is_jni) -> void* |
| 51 | + { |
| 52 | + if (is_jni) { |
| 53 | + return load_jni (name, true /* name_is_path */); |
| 54 | + } |
| 55 | + |
| 56 | + android_dlextinfo dli; |
| 57 | + dli.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
| 58 | + dli.library_fd = fd; |
| 59 | + dli.library_fd_offset = offset; |
| 60 | + |
| 61 | + return log_and_return (android_dlopen_ext (name.data (), flags, &dli), name); |
| 62 | + } |
| 63 | + |
| 64 | + private: |
| 65 | + [[gnu::always_inline]] |
| 66 | + static auto log_and_return (void *handle, std::string_view const& full_name) -> void* |
| 67 | + { |
| 68 | + if (handle != nullptr) [[likely]] { |
| 69 | + return handle; |
| 70 | + } |
| 71 | + |
| 72 | + const char *load_error = dlerror (); |
| 73 | + if (load_error == nullptr) { |
| 74 | + load_error = "Unknown error"; |
| 75 | + } |
| 76 | + log_error ( |
| 77 | + LOG_ASSEMBLY, |
| 78 | + "Could not load library '{}'. {}"sv, |
| 79 | + full_name, |
| 80 | + load_error |
| 81 | + ); |
| 82 | + |
| 83 | + return nullptr; |
| 84 | + } |
| 85 | + |
| 86 | + static auto load_jni (std::string_view const& name, bool name_is_path) -> void* |
| 87 | + { |
| 88 | + if (jni_env == nullptr || systemKlass == nullptr) [[unlikely]] { |
| 89 | + Helpers::abort_application ("DSO loader class not initialized properly."sv); |
| 90 | + } |
| 91 | + |
| 92 | + // System.loadLibrary call is going to be slow anyway, so we can spend some more time generating an |
| 93 | + // undecorated library name here instead of at build time. This saves us a little bit of space in |
| 94 | + // `libxamarin-app.so` and makes the build code less complicated. |
| 95 | + auto get_undecorated_name = [](std::string_view const& full_name, bool is_path) -> std::string_view { |
| 96 | + std::string_view name; |
| 97 | + |
| 98 | + if (!is_path) { |
| 99 | + name = full_name; |
| 100 | + } else { |
| 101 | + name = full_name; // TODO: cut the path |
| 102 | + } |
| 103 | + |
| 104 | + size_t name_start = 0; |
| 105 | + size_t name_end = full_name.length (); |
| 106 | + |
| 107 | + if (name.starts_with ("lib"sv) && name.length () > 3) { |
| 108 | + name_start = 3; |
| 109 | + } |
| 110 | + |
| 111 | + if (name.ends_with (".so"sv) && name.length () > 3) { |
| 112 | + name_end -= 3; |
| 113 | + } |
| 114 | + |
| 115 | + if (name_start >= name_end) [[unlikely]] { |
| 116 | + return name; |
| 117 | + } |
| 118 | + |
| 119 | + return std::move (name.substr (name_start, name.length () - name_end)); |
| 120 | + }; |
| 121 | + |
| 122 | + const std::string_view undecorated_lib_name = get_undecorated_name (name, name_is_path); |
| 123 | + |
| 124 | + jstring lib_name = jni_env->NewStringUTF (undecorated_lib_name.data ()); |
| 125 | + jni_env->CallStaticVoidMethod (systemKlass, System_loadLibrary, lib_name); |
| 126 | + |
| 127 | + // This is unfortunate, but since `System.loadLibrary` doesn't return the class handle, we must get it this |
| 128 | + // way :( |
| 129 | + return log_and_return (dlopen (name.data (), RTLD_NOLOAD), name); |
| 130 | + } |
| 131 | + |
| 132 | + private: |
| 133 | + static inline jmethodID System_loadLibrary = nullptr; |
| 134 | + static inline jclass systemKlass = nullptr; |
| 135 | + static inline JNIEnv *jni_env = nullptr; |
| 136 | + }; |
| 137 | +} |
0 commit comments