Add inline api docs for mono_dll_insert
[mono.git] / mono / metadata / loader.c
index 9e06e131315d27f7225e85f70e98f59ba09317bd..d69095a6665d2817443b25358ac15a352c1d75eb 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
@@ -43,6 +44,7 @@
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-tls.h>
 
 MonoDefaults mono_defaults;
 
@@ -65,13 +67,13 @@ static guint32 signatures_size;
 /*
  * This TLS variable contains the last type load error encountered by the loader.
  */
-guint32 loader_error_thread_id;
+MonoNativeTlsKey loader_error_thread_id;
 
 /*
  * This TLS variable holds how many times the current thread has acquired the loader 
  * lock.
  */
-guint32 loader_lock_nest_id;
+MonoNativeTlsKey loader_lock_nest_id;
 
 static void dllmap_cleanup (void);
 
@@ -84,8 +86,8 @@ mono_loader_init ()
                InitializeCriticalSection (&loader_mutex);
                loader_lock_inited = TRUE;
 
-               loader_error_thread_id = TlsAlloc ();
-               loader_lock_nest_id = TlsAlloc ();
+               mono_native_tls_alloc (&loader_error_thread_id, NULL);
+               mono_native_tls_alloc (&loader_lock_nest_id, NULL);
 
                mono_counters_register ("Inflated signatures size",
                                                                MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
@@ -105,8 +107,8 @@ mono_loader_cleanup (void)
 {
        dllmap_cleanup ();
 
-       TlsFree (loader_error_thread_id);
-       TlsFree (loader_lock_nest_id);
+       mono_native_tls_free (loader_error_thread_id);
+       mono_native_tls_free (loader_lock_nest_id);
 
        DeleteCriticalSection (&loader_mutex);
        loader_lock_inited = FALSE;     
@@ -127,7 +129,7 @@ mono_loader_cleanup (void)
 static void
 set_loader_error (MonoLoaderError *error)
 {
-       TlsSetValue (loader_error_thread_id, error);
+       mono_native_tls_set_value (loader_error_thread_id, error);
 }
 
 /**
@@ -265,7 +267,7 @@ mono_loader_set_error_bad_image (char *msg)
 MonoLoaderError*
 mono_loader_get_last_error (void)
 {
-       return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
+       return (MonoLoaderError*)mono_native_tls_get_value (loader_error_thread_id);
 }
 
 /**
@@ -276,15 +278,15 @@ mono_loader_get_last_error (void)
 void
 mono_loader_clear_error (void)
 {
-       MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
+       MonoLoaderError *ex = (MonoLoaderError*)mono_native_tls_get_value (loader_error_thread_id);
 
        if (ex) {
                g_free (ex->class_name);
                g_free (ex->assembly_name);
                g_free (ex->msg);
                g_free (ex);
-       
-               TlsSetValue (loader_error_thread_id, NULL);
+
+               mono_native_tls_set_value (loader_error_thread_id, NULL);
        }
 }
 
@@ -328,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;
         }
        
@@ -1179,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 ().
  */
@@ -1283,6 +1305,11 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
 
        g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
 
+       if (exc_class) {
+               *exc_class = NULL;
+               *exc_arg = NULL;
+       }
+
        if (piinfo->addr)
                return piinfo->addr;
 
@@ -1316,11 +1343,6 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
        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;
-       }
-
        /* 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);
@@ -1730,37 +1752,17 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
        return result;
 }
 
-/**
- * mono_get_method_constrained:
- *
- * This is used when JITing the `constrained.' opcode.
- *
- * This returns two values: the contrained method, which has been inflated
- * as the function return value;   And the original CIL-stream method as
- * declared in cil_method.  The later is used for verification.
- */
-MonoMethod *
-mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
-                            MonoGenericContext *context, MonoMethod **cil_method)
+static MonoMethod *
+get_method_constrained (MonoImage *image, MonoMethod *method, MonoClass *constrained_class, MonoGenericContext *context)
 {
-       MonoMethod *method, *result;
+       MonoMethod *result;
        MonoClass *ic = NULL;
        MonoGenericContext *method_context = NULL;
        MonoMethodSignature *sig, *original_sig;
 
-       mono_loader_lock ();
-
-       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
-       if (!*cil_method) {
-               mono_loader_unlock ();
-               return NULL;
-       }
-
        mono_class_init (constrained_class);
-       method = *cil_method;
        original_sig = sig = mono_method_signature (method);
        if (sig == NULL) {
-               mono_loader_unlock ();
                return NULL;
        }
 
@@ -1783,7 +1785,6 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
                        /*Fixme, property propagate this error*/
                        sig = inflate_generic_signature_checked (method->klass->image, sig, &ctx, &error);
                        if (!mono_error_ok (&error)) {
-                               mono_loader_unlock ();
                                mono_error_cleanup (&error);
                                return NULL;
                        }
@@ -1798,15 +1799,59 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
                mono_metadata_free_inflated_signature (sig);
 
        if (!result) {
-               g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
-                          method->klass->name, method->name, image->name, token);
-               mono_loader_unlock ();
+               char *m = mono_method_full_name (method, 1);
+               g_warning ("Missing method %s.%s.%s in assembly %s method %s", method->klass->name_space,
+                          method->klass->name, method->name, image->name, m);
+               g_free (m);
                return NULL;
        }
 
        if (method_context)
                result = mono_class_inflate_generic_method (result, method_context);
 
+       return result;
+}
+
+MonoMethod *
+mono_get_method_constrained_with_method (MonoImage *image, MonoMethod *method, MonoClass *constrained_class,
+                            MonoGenericContext *context)
+{
+       MonoMethod *result;
+
+       g_assert (method);
+
+       mono_loader_lock ();
+
+       result = get_method_constrained (image, method, constrained_class, context);
+
+       mono_loader_unlock ();
+       return result;  
+}
+/**
+ * mono_get_method_constrained:
+ *
+ * This is used when JITing the `constrained.' opcode.
+ *
+ * This returns two values: the contrained method, which has been inflated
+ * as the function return value;   And the original CIL-stream method as
+ * declared in cil_method.  The later is used for verification.
+ */
+MonoMethod *
+mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
+                            MonoGenericContext *context, MonoMethod **cil_method)
+{
+       MonoMethod *result;
+
+       mono_loader_lock ();
+
+       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
+       if (!*cil_method) {
+               mono_loader_unlock ();
+               return NULL;
+       }
+
+       result = get_method_constrained (image, *cil_method, constrained_class, context);
+
        mono_loader_unlock ();
        return result;
 }
@@ -1895,6 +1940,21 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
                return;
        }
 
+       if (method->wrapper_type) {
+               char **pnames = NULL;
+
+               mono_image_lock (klass->image);
+               if (klass->image->wrapper_param_names)
+                       pnames = g_hash_table_lookup (klass->image->wrapper_param_names, method);
+               mono_image_unlock (klass->image);
+
+               if (pnames) {
+                       for (i = 0; i < signature->param_count; ++i)
+                               names [i] = pnames [i];
+               }
+               return;
+       }
+
        methodt = &klass->image->tables [MONO_TABLE_METHOD];
        paramt = &klass->image->tables [MONO_TABLE_PARAM];
        idx = mono_method_get_index (method);
@@ -2068,29 +2128,42 @@ mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
        return data [id];
 }
 
-static void
-default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
-       g_error ("stack walk not installed");
-}
-
-static MonoStackWalkImpl stack_walk = default_stack_walk;
+typedef struct {
+       MonoStackWalk func;
+       gpointer user_data;
+} StackWalkUserData;
 
-void
-mono_stack_walk (MonoStackWalk func, gpointer user_data)
+static gboolean
+stack_walk_adapter (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
-       stack_walk (func, TRUE, user_data);
+       StackWalkUserData *d = data;
+
+       switch (frame->type) {
+       case FRAME_TYPE_DEBUGGER_INVOKE:
+       case FRAME_TYPE_MANAGED_TO_NATIVE:
+               return FALSE;
+       case FRAME_TYPE_MANAGED:
+               g_assert (frame->ji);
+               return d->func (frame->ji->method, frame->native_offset, frame->il_offset, frame->managed, d->user_data);
+               break;
+       default:
+               g_assert_not_reached ();
+               return FALSE;
+       }
 }
 
 void
-mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
+mono_stack_walk (MonoStackWalk func, gpointer user_data)
 {
-       stack_walk (func, FALSE, user_data);
+       StackWalkUserData ud = { func, user_data };
+       mono_get_eh_callbacks ()->mono_walk_stack_with_ctx (stack_walk_adapter, NULL, MONO_UNWIND_LOOKUP_ALL, &ud);
 }
 
 void
-mono_install_stack_walk (MonoStackWalkImpl func)
+mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
 {
-       stack_walk = func;
+       StackWalkUserData ud = { func, user_data };
+       mono_get_eh_callbacks ()->mono_walk_stack_with_ctx (stack_walk_adapter, NULL, MONO_UNWIND_DEFAULT, &ud);
 }
 
 static gboolean
@@ -2107,7 +2180,7 @@ MonoMethod*
 mono_method_get_last_managed (void)
 {
        MonoMethod *m = NULL;
-       stack_walk (last_managed, FALSE, &m);
+       mono_stack_walk_no_il (last_managed, &m);
        return m;
 }
 
@@ -2123,7 +2196,7 @@ mono_loader_lock (void)
 {
        mono_locks_acquire (&loader_mutex, LoaderLock);
        if (G_UNLIKELY (loader_lock_track_ownership)) {
-               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) + 1));
+               mono_native_tls_set_value (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) + 1));
        }
 }
 
@@ -2132,7 +2205,7 @@ mono_loader_unlock (void)
 {
        mono_locks_release (&loader_mutex, LoaderLock);
        if (G_UNLIKELY (loader_lock_track_ownership)) {
-               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) - 1));
+               mono_native_tls_set_value (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) - 1));
        }
 }
 
@@ -2160,7 +2233,7 @@ mono_loader_lock_is_owned_by_self (void)
 {
        g_assert (loader_lock_track_ownership);
 
-       return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) > 0;
+       return GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) > 0;
 }
 
 /*