@@ -1152,26 +1152,101 @@ MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err, [[m
11521152 unsigned int dl_flags = monodroidRuntime.convert_dl_flags (flags);
11531153 bool libmonodroid_fallback = false ;
11541154
1155+ // Debugging, will be removed after the designer issue is fixed
1156+ log_warn (LOG_DEFAULT, " grendel# %s (\" %s\" , %d, %p, %p)" , " MonodroidRuntime::monodroid_dlopen" , name, flags, err, user_data);
1157+
1158+ bool name_is_full_path = false ;
1159+ bool name_needs_free = false ;
11551160 /* name is nullptr when we're P/Invoking __Internal, so remap to libxa-internal-api */
1156- if (name == nullptr ) {
1161+ if (name == nullptr || strstr (name, " xa-internal-api" ) != nullptr ) {
1162+ log_warn (LOG_DEFAULT, " grendel# request for xa-internal-api" );
1163+ #if defined (WINDOWS)
1164+ const char *last_sep = strrchr (name, MONODROID_PATH_SEPARATOR_CHAR);
1165+ char *tmp_name = nullptr ;
1166+
1167+ // First try to see if it exist at the path pointed to by `name`
1168+ if (last_sep != nullptr ) {
1169+ tmp_name = utils.strdup_new (name, last_sep - name);
1170+ tmp_name = utils.string_concat (tmp_name, MONODROID_PATH_SEPARATOR, API_DSO_NAME);
1171+ log_warn (LOG_DEFAULT, " grendel# checking of %s exists" , tmp_name);
1172+ if (!utils.file_exists (tmp_name)) {
1173+ log_warn (LOG_DEFAULT, " grendel# nope, not there" );
1174+ delete[] tmp_name;
1175+ tmp_name = nullptr ;
1176+ }
1177+ }
1178+
1179+ if (tmp_name == nullptr ) {
1180+ log_warn (LOG_DEFAULT, " Looking for %s in the same directory as the XA runtime" , API_DSO_NAME);
1181+ // Next lets try our location
1182+ HMODULE hm;
1183+ const void *func = reinterpret_cast <const void *>(&Java_mono_android_Runtime_initInternal);
1184+ if (GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast <LPCSTR>(func), &hm) == 0 ) {
1185+ log_fatal (LOG_DEFAULT, " Failed to get module handle for the Xamarin.Android runtime" );
1186+ exit (1 );
1187+ }
1188+
1189+ char path[MAX_PATH];
1190+ if (GetModuleFileName (hm, path, sizeof (path)) == 0 ) {
1191+ log_fatal (LOG_DEFAULT, " Failed to obtain path of the Xamarin.Android runtime" );
1192+ exit (1 );
1193+ }
1194+
1195+ log_warn (LOG_DEFAULT, " XA runtime location: %s" , path);
1196+ last_sep = strrchr (path, MONODROID_PATH_SEPARATOR_CHAR);
1197+ if (last_sep != nullptr ) {
1198+ tmp_name = utils.strdup_new (path, last_sep - path);
1199+ tmp_name = utils.string_concat (tmp_name, MONODROID_PATH_SEPARATOR, API_DSO_NAME);
1200+ log_warn (LOG_DEFAULT, " grendel# checking of %s exists" , tmp_name);
1201+ if (!utils.file_exists (tmp_name)) {
1202+ log_warn (LOG_DEFAULT, " grendel# nope, not there" );
1203+ delete[] tmp_name;
1204+ tmp_name = nullptr ;
1205+ }
1206+ }
1207+
1208+ if (tmp_name == nullptr ) {
1209+ log_warn (LOG_DEFAULT, " Failed to locate %s, using file name without the path" , API_DSO_NAME);
1210+ name = API_DSO_NAME;
1211+ } else {
1212+ name = tmp_name;
1213+ name_is_full_path = true ;
1214+ name_needs_free = true ;
1215+ }
1216+ }
1217+ #else
11571218 name = API_DSO_NAME;
1219+ #endif
1220+ log_warn (LOG_DEFAULT, " grendel# will use library name: %s" , name);
11581221 libmonodroid_fallback = true ;
11591222 }
11601223
1161- void *h = androidSystem.load_dso_from_any_directories (name, dl_flags);
1224+ void *h = nullptr ;
1225+ if (!name_is_full_path)
1226+ h = androidSystem.load_dso_from_any_directories (name, dl_flags);
11621227
11631228 if (h != nullptr ) {
1164- return monodroid_dlopen_log_and_return (h, err, name, false , libmonodroid_fallback);
1229+ return monodroid_dlopen_log_and_return (h, err, name, name_needs_free , libmonodroid_fallback);
11651230 }
11661231
11671232 if (libmonodroid_fallback) {
1168- char *full_name = utils.path_combine (AndroidSystem::SYSTEM_LIB_PATH, API_DSO_NAME);
1233+ const char *full_name;
1234+ if (name_is_full_path) {
1235+ full_name = name;
1236+ } else {
1237+ if (name_needs_free) {
1238+ delete[] name;
1239+ }
1240+ full_name = utils.path_combine (AndroidSystem::SYSTEM_LIB_PATH, API_DSO_NAME);
1241+ name_needs_free = true ;
1242+ }
1243+ log_warn (LOG_DEFAULT, " grendel# loading from %s" , full_name);
11691244 h = androidSystem.load_dso (full_name, dl_flags, false );
1170- return monodroid_dlopen_log_and_return (h, err, full_name, true , true );
1245+ return monodroid_dlopen_log_and_return (h, err, full_name, name_needs_free , true );
11711246 }
11721247
11731248 if (!utils.ends_with (name, " .dll.so" ) && !utils.ends_with (name, " .exe.so" )) {
1174- return monodroid_dlopen_log_and_return (h, err, name, false );
1249+ return monodroid_dlopen_log_and_return (h, err, name, name_needs_free );
11751250 }
11761251
11771252 char *basename_part = const_cast <char *> (strrchr (name, ' /' ));
@@ -1186,6 +1261,10 @@ MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err, [[m
11861261 if (h != nullptr && XA_UNLIKELY (utils.should_log (LOG_ASSEMBLY)))
11871262 log_info_nocheck (LOG_ASSEMBLY, " Loaded AOT image '%s'" , static_cast <const char *>(basename));
11881263
1264+ if (name_needs_free) {
1265+ delete[] name;
1266+ }
1267+
11891268 return h;
11901269}
11911270
0 commit comments