@@ -19,7 +19,7 @@ namespace xamarin::android {
19
19
{
20
20
jni_env = env;
21
21
systemKlass = systemClass;
22
- System_loadLibrary = env->GetMethodID (systemClass, " loadLibrary" , " (Ljava/lang/String;)V" );
22
+ System_loadLibrary = env->GetStaticMethodID (systemClass, " loadLibrary" , " (Ljava/lang/String;)V" );
23
23
if (System_loadLibrary == nullptr ) [[unlikely]] {
24
24
Helpers::abort_application (" Failed to look up the Java System.loadLibrary method." );
25
25
}
@@ -34,7 +34,7 @@ namespace xamarin::android {
34
34
return load_jni (path, true /* name_is_path */ );
35
35
}
36
36
37
- log_info (LOG_ASSEMBLY, " Trying to load shared library '{}'" , path);
37
+ log_info (LOG_ASSEMBLY, " [filesystem] Trying to load shared library '{}'" , path);
38
38
if constexpr (!SkipExistsCheck) {
39
39
if (!AndroidSystem::is_embedded_dso_mode_enabled () && !Util::file_exists (path)) {
40
40
log_info (LOG_ASSEMBLY, " Shared library '{}' not found" , path);
@@ -53,6 +53,8 @@ namespace xamarin::android {
53
53
return load_jni (name, true /* name_is_path */ );
54
54
}
55
55
56
+ log_info (LOG_ASSEMBLY, " [apk] Trying to load shared library '{}', offset in the apk == {}" , name, offset);
57
+
56
58
android_dlextinfo dli;
57
59
dli.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
58
60
dli.library_fd = fd;
@@ -66,6 +68,7 @@ namespace xamarin::android {
66
68
static auto log_and_return (void *handle, std::string_view const & full_name) -> void*
67
69
{
68
70
if (handle != nullptr ) [[likely]] {
71
+ log_debug (LOG_ASSEMBLY, " Shared library {} loaded (handle {:p})" , full_name, handle);
69
72
return handle;
70
73
}
71
74
@@ -85,6 +88,8 @@ namespace xamarin::android {
85
88
86
89
static auto load_jni (std::string_view const & name, bool name_is_path) -> void*
87
90
{
91
+ log_debug (LOG_ASSEMBLY, " Loading JNI library {} with System.loadLibrary" , name);
92
+
88
93
if (jni_env == nullptr || systemKlass == nullptr ) [[unlikely]] {
89
94
Helpers::abort_application (" DSO loader class not initialized properly." sv);
90
95
}
@@ -98,34 +103,46 @@ namespace xamarin::android {
98
103
if (!is_path) {
99
104
name = full_name;
100
105
} 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 ;
106
+ name = full_name;
107
+ size_t last_slash = name. find_last_of ( ' / ' );
108
+ if (last_slash != std::string_view::npos) [[likely]] {
109
+ last_slash++ ;
110
+ if (last_slash <= name .length ()) {
111
+ name. remove_prefix (last_slash);
112
+ }
113
+ }
109
114
}
110
115
111
- if (name.ends_with (" .so" sv) && name.length () > 3 ) {
112
- name_end -= 3 ;
116
+ constexpr std::string_view lib_prefix { " lib" };
117
+ if (name.starts_with (lib_prefix) && name.length () > 3 ) {
118
+ if (lib_prefix.length () <= name.length ()) {
119
+ name.remove_prefix (lib_prefix.length ());
120
+ }
113
121
}
114
122
115
- if (name_start >= name_end) [[unlikely]] {
116
- return name;
123
+ constexpr std::string_view lib_ext { " .so" };
124
+ if (name.ends_with (lib_ext) && name.length () > 3 ) {
125
+ if (lib_ext.length () <= name.length ()) {
126
+ name.remove_suffix (lib_ext.length ());
127
+ }
117
128
}
118
129
119
- return std::move ( name. substr (name_start, name. length () - name_end)) ;
130
+ return name;
120
131
};
121
132
122
- const std::string_view undecorated_lib_name = get_undecorated_name (name, name_is_path);
133
+ const std::string undecorated_lib_name { get_undecorated_name (name, name_is_path) };
134
+ log_debug (LOG_ASSEMBLY, " Undecorated library name: {}" , undecorated_lib_name);
123
135
124
- jstring lib_name = jni_env->NewStringUTF (undecorated_lib_name.data ());
136
+ jstring lib_name = jni_env->NewStringUTF (undecorated_lib_name.c_str ());
137
+ if (lib_name == nullptr ) [[unlikely]] {
138
+ // It's an OOM, there's nothing better we can do
139
+ Helpers::abort_application (" Java string allocation failed while loading a shared library." );
140
+ }
125
141
jni_env->CallStaticVoidMethod (systemKlass, System_loadLibrary, lib_name);
126
142
127
143
// This is unfortunate, but since `System.loadLibrary` doesn't return the class handle, we must get it this
128
144
// way :(
145
+ log_debug (LOG_ASSEMBLY, " Attempting to get library {} handle after System.loadLibrary" , name);
129
146
return log_and_return (dlopen (name.data (), RTLD_NOLOAD), name);
130
147
}
131
148
0 commit comments