Add field to Exception to hold the unmanaged backtrace.
[mono.git] / mono / metadata / loader.c
index 3f0fd5b7b9f92c17a15509597305fd78a9058b4e..ab0c3223bcdc3e1f8c16878015b07ee116b1de6f 100644 (file)
@@ -8,6 +8,7 @@
  *
  * 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
@@ -329,19 +330,18 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
        }
                
        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;
         }
        
@@ -1180,11 +1180,32 @@ mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, cons
        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 ().
  */
@@ -1264,6 +1285,8 @@ cached_module_load (const char *name, int flags, char **err)
        return res;
 }
 
+static MonoDl *internal_module;
+
 gpointer
 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
 {
@@ -1278,12 +1301,18 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
        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;
 
@@ -1314,17 +1343,32 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
 
        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
@@ -1366,18 +1410,30 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
 #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)
@@ -1389,14 +1445,14 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                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)
@@ -1405,13 +1461,13 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                }
 
                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);
                        }
                }
 
@@ -1434,6 +1490,21 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                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;