*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
* Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
*
* This file is used by the interpreter and the JIT engine to locate
* assemblies. Used to load AssemblyRef and later to resolve various
}
case MONO_EXCEPTION_MISSING_FIELD: {
- char *cnspace = g_strdup ((error->klass && *error->klass->name_space) ? error->klass->name_space : "");
- char *cname = g_strdup (error->klass ? error->klass->name : "");
+ char *class_name;
char *cmembername = g_strdup (error->member_name);
- char *class_name;
+ if (error->klass)
+ class_name = mono_type_get_full_name (error->klass);
+ else
+ class_name = g_strdup ("");
mono_loader_clear_error ();
- class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
ex = mono_get_exception_missing_field (class_name, cmembername);
g_free (class_name);
- g_free (cname);
g_free (cmembername);
- g_free (cnspace);
break;
}
return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
}
-/*
+/**
* mono_dllmap_insert:
+ * @assembly: if NULL, this is a global mapping, otherwise the remapping of the dynamic library will only apply to the specified assembly
+ * @dll: The name of the external library, as it would be found in the DllImport declaration. If prefixed with 'i:' the matching of the library name is done without case sensitivity
+ * @func: if not null, the mapping will only applied to the named function (the value of EntryPoint)
+ * @tdll: The name of the library to map the specified @dll if it matches.
+ * @tfunc: if func is not NULL, the name of the function that replaces the invocation
*
* LOCKING: Acquires the loader lock.
*
+ * This function is used to programatically add DllImport remapping in either
+ * a specific assembly, or as a global remapping. This is done by remapping
+ * references in a DllImport attribute from the @dll library name into the @tdll
+ * name. If the @dll name contains the prefix "i:", the comparison of the
+ * library name is done without case sensitivity.
+ *
+ * If you pass @func, this is the name of the EntryPoint in a DllImport if specified
+ * or the name of the function as determined by DllImport. If you pass @func, you
+ * must also pass @tfunc which is the name of the target function to invoke on a match.
+ *
+ * Example:
+ * mono_dllmap_insert (NULL, "i:libdemo.dll", NULL, relocated_demo_path, NULL);
+ *
+ * The above will remap DllImport statments for "libdemo.dll" and "LIBDEMO.DLL" to
+ * the contents of relocated_demo_path for all assemblies in the Mono process.
+ *
* NOTE: This can be called before the runtime is initialized, for example from
* mono_config_parse ().
*/
return res;
}
+static MonoDl *internal_module;
+
gpointer
mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
{
const char *orig_scope;
const char *new_scope;
char *error_msg;
- char *full_name, *file_name;
+ char *full_name, *file_name, *found_name = NULL;
int i;
MonoDl *module = NULL;
+ gboolean cached = FALSE;
g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
+ if (exc_class) {
+ *exc_class = NULL;
+ *exc_arg = NULL;
+ }
+
if (piinfo->addr)
return piinfo->addr;
mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
- mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport attempting to load: '%s'.", new_scope);
-
- if (exc_class) {
- *exc_class = NULL;
- *exc_arg = NULL;
+ if (!module) {
+ mono_loader_lock ();
+ if (!image->pinvoke_scopes) {
+ image->pinvoke_scopes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+ image->pinvoke_scope_filenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+ }
+ module = g_hash_table_lookup (image->pinvoke_scopes, new_scope);
+ found_name = g_hash_table_lookup (image->pinvoke_scope_filenames, new_scope);
+ mono_loader_unlock ();
+ if (module)
+ cached = TRUE;
+ if (found_name)
+ found_name = g_strdup (found_name);
}
- /* we allow a special name to dlopen from the running process namespace */
- if (strcmp (new_scope, "__Internal") == 0)
- module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
+ if (!module) {
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+ "DllImport attempting to load: '%s'.", new_scope);
+
+ /* we allow a special name to dlopen from the running process namespace */
+ if (strcmp (new_scope, "__Internal") == 0){
+ if (internal_module == NULL)
+ internal_module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
+ module = internal_module;
+ }
+ }
/*
* Try loading the module using a variety of names
#endif
}
+ if (!module && g_path_is_absolute (file_name)) {
+ module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
+ if (!module) {
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+ "DllImport error loading library '%s': '%s'.",
+ file_name, error_msg);
+ g_free (error_msg);
+ } else {
+ found_name = g_strdup (file_name);
+ }
+ }
+
if (!module) {
void *iter = NULL;
char *mdirname = g_path_get_dirname (image->name);
while ((full_name = mono_dl_build_path (mdirname, file_name, &iter))) {
- mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport loading library: '%s'.", full_name);
module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
if (!module) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport error loading library '%s'.",
- error_msg);
+ "DllImport error loading library '%s': '%s'.",
+ full_name, error_msg);
g_free (error_msg);
+ } else {
+ found_name = g_strdup (full_name);
}
g_free (full_name);
if (module)
if (!module) {
void *iter = NULL;
while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
- mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport loading location: '%s'.", full_name);
module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
if (!module) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport error loading library: '%s'.",
- error_msg);
+ "DllImport error loading library '%s': '%s'.",
+ full_name, error_msg);
g_free (error_msg);
+ } else {
+ found_name = g_strdup (full_name);
}
g_free (full_name);
if (module)
}
if (!module) {
- mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport loading: '%s'.", file_name);
module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
if (!module) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
- "DllImport error loading library '%s'.",
- error_msg);
+ "DllImport error loading library '%s': '%s'.",
+ file_name, error_msg);
+ } else {
+ found_name = g_strdup (file_name);
}
}
return NULL;
}
+ if (!cached) {
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+ "DllImport loaded library '%s'.", found_name);
+ mono_loader_lock ();
+ if (!g_hash_table_lookup (image->pinvoke_scopes, new_scope)) {
+ g_hash_table_insert (image->pinvoke_scopes, g_strdup (new_scope), module);
+ g_hash_table_insert (image->pinvoke_scope_filenames, g_strdup (new_scope), g_strdup (found_name));
+ }
+ mono_loader_unlock ();
+ }
+
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+ "DllImport searching in: '%s' ('%s').", new_scope, found_name);
+ g_free (found_name);
+
#ifdef TARGET_WIN32
if (import && import [0] == '#' && isdigit (import [1])) {
char *end;