Add a new 'MONO_DEBUGGER_EVENT_TRAMPOLINE' notification for the debugger which also...
[mono.git] / mono / metadata / marshal.c
index d5369bb3dfc5735f0a0e6d20d178f36b57d8631a..ea59d1fdb71593b47c00747bea29f32ce50677ad 100644 (file)
@@ -4,11 +4,16 @@
  * Author:
  *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2002 Ximian, Inc.  http://www.ximian.com
+ * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  *
  */
 
 #include "config.h"
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
 #include "object.h"
 #include "loader.h"
 #include "cil-coff.h"
@@ -28,6 +33,7 @@
 #include "mono/metadata/string-icalls.h"
 #include "mono/metadata/attrdefs.h"
 #include "mono/metadata/gc-internal.h"
+#include "mono/metadata/cominterop.h"
 #include "mono/utils/mono-counters.h"
 #include <string.h>
 #include <errno.h>
@@ -44,13 +50,6 @@ typedef enum {
        MONO_MARSHAL_SERIALIZE          /* Value needs to be serialized into the new domain */
 } MonoXDomainMarshalType;
 
-typedef enum {
-       MONO_COM_DEFAULT,
-       MONO_COM_MS
-} MonoCOMProvider;
-
-static MonoCOMProvider com_provider = MONO_COM_DEFAULT;
-
 enum {
 #include "mono/cil/opcode.def"
        LAST = 0xff
@@ -75,18 +74,14 @@ typedef struct _MonoRemotingMethods MonoRemotingMethods;
 #define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
 #define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
 static CRITICAL_SECTION marshal_mutex;
-
-/* This mutex protects the various cominterop related caches in MonoImage */
-#define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
-#define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
-static CRITICAL_SECTION cominterop_mutex;
+static gboolean marshal_mutex_initialized;
 
 static guint32 last_error_tls_id;
 
 static guint32 load_type_info_tls_id;
 
 static void
-delegate_hash_table_add (MonoDelegate *d);
+delegate_hash_table_add (MonoDelegate *d, MonoObject **target_loc);
 
 static void
 emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object);
@@ -103,15 +98,12 @@ mono_marshal_string_to_utf16_copy (MonoString *s);
 static gpointer
 mono_string_to_lpstr (MonoString *string_obj);
 
-static MonoString * 
-mono_string_from_bstr (gpointer bstr);
-
-static void 
-mono_free_bstr (gpointer bstr);
-
 static MonoStringBuilder *
 mono_string_utf8_to_builder2 (char *text);
 
+static MonoStringBuilder *
+mono_string_utf16_to_builder2 (gunichar2 *text);
+
 static void
 mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *eltype, guint32 elnum);
 
@@ -127,9 +119,6 @@ mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params);
 static MonoObject *
 mono_delegate_end_invoke (MonoDelegate *delegate, gpointer *params);
 
-static MonoObject *
-mono_marshal_xdomain_copy_value (MonoObject *val);
-
 static void
 mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst);
 
@@ -148,11 +137,10 @@ type_from_handle (MonoType *handle);
 static void
 mono_marshal_set_last_error_windows (int error);
 
-static void
-mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean aot, gboolean check_exceptions);
-
 static void init_safe_handle (void);
 
+static void runtime_invoke_reset_abort (MonoException *ex);
+
 /* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
 static MonoMethod *sh_dangerous_add_ref;
 static MonoMethod *sh_dangerous_release;
@@ -182,7 +170,7 @@ signature_dup (MonoImage *image, MonoMethodSignature *sig)
        int sigsize;
 
        res = mono_metadata_signature_alloc (image, sig->param_count);
-       sigsize = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
+       sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
        memcpy (res, sig, sigsize);
 
        return res;
@@ -200,368 +188,18 @@ mono_signature_no_pinvoke (MonoMethod *method)
        return sig;
 }
 
-/* Begin COM Interop related stuff until seperate file */
-
-
-/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
-#ifdef  PLATFORM_WIN32
-#define STDCALL __stdcall
-#else
-#define STDCALL
-#endif
-
-/* Upon creation of a CCW, only allocate a weak handle and set the
- * reference count to 0. If the unmanaged client code decides to addref and
- * hold onto the CCW, I then allocate a strong handle. Once the reference count
- * goes back to 0, convert back to a weak handle.
- */
-typedef struct {
-       guint32 ref_count;
-       guint32 gc_handle;
-       GHashTable* vtable_hash;
-#ifdef  PLATFORM_WIN32
-       gpointer free_marshaler;
-#endif
-} MonoCCW;
-
-/* This type is the actual pointer passed to unmanaged code
- * to represent a COM interface.
- */
-typedef struct {
-       gpointer vtable;
-       MonoCCW* ccw;
-} MonoCCWInterface;
-
-#ifndef DISABLE_COM
-
-/* IUnknown */
-static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);
-
-/* IDispatch */
-static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);
-
-static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);
-
-static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
-                                                                                        gunichar2** rgszNames, guint32 cNames,
-                                                                                        guint32 lcid, gint32 *rgDispId);
-
-static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
-                                                                  gpointer riid, guint32 lcid,
-                                                                  guint16 wFlags, gpointer pDispParams,
-                                                                  gpointer pVarResult, gpointer pExcepInfo,
-                                                                  guint32 *puArgErr);
-
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method);
-
-static gpointer
-cominterop_get_ccw (MonoObject* object, MonoClass* itf);
-
-static MonoObject*
-cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);
-
-/**
- * cominterop_method_signature:
- * @method: a method
- *
- * Returns: the corresponding unmanaged method signature for a managed COM 
- * method.
- */
-static MonoMethodSignature*
-cominterop_method_signature (MonoMethod* method)
-{
-       MonoMethodSignature *res;
-       MonoImage *image = method->klass->image;
-       MonoMethodSignature *sig = mono_method_signature (method);
-       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-       int sigsize;
-       int i;
-       int param_count = sig->param_count + 1; // convert this arg into IntPtr arg
-
-       if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
-               param_count++;
-
-       res = mono_metadata_signature_alloc (image, param_count);
-       sigsize = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
-       memcpy (res, sig, sigsize);
-
-       // now move args forward one
-       for (i = sig->param_count-1; i >= 0; i--)
-               res->params[i+1] = sig->params[i];
-
-       // first arg is interface pointer
-       res->params[0] = &mono_defaults.int_class->byval_arg;
-
-       if (preserve_sig) {
-               res->ret = sig->ret;
-       }
-       else {
-               // last arg is return type
-               if (!MONO_TYPE_IS_VOID (sig->ret)) {
-                       res->params[param_count-1] = mono_metadata_type_dup (image->mempool, sig->ret);
-                       res->params[param_count-1]->byref = 1;
-                       res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
-               }
-
-               // return type is always int32 (HRESULT)
-               res->ret = &mono_defaults.int32_class->byval_arg;
-       }
-
-       // no pinvoke
-       res->pinvoke = FALSE;
-
-       // no hasthis
-       res->hasthis = 0;
-
-       // set param_count
-       res->param_count = param_count;
-
-       // STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
-#ifdef PLATFORM_WIN32
-       res->call_convention = MONO_CALL_STDCALL;
-#else
-       res->call_convention = MONO_CALL_C;
-#endif
-
-       return res;
-}
-
-/**
- * cominterop_get_function_pointer:
- * @itf: a pointer to the COM interface
- * @slot: the vtable slot of the method pointer to return
- *
- * Returns: the unmanaged vtable function pointer from the interface
- */
-static gpointer
-cominterop_get_function_pointer (gpointer itf, int slot)
-{
-       gpointer func;
-       func = *((*(gpointer**)itf)+slot);
-       return func;
-}
-
-/**
- * cominterop_object_is_com_object:
- * @obj: a pointer to the object
- *
- * Returns: a value indicating if the object is a
- * Runtime Callable Wrapper (RCW) for a COM object
- */
-static gboolean
-cominterop_object_is_rcw (MonoObject *obj)
-{
-       MonoClass *klass = NULL;
-       MonoRealProxy* real_proxy = NULL;
-       if (!obj)
-               return FALSE;
-       klass = mono_object_class (obj);
-       if (klass != mono_defaults.transparent_proxy_class)
-               return FALSE;
-
-       real_proxy = ((MonoTransparentProxy*)obj)->rp;
-       if (!real_proxy)
-               return FALSE;
-
-       klass = mono_object_class (real_proxy);
-       return (klass && klass == mono_defaults.com_interop_proxy_class);
-}
-
-static int
-cominterop_get_com_slot_begin (MonoClass* klass)
-{
-       static MonoClass *interface_type_attribute = NULL;
-       MonoCustomAttrInfo *cinfo = NULL;
-       MonoInterfaceTypeAttribute* itf_attr = NULL; 
-
-       if (!interface_type_attribute)
-               interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
-       cinfo = mono_custom_attrs_from_class (klass);
-       if (cinfo) {
-               itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr (cinfo, interface_type_attribute);
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-       }
-
-       if (itf_attr && itf_attr->intType == 1)
-               return 3; /* 3 methods in IUnknown*/
-       else
-               return 7; /* 7 methods in IDispatch*/
-}
-
-/**
- * cominterop_get_method_interface:
- * @method: method being called
- *
- * Returns: the MonoClass* representing the interface on which
- * the method is defined.
- */
-static MonoClass*
-cominterop_get_method_interface (MonoMethod* method)
-{
-       MonoClass *ic = method->klass;
-
-       /* if method is on a class, we need to look up interface method exists on */
-       if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
-               GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass);
-               if (ifaces) {
-                       int i;
-                       for (i = 0; i < ifaces->len; ++i) {
-                               int offset;
-                               ic = g_ptr_array_index (ifaces, i);
-                               offset = mono_class_interface_offset (method->klass, ic);
-                               if (method->slot >= offset && method->slot < offset + ic->method.count)
-                                       break;
-                               ic = NULL;
-                       }
-                       g_ptr_array_free (ifaces, TRUE);
-               }
-       }
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       return ic;
-}
-
-/**
- * cominterop_get_com_slot_for_method:
- * @method: a method
- *
- * Returns: the method's slot in the COM interface vtable
- */
-static int
-cominterop_get_com_slot_for_method (MonoMethod* method)
-{
-       guint32 slot = method->slot;
-       MonoClass *ic = method->klass;
-
-       /* if method is on a class, we need to look up interface method exists on */
-       if (!MONO_CLASS_IS_INTERFACE(ic)) {
-               int offset = 0;
-               ic = cominterop_get_method_interface (method);
-               offset = mono_class_interface_offset (method->klass, ic);
-               g_assert(offset >= 0);
-               slot -= offset;
-       }
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       return slot + cominterop_get_com_slot_begin (ic);
-}
-
-
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid);
-
-static gboolean
-cominterop_class_guid (MonoClass* klass, guint8* guid)
-{
-       static MonoClass *GuidAttribute = NULL;
-       MonoCustomAttrInfo *cinfo;
-
-       /* Handle the GuidAttribute */
-       if (!GuidAttribute)
-               GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");
-
-       cinfo = mono_custom_attrs_from_class (klass);   
-       if (cinfo) {
-               MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr (cinfo, GuidAttribute);
-
-               if (!attr)
-                       return FALSE;
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-
-               cominterop_mono_string_to_guid (attr->guid, guid);
-               return TRUE;
-       }
-       return FALSE;
-}
-
-/**
- * cominterop_get_interface:
- * @obj: managed wrapper object containing COM object
- * @ic: interface type to retrieve for COM object
- *
- * Returns: the COM interface requested
- */
-static gpointer
-cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
-{
-       gpointer itf = NULL;
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       mono_cominterop_lock ();
-       if (obj->itf_hash)
-               itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
-       mono_cominterop_unlock ();
-
-       if (!itf) {
-               guint8 iid [16];
-               int found = cominterop_class_guid (ic, iid);
-               int hr;
-               g_assert(found);
-               hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
-               if (hr < 0 && throw_exception) {
-                       static MonoMethod* throw_exception_for_hr = NULL;
-                       MonoException* ex;
-                       void* params[1] = {&hr};
-                       if (!throw_exception_for_hr)
-                               throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
-                       ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
-                       mono_raise_exception (ex);
-               }
-
-               if (hr >= 0 && itf) {
-                       mono_cominterop_lock ();
-                       if (!obj->itf_hash)
-                               obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-                       g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
-                       mono_cominterop_unlock ();
-               }
-
-       }
-       if (throw_exception)
-               g_assert (itf);
-
-       return itf;
-}
-
-static int
-cominterop_get_hresult_for_exception (MonoException* exc)
-{
-       int hr = 0;
-       return hr;
-}
-
-#endif /* DISABLE_COM */
-
 void
 mono_marshal_init (void)
 {
        static gboolean module_initialized = FALSE;
 
        if (!module_initialized) {
-               char* com_provider_env = NULL;
                module_initialized = TRUE;
                InitializeCriticalSection (&marshal_mutex);
-               InitializeCriticalSection (&cominterop_mutex);
+               marshal_mutex_initialized = TRUE;
                last_error_tls_id = TlsAlloc ();
                load_type_info_tls_id = TlsAlloc ();
 
-               com_provider_env = getenv ("MONO_COM");
-               if (com_provider_env && !strcmp(com_provider_env, "MS"))
-                       com_provider = MONO_COM_MS;
-
                register_icall (ves_icall_System_Threading_Thread_ResetAbort, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE);
                register_icall (mono_marshal_string_to_utf16, "mono_marshal_string_to_utf16", "ptr obj", FALSE);
                register_icall (mono_marshal_string_to_utf16_copy, "mono_marshal_string_to_utf16_copy", "ptr obj", FALSE);
@@ -570,14 +208,12 @@ mono_marshal_init (void)
                register_icall (mono_string_new_wrapper, "mono_string_new_wrapper", "obj ptr", FALSE);
                register_icall (mono_string_to_utf8, "mono_string_to_utf8", "ptr obj", FALSE);
                register_icall (mono_string_to_lpstr, "mono_string_to_lpstr", "ptr obj", FALSE);
-               register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
-               register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
-               register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
                register_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", "ptr object", FALSE);
                register_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", "ptr object", FALSE);
                register_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", "ptr object", FALSE);
                register_icall (mono_array_to_savearray, "mono_array_to_savearray", "ptr object", FALSE);
                register_icall (mono_array_to_lparray, "mono_array_to_lparray", "ptr object", FALSE);
+               register_icall (mono_free_lparray, "mono_free_lparray", "void object ptr", FALSE);
                register_icall (mono_byvalarray_to_array, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE);
                register_icall (mono_array_to_byvalarray, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE);
                register_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", "ptr object", FALSE);
@@ -591,6 +227,7 @@ mono_marshal_init (void)
                register_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", "void ptr ptr", FALSE);
                register_icall (mono_string_utf8_to_builder2, "mono_string_utf8_to_builder2", "object ptr", FALSE);
                register_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", "void ptr ptr", FALSE);
+               register_icall (mono_string_utf16_to_builder2, "mono_string_utf16_to_builder2", "object ptr", FALSE);
                register_icall (mono_marshal_free_array, "mono_marshal_free_array", "void ptr int32", FALSE);
                register_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", "void ptr ptr int32", FALSE);
                register_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", "void ptr ptr int32", FALSE);
@@ -609,26 +246,22 @@ mono_marshal_init (void)
                register_icall (mono_context_set, "mono_context_set", "void object", FALSE);
                register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
                register_icall (type_from_handle, "type_from_handle", "object ptr", FALSE);
-               register_icall (mono_gc_wbarrier_generic_store, "wb_generic", "void ptr object", FALSE);
-#ifndef DISABLE_COM
-               register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
-               register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
-               register_icall (cominterop_object_is_rcw, "cominterop_object_is_rcw", "int32 object", FALSE);
-               register_icall (cominterop_get_ccw, "cominterop_get_ccw", "ptr object ptr", FALSE);
-               register_icall (cominterop_get_ccw_object, "cominterop_get_ccw_object", "object ptr int32", FALSE);
-               register_icall (cominterop_get_hresult_for_exception, "cominterop_get_hresult_for_exception", "int32 object", FALSE);
-               register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);
-#endif /* DISABLE_COM */
+               register_icall (mono_gc_wbarrier_generic_nostore, "wb_generic", "void ptr", FALSE);
+               register_icall (runtime_invoke_reset_abort, "runtime_invoke_reset_abort", "void object", FALSE);
+
+               mono_cominterop_init ();
        }
 }
 
 void
 mono_marshal_cleanup (void)
 {
+       mono_cominterop_cleanup ();
+
        TlsFree (load_type_info_tls_id);
        TlsFree (last_error_tls_id);
        DeleteCriticalSection (&marshal_mutex);
-       DeleteCriticalSection (&cominterop_mutex);
+       marshal_mutex_initialized = FALSE;
 }
 
 MonoClass *byte_array_class;
@@ -677,6 +310,7 @@ mono_delegate_to_ftnptr (MonoDelegate *delegate)
 {
        MonoMethod *method, *wrapper;
        MonoClass *klass;
+       MonoObject **target_loc;
 
        if (!delegate)
                return NULL;
@@ -689,12 +323,32 @@ mono_delegate_to_ftnptr (MonoDelegate *delegate)
 
        method = delegate->method;
 
-       wrapper = mono_marshal_get_managed_wrapper (method, klass, delegate->target);
+       if (mono_method_signature (method)->pinvoke) {
+               const char *exc_class, *exc_arg;
+               gpointer ftnptr;
+
+               ftnptr = mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
+               if (!ftnptr) {
+                       g_assert (exc_class);
+                       mono_raise_exception (mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
+               }
+               return ftnptr;
+       }
+
+       if (delegate->target) {
+               /* Produce a location which can be embedded in JITted code */
+               target_loc = mono_gc_alloc_fixed (sizeof (MonoObject*), NULL);
+               *target_loc = delegate->target;
+       } else {
+               target_loc = NULL;
+       }
+
+       wrapper = mono_marshal_get_managed_wrapper (method, klass, target_loc);
 
-       delegate->delegate_trampoline =  mono_compile_method (wrapper);
+       delegate->delegate_trampoline = mono_compile_method (wrapper);
 
        // Add the delegate to the delegate hash table
-       delegate_hash_table_add (delegate);
+       delegate_hash_table_add (delegate, target_loc);
 
        /* when the object is collected, collect the dynamic method, too */
        mono_object_register_finalizer ((MonoObject*)delegate);
@@ -708,6 +362,8 @@ mono_delegate_to_ftnptr (MonoDelegate *delegate)
  * object pointer itself, otherwise we use a GC handle.
  */
 static GHashTable *delegate_hash_table;
+/* Contains root locations pointing to the this arguments of delegates */
+static MonoGHashTable *delegate_target_locations;
 
 static GHashTable *
 delegate_hash_table_new (void) {
@@ -717,6 +373,7 @@ delegate_hash_table_new (void) {
 static void 
 delegate_hash_table_remove (MonoDelegate *d)
 {
+       MonoObject **target_loc;
 #ifdef HAVE_MOVING_COLLECTOR
        guint32 gchandle;
 #endif
@@ -727,26 +384,47 @@ delegate_hash_table_remove (MonoDelegate *d)
        gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
 #endif
        g_hash_table_remove (delegate_hash_table, d->delegate_trampoline);
+       if (delegate_target_locations)
+               target_loc = mono_g_hash_table_lookup (delegate_target_locations, d->delegate_trampoline);
+       else
+               target_loc = NULL;
+       if (target_loc)
+               mono_g_hash_table_remove (delegate_target_locations, d->delegate_trampoline);
        mono_marshal_unlock ();
+       if (target_loc) {
+               mono_gc_free_fixed (target_loc);
+       }
 #ifdef HAVE_MOVING_COLLECTOR
        mono_gchandle_free (gchandle);
 #endif
 }
 
 static void
-delegate_hash_table_add (MonoDelegate *d) 
+delegate_hash_table_add (MonoDelegate *d, MonoObject **target_loc
 {
 #ifdef HAVE_MOVING_COLLECTOR
        guint32 gchandle = mono_gchandle_new_weakref ((MonoObject*)d, FALSE);
+       guint32 old_gchandle;
 #endif
        mono_marshal_lock ();
        if (delegate_hash_table == NULL)
                delegate_hash_table = delegate_hash_table_new ();
+       if (delegate_target_locations == NULL) {
+               /* Has to be conservative as the values are not object references */
+               delegate_target_locations = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_CONSERVATIVE_GC);
+               MONO_GC_REGISTER_ROOT (delegate_target_locations);
+       }
 #ifdef HAVE_MOVING_COLLECTOR
+       old_gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
        g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, GUINT_TO_POINTER (gchandle));
+       if (old_gchandle)
+               mono_gchandle_free (old_gchandle);
 #else
        g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, d);
 #endif
+       if (target_loc)
+               /* This keeps target_loc alive for Boehm */
+               mono_g_hash_table_insert (delegate_target_locations, d->delegate_trampoline, target_loc);
        mono_marshal_unlock ();
 }
 
@@ -758,6 +436,9 @@ mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
 #endif
        MonoDelegate *d;
 
+       if (ftn == NULL)
+               return NULL;
+
        mono_marshal_lock ();
        if (delegate_hash_table == NULL)
                delegate_hash_table = delegate_hash_table_new ();
@@ -866,13 +547,83 @@ mono_array_to_savearray (MonoArray *array)
 gpointer
 mono_array_to_lparray (MonoArray *array)
 {
+       gpointer *nativeArray = NULL;
+       int nativeArraySize = 0;
+
+       int i = 0;
+       MonoClass *klass;
+
        if (!array)
                return NULL;
+#ifndef DISABLE_COM
 
-       /* fixme: maybe we need to make a copy */
+       klass = array->obj.vtable->klass;
+
+       switch (klass->element_class->byval_arg.type) {
+       case MONO_TYPE_VOID:
+               g_assert_not_reached ();
+               break;
+       case MONO_TYPE_CLASS:
+               nativeArraySize = array->max_length;
+               nativeArray = malloc(sizeof(gpointer) * nativeArraySize);
+               for(i = 0; i < nativeArraySize; ++i)    
+                       nativeArray[i] = ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal(((gpointer*)array->vector)[i]);
+               return nativeArray;
+       case MONO_TYPE_U1:
+       case MONO_TYPE_BOOLEAN:
+       case MONO_TYPE_I1:
+       case MONO_TYPE_U2:
+       case MONO_TYPE_CHAR:
+       case MONO_TYPE_I2:
+       case MONO_TYPE_I:
+       case MONO_TYPE_U:
+       case MONO_TYPE_I4:
+       case MONO_TYPE_U4:
+       case MONO_TYPE_U8:
+       case MONO_TYPE_I8:
+       case MONO_TYPE_R4:
+       case MONO_TYPE_R8:
+       case MONO_TYPE_VALUETYPE:
+               /* nothing to do */
+               break;
+       case MONO_TYPE_GENERICINST:
+       case MONO_TYPE_PTR:
+       case MONO_TYPE_OBJECT:
+       case MONO_TYPE_ARRAY: 
+       case MONO_TYPE_SZARRAY:
+       case MONO_TYPE_STRING:
+       default:
+               g_warning ("type 0x%x not handled", klass->element_class->byval_arg.type);
+               g_assert_not_reached ();
+       }
+#endif
        return array->vector;
 }
 
+void
+mono_free_lparray (MonoArray *array, gpointer* nativeArray)
+{
+#ifndef DISABLE_COM
+       MonoClass *klass;
+       int i = 0;
+
+       if (!array)
+               return;
+
+       if (!nativeArray)
+               return;
+       klass = array->obj.vtable->klass;
+
+       switch (klass->element_class->byval_arg.type) {
+               case MONO_TYPE_CLASS:
+                       for(i = 0; i < array->max_length; ++i)  
+                               mono_marshal_free_ccw(nativeArray[i]);
+                       free(nativeArray);
+               break;
+       }               
+#endif
+}
+
 static void
 mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *elclass, guint32 elnum)
 {
@@ -1008,29 +759,68 @@ mono_string_utf16_to_builder (MonoStringBuilder *sb, gunichar2 *text)
        sb->length = len;
 }
 
-/**
- * mono_string_builder_to_utf8:
- * @sb: the string builder
- *
- * Converts to utf8 the contents of the MonoStringBuilder.
- *
- * Returns: a utf8 string with the contents of the StringBuilder.
- *
- * The return value must be released with g_free.
- */
-gpointer
-mono_string_builder_to_utf8 (MonoStringBuilder *sb)
+MonoStringBuilder *
+mono_string_utf16_to_builder2 (gunichar2 *text)
 {
-       GError *error = NULL;
-       gchar *tmp, *res = NULL;
+       int len;
+       MonoStringBuilder *sb;
+       static MonoClass *string_builder_class;
+       static MonoMethod *sb_ctor;
+       void *args [1];
+       MonoObject *exc;
 
-       if (!sb)
+       if (!text)
                return NULL;
 
-       if ((sb->str == sb->cached_str) && (sb->str->length == 0)) {
-               /* 
-                * The sb could have been allocated with the default capacity and be empty.
-                * we need to alloc a buffer of the default capacity in this case.
+       if (!string_builder_class) {
+               MonoMethodDesc *desc;
+
+               string_builder_class = mono_class_from_name (mono_defaults.corlib, "System.Text", "StringBuilder");
+               g_assert (string_builder_class);
+               desc = mono_method_desc_new (":.ctor(int)", FALSE);
+               sb_ctor = mono_method_desc_search_in_class (desc, string_builder_class);
+               g_assert (sb_ctor);
+               mono_method_desc_free (desc);
+       }
+
+       for (len = 0; text [len] != 0; ++len)
+               ;
+
+       sb = (MonoStringBuilder*)mono_object_new (mono_domain_get (), string_builder_class);
+       g_assert (sb);
+       args [0] = &len;
+       mono_runtime_invoke (sb_ctor, sb, args, &exc);
+       g_assert (!exc);
+
+       sb->length = len;
+       memcpy (mono_string_chars (sb->str), text, len * 2);
+
+       return sb;
+}
+
+/**
+ * mono_string_builder_to_utf8:
+ * @sb: the string builder
+ *
+ * Converts to utf8 the contents of the MonoStringBuilder.
+ *
+ * Returns: a utf8 string with the contents of the StringBuilder.
+ *
+ * The return value must be released with g_free.
+ */
+gpointer
+mono_string_builder_to_utf8 (MonoStringBuilder *sb)
+{
+       GError *error = NULL;
+       gchar *tmp, *res = NULL;
+
+       if (!sb)
+               return NULL;
+
+       if ((sb->str == sb->cached_str) && (sb->str->length == 0)) {
+               /* 
+                * The sb could have been allocated with the default capacity and be empty.
+                * we need to alloc a buffer of the default capacity in this case.
                 */
                MONO_OBJECT_SETREF (sb, str, mono_string_new_size (mono_domain_get (), 16));
                sb->cached_str = NULL;
@@ -1128,137 +918,6 @@ mono_string_to_ansibstr (MonoString *string_obj)
        return NULL;
 }
 
-typedef gpointer (*SysAllocStringLenFunc)(gunichar* str, guint32 len);
-typedef guint32 (*SysStringLenFunc)(gpointer bstr);
-typedef void (*SysFreeStringFunc)(gunichar* str);
-
-static SysAllocStringLenFunc sys_alloc_string_len_ms = NULL;
-static SysStringLenFunc sys_string_len_ms = NULL;
-static SysFreeStringFunc sys_free_string_ms = NULL;
-
-static gboolean
-init_com_provider_ms (void)
-{
-       static gboolean initialized = FALSE;
-       char *error_msg;
-       MonoDl *module = NULL;
-       const char* scope = "liboleaut32.so";
-
-       if (initialized)
-               return TRUE;
-
-       module = mono_dl_open(scope, MONO_DL_LAZY, &error_msg);
-       if (error_msg) {
-               g_warning ("Error loading COM support library '%s': %s", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-       error_msg = mono_dl_symbol (module, "SysAllocStringLen", (gpointer*)&sys_alloc_string_len_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysAllocStringLen", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       error_msg = mono_dl_symbol (module, "SysStringLen", (gpointer*)&sys_string_len_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysStringLen", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       error_msg = mono_dl_symbol (module, "SysFreeString", (gpointer*)&sys_free_string_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysFreeString", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       initialized = TRUE;
-       return TRUE;
-}
-
-gpointer
-mono_string_to_bstr (MonoString *string_obj)
-{
-       if (!string_obj)
-               return NULL;
-#ifdef PLATFORM_WIN32
-       return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               int slen = mono_string_length (string_obj);
-               /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
-               char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
-               if (ret == NULL)
-                       return NULL;
-               memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
-               * ((guint32 *) ret) = slen * sizeof(gunichar2);
-               ret [4 + slen * sizeof(gunichar2)] = 0;
-               ret [5 + slen * sizeof(gunichar2)] = 0;
-
-               return ret + 4;
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               gpointer ret = NULL;
-               gunichar* str = NULL;
-               guint32 len;
-               len = mono_string_length (string_obj);
-               str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
-                       NULL, NULL, NULL);
-               ret = sys_alloc_string_len_ms (str, len);
-               g_free(str);
-               return ret;
-       } else {
-               g_assert_not_reached ();
-       }
-#endif
-}
-
-MonoString *
-mono_string_from_bstr (gpointer bstr)
-{
-       if (!bstr)
-               return NULL;
-#ifdef PLATFORM_WIN32
-       return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               MonoString* str = NULL;
-               glong written = 0;
-               gunichar2* utf16 = NULL;
-
-               utf16 = g_ucs4_to_utf16 (bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
-               str = mono_string_new_utf16 (mono_domain_get (), utf16, written);
-               g_free (utf16);
-               return str;
-       } else {
-               g_assert_not_reached ();
-       }
-
-#endif
-}
-
-void
-mono_free_bstr (gpointer bstr)
-{
-       if (!bstr)
-               return;
-#ifdef PLATFORM_WIN32
-       SysFreeString ((BSTR)bstr);
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               g_free (((char *)bstr) - 4);
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               sys_free_string_ms (bstr);
-       } else {
-               g_assert_not_reached ();
-       }
-
-#endif
-}
-
 /**
  * mono_string_to_byvalstr:
  * @dst: Where to store the null-terminated utf8 decoded string.
@@ -1369,25 +1028,6 @@ mono_mb_emit_contextbound_check (MonoMethodBuilder *mb, int branch_code)
        return mono_mb_emit_branch (mb, branch_code);
 }
 
-#ifndef DISABLE_COM
-
-static void
-mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
-{
-       // get function pointer from 1st arg, the COM interface pointer
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
-       mono_mb_emit_icall (mb, cominterop_get_function_pointer);
-
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
-       mono_mb_emit_calli (mb, sig);
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
-}
-
-#endif /* DISABLE_COM */
-
 static void
 mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, const char *msg)
 {
@@ -1436,7 +1076,7 @@ handle_enum:
                return CEE_LDIND_R8;
        case MONO_TYPE_VALUETYPE:
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                }
                return CEE_LDOBJ;
@@ -1490,7 +1130,7 @@ handle_enum:
                return CEE_STIND_R8;
        case MONO_TYPE_VALUETYPE:
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                }
                return CEE_STOBJ;
@@ -1731,76 +1371,9 @@ emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
 #ifndef DISABLE_COM
        case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
        case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
-       case MONO_MARSHAL_CONV_OBJECT_IDISPATCH: {
-               static MonoClass* com_interop_proxy_class = NULL;
-               static MonoMethod* com_interop_proxy_get_proxy = NULL;
-               static MonoMethod* get_transparent_proxy = NULL;
-               int real_proxy;
-               guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
-               MonoClass *klass = NULL; 
-               
-               /* COM types are initialized lazily */
-               mono_init_com_types ();
-
-               klass = mono_class_from_mono_type (type);
-
-               mono_mb_emit_ldloc (mb, 1);
-               mono_mb_emit_byte (mb, CEE_LDNULL);
-               mono_mb_emit_byte (mb, CEE_STIND_REF);
-
-               mono_mb_emit_ldloc (mb, 0);
-               mono_mb_emit_byte (mb, CEE_LDIND_I);
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               /* load dst to store later */
-               mono_mb_emit_ldloc (mb, 1);
-
-               mono_mb_emit_ldloc (mb, 0);
-               mono_mb_emit_byte (mb, CEE_LDIND_I);
-               mono_mb_emit_icon (mb, TRUE);
-               mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-               pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-               if (!com_interop_proxy_class)
-                       com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
-               if (!com_interop_proxy_get_proxy)
-                       com_interop_proxy_get_proxy = mono_class_get_method_from_name_flags (com_interop_proxy_class, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE);
-               if (!get_transparent_proxy)
-                       get_transparent_proxy = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
-
-               real_proxy = mono_mb_add_local (mb, &com_interop_proxy_class->byval_arg);
-
-               mono_mb_emit_ldloc (mb, 0);
-               mono_mb_emit_byte (mb, CEE_LDIND_I);
-               mono_mb_emit_ptr (mb, &mono_defaults.com_object_class->byval_arg);
-               mono_mb_emit_icall (mb, type_from_handle);
-               mono_mb_emit_managed_call (mb, com_interop_proxy_get_proxy, NULL);
-               mono_mb_emit_managed_call (mb, get_transparent_proxy, NULL);
-               if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
-                       g_assert (klass);
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               }
-               mono_mb_emit_byte (mb, CEE_STIND_REF);
-               pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
-               /* is already managed object */
-               mono_mb_patch_short_branch (mb, pos_ccw);
-               mono_mb_emit_ldloc (mb, 0);
-               mono_mb_emit_byte (mb, CEE_LDIND_I);
-               mono_mb_emit_icon (mb, TRUE);
-               mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-
-               if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
-                       g_assert (klass);
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               }
-               mono_mb_emit_byte (mb, CEE_STIND_REF);
-
-               mono_mb_patch_short_branch (mb, pos_end);
-               /* case if null */
-               mono_mb_patch_short_branch (mb, pos_null);
+       case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
+               mono_cominterop_emit_ptr_to_object_conv (mb, type, conv, mspec);
                break;
-       }
 #endif /* DISABLE_COM */
 
        case MONO_MARSHAL_CONV_SAFEHANDLE: {
@@ -1831,9 +1404,13 @@ emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
        case MONO_MARSHAL_CONV_STR_ANSIBSTR:
        case MONO_MARSHAL_CONV_STR_TBSTR:
        case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
-       default:
-               g_warning ("marshaling conversion %d not implemented", conv);
-               g_assert_not_reached ();
+       default: {
+               char *msg = g_strdup_printf ("marshaling conversion %d not implemented", conv);
+
+               mono_mb_emit_exception_marshal_directive (mb, msg);
+               g_free (msg);
+               break;
+       }
        }
 }
 
@@ -1876,6 +1453,8 @@ conv_to_icall (MonoMarshalConv conv)
                return mono_array_to_savearray;
        case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
                return mono_array_to_lparray;
+       case MONO_MARSHAL_FREE_LPARRAY:
+               return mono_free_lparray;
        case MONO_MARSHAL_CONV_DEL_FTN:
                return mono_delegate_to_ftnptr;
        case MONO_MARSHAL_CONV_FTN_DEL:
@@ -2107,91 +1686,9 @@ emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
 #ifndef DISABLE_COM
        case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
        case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
-       case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
-               guint32 pos_null = 0, pos_rcw = 0, pos_end = 0;
-               /* COM types are initialized lazily */
-               mono_init_com_types ();
-
-
-               mono_mb_emit_ldloc (mb, 1);
-               mono_mb_emit_icon (mb, 0);
-               mono_mb_emit_byte (mb, CEE_CONV_U);
-               mono_mb_emit_byte (mb, CEE_STIND_I);
-
-               mono_mb_emit_ldloc (mb, 0);     
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-               // if null just break, dst was already inited to 0
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               mono_mb_emit_ldloc (mb, 0);     
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_icall (mb, cominterop_object_is_rcw);
-               pos_rcw = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               // load dst to store later
-               mono_mb_emit_ldloc (mb, 1);
-
-               // load src
-               mono_mb_emit_ldloc (mb, 0);     
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-               /* load the RCW from the ComInteropProxy*/
-               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-               if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
-                       mono_mb_emit_ptr (mb, mono_type_get_class (type));
-                       mono_mb_emit_icon (mb, TRUE);
-                       mono_mb_emit_icall (mb, cominterop_get_interface);
-
-               }
-               else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN) {
-                       static MonoProperty* iunknown = NULL;
-                       
-                       if (!iunknown)
-                               iunknown = mono_class_get_property_from_name (mono_defaults.com_object_class, "IUnknown");
-                       mono_mb_emit_managed_call (mb, iunknown->get, NULL);
-               }
-               else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH) {
-                       static MonoProperty* idispatch = NULL;
-                       
-                       if (!idispatch)
-                               idispatch = mono_class_get_property_from_name (mono_defaults.com_object_class, "IDispatch");
-                       mono_mb_emit_managed_call (mb, idispatch->get, NULL);
-               }
-               else {
-                       g_assert_not_reached ();
-               }
-               mono_mb_emit_byte (mb, CEE_STIND_I);
-               pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-               
-               // if not rcw
-               mono_mb_patch_short_branch (mb, pos_rcw);
-               /* load dst to store later */
-               mono_mb_emit_ldloc (mb, 1);
-               /* load src */
-               mono_mb_emit_ldloc (mb, 0);     
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               
-               if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE)
-                       mono_mb_emit_ptr (mb, mono_type_get_class (type));
-               else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN)
-                       mono_mb_emit_ptr (mb, mono_defaults.iunknown_class);
-               else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH)
-                       mono_mb_emit_ptr (mb, mono_defaults.idispatch_class);
-               else
-                       g_assert_not_reached ();
-               mono_mb_emit_icall (mb, cominterop_get_ccw);
-               mono_mb_emit_byte (mb, CEE_STIND_I);
-
-               mono_mb_patch_short_branch (mb, pos_end);
-               mono_mb_patch_short_branch (mb, pos_null);
+       case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
+               mono_cominterop_emit_object_to_ptr_conv (mb, type, conv, mspec);
                break;
-       }
 #endif /* DISABLE_COM */
 
        case MONO_MARSHAL_CONV_SAFEHANDLE: {
@@ -2245,7 +1742,7 @@ emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
        default: {
                char *msg = g_strdup_printf ("marshalling conversion %d not implemented", conv);
                MonoException *exc = mono_get_exception_not_implemented (msg);
-               g_warning (msg);
+               g_warning ("%s", msg);
                g_free (msg);
                mono_raise_exception (exc);
        }
@@ -2280,6 +1777,15 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
                return;
        }
 
+       if (klass != mono_defaults.safehandle_class) {
+               if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+                       char *msg = g_strdup_printf ("Type %s which is passed to unmanaged code must have a StructLayout attribute.",
+                                                                                mono_type_full_name (&klass->byval_arg));
+                       mono_mb_emit_exception_marshal_directive (mb, msg);
+                       return;
+               }
+       }
+
        for (i = 0; i < info->num_fields; i++) {
                MonoMarshalNative ntype;
                MonoMarshalConv conv;
@@ -2314,11 +1820,6 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
                                                 "reference field at the same offset as another field.",
                                                 mono_type_full_name (&klass->byval_arg));
                        }
-                       
-                       if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
-                               g_error ("Type %s which is passed to unmanaged code must have a StructLayout attribute",
-                                        mono_type_full_name (&klass->byval_arg));
-                       
                }
                
                switch (conv) {
@@ -2359,7 +1860,7 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
                                int src_var, dst_var;
 
                                if (ftype->data.klass->enumtype) {
-                                       ftype = ftype->data.klass->enum_basetype;
+                                       ftype = mono_class_enum_basetype (ftype->data.klass);
                                        goto handle_enum;
                                }
 
@@ -2501,6 +2002,12 @@ emit_thread_force_interrupt_checkpoint (MonoMethodBuilder *mb)
        emit_thread_interrupt_checkpoint_call (mb, mono_thread_force_interruption_checkpoint);
 }
 
+void
+mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder *mb)
+{
+       emit_thread_interrupt_checkpoint (mb);
+}
+
 static MonoAsyncResult *
 mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
 {
@@ -2524,19 +2031,19 @@ mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
                        MonoAsyncResult *ares;
                        MonoObject *exc;
                        MonoArray *out_args;
-                       HANDLE handle;
                        method = delegate->method;
 
                        msg = mono_method_call_message_new (mono_marshal_method_from_wrapper (method), params, NULL, &async_callback, &state);
-                       handle = CreateEvent (NULL, TRUE, FALSE, NULL);
-                       g_assert(handle != NULL);
-                       ares = mono_async_result_new (mono_domain_get (), handle, state, handle, NULL);
+                       ares = mono_async_result_new (mono_domain_get (), NULL, state, NULL, NULL);
                        MONO_OBJECT_SETREF (ares, async_delegate, (MonoObject *)delegate);
                        MONO_OBJECT_SETREF (ares, async_callback, (MonoObject *)async_callback);
                        MONO_OBJECT_SETREF (msg, async_result, ares);
                        msg->call_type = CallType_BeginInvoke;
 
+                       exc = NULL;
                        mono_remoting_invoke ((MonoObject *)tp->rp, msg, &exc, &out_args);
+                       if (exc)
+                               mono_raise_exception ((MonoException *) exc);
                        return ares;
                }
        }
@@ -2797,7 +2304,13 @@ get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
        return *var;
 }
 
-static inline MonoMethod*
+GHashTable*
+mono_marshal_get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
+{
+       return get_cache (var, hash_func, equal_func);
+}
+
+MonoMethod*
 mono_marshal_find_in_cache (GHashTable *cache, gpointer key)
 {
        MonoMethod *res;
@@ -2821,7 +2334,7 @@ mono_marshal_method_set_wrapper_data (MonoMethod *method, gpointer data)
 }
 
 /* Create the method from the builder and place it in the cache */
-static inline MonoMethod*
+MonoMethod*
 mono_mb_create_and_cache (GHashTable *cache, gpointer key,
                                                           MonoMethodBuilder *mb, MonoMethodSignature *sig,
                                                           int max_stack)
@@ -3008,8 +2521,10 @@ mono_delegate_end_invoke (MonoDelegate *delegate, gpointer *params)
        msg = mono_method_call_message_new (method, params, NULL, NULL, NULL);
 
        ares = mono_array_get (msg->args, gpointer, sig->param_count - 1);
-       if (ares == NULL)
+       if (ares == NULL) {
                mono_raise_exception (mono_exception_from_name_msg (mono_defaults.corlib, "System.Runtime.Remoting", "RemotingException", "The async result object is null or of an unexpected type."));
+               return NULL;
+       }
 
        if (ares->async_delegate != (MonoObject*)delegate && mono_framework_version () >= 2) {
                mono_raise_exception (mono_get_exception_invalid_operation (
@@ -3200,350 +2715,44 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
        return res;
 } 
 
-#ifndef DISABLE_COM
-
-/**
- * cominterop_get_native_wrapper_adjusted:
- * @method: managed COM Interop method
- *
- * Returns: the generated method to call with signature matching
- * the unmanaged COM Method signature
- */
-static MonoMethod *
-cominterop_get_native_wrapper_adjusted (MonoMethod *method)
+MonoMethod *
+mono_marshal_get_remoting_invoke (MonoMethod *method)
 {
+       MonoMethodSignature *sig;
+       MonoMethodBuilder *mb;
        MonoMethod *res;
-       MonoMethodBuilder *mb_native;
-       MonoMarshalSpec **mspecs;
-       MonoMethodSignature *sig, *sig_native;
-       MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
-       int i;
+       int params_var;
 
-       sig = mono_method_signature (method);
+       g_assert (method);
 
-       // create unmanaged wrapper
-       mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
-       sig_native = cominterop_method_signature (method);
+       if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
+               return method;
 
-       mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
-       memset (mspecs, 0, sizeof(MonoMarshalSpec*)*(sig_native->param_count+1));
+       /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
+       if (method->klass->is_com_object || method->klass == mono_defaults.com_object_class) {
+               MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
+               g_assert (vtable); /*FIXME do proper error handling*/
 
-       mono_method_get_marshal_info (method, mspecs);
+               if (!vtable->remote) {
+#ifndef DISABLE_COM
+                       return mono_cominterop_get_invoke (method);
+#else
+                       g_assert_not_reached ();
+#endif
+               }
+       }
 
-       // move managed args up one
-       for (i = sig->param_count; i >= 1; i--)
-               mspecs[i+1] = mspecs[i];
+       sig = mono_signature_no_pinvoke (method);
 
-       // first arg is IntPtr for interface
-       mspecs[1] = NULL;
+       /* we cant remote methods without this pointer */
+       if (!sig->hasthis)
+               return method;
 
-       if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG)) {
-               // move return spec to last param
-               if (!MONO_TYPE_IS_VOID (sig->ret))
-                       mspecs[sig_native->param_count] = mspecs[0];
+       if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
+               return res;
 
-               mspecs[0] = NULL;
-       }
-
-       for (i = 1; i < sig_native->param_count; i++) {
-               int mspec_index = i + 1;
-               if (mspecs[mspec_index] == NULL) {
-                       // default object to VARIANT
-                       if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
-                       }
-                       else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
-                       }
-                       else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
-                       }
-               }
-       }
-
-       if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
-               // move return spec to last param
-               if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {                       
-                       // default object to VARIANT
-                       if (sig->ret->type == MONO_TYPE_OBJECT) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_STRUCT;
-                       }
-                       else if (sig->ret->type == MONO_TYPE_STRING) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_BSTR;
-                       }
-                       else if (sig->ret->type == MONO_TYPE_CLASS) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_INTERFACE;
-                       }
-               }
-       }
-
-       mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, FALSE, TRUE);
-
-       res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);      
-
-       mono_mb_free (mb_native);
-
-       for (i = sig_native->param_count; i >= 0; i--)
-               if (mspecs [i])
-                       mono_metadata_free_marshal_spec (mspecs [i]);
-       g_free (mspecs);
-
-       return res;
-}
-
-/**
- * cominterop_get_native_wrapper:
- * @method: managed method
- *
- * Returns: the generated method to call
- */
-static MonoMethod *
-cominterop_get_native_wrapper (MonoMethod *method)
-{
-       MonoMethod *res;
-       GHashTable *cache;
-       MonoMethodBuilder *mb;
-       MonoMethodSignature *sig, *csig;
-
-       g_assert (method);
-
-       cache = get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
-
-       mono_init_com_types ();
-
-       if (!method->klass->vtable)
-               mono_class_setup_vtable (method->klass);
-       
-       if (!method->klass->methods)
-               mono_class_setup_methods (method->klass);
-
-       sig = mono_method_signature (method);
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
-       /* if method klass is import, that means method
-        * is really a com call. let interop system emit it.
-       */
-       if (MONO_CLASS_IS_IMPORT(method->klass)) {
-               /* FIXME: we have to call actual class .ctor
-                * instead of just __ComObject .ctor.
-                */
-               if (!strcmp(method->name, ".ctor")) {
-                       static MonoMethod *ctor = NULL;
-
-                       if (!ctor)
-                               ctor = mono_class_get_method_from_name (mono_defaults.com_object_class, ".ctor", 0);
-                       mono_mb_emit_ldarg (mb, 0);
-                       mono_mb_emit_managed_call (mb, ctor, NULL);
-                       mono_mb_emit_byte (mb, CEE_RET);
-               }
-               else {
-                       static MonoMethod * ThrowExceptionForHR = NULL;
-                       MonoMethod *adjusted_method;
-                       int retval = 0;
-                       int ptr_this;
-                       int i;
-                       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-                       // add local variables
-                       ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-                       if (!MONO_TYPE_IS_VOID (sig->ret))
-                               retval =  mono_mb_add_local (mb, sig->ret);
-
-                       // get the type for the interface the method is defined on
-                       // and then get the underlying COM interface for that type
-                       mono_mb_emit_ldarg (mb, 0);
-                       mono_mb_emit_ptr (mb, method);
-                       mono_mb_emit_icall (mb, cominterop_get_method_interface);
-                       mono_mb_emit_icon (mb, TRUE);
-                       mono_mb_emit_icall (mb, cominterop_get_interface);
-                       mono_mb_emit_stloc (mb, ptr_this);
-
-                       // arg 1 is unmanaged this pointer
-                       mono_mb_emit_ldloc (mb, ptr_this);
-
-                       // load args
-                       for (i = 1; i <= sig->param_count; i++)
-                               mono_mb_emit_ldarg (mb, i);
-
-                       // push managed return value as byref last argument
-                       if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
-                               mono_mb_emit_ldloc_addr (mb, retval);
-                       
-                       adjusted_method = cominterop_get_native_wrapper_adjusted (method);
-                       mono_mb_emit_managed_call (mb, adjusted_method, NULL);
-
-                       if (!preserve_sig) {
-                               if (!ThrowExceptionForHR)
-                                       ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
-                               mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);
-
-                               // load return value managed is expecting
-                               if (!MONO_TYPE_IS_VOID (sig->ret))
-                                       mono_mb_emit_ldloc (mb, retval);
-                       }
-
-                       mono_mb_emit_byte (mb, CEE_RET);
-               }
-               
-               
-       }
-       /* Does this case ever get hit? */
-       else {
-               char *msg = g_strdup ("non imported interfaces on \
-                       imported classes is not yet implemented.");
-               mono_mb_emit_exception (mb, "NotSupportedException", msg);
-       }
-       csig = signature_dup (method->klass->image, sig);
-       csig->pinvoke = 0;
-       res = mono_mb_create_and_cache (cache, method,
-                                                                       mb, csig, csig->param_count + 16);
-       mono_mb_free (mb);
-       return res;
-}
-
-/**
- * cominterop_get_invoke:
- * @method: managed method
- *
- * Returns: the generated method that calls the underlying __ComObject
- * rather than the proxy object.
- */
-static MonoMethod *
-cominterop_get_invoke (MonoMethod *method)
-{
-       MonoMethodSignature *sig;
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       int i, temp_obj;
-       GHashTable* cache = get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);
-
-       g_assert (method);
-
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
-
-       sig = mono_signature_no_pinvoke (method);
-
-       /* we cant remote methods without this pointer */
-       if (!sig->hasthis)
-               return method;
-
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);
-
-       /* get real proxy object, which is a ComInteropProxy in this case*/
-       temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
-       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-       /* load the RCW from the ComInteropProxy*/
-       mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
-       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-       /* load args and make the call on the RCW */
-       for (i = 1; i <= sig->param_count; i++)
-               mono_mb_emit_ldarg (mb, i);
-
-       if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
-               MonoMethod * native_wrapper = cominterop_get_native_wrapper(method);
-               mono_mb_emit_managed_call (mb, native_wrapper, NULL);
-       }
-       else {
-               if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
-                       mono_mb_emit_op (mb, CEE_CALLVIRT, method);
-               else
-                       mono_mb_emit_op (mb, CEE_CALL, method);
-       }
-
-       if (!strcmp(method->name, ".ctor"))     {
-               static MonoClass *com_interop_proxy_class = NULL;
-               static MonoMethod *cache_proxy = NULL;
-
-               if (!com_interop_proxy_class)
-                       com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
-               if (!cache_proxy)
-                       cache_proxy = mono_class_get_method_from_name (com_interop_proxy_class, "CacheProxy", 0);
-
-               mono_mb_emit_ldarg (mb, 0);
-               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_managed_call (mb, cache_proxy, NULL);
-       }
-
-       emit_thread_interrupt_checkpoint (mb);
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
-       mono_mb_free (mb);
-
-       return res;
-}
-
-#endif /* DISABLE_COM */
-
-/* Maps a managed object to its unmanaged representation 
- * i.e. it's COM Callable Wrapper (CCW). 
- * Key: MonoObject*
- * Value: MonoCCW*
- */
-static GHashTable* ccw_hash = NULL;
-
-/* Maps a CCW interface to it's containing CCW. 
- * Note that a CCW support many interfaces.
- * Key: MonoCCW*
- * Value: MonoCCWInterface*
- */
-static GHashTable* ccw_interface_hash = NULL;
-
-/* Maps the IUnknown value of a RCW to
- * it's MonoComInteropProxy*.
- * Key: void*
- * Value: gchandle
- */
-static GHashTable* rcw_hash = NULL;
-
-MonoMethod *
-mono_marshal_get_remoting_invoke (MonoMethod *method)
-{
-       MonoMethodSignature *sig;
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       int params_var;
-
-       g_assert (method);
-
-       if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
-               return method;
-
-       /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
-       if ((method->klass->is_com_object || method->klass == mono_defaults.com_object_class) && !mono_class_vtable (mono_domain_get (), method->klass)->remote) {
-#ifndef DISABLE_COM
-               return cominterop_get_invoke(method);
-#else
-               g_assert_not_reached ();
-#endif
-       }
-
-       sig = mono_signature_no_pinvoke (method);
-
-       /* we cant remote methods without this pointer */
-       if (!sig->hasthis)
-               return method;
-
-       if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
-               return res;
-
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
-       mb->method->save_lmf = 1;
+       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
+       mb->method->save_lmf = 1;
 
        params_var = mono_mb_emit_save_args (mb, sig, TRUE);
 
@@ -3606,7 +2815,7 @@ mono_get_xdomain_marshal_type (MonoType *t)
 /* mono_marshal_xdomain_copy_value
  * Makes a copy of "val" suitable for the current domain.
  */
-static MonoObject *
+MonoObject *
 mono_marshal_xdomain_copy_value (MonoObject *val)
 {
        MonoDomain *domain;
@@ -3802,7 +3011,6 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
        MonoClass *ret_class = NULL;
        int loc_array=0, loc_return=0, loc_serialized_exc=0;
        MonoExceptionClause *main_clause;
-       MonoMethodHeader *header;
        int pos, pos_leave;
        gboolean copy_return;
 
@@ -3843,9 +3051,7 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
 
        /* try */
 
-       mono_loader_lock ();
        main_clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        main_clause->try_offset = mono_mb_get_label (mb);
 
        /* Clean the call context */
@@ -3984,6 +3190,8 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
                        mono_mb_emit_ldloc (mb, loc_array);
                        mono_mb_emit_icon (mb, complex_count);  /* The array has an additional slot to hold the ret value */
                        mono_mb_emit_ldloc (mb, loc_return);
+
+                       g_assert (ret_class); /*FIXME properly fail here*/
                        if (ret_class->valuetype) {
                                mono_mb_emit_op (mb, CEE_BOX, ret_class);
                        }
@@ -4039,13 +3247,11 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
 
        mono_mb_emit_byte (mb, CEE_RET);
 
+       mono_mb_set_clauses (mb, 1, main_clause);
+
        res = mono_remoting_mb_create_and_cache (method, mb, csig, csig->param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = main_clause;
-
        return res;
 }
 
@@ -4381,13 +3587,15 @@ mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTar
                return mono_marshal_get_xappdomain_invoke (method);
        } else if (target_type == MONO_REMOTING_TARGET_COMINTEROP) {
 #ifndef DISABLE_COM
-               return cominterop_get_invoke (method);
+               return mono_cominterop_get_invoke (method);
 #else
                g_assert_not_reached ();
 #endif
        } else {
                return mono_marshal_get_remoting_invoke (method);
        }
+       /* Not erached */
+       return NULL;
 }
 
 G_GNUC_UNUSED static gpointer
@@ -4641,8 +3849,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
 
        if (callvirt) {
                // From mono_mb_create_and_cache
+               mb->skip_visibility = 1;
                newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
-               newm->skip_visibility = 1;
                /*We perform double checked locking, so must fence before publishing*/
                mono_memory_barrier ();
                mono_marshal_lock ();
@@ -4660,8 +3868,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                        mono_free_method (newm);
                }
        } else {
+               mb->skip_visibility = 1;
                res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
-               res->skip_visibility = 1;
        }
        mono_mb_free (mb);
 
@@ -4680,7 +3888,7 @@ signature_dup_add_this (MonoMethodSignature *sig, MonoClass *klass)
        int i;
 
        res = mono_metadata_signature_alloc (klass->image, sig->param_count + 1);
-       memcpy (res, sig, sizeof (MonoMethodSignature));
+       memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
        res->param_count = sig->param_count + 1;
        res->hasthis = FALSE;
        for (i = sig->param_count - 1; i >= 0; i --)
@@ -4739,12 +3947,40 @@ add_string_ctor_signature (MonoMethod *method)
        return callsig;
 }
 
+/*
+ * mono_marshal_get_string_ctor_signature:
+ *
+ *   Return the modified signature used by string ctors (they return the newly created
+ * string).
+ */
+MonoMethodSignature*
+mono_marshal_get_string_ctor_signature (MonoMethod *method)
+{
+       MonoMethodSignature *sig = lookup_string_ctor_signature (mono_method_signature (method));
+       if (!sig)
+               sig = add_string_ctor_signature (method);
+
+       return sig;
+}
+
 static MonoType*
-get_runtime_invoke_type (MonoType *t)
+get_runtime_invoke_type (MonoType *t, gboolean ret)
 {
-       if (t->byref)
-               return &mono_defaults.int_class->byval_arg;
+       if (t->byref) {
+               if (t->type == MONO_TYPE_GENERICINST)
+                       return t;
+               else
+                       return &mono_defaults.int_class->byval_arg;
+       }
+
+       if (MONO_TYPE_IS_REFERENCE (t))
+               return &mono_defaults.object_class->byval_arg;
 
+       if (ret)
+               /* The result needs to be boxed */
+               return t;
+
+handle_enum:
        switch (t->type) {
        case MONO_TYPE_U1:
                return &mono_defaults.sbyte_class->byval_arg;
@@ -4755,19 +3991,19 @@ get_runtime_invoke_type (MonoType *t)
        case MONO_TYPE_U8:
                return &mono_defaults.int64_class->byval_arg;
        case MONO_TYPE_BOOLEAN:
-               return &mono_defaults.byte_class->byval_arg;
+               return &mono_defaults.sbyte_class->byval_arg;
        case MONO_TYPE_CHAR:
                return &mono_defaults.int16_class->byval_arg;
        case MONO_TYPE_U:
+       case MONO_TYPE_PTR:
                return &mono_defaults.int_class->byval_arg;
        case MONO_TYPE_VALUETYPE:
-               if (t->data.klass->enumtype)
-                       return mono_type_get_underlying_type (t);
-               else
-                       return t;
+               if (t->data.klass->enumtype) {
+                       t = mono_class_enum_basetype (t->data.klass);
+                       goto handle_enum;
+               }
+               return t;
        default:
-               if (MONO_TYPE_IS_REFERENCE (t))
-                       return &mono_defaults.object_class->byval_arg;
                return t;
        }
 }
@@ -4783,9 +4019,9 @@ mono_marshal_get_runtime_invoke_sig (MonoMethodSignature *sig)
        MonoMethodSignature *res = mono_metadata_signature_dup (sig);
        int i;
 
-       res->ret = get_runtime_invoke_type (sig->ret);
+       res->ret = get_runtime_invoke_type (sig->ret, TRUE);
        for (i = 0; i < res->param_count; ++i)
-               res->params [i] = get_runtime_invoke_type (sig->params [i]);
+               res->params [i] = get_runtime_invoke_type (sig->params [i], FALSE);
 
        return res;
 }
@@ -4794,24 +4030,66 @@ static gboolean
 runtime_invoke_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
 {
        /* Can't share wrappers which return a vtype since it needs to be boxed */
-       if (sig1->ret != sig2->ret && !(MONO_TYPE_IS_REFERENCE (sig1->ret) && MONO_TYPE_IS_REFERENCE (sig2->ret)))
+       if (sig1->ret != sig2->ret && !(MONO_TYPE_IS_REFERENCE (sig1->ret) && MONO_TYPE_IS_REFERENCE (sig2->ret)) && !mono_metadata_type_equal (sig1->ret, sig2->ret))
                return FALSE;
        else
                return mono_metadata_signature_equal (sig1, sig2);
 }
 
+/*
+ * get_wrapper_target_class:
+ *
+ *   Return the class where a wrapper method should be placed.
+ */
+static MonoClass*
+get_wrapper_target_class (MonoImage *image)
+{
+       MonoClass *klass;
+
+       /*
+        * Notes:
+        * - can't put all wrappers into an mscorlib class, because they reference
+        *   metadata (signature) so they should be put into the same image as the 
+        *   method they wrap, so they are unloaded together.
+        * - putting them into a class with a type initalizer could cause the 
+        *   initializer to be executed which can be a problem if the wrappers are 
+        *   shared.
+        * - putting them into an inflated class can cause problems if the the 
+        *   class is deleted because it references an image which is unloaded.
+        * To avoid these problems, we put the wrappers into the <Module> class of 
+        * the image.
+        */
+       if (image->dynamic)
+               klass = ((MonoDynamicImage*)image)->wrappers_type;
+       else
+               klass = mono_class_get (image, mono_metadata_make_token (MONO_TABLE_TYPEDEF, 1));
+       g_assert (klass);
+
+       return klass;
+}
+
+static void
+runtime_invoke_reset_abort (MonoException *ex)
+{
+       if (ex->object.vtable->klass == mono_defaults.threadabortexception_class)
+               ves_icall_System_Threading_Thread_ResetAbort ();
+}
+
 /*
  * generates IL code for the runtime invoke function 
  * MonoObject *runtime_invoke (MonoObject *this, void **params, MonoObject **exc, void* method)
  *
  * we also catch exceptions if exc != null
+ * If VIRTUAL is TRUE, then METHOD is invoked virtually on THIS. This is useful since
+ * it means that the compiled code for METHOD does not have to be looked up 
+ * before calling the runtime invoke wrapper. In this case, the wrapper ignores
+ * its METHOD argument.
  */
 MonoMethod *
-mono_marshal_get_runtime_invoke (MonoMethod *method)
+mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
 {
        MonoMethodSignature *sig, *csig, *callsig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoMethodBuilder *mb;
        GHashTable *cache = NULL;
        MonoClass *target_klass;
@@ -4819,9 +4097,10 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
        static MonoString *string_dummy = NULL;
        static MonoMethodSignature *cctor_signature = NULL;
        static MonoMethodSignature *finalize_signature = NULL;
-       int i, pos, posna;
+       int i, pos;
        char *name;
        gboolean need_direct_wrapper = FALSE;
+       int *tmp_nullable_locals;
 
        g_assert (method);
 
@@ -4835,15 +4114,21 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                finalize_signature->hasthis = 1;
        }
 
+       if (virtual)
+               need_direct_wrapper = TRUE;
+
        /* 
         * Use a separate cache indexed by methods to speed things up and to avoid the
         * boundless mempool growth caused by the signature_dup stuff below.
         */
-       cache = get_cache (&method->klass->image->runtime_invoke_direct_cache, mono_aligned_addr_hash, NULL);
+       if (virtual)
+               cache = get_cache (&method->klass->image->runtime_invoke_vcall_cache, mono_aligned_addr_hash, NULL);
+       else
+               cache = get_cache (&method->klass->image->runtime_invoke_direct_cache, mono_aligned_addr_hash, NULL);
        res = mono_marshal_find_in_cache (cache, method);
        if (res)
                return res;
-
+               
        if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
                (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
                /* 
@@ -4857,6 +4142,8 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                callsig = lookup_string_ctor_signature (mono_method_signature (method));
                if (!callsig)
                        callsig = add_string_ctor_signature (method);
+               /* Can't share this as we push a string as this */
+               need_direct_wrapper = TRUE;
        } else {
                if (method->klass->valuetype && mono_method_signature (method)->hasthis) {
                        /* 
@@ -4864,6 +4151,8 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                         * Create a new signature to reflect this.
                         */
                        callsig = signature_dup_add_this (mono_method_signature (method), method->klass);
+                       /* Can't share this as it would be shared with static methods taking an IntPtr argument */
+                       need_direct_wrapper = TRUE;
                } else {
                        if (method->dynamic)
                                callsig = signature_dup (method->klass->image, mono_method_signature (method));
@@ -4872,6 +4161,14 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                }
        }
 
+#if 0
+       /* Vtypes/nullables/Byrefs cause too many problems */
+       for (i = 0; i < callsig->param_count; ++i) {
+               if (MONO_TYPE_ISSTRUCT (callsig->params [i]) || callsig->params [i]->byref)
+                       need_direct_wrapper = TRUE;
+       }
+#endif
+
        /*
         * We try to share runtime invoke wrappers between different methods but have to
         * be careful about methods whose klass has a type cctor, since putting the wrapper
@@ -4897,11 +4194,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                                need_direct_wrapper = TRUE;
                        */
 
-                       /*
-                        * Can't put these wrappers into object, since they reference non-corlib
-                        * metadata (callsig).
-                        */
-                       target_klass = method->klass;
+                       target_klass = get_wrapper_target_class (method->klass->image);
                }
        }
 
@@ -4926,7 +4219,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
 
                // FIXME: When to free callsig ?
        }
-       
+
        /* to make it work with our special string constructors */
        if (!string_dummy) {
                MONO_GC_REGISTER_ROOT (string_dummy);
@@ -4946,7 +4239,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
        csig->params [2] = &mono_defaults.int_class->byval_arg;
        csig->params [3] = &mono_defaults.int_class->byval_arg;
 
-       name = mono_signature_to_name (callsig, "runtime_invoke");
+       name = mono_signature_to_name (callsig, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
        mb = mono_mb_new (target_klass, name,  MONO_WRAPPER_RUNTIME_INVOKE);
        g_free (name);
 
@@ -4965,6 +4258,11 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
 
        emit_thread_force_interrupt_checkpoint (mb);
 
+       if (virtual) {
+               g_assert (sig->hasthis);
+               g_assert (method->flags & METHOD_ATTRIBUTE_VIRTUAL);
+       }
+
        if (sig->hasthis) {
                if (method->string_ctor) {
                        mono_mb_emit_ptr (mb, string_dummy);
@@ -4973,6 +4271,8 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                }
        }
 
+       tmp_nullable_locals = g_new0 (int, sig->param_count);
+
        for (i = 0; i < sig->param_count; i++) {
                MonoType *t = sig->params [i];
                int type;
@@ -4985,6 +4285,16 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
 
                if (t->byref) {
                        mono_mb_emit_byte (mb, CEE_LDIND_I);
+                       /* A Nullable<T> type don't have a boxed form, it's either null or a boxed T.
+                        * So to make this work we unbox it to a local variablee and push a reference to that.
+                        */
+                       if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+                               tmp_nullable_locals [i] = mono_mb_add_local (mb, &mono_class_from_mono_type (t)->byval_arg);
+
+                               mono_mb_emit_op (mb, CEE_UNBOX_ANY, mono_class_from_mono_type (t));
+                               mono_mb_emit_stloc (mb, tmp_nullable_locals [i]);
+                               mono_mb_emit_ldloc_addr (mb, tmp_nullable_locals [i]);
+                       }
                        continue;
                }
 
@@ -5018,14 +4328,14 @@ handle_enum:
                        break;
                case MONO_TYPE_GENERICINST:
                        if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
-                               mono_mb_emit_byte (mb, CEE_LDIND_I);
+                               mono_mb_emit_byte (mb, mono_type_to_ldind (sig->params [i]));
                                break;
                        }
 
                        /* fall through */
                case MONO_TYPE_VALUETYPE:
                        if (type == MONO_TYPE_VALUETYPE && t->data.klass->enumtype) {
-                               type = t->data.klass->enum_basetype->type;
+                               type = mono_class_enum_basetype (t->data.klass)->type;
                                goto handle_enum;
                        }
                        mono_mb_emit_byte (mb, CEE_LDIND_I);
@@ -5042,7 +4352,9 @@ handle_enum:
                }               
        }
        
-       if (need_direct_wrapper) {
+       if (virtual) {
+               mono_mb_emit_op (mb, CEE_CALLVIRT, method);
+       } else if (need_direct_wrapper) {
                mono_mb_emit_op (mb, CEE_CALL, method);
        } else {
                mono_mb_emit_ldarg (mb, 3);
@@ -5054,7 +4366,6 @@ handle_enum:
                g_assert_not_reached ();
        }
 
-
        switch (sig->ret->type) {
        case MONO_TYPE_VOID:
                if (!method->string_ctor)
@@ -5088,17 +4399,38 @@ handle_enum:
                /* nothing to do */
                break;
        case MONO_TYPE_PTR:
+               /* The result is an IntPtr */
+               mono_mb_emit_op (mb, CEE_BOX, mono_defaults.int_class);
+               break;
        default:
                g_assert_not_reached ();
        }
 
        mono_mb_emit_stloc (mb, 0);
+
+       /* Convert back nullable-byref arguments */
+       for (i = 0; i < sig->param_count; i++) {
+               MonoType *t = sig->params [i];
+
+               /* 
+                * Box the result and put it back into the array, the caller will have
+                * to obtain it from there.
+                */
+               if (t->byref && t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+                       mono_mb_emit_ldarg (mb, 1);                     
+                       mono_mb_emit_icon (mb, sizeof (gpointer) * i);
+                       mono_mb_emit_byte (mb, CEE_ADD);
+
+                       mono_mb_emit_ldloc (mb, tmp_nullable_locals [i]);
+                       mono_mb_emit_op (mb, CEE_BOX, mono_class_from_mono_type (t));
+
+                       mono_mb_emit_byte (mb, CEE_STIND_REF);
+               }
+       }
                        
        pos = mono_mb_emit_branch (mb, CEE_LEAVE);
 
-       mono_loader_lock ();
        clause = mono_image_alloc0 (target_klass->image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        clause->flags = MONO_EXCEPTION_CLAUSE_FILTER;
        clause->try_len = mono_mb_get_label (mb);
 
@@ -5126,25 +4458,24 @@ handle_enum:
        mono_mb_emit_byte (mb, CEE_LDNULL);
        mono_mb_emit_stloc (mb, 0);
 
-       /* Check for the abort exception */
+       /* Check and reset abort exception */
+       /* Done in a separate function to reduce code size */
        mono_mb_emit_ldloc (mb, 1);
-       mono_mb_emit_op (mb, CEE_ISINST, mono_defaults.threadabortexception_class);
-       posna = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
+       mono_mb_emit_icall (mb, runtime_invoke_reset_abort);
 
-       /* Delay the abort exception */
-       mono_mb_emit_icall (mb, ves_icall_System_Threading_Thread_ResetAbort);
-
-       mono_mb_patch_short_branch (mb, posna);
        mono_mb_emit_branch (mb, CEE_LEAVE);
 
        clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        /* return result */
        mono_mb_patch_branch (mb, pos);
        mono_mb_emit_ldloc (mb, 0);
        mono_mb_emit_byte (mb, CEE_RET);
 
        if (need_direct_wrapper) {
+               mb->skip_visibility = 1;
                res = mono_mb_create_and_cache (cache, method, mb, csig, sig->param_count + 16);
        } else {
                /* taken from mono_mb_create_and_cache */
@@ -5175,67 +4506,128 @@ handle_enum:
 
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;     
 }
 
 /*
- * mono_marshal_get_static_rgctx_invoke:
- * @method: a method
+ * mono_marshal_get_runtime_invoke_dynamic:
  *
- * Generates a wrapper for calling a generic shared method, either
- * static or generic.  We need this for virtual generic method lookup
- * and ldftn when we do generic code sharing.  Instead of producing
- * the address of the method we produce the address of a wrapper for
- * the method because the wrapper passes the (method) runtime generic
- * context argument which calli cannot do.
+ *   Return a method which can be used to invoke managed methods from native code
+ * dynamically.
+ * The signature of the returned method is given by RuntimeInvokeDynamicFunction:
+ * void runtime_invoke (void *args, MonoObject **exc, void *compiled_method)
+ * ARGS should point to an architecture specific structure containing 
+ * the arguments and space for the return value.
+ * The other arguments are the same as for runtime_invoke (), except that
+ * ARGS should contain the this argument too.
+ * This wrapper serves the same purpose as the runtime-invoke wrappers, but there
+ * is only one copy of it, which is useful in full-aot.
  */
-MonoMethod *
-mono_marshal_get_static_rgctx_invoke (MonoMethod *method)
+MonoMethod*
+mono_marshal_get_runtime_invoke_dynamic (void)
 {
-       static gboolean inited = FALSE;
-       static int num_wrappers = 0;
-
+       static MonoMethod *method;
+       MonoMethodSignature *csig;
+       MonoExceptionClause *clause;
        MonoMethodBuilder *mb;
-       MonoMethod *res;
-       MonoClass *target_klass = method->klass;
-       MonoMethodSignature *sig = mono_method_signature (method);
-       int i;
+       int pos, posna;
        char *name;
-       GHashTable *cache;
-       MonoImage *image = method->klass->image;
 
-       cache = get_cache (&image->static_rgctx_invoke_cache, mono_aligned_addr_hash, NULL);
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
+       if (method)
+               return method;
 
-       if (!inited) {
-               mono_counters_register ("static rgctx invoke wrappers",
-                               MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_wrappers);
-               inited = TRUE;
-       }
-       ++num_wrappers;
+       csig = mono_metadata_signature_alloc (mono_defaults.corlib, 4);
+
+       csig->ret = &mono_defaults.void_class->byval_arg;
+       csig->params [0] = &mono_defaults.int_class->byval_arg;
+       csig->params [1] = &mono_defaults.int_class->byval_arg;
+       csig->params [2] = &mono_defaults.int_class->byval_arg;
+       csig->params [3] = &mono_defaults.int_class->byval_arg;
 
-       name = mono_signature_to_name (mono_method_signature (method), "static_rgctx_invoke");
-       mb = mono_mb_new (target_klass, name, MONO_WRAPPER_STATIC_RGCTX_INVOKE);
+       name = g_strdup ("runtime_invoke_dynamic");
+       mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_RUNTIME_INVOKE);
        g_free (name);
 
-       for (i = 0; i < sig->param_count + sig->hasthis; i++)
-               mono_mb_emit_ldarg (mb, i);
-       mono_mb_emit_op (mb, CEE_CALL, method);
+       /* allocate local 0 (object) tmp */
+       mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
+       /* allocate local 1 (object) exc */
+       mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
+
+       /* cond set *exc to null */
+       mono_mb_emit_byte (mb, CEE_LDARG_1);
+       mono_mb_emit_byte (mb, CEE_BRFALSE_S);
+       mono_mb_emit_byte (mb, 3);      
+       mono_mb_emit_byte (mb, CEE_LDARG_1);
+       mono_mb_emit_byte (mb, CEE_LDNULL);
+       mono_mb_emit_byte (mb, CEE_STIND_REF);
+
+       emit_thread_force_interrupt_checkpoint (mb);
+
+       mono_mb_emit_byte (mb, CEE_LDARG_0);
+       mono_mb_emit_byte (mb, CEE_LDARG_2);
+       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+       mono_mb_emit_byte (mb, CEE_MONO_DYN_CALL);
+
+       pos = mono_mb_emit_branch (mb, CEE_LEAVE);
+
+       clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
+       clause->flags = MONO_EXCEPTION_CLAUSE_FILTER;
+       clause->try_len = mono_mb_get_label (mb);
+
+       /* filter code */
+       clause->data.filter_offset = mono_mb_get_label (mb);
+       
+       mono_mb_emit_byte (mb, CEE_POP);
+       mono_mb_emit_byte (mb, CEE_LDARG_1);
+       mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+       mono_mb_emit_byte (mb, CEE_PREFIX1);
+       mono_mb_emit_byte (mb, CEE_CGT_UN);
+       mono_mb_emit_byte (mb, CEE_PREFIX1);
+       mono_mb_emit_byte (mb, CEE_ENDFILTER);
+
+       clause->handler_offset = mono_mb_get_label (mb);
+
+       /* handler code */
+       /* store exception */
+       mono_mb_emit_stloc (mb, 1);
+       
+       mono_mb_emit_byte (mb, CEE_LDARG_1);
+       mono_mb_emit_ldloc (mb, 1);
+       mono_mb_emit_byte (mb, CEE_STIND_REF);
+
+       mono_mb_emit_byte (mb, CEE_LDNULL);
+       mono_mb_emit_stloc (mb, 0);
+
+       /* Check for the abort exception */
+       mono_mb_emit_ldloc (mb, 1);
+       mono_mb_emit_op (mb, CEE_ISINST, mono_defaults.threadabortexception_class);
+       posna = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
+
+       /* Delay the abort exception */
+       mono_mb_emit_icall (mb, ves_icall_System_Threading_Thread_ResetAbort);
+
+       mono_mb_patch_short_branch (mb, posna);
+       mono_mb_emit_branch (mb, CEE_LEAVE);
+
+       clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
+
+       mono_mb_set_clauses (mb, 1, clause);
+
+       /* return result */
+       mono_mb_patch_branch (mb, pos);
+       //mono_mb_emit_ldloc (mb, 0);
        mono_mb_emit_byte (mb, CEE_RET);
 
-       res = mono_mb_create_and_cache (cache, method, mb, mono_method_signature (method),
-               sig->param_count + sig->hasthis + 4);
-       res->skip_visibility = TRUE;
-       res->flags = method->flags;
+       mono_loader_lock ();
+       /* double-checked locking */
+       if (!method) {
+               method = mono_mb_create_method (mb, csig, 16);
+       }
+       mono_loader_unlock ();
 
        mono_mb_free (mb);
 
-       return res;
+       return method;
 }
 
 static void
@@ -5834,51 +5226,6 @@ mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gcon
        return res;
 }
 
-typedef struct {
-       MonoMethodBuilder *mb;
-       MonoMethodSignature *sig;
-       MonoMethodPInvoke *piinfo;
-       int *orig_conv_args; /* Locals containing the original values of byref args */
-       int retobj_var;
-       MonoClass *retobj_class;
-       MonoMethodSignature *csig; /* Might need to be changed due to MarshalAs directives */
-       MonoImage *image; /* The image to use for looking up custom marshallers */
-} EmitMarshalContext;
-
-typedef enum {
-       /*
-        * This is invoked to convert arguments from the current types to
-        * the underlying types expected by the platform routine.  If required,
-        * the methods create a temporary variable with the proper type, and return
-        * the location for it (either the passed argument, or the newly allocated
-        * local slot).
-        */
-       MARSHAL_ACTION_CONV_IN,
-
-       /*
-        * This operation is called to push the actual value that was optionally
-        * converted on the first stage
-        */
-       MARSHAL_ACTION_PUSH,
-
-       /*
-        * Convert byref arguments back or free resources allocated during the
-        * CONV_IN stage
-        */
-       MARSHAL_ACTION_CONV_OUT,
-
-       /*
-        * The result from the unmanaged call is at the top of the stack when
-        * this action is invoked.    The result should be stored in the
-        * third local variable slot. 
-        */
-       MARSHAL_ACTION_CONV_RESULT,
-
-       MARSHAL_ACTION_MANAGED_CONV_IN,
-       MARSHAL_ACTION_MANAGED_CONV_OUT,
-       MARSHAL_ACTION_MANAGED_CONV_RESULT
-} MarshalAction;
-
 static int
 emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
                                         MonoMarshalSpec *spec, 
@@ -6029,7 +5376,7 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
                        mono_mb_emit_ldloc (mb, conv_arg);
                        mono_mb_emit_op (mb, CEE_CALLVIRT, marshal_native_to_managed);
                        mono_mb_emit_byte (mb, CEE_STIND_REF);
-               } else {
+               } else if (t->attrs & PARAM_ATTRIBUTE_OUT) {
                        mono_mb_emit_ldstr (mb, g_strdup (spec->data.custom_data.cookie));
 
                        mono_mb_emit_op (mb, CEE_CALL, get_instance);
@@ -6231,17 +5578,38 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                                        MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
-       MonoClass *klass;
+       MonoClass *klass, *date_time_class;
        int pos = 0, pos2;
 
        klass = mono_class_from_mono_type (t);
 
+       date_time_class = mono_class_from_name_cached (mono_defaults.corlib, "System", "DateTime");
+
        switch (action) {
        case MARSHAL_ACTION_CONV_IN:
                if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) ||
                        klass->blittable || klass->enumtype)
                        break;
 
+               if (klass == date_time_class) {
+                       /* Convert it to an OLE DATE type */
+                       static MonoMethod *to_oadate;
+
+                       if (!to_oadate)
+                               to_oadate = mono_class_get_method_from_name (date_time_class, "ToOADate", 0);
+                       g_assert (to_oadate);
+
+                       if (t->byref)
+                               g_assert_not_reached ();
+
+                       conv_arg = mono_mb_add_local (mb, &mono_defaults.double_class->byval_arg);
+
+                       mono_mb_emit_ldarg_addr (mb, argnum);
+                       mono_mb_emit_managed_call (mb, to_oadate, NULL);
+                       mono_mb_emit_stloc (mb, conv_arg);
+                       break;
+               }
+
                conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
                        
                /* store the address of the source into local variable 0 */
@@ -6293,6 +5661,11 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                        break;
                }
 
+               if (klass == date_time_class) {
+                       mono_mb_emit_ldloc (mb, conv_arg);
+                       break;
+               }
+
                if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) ||
                        klass->blittable || klass->enumtype) {
                        mono_mb_emit_ldarg (mb, argnum);
@@ -6340,6 +5713,7 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                        mono_mb_emit_stloc (mb, 3);
                        break;
                }
+
                /* load pointer to returned value type */
                mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
                mono_mb_emit_byte (mb, CEE_MONO_VTADDR);
@@ -6476,7 +5850,7 @@ emit_marshal_string (EmitMarshalContext *m, int argnum, MonoType *t,
                if (conv == -1) {
                        char *msg = g_strdup_printf ("string marshalling conversion %d not implemented", encoding);
                        MonoException *exc = mono_get_exception_not_implemented (msg);
-                       g_warning (msg);
+                       g_warning ("%s", msg);
                        g_free (msg);
                        mono_raise_exception (exc);
                }
@@ -6815,17 +6189,19 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
        MonoClass *klass = mono_class_from_mono_type (t);
        int pos, pos2, loc;
 
-       if (mono_class_from_mono_type (t) == mono_defaults.object_class) {
-               mono_raise_exception (mono_get_exception_not_implemented ("Marshalling of type object is not implemented"));
-       }
-
        switch (action) {
        case MARSHAL_ACTION_CONV_IN:
                *conv_arg_type = &mono_defaults.int_class->byval_arg;
                conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
 
                m->orig_conv_args [argnum] = 0;
-               
+
+               if (mono_class_from_mono_type (t) == mono_defaults.object_class) {
+                       char *msg = g_strdup_printf ("Marshalling of type object is not implemented");
+                       mono_mb_emit_exception_marshal_directive (mb, msg);
+                       break;
+               }
+
                if (klass->delegate) {
                        if (t->byref) {
                                if (!(t->attrs & PARAM_ATTRIBUTE_OUT)) {
@@ -6843,7 +6219,14 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                        MonoMarshalNative encoding = mono_marshal_get_string_encoding (m->piinfo, spec);
                        MonoMarshalConv conv = mono_marshal_get_stringbuilder_to_ptr_conv (m->piinfo, spec);
                        
-                       g_assert (!t->byref);
+                       if (t->byref) {
+                               if (!(t->attrs & PARAM_ATTRIBUTE_OUT)) {
+                                       char *msg = g_strdup_printf ("Byref marshalling of stringbuilders is not implemented.");
+                                       mono_mb_emit_exception_marshal_directive (mb, msg);
+                               }
+                               break;
+                       }
+
                        mono_mb_emit_ldarg (mb, argnum);
 
                        if (conv != -1)
@@ -6851,7 +6234,7 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                        else {
                                char *msg = g_strdup_printf ("stringbuilder marshalling conversion %d not implemented", encoding);
                                MonoException *exc = mono_get_exception_not_implemented (msg);
-                               g_warning (msg);
+                               g_warning ("%s", msg);
                                g_free (msg);
                                mono_raise_exception (exc);
                        }
@@ -6931,13 +6314,34 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                        encoding = mono_marshal_get_string_encoding (m->piinfo, spec);
                        conv = mono_marshal_get_ptr_to_stringbuilder_conv (m->piinfo, spec, &need_free);
 
-                       g_assert (!t->byref);
                        g_assert (encoding != -1);
 
-                       mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_ldloc (mb, conv_arg);
+                       if (t->byref) {
+                               g_assert ((t->attrs & PARAM_ATTRIBUTE_OUT));
 
-                       mono_mb_emit_icall (mb, conv_to_icall (conv));
+                               need_free = TRUE;
+
+                               mono_mb_emit_ldarg (mb, argnum);
+                               mono_mb_emit_ldloc (mb, conv_arg);
+
+                               switch (encoding) {
+                               case MONO_NATIVE_LPWSTR:
+                                       mono_mb_emit_icall (mb, mono_string_utf16_to_builder2);
+                                       break;
+                               case MONO_NATIVE_LPSTR:
+                                       mono_mb_emit_icall (mb, mono_string_utf8_to_builder2);
+                                       break;
+                               default:
+                                       g_assert_not_reached ();
+                               }
+
+                               mono_mb_emit_byte (mb, CEE_STIND_REF);
+                       } else {
+                               mono_mb_emit_ldarg (mb, argnum);
+                               mono_mb_emit_ldloc (mb, conv_arg);
+
+                               mono_mb_emit_icall (mb, conv_to_icall (conv));
+                       }
 
                        if (need_free) {
                                mono_mb_emit_ldloc (mb, conv_arg);
@@ -7254,129 +6658,66 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
        return conv_arg;
 }
 
-#ifndef DISABLE_COM
-
 static int
-emit_marshal_com_interface (EmitMarshalContext *m, int argnum, MonoType *t,
+emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
                     MonoMarshalSpec *spec, 
                     int conv_arg, MonoType **conv_arg_type, 
                     MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
-       MonoClass *klass = t->data.klass;
-       static MonoMethod* get_object_for_iunknown = NULL;
-       static MonoMethod* get_iunknown_for_object_internal = NULL;
-       static MonoMethod* get_com_interface_for_object_internal = NULL;
-       static MonoMethod* get_idispatch_for_object_internal = NULL;
-       static MonoMethod* marshal_release = NULL;
-       static MonoMethod* AddRef = NULL;
-       if (!get_object_for_iunknown)
-               get_object_for_iunknown = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForIUnknown", 1);
-       if (!get_iunknown_for_object_internal)
-               get_iunknown_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIUnknownForObjectInternal", 1);
-       if (!get_idispatch_for_object_internal)
-               get_idispatch_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIDispatchForObjectInternal", 1);
-       if (!get_com_interface_for_object_internal)
-               get_com_interface_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetComInterfaceForObjectInternal", 2);
-       if (!marshal_release)
-               marshal_release = mono_class_get_method_from_name (mono_defaults.marshal_class, "Release", 1);
-
-       /* COM types are initialized lazily */
+       static MonoMethod *get_object_for_native_variant = NULL;
+       static MonoMethod *get_native_variant_for_object = NULL;
+
        mono_init_com_types ();
+       
+       if (!get_object_for_native_variant)
+               get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
+       g_assert (get_object_for_native_variant);
+
+       if (!get_native_variant_for_object)
+               get_native_variant_for_object = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetNativeVariantForObject", 2);
+       g_assert (get_native_variant_for_object);
 
        switch (action) {
        case MARSHAL_ACTION_CONV_IN: {
-               guint32 pos_null = 0;
-
-               *conv_arg_type = &mono_defaults.int_class->byval_arg;
-               conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
-               mono_mb_emit_ptr (mb, NULL);
-               mono_mb_emit_stloc (mb, conv_arg);      
+               conv_arg = mono_mb_add_local (mb, &mono_defaults.variant_class->byval_arg);
+               
+               if (t->byref)
+                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
+               else
+                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
 
-               /* we dont need any conversions for out parameters */
-               if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT)
+               if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
                        break;
 
-               mono_mb_emit_ldarg (mb, argnum);        
-               if (t->byref)
-                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               /* if null just break, conv arg was already inited to 0 */
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
                mono_mb_emit_ldarg (mb, argnum);
                if (t->byref)
-                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-               if (klass && klass != mono_defaults.object_class) {
-                       mono_mb_emit_ptr (mb, t);
-                       mono_mb_emit_icall (mb, type_from_handle);
-                       mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
-               }
-               else if (spec->native == MONO_NATIVE_IUNKNOWN)
-                       mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-               else if (spec->native == MONO_NATIVE_IDISPATCH)
-                       mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
-               else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
-                       mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-               else
-                       g_assert_not_reached ();
-               mono_mb_emit_stloc (mb, conv_arg);
-               mono_mb_patch_short_branch (mb, pos_null);
+                       mono_mb_emit_byte(mb, CEE_LDIND_REF);
+               mono_mb_emit_ldloc_addr (mb, conv_arg);
+               mono_mb_emit_managed_call (mb, get_native_variant_for_object, NULL);
                break;
        }
 
        case MARSHAL_ACTION_CONV_OUT: {
-               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
-                       int ccw_obj;
-                       guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
-                       ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
-                       mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_byte (mb, CEE_LDNULL);
-                       mono_mb_emit_byte (mb, CEE_STIND_REF);
-
-                       mono_mb_emit_ldloc (mb, conv_arg);
-                       pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-                       mono_mb_emit_ldloc (mb, conv_arg);
-                       mono_mb_emit_icon (mb, TRUE);
-                       mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-                       mono_mb_emit_stloc (mb, ccw_obj);
-                       mono_mb_emit_ldloc (mb, ccw_obj);
-                       pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-                       mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_ldloc (mb, conv_arg);
-                       mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
+               static MonoMethod *variant_clear = NULL;
 
-                       if (klass && klass != mono_defaults.object_class)
-                               mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-                       mono_mb_emit_byte (mb, CEE_STIND_REF);
+               if (!variant_clear)
+                       variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
+               g_assert (variant_clear);
 
-                       pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
 
-                       /* is already managed object */
-                       mono_mb_patch_short_branch (mb, pos_ccw);
+               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT || !(t->attrs & PARAM_ATTRIBUTE_IN))) {
                        mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_ldloc (mb, ccw_obj);
-
-                       if (klass && klass != mono_defaults.object_class)
-                               mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
+                       mono_mb_emit_ldloc_addr (mb, conv_arg);
+                       mono_mb_emit_managed_call (mb, get_object_for_native_variant, NULL);
                        mono_mb_emit_byte (mb, CEE_STIND_REF);
-
-                       mono_mb_patch_short_branch (mb, pos_end);
-
-                       /* need to call Release to follow COM rules of ownership */
-                       mono_mb_emit_ldloc (mb, conv_arg);
-                       mono_mb_emit_managed_call (mb, marshal_release, NULL);
-                       mono_mb_emit_byte (mb, CEE_POP);
-
-                       /* case if null */
-                       mono_mb_patch_short_branch (mb, pos_null);
                }
+
+               mono_mb_emit_ldloc_addr (mb, conv_arg);
+               mono_mb_emit_managed_call (mb, variant_clear, NULL);
                break;
        }
+
        case MARSHAL_ACTION_PUSH:
                if (t->byref)
                        mono_mb_emit_ldloc_addr (mb, conv_arg);
@@ -7385,276 +6726,20 @@ emit_marshal_com_interface (EmitMarshalContext *m, int argnum, MonoType *t,
                break;
 
        case MARSHAL_ACTION_CONV_RESULT: {
-               int ccw_obj, ret_ptr;
-               guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
-               ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-               ret_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
-               /* store return value */
-               mono_mb_emit_stloc (mb, ret_ptr);
-
-               mono_mb_emit_ldloc (mb, ret_ptr);
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               mono_mb_emit_ldloc (mb, ret_ptr);
-               mono_mb_emit_icon (mb, TRUE);
-               mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-               mono_mb_emit_stloc (mb, ccw_obj);
-               mono_mb_emit_ldloc (mb, ccw_obj);
-               pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-               mono_mb_emit_ldloc (mb, ret_ptr);
-               mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
-
-               if (klass && klass != mono_defaults.object_class)
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               mono_mb_emit_stloc (mb, 3);
-
-               pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
-               /* is already managed object */
-               mono_mb_patch_short_branch (mb, pos_ccw);
-               mono_mb_emit_ldloc (mb, ccw_obj);
-
-               if (klass && klass != mono_defaults.object_class)
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               mono_mb_emit_stloc (mb, 3);
-
-               mono_mb_patch_short_branch (mb, pos_end);
-
-               /* need to call Release to follow COM rules of ownership */
-               mono_mb_emit_ldloc (mb, ret_ptr);
-               mono_mb_emit_managed_call (mb, marshal_release, NULL);
-               mono_mb_emit_byte (mb, CEE_POP);
-
-               /* case if null */
-               mono_mb_patch_short_branch (mb, pos_null);
+               char *msg = g_strdup ("Marshalling of VARIANT not supported as a return type.");
+               mono_mb_emit_exception_marshal_directive (mb, msg);
                break;
-       } 
+       }
 
        case MARSHAL_ACTION_MANAGED_CONV_IN: {
-               int ccw_obj;
-               guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
-               ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
+               conv_arg = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
-               klass = mono_class_from_mono_type (t);
-               conv_arg = mono_mb_add_local (mb, &klass->byval_arg);
-               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               if (t->byref)
+                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
+               else
+                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
 
-               mono_mb_emit_byte (mb, CEE_LDNULL);
-               mono_mb_emit_stloc (mb, conv_arg);
-               if (t->attrs & PARAM_ATTRIBUTE_OUT)
-                       break;
-
-               mono_mb_emit_ldarg (mb, argnum);
-               if (t->byref)
-                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               mono_mb_emit_ldarg (mb, argnum);
-               if (t->byref)
-                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_icon (mb, TRUE);
-               mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-               mono_mb_emit_stloc (mb, ccw_obj);
-               mono_mb_emit_ldloc (mb, ccw_obj);
-               pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-
-               mono_mb_emit_ldarg (mb, argnum);
-               if (t->byref)
-                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
-
-               if (klass && klass != mono_defaults.object_class)
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               mono_mb_emit_stloc (mb, conv_arg);
-               pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
-               /* is already managed object */
-               mono_mb_patch_short_branch (mb, pos_ccw);
-               mono_mb_emit_ldloc (mb, ccw_obj);
-               if (klass && klass != mono_defaults.object_class)
-                       mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
-               mono_mb_emit_stloc (mb, conv_arg);
-
-               mono_mb_patch_short_branch (mb, pos_end);
-               /* case if null */
-               mono_mb_patch_short_branch (mb, pos_null);
-               break;
-       }
-
-       case MARSHAL_ACTION_MANAGED_CONV_OUT: {
-               if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT) {
-                       guint32 pos_null = 0;
-
-                       if (!AddRef)
-                               AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
-
-                       mono_mb_emit_ldloc (mb, conv_arg);      
-                       /* if null just break, conv arg was already inited to 0 */
-                       pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-                       /* to store later */
-                       mono_mb_emit_ldarg (mb, argnum);        
-                       mono_mb_emit_ldloc (mb, conv_arg);
-                       if (klass && klass != mono_defaults.object_class) {
-                               mono_mb_emit_ptr (mb, t);
-                               mono_mb_emit_icall (mb, type_from_handle);
-                               mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
-                       }
-                       else if (spec->native == MONO_NATIVE_IUNKNOWN)
-                               mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-                       else if (spec->native == MONO_NATIVE_IDISPATCH)
-                               mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
-                       else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
-                               mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-                       else
-                               g_assert_not_reached ();
-                       mono_mb_emit_byte (mb, CEE_STIND_I);
-
-                       mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_byte (mb, CEE_LDIND_I);
-                       mono_mb_emit_managed_call (mb, AddRef, NULL);
-                       mono_mb_emit_byte (mb, CEE_POP);
-
-                       mono_mb_patch_short_branch (mb, pos_null);
-               }
-               break;
-       }
-
-       case MARSHAL_ACTION_MANAGED_CONV_RESULT: {
-               guint32 pos_null = 0;
-               int ccw_obj;
-               ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
-               if (!AddRef)
-                       AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
-
-               /* store return value */
-               mono_mb_emit_stloc (mb, ccw_obj);
-
-               mono_mb_emit_ldloc (mb, ccw_obj);
-
-               /* if null just break, conv arg was already inited to 0 */
-               pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
-               /* to store later */
-               mono_mb_emit_ldloc (mb, ccw_obj);
-               if (klass && klass != mono_defaults.object_class) {
-                       mono_mb_emit_ptr (mb, t);
-                       mono_mb_emit_icall (mb, type_from_handle);
-                       mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
-               }
-               else if (spec->native == MONO_NATIVE_IUNKNOWN)
-                       mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-               else if (spec->native == MONO_NATIVE_IDISPATCH)
-                       mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
-               else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
-                       mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
-               else
-                       g_assert_not_reached ();
-               mono_mb_emit_stloc (mb, 3);
-               mono_mb_emit_ldloc (mb, 3);
-               
-               mono_mb_emit_managed_call (mb, AddRef, NULL);
-               mono_mb_emit_byte (mb, CEE_POP);
-
-               mono_mb_patch_short_branch (mb, pos_null);
-               break;
-       }
-
-       default:
-               g_assert_not_reached ();
-       }
-
-       return conv_arg;
-}
-
-#endif /* DISABLE_COM */
-
-static int
-emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
-                    MonoMarshalSpec *spec, 
-                    int conv_arg, MonoType **conv_arg_type, 
-                    MarshalAction action)
-{
-       MonoMethodBuilder *mb = m->mb;
-       static MonoMethod *get_object_for_native_variant = NULL;
-       static MonoMethod *get_native_variant_for_object = NULL;
-
-       mono_init_com_types ();
-       
-       if (!get_object_for_native_variant)
-               get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
-       g_assert (get_object_for_native_variant);
-
-       if (!get_native_variant_for_object)
-               get_native_variant_for_object = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetNativeVariantForObject", 2);
-       g_assert (get_native_variant_for_object);
-
-       switch (action) {
-       case MARSHAL_ACTION_CONV_IN: {
-               conv_arg = mono_mb_add_local (mb, &mono_defaults.variant_class->byval_arg);
-               
-               if (t->byref)
-                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
-               else
-                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
-
-               if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
-                       break;
-
-               mono_mb_emit_ldarg (mb, argnum);
-               if (t->byref)
-                       mono_mb_emit_byte(mb, CEE_LDIND_REF);
-               mono_mb_emit_ldloc_addr (mb, conv_arg);
-               mono_mb_emit_managed_call (mb, get_native_variant_for_object, NULL);
-               break;
-       }
-
-       case MARSHAL_ACTION_CONV_OUT: {
-               static MonoMethod *variant_clear = NULL;
-
-               if (!variant_clear)
-                       variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
-               g_assert (variant_clear);
-
-
-               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT || !(t->attrs & PARAM_ATTRIBUTE_IN))) {
-                       mono_mb_emit_ldarg (mb, argnum);
-                       mono_mb_emit_ldloc_addr (mb, conv_arg);
-                       mono_mb_emit_managed_call (mb, get_object_for_native_variant, NULL);
-                       mono_mb_emit_byte (mb, CEE_STIND_REF);
-               }
-
-               mono_mb_emit_ldloc_addr (mb, conv_arg);
-               mono_mb_emit_managed_call (mb, variant_clear, NULL);
-               break;
-       }
-
-       case MARSHAL_ACTION_PUSH:
-               if (t->byref)
-                       mono_mb_emit_ldloc_addr (mb, conv_arg);
-               else
-                       mono_mb_emit_ldloc (mb, conv_arg);
-               break;
-
-       case MARSHAL_ACTION_CONV_RESULT: {
-               char *msg = g_strdup ("Marshalling of VARIANT not supported as a return type.");
-               mono_mb_emit_exception_marshal_directive (mb, msg);
-               break;
-       }
-
-       case MARSHAL_ACTION_MANAGED_CONV_IN: {
-               conv_arg = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
-               if (t->byref)
-                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
-               else
-                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
-
-               if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
+               if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
                        break;
 
                if (t->byref)
@@ -7750,7 +6835,7 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                                if (conv == -1) {
                                        char *msg = g_strdup_printf ("string/stringbuilder marshalling conversion %d not implemented", encoding);
                                        MonoException *exc = mono_get_exception_not_implemented (msg);
-                                       g_warning (msg);
+                                       g_warning ("%s", msg);
                                        g_free (msg);
                                        mono_raise_exception (exc);
                                }
@@ -7946,6 +7031,15 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                        mono_mb_patch_branch (mb, label1);
                        mono_mb_patch_branch (mb, label3);
                }
+               
+               if (klass->element_class->blittable) {
+                       /* free memory allocated (if any) by MONO_MARSHAL_CONV_ARRAY_LPARRAY */
+
+                       mono_mb_emit_ldarg (mb, argnum);
+                       mono_mb_emit_ldloc (mb, conv_arg);
+                       mono_mb_emit_icall (mb, conv_to_icall (MONO_MARSHAL_FREE_LPARRAY));
+               }
+
                break;
 
        case MARSHAL_ACTION_PUSH:
@@ -8070,15 +7164,15 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                 * from 0.
                 */
 
-               if (param_num == -1)
+               if (param_num == -1) {
                        mono_mb_emit_icon (mb, num_elem);
-               else {
-                       /* FIXME: Add the two together */
+               } else {
                        mono_mb_emit_ldarg (mb, param_num);
                        if (num_elem > 0) {
                                mono_mb_emit_icon (mb, num_elem);
                                mono_mb_emit_byte (mb, CEE_ADD);
                        }
+                       mono_mb_emit_byte (mb, CEE_CONV_OVF_I);
                }
 
                mono_mb_emit_op (mb, CEE_NEWARR, eklass);
@@ -8370,9 +7464,9 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
        switch (action) {
        case MARSHAL_ACTION_CONV_IN: {
                MonoType *local_type;
-               int variant_bool = 0;
-               if (!t->byref)
-                       break;
+               int label_false;
+               guint8 ldc_op = CEE_LDC_I4_1;
+
                if (spec == NULL) {
                        local_type = &mono_defaults.int32_class->byval_arg;
                } else {
@@ -8383,7 +7477,10 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                                break;
                        case MONO_NATIVE_VARIANTBOOL:
                                local_type = &mono_defaults.int16_class->byval_arg;
-                               variant_bool = 1;
+                               ldc_op = CEE_LDC_I4_M1;
+                               break;
+                       case MONO_NATIVE_BOOLEAN:
+                               local_type = &mono_defaults.int32_class->byval_arg;
                                break;
                        default:
                                g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
@@ -8391,29 +7488,49 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                                break;
                        }
                }
-               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               if (t->byref)
+                       *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               else
+                       *conv_arg_type = local_type;
                conv_arg = mono_mb_add_local (mb, local_type);
+               
                mono_mb_emit_ldarg (mb, argnum);
-               mono_mb_emit_byte (mb, CEE_LDIND_I1);
-               if (variant_bool)
-                       mono_mb_emit_byte (mb, CEE_NEG);
+               if (t->byref)
+                       mono_mb_emit_byte (mb, CEE_LDIND_I1);
+               label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+               mono_mb_emit_byte (mb, ldc_op);
                mono_mb_emit_stloc (mb, conv_arg);
+               mono_mb_patch_branch (mb, label_false);
+
                break;
        }
 
        case MARSHAL_ACTION_CONV_OUT:
+       {
+               int label_false, label_end;
                if (!t->byref)
                        break;
+
                mono_mb_emit_ldarg (mb, argnum);
                mono_mb_emit_ldloc (mb, conv_arg);
-               if (spec != NULL && spec->native == MONO_NATIVE_VARIANTBOOL)
-                       mono_mb_emit_byte (mb, CEE_NEG);
+               
+               label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+               mono_mb_emit_byte (mb, CEE_LDC_I4_1);
+
+               label_end = mono_mb_emit_branch (mb, CEE_BR);
+               mono_mb_patch_branch (mb, label_false);
+               mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+               mono_mb_patch_branch (mb, label_end);
+
                mono_mb_emit_byte (mb, CEE_STIND_I1);
                break;
+       }
 
        case MARSHAL_ACTION_PUSH:
                if (t->byref)
                        mono_mb_emit_ldloc_addr (mb, conv_arg);
+               else if (conv_arg)
+                       mono_mb_emit_ldloc (mb, conv_arg);
                else
                        mono_mb_emit_ldarg (mb, argnum);
                break;
@@ -8423,6 +7540,99 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                mono_mb_emit_stloc (mb, 3);
                break;
 
+       case MARSHAL_ACTION_MANAGED_CONV_IN: {
+               MonoClass* conv_arg_class = mono_defaults.int32_class;
+               guint8 ldop = CEE_LDIND_I4;
+               int label_null, label_false;
+
+               conv_arg = mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
+
+               if (spec) {
+                       switch (spec->native) {
+                       case MONO_NATIVE_I1:
+                       case MONO_NATIVE_U1:
+                               conv_arg_class = mono_defaults.byte_class;
+                               ldop = CEE_LDIND_I1;
+                               break;
+                       case MONO_NATIVE_VARIANTBOOL:
+                               conv_arg_class = mono_defaults.int16_class;
+                               ldop = CEE_LDIND_I2;
+                               break;
+                       case MONO_NATIVE_BOOLEAN:
+                               break;
+                       default:
+                               g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
+                       }
+               }
+
+               if (t->byref)
+                       *conv_arg_type = &conv_arg_class->this_arg;
+               else
+                       *conv_arg_type = &conv_arg_class->byval_arg;
+
+
+               mono_mb_emit_ldarg (mb, argnum);
+               
+               /* Check null */
+               if (t->byref) {
+                       
+                       label_null = mono_mb_emit_branch (mb, CEE_BRFALSE);
+                       mono_mb_emit_ldarg (mb, argnum);
+                       mono_mb_emit_byte (mb, ldop);
+               }
+
+               label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+               mono_mb_emit_byte (mb, CEE_LDC_I4_1);
+               mono_mb_emit_stloc (mb, conv_arg);
+               mono_mb_patch_branch (mb, label_false);
+
+               if (t->byref) 
+                       mono_mb_patch_branch (mb, label_null);
+               break;
+       }
+
+       case MARSHAL_ACTION_MANAGED_CONV_OUT: {
+               guint8 stop = CEE_STIND_I4;
+               guint8 ldc_op = CEE_LDC_I4_1;
+               int label_null,label_false, label_end;;
+
+               if (!t->byref)
+                       break;
+               if (spec) {
+                       switch (spec->native) {
+                       case MONO_NATIVE_I1:
+                       case MONO_NATIVE_U1:
+                               stop = CEE_STIND_I1;
+                               break;
+                       case MONO_NATIVE_VARIANTBOOL:
+                               stop = CEE_STIND_I2;
+                               ldc_op = CEE_LDC_I4_M1;
+                               break;
+                       default:
+                               break;
+                       }
+               }
+               
+               /* Check null */
+               mono_mb_emit_ldarg (mb, argnum);
+               label_null = mono_mb_emit_branch (mb, CEE_BRFALSE);
+
+               mono_mb_emit_ldarg (mb, argnum);
+               mono_mb_emit_ldloc (mb, conv_arg);
+
+               label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+               mono_mb_emit_byte (mb, ldc_op);
+               label_end = mono_mb_emit_branch (mb, CEE_BR);
+
+               mono_mb_patch_branch (mb, label_false);
+               mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+               mono_mb_patch_branch (mb, label_end);
+
+               mono_mb_emit_byte (mb, stop);
+               mono_mb_patch_branch (mb, label_null);
+               break;
+       }
+
        default:
                g_assert_not_reached ();
        }
@@ -8439,7 +7649,7 @@ emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
 
        switch (action) {
        case MARSHAL_ACTION_CONV_IN:
-               if (MONO_TYPE_ISSTRUCT (t->data.type)) {
+               if (MONO_TYPE_ISSTRUCT (t->data.type) && !mono_class_from_mono_type (t->data.type)->blittable) {
                        char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
                        mono_mb_emit_exception_marshal_directive (m->mb, msg);
                }
@@ -8543,10 +7753,14 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
                if (spec && (spec->native == MONO_NATIVE_IUNKNOWN ||
                        spec->native == MONO_NATIVE_IDISPATCH ||
                        spec->native == MONO_NATIVE_INTERFACE))
-                       return emit_marshal_com_interface (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+                       return mono_cominterop_emit_marshal_com_interface (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+               if (spec && (spec->native == MONO_NATIVE_SAFEARRAY) && 
+                       (spec->data.safearray_data.elem_type == MONO_VARIANT_VARIANT) && 
+                       (action == MARSHAL_ACTION_CONV_OUT))
+                       return mono_cominterop_emit_marshal_safearray (m, argnum, t, spec, conv_arg, conv_arg_type, action);
 #endif
 
-               if (mono_defaults.safehandle_class != NULL &&
+               if (mono_defaults.safehandle_class != NULL && t->data.klass &&
                    mono_class_is_subclass_of (t->data.klass,  mono_defaults.safehandle_class, FALSE))
                        return emit_marshal_safehandle (m, argnum, t, spec, conv_arg, conv_arg_type, action);
                
@@ -8596,7 +7810,7 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
  *
  * generates IL code for the pinvoke wrapper, the generated code calls @func.
  */
-static void
+void
 mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean aot, gboolean check_exceptions)
 {
        EmitMarshalContext m;
@@ -8714,7 +7928,7 @@ mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoM
                        case MONO_TYPE_VALUETYPE:
                                klass = sig->ret->data.klass;
                                if (klass->enumtype) {
-                                       type = sig->ret->data.klass->enum_basetype->type;
+                                       type = mono_class_enum_basetype (sig->ret->data.klass)->type;
                                        goto handle_enum;
                                }
                                emit_marshal (&m, 0, sig->ret, spec, 0, NULL, MARSHAL_ACTION_CONV_RESULT);
@@ -8816,13 +8030,16 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
        g_assert (method != NULL);
        g_assert (mono_method_signature (method)->pinvoke);
 
-       cache = method->klass->image->native_wrapper_cache;
+       if (aot)
+               cache = get_cache (&method->klass->image->native_wrapper_aot_cache, mono_aligned_addr_hash, NULL);
+       else
+               cache = get_cache (&method->klass->image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
        if ((res = mono_marshal_find_in_cache (cache, method)))
                return res;
 
        if (MONO_CLASS_IS_IMPORT (method->klass)) {
 #ifndef DISABLE_COM
-               return cominterop_get_native_wrapper (method);
+               return mono_cominterop_get_native_wrapper (method);
 #else
                g_assert_not_reached ();
 #endif
@@ -8986,7 +8203,7 @@ mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig
        GHashTable *cache;
        char *name;
 
-       cache = image->native_wrapper_cache;
+       cache = get_cache (&image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
        if ((res = mono_marshal_find_in_cache (cache, func)))
                return res;
 
@@ -9007,12 +8224,20 @@ mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig
        return res;
 }
                            
-/* FIXME: moving GC */
-static void
-mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject* this)
+/*
+ * mono_marshal_emit_managed_wrapper:
+ *
+ *   Emit the body of a native-to-managed wrapper. INVOKE_SIG is the signature of
+ * the delegate which wraps the managed method to be called. For closed delegates,
+ * it could have fewer parameters than the method it wraps.
+ * THIS_LOC is the memory location where the target of the delegate is stored.
+ */
+void
+mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject** this_loc)
 {
        MonoMethodSignature *sig, *csig;
        int i, *tmp_locals;
+       gboolean closed = FALSE;
 
        sig = m->sig;
        csig = m->csig;
@@ -9024,6 +8249,16 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
        /* allocate local 2 (boolean) delete_old */
        mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
 
+       if (!sig->hasthis && sig->param_count != invoke_sig->param_count) {
+               /* Closed delegate */
+               g_assert (sig->param_count == invoke_sig->param_count + 1);
+               closed = TRUE;
+               /* Use a new signature without the first argument */
+               sig = mono_metadata_signature_dup (sig);
+               memmove (&sig->params [0], &sig->params [1], (sig->param_count - 1) * sizeof (MonoType*));
+               sig->param_count --;
+       }
+
        if (!MONO_TYPE_IS_VOID(sig->ret)) {
                /* allocate local 3 to store the return value */
                mono_mb_add_local (mb, sig->ret);
@@ -9036,7 +8271,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
        tmp_locals = alloca (sizeof (int) * sig->param_count);
        for (i = 0; i < sig->param_count; i ++) {
                MonoType *t = sig->params [i];
-               
+
                switch (t->type) {
                case MONO_TYPE_OBJECT:
                case MONO_TYPE_CLASS:
@@ -9044,6 +8279,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                case MONO_TYPE_ARRAY:
                case MONO_TYPE_SZARRAY:
                case MONO_TYPE_STRING:
+               case MONO_TYPE_BOOLEAN:
                        tmp_locals [i] = emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
 
                        break;
@@ -9055,16 +8291,18 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
 
        emit_thread_interrupt_checkpoint (mb);
 
-       /* fixme: howto handle this ? */
        if (sig->hasthis) {
-               if (this) {
-                       /* FIXME: need a solution for the moving GC here */
-                       mono_mb_emit_ptr (mb, this);
+               if (this_loc) {
+                       mono_mb_emit_ptr (mb, this_loc);
+                       mono_mb_emit_byte (mb, CEE_LDIND_REF);
                } else {
                        /* fixme: */
                        g_assert_not_reached ();
                }
-       } 
+       } else if (closed) {
+               mono_mb_emit_ptr (mb, this_loc);
+               mono_mb_emit_byte (mb, CEE_LDIND_REF);
+       }
 
        for (i = 0; i < sig->param_count; i++) {
                MonoType *t = sig->params [i];
@@ -9083,15 +8321,14 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
 
        if (mspecs [0] && mspecs [0]->native == MONO_NATIVE_CUSTOM) {
                emit_marshal (m, 0, sig->ret, mspecs [0], 0, NULL, MARSHAL_ACTION_MANAGED_CONV_RESULT);
-       }
-       else
-       if (!sig->ret->byref) { 
+       } else if (!sig->ret->byref) { 
                switch (sig->ret->type) {
                case MONO_TYPE_VOID:
                        break;
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
+               case MONO_TYPE_CHAR:
                case MONO_TYPE_I2:
                case MONO_TYPE_U2:
                case MONO_TYPE_I4:
@@ -9137,6 +8374,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                        case MONO_TYPE_VALUETYPE:
                        case MONO_TYPE_OBJECT:
                        case MONO_TYPE_STRING:
+                       case MONO_TYPE_BOOLEAN:
                                emit_marshal (m, i, t, mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_MANAGED_CONV_OUT);
                                break;
                        }
@@ -9165,6 +8403,9 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                        mono_mb_emit_ldloc (mb, 3);
                mono_mb_emit_byte (mb, CEE_RET);
        }
+
+       if (closed)
+               g_free (sig);
 }
 
 
@@ -9208,7 +8449,7 @@ mono_marshal_set_callconv_from_modopt (MonoMethod *method, MonoMethodSignature *
  * generates IL code to call managed methods from unmanaged code 
  */
 MonoMethod *
-mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject *this)
+mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject **this_loc)
 {
        static MonoClass *UnmanagedFunctionPointerAttribute;
        MonoMethodSignature *sig, *csig, *invoke_sig;
@@ -9229,10 +8470,10 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass,
         * options.
         */
        cache = get_cache (&method->klass->image->managed_wrapper_cache, mono_aligned_addr_hash, NULL);
-       if (!this && (res = mono_marshal_find_in_cache (cache, method)))
+       if (!this_loc && (res = mono_marshal_find_in_cache (cache, method)))
                return res;
 
-       invoke = mono_class_get_method_from_name (delegate_klass, "Invoke", mono_method_signature (method)->param_count);
+       invoke = mono_get_delegate_invoke (delegate_klass);
        invoke_sig = mono_method_signature (invoke);
 
        mspecs = g_new0 (MonoMarshalSpec*, mono_method_signature (invoke)->param_count + 1);
@@ -9242,13 +8483,12 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass,
 
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
 
-
        /* we copy the signature, so that we can modify it */
-       if (this)
+       if (this_loc)
                /* Need to free this later */
-               csig = mono_metadata_signature_dup (sig);
+               csig = mono_metadata_signature_dup (invoke_sig);
        else
-               csig = signature_dup (method->klass->image, sig);
+               csig = signature_dup (method->klass->image, invoke_sig);
        csig->hasthis = 0;
        csig->pinvoke = 1;
 
@@ -9289,9 +8529,9 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass,
                }
        }
 
-       mono_marshal_emit_managed_wrapper (mb, invoke_sig, mspecs, &m, method, this);
+       mono_marshal_emit_managed_wrapper (mb, invoke_sig, mspecs, &m, method, this_loc);
 
-       if (!this)
+       if (!this_loc)
                res = mono_mb_create_and_cache (cache, method,
                                                                                         mb, csig, sig->param_count + 16);
        else {
@@ -9780,7 +9020,6 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        static MonoMethod *enter_method, *exit_method, *gettypefromhandle_method;
        MonoMethodSignature *sig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoMethodBuilder *mb;
        MonoMethod *res;
        GHashTable *cache;
@@ -9825,11 +9064,11 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        /* this */
        this_local = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
-       mono_loader_lock ();
        clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
 
+       mono_loader_lock ();
+
        if (!enter_method) {
                MonoMethodDesc *desc;
 
@@ -9849,6 +9088,8 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                mono_method_desc_free (desc);
        }
 
+       mono_loader_unlock ();
+
        /* Push this or the type object */
        if (method->flags & METHOD_ATTRIBUTE_STATIC) {
                /* We have special handling for this in the JIT */
@@ -9897,14 +9138,12 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                mono_mb_emit_ldloc (mb, ret_local);
        mono_mb_emit_byte (mb, CEE_RET);
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        res = mono_mb_create_and_cache (cache, method,
                                                                        mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;     
 }
 
@@ -10253,46 +9492,6 @@ mono_marshal_get_array_address (int rank, int elem_size)
        return ret;
 }
 
-MonoMethod*
-mono_marshal_get_write_barrier (void)
-{
-       static MonoMethod* ret = NULL;
-       MonoMethodSignature *sig;
-       MonoMethodBuilder *mb;
-       int max_stack = 2;
-
-       if (ret)
-               return ret;
-       
-       mb = mono_mb_new (mono_defaults.object_class, "writebarrier", MONO_WRAPPER_WRITE_BARRIER);
-
-       sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
-
-       /* void writebarrier (MonoObject** addr, MonoObject* obj) */
-       sig->ret = &mono_defaults.void_class->byval_arg;
-       sig->params [0] = &mono_defaults.object_class->this_arg;
-       sig->params [1] = &mono_defaults.object_class->byval_arg;
-
-       /* just the store right now: add an hook for the GC to use, maybe something
-        * that can be used for stelemref as well
-        * We need a write barrier variant to be used with struct copies as well, though
-        * there are also other approaches possible, like writing a wrapper specific to
-        * the struct or to the reference pattern in the struct...
-        * Depending on the GC, we may want variants that take the object we store to
-        * when it is available.
-        */
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_ldarg (mb, 1);
-       mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_store);
-       /*mono_mb_emit_byte (mb, CEE_STIND_REF);*/
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       ret = mono_mb_create_method (mb, sig, max_stack);
-       mono_mb_free (mb);
-       return ret;
-}
-
 void*
 mono_marshal_alloc (gulong size)
 {
@@ -10607,216 +9806,6 @@ ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16 *pt
        }
 }
 
-MonoString *
-ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return mono_string_from_bstr(ptr);
-}
-
-gpointer
-ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return mono_string_to_bstr(ptr);
-}
-
-typedef struct
-{
-       int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
-       int (STDCALL *AddRef)(gpointer pUnk);
-       int (STDCALL *Release)(gpointer pUnk);
-} MonoIUnknown;
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       mono_free_bstr (ptr);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->AddRef(pUnk);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->QueryInterface(pUnk, riid, ppv);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->Release(pUnk);
-}
-
-#ifndef DISABLE_COM
-
-static void*
-cominterop_get_idispatch_for_object (MonoObject* object)
-{
-       if (!object)
-               return NULL;
-
-       if (cominterop_object_is_rcw (object)) {
-               return cominterop_get_interface (((MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp)->com_object, 
-                       mono_defaults.idispatch_class, TRUE);
-       }
-       else {
-               return cominterop_get_ccw (object, mono_defaults.idispatch_class);
-       }
-}
-
-#endif
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       if (!object)
-               return NULL;
-
-       if (cominterop_object_is_rcw (object)) {
-               MonoClass *klass = NULL;
-               MonoRealProxy* real_proxy = NULL;
-               if (!object)
-                       return NULL;
-               klass = mono_object_class (object);
-               if (klass != mono_defaults.transparent_proxy_class) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               real_proxy = ((MonoTransparentProxy*)object)->rp;
-               if (!real_proxy) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               klass = mono_object_class (real_proxy);
-               if (klass != mono_defaults.com_interop_proxy_class) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               if (!((MonoComInteropProxy*)real_proxy)->com_object) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               return ((MonoComInteropProxy*)real_proxy)->com_object->iunknown;
-       }
-       else {
-               return cominterop_get_ccw (object, mono_defaults.iunknown_class);
-       }
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-MonoObject*
-ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk)
-{
-#ifndef DISABLE_COM
-       MonoObject* object = NULL;
-
-       if (!pUnk)
-               return NULL;
-
-       /* see if it is a CCW */
-       object = cominterop_get_ccw_object ((MonoCCWInterface*)pUnk, TRUE);
-
-       return object;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       return cominterop_get_idispatch_for_object (object);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type)
-{
-#ifndef DISABLE_COM
-       MonoClass* klass = NULL;
-       void* itf = NULL;
-       g_assert (type);
-       g_assert (type->type);
-       klass = mono_type_get_class (type->type);
-       g_assert (klass);
-       itf = cominterop_get_ccw (object, klass);
-       g_assert (itf);
-       return itf;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-
-MonoBoolean
-ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       return (MonoBoolean)cominterop_object_is_rcw (object);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-gint32
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       MonoComInteropProxy* proxy = NULL;
-       gint32 ref_count = 0;
-
-       g_assert (object);
-       g_assert (cominterop_object_is_rcw (object));
-
-       proxy = (MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp;
-       g_assert (proxy);
-
-       ref_count = InterlockedDecrement (&proxy->ref_count);
-       g_assert (ref_count >= 0);
-
-       if (ref_count == 0)
-               ves_icall_System_ComObject_ReleaseInterfaces (proxy->com_object);
-
-       return ref_count;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-guint32
-ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m)
-{
-       MONO_ARCH_SAVE_REGS;
-
-#ifndef DISABLE_COM
-       return cominterop_get_com_slot_for_method (m->method);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
 guint32 
 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
 {
@@ -11197,157 +10186,6 @@ ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerIn
        return mono_ftnptr_to_delegate (mono_type_get_class (type->type), ftn);
 }
 
-/* Only used for COM RCWs */
-MonoObject *
-ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type)
-{
-       MonoClass *klass;
-       MonoDomain *domain;
-       MonoObject *obj;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_object_domain (type);
-       klass = mono_class_from_mono_type (type->type);
-
-       /* call mono_object_new_alloc_specific instead of mono_object_new
-        * because we want to actually create object. mono_object_new checks
-        * to see if type is import and creates transparent proxy. this method
-        * is called by the corresponding real proxy to create the real RCW.
-        * Constructor does not need to be called. Will be called later.
-       */
-       obj = mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
-       return obj;
-}
-
-static gboolean    
-cominterop_rcw_interface_finalizer (gpointer key, gpointer value, gpointer user_data)
-{
-       ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (value);
-       return TRUE;
-}
-
-void
-ves_icall_System_ComObject_ReleaseInterfaces (MonoComObject* obj)
-{
-       g_assert(obj);
-       if (obj->itf_hash) {
-               guint32 gchandle = 0;
-               mono_cominterop_lock ();
-               gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, obj->iunknown));
-               if (gchandle) {
-                       mono_gchandle_free (gchandle);
-                       g_hash_table_remove (rcw_hash, obj->iunknown);
-               }
-
-               g_hash_table_foreach_remove (obj->itf_hash, cominterop_rcw_interface_finalizer, NULL);
-               g_hash_table_destroy (obj->itf_hash);
-               ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj->iunknown);
-               obj->itf_hash = obj->iunknown = NULL;
-               mono_cominterop_unlock ();
-       }
-}
-
-#ifndef DISABLE_COM
-
-static gboolean    
-cominterop_rcw_finalizer (gpointer key, gpointer value, gpointer user_data)
-{
-       guint32 gchandle = 0;
-
-       gchandle = GPOINTER_TO_UINT (value);
-       if (gchandle) {
-               MonoComInteropProxy* proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
-               
-               if (proxy) {
-                       if (proxy->com_object->itf_hash) {
-                               g_hash_table_foreach_remove (proxy->com_object->itf_hash, cominterop_rcw_interface_finalizer, NULL);
-                               g_hash_table_destroy (proxy->com_object->itf_hash);
-                       }
-                       if (proxy->com_object->iunknown)
-                               ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (proxy->com_object->iunknown);
-                       proxy->com_object->itf_hash = proxy->com_object->iunknown = NULL;
-               }
-               
-               mono_gchandle_free (gchandle);
-       }
-
-       return TRUE;
-}
-
-void
-cominterop_release_all_rcws ()
-{
-       if (!rcw_hash)
-               return;
-
-       mono_cominterop_lock ();
-
-       g_hash_table_foreach_remove (rcw_hash, cominterop_rcw_finalizer, NULL);
-       g_hash_table_destroy (rcw_hash);
-       rcw_hash = NULL;
-
-       mono_cominterop_unlock ();
-}
-
-#endif
-
-gpointer
-ves_icall_System_ComObject_GetInterfaceInternal (MonoComObject* obj, MonoReflectionType* type, MonoBoolean throw_exception)
-{
-#ifndef DISABLE_COM
-       return cominterop_get_interface (obj, mono_type_get_class (type->type), (gboolean)throw_exception);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void
-ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk, MonoComInteropProxy* proxy)
-{
-#ifndef DISABLE_COM
-       guint32 gchandle = 0;
-       if (!rcw_hash) {
-               mono_cominterop_lock ();
-               rcw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-               mono_cominterop_unlock ();
-       }
-
-       gchandle = mono_gchandle_new_weakref ((MonoObject*)proxy, FALSE);
-
-       mono_cominterop_lock ();
-       g_hash_table_insert (rcw_hash, pUnk, GUINT_TO_POINTER (gchandle));
-       mono_cominterop_unlock ();
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-MonoComInteropProxy*
-ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk)
-{
-#ifndef DISABLE_COM
-       MonoComInteropProxy* proxy = NULL;
-       guint32 gchandle = 0;
-
-       mono_cominterop_lock ();
-       if (rcw_hash)
-               gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, pUnk));
-       mono_cominterop_unlock ();
-       if (gchandle) {
-               proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
-               /* proxy is null means we need to free up old RCW */
-               if (!proxy) {
-                       mono_gchandle_free (gchandle);
-                       g_hash_table_remove (rcw_hash, pUnk);
-               }
-       }
-       return proxy;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
 /**
  * mono_marshal_is_loading_type_info:
  *
@@ -11418,7 +10256,7 @@ mono_marshal_load_type_info (MonoClass* klass)
        layout = klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
 
        /* The mempool is protected by the loader lock */
-       info = mono_image_alloc0 (klass->image, sizeof (MonoMarshalType) + sizeof (MonoMarshalField) * count);
+       info = mono_image_alloc0 (klass->image, MONO_SIZEOF_MARSHAL_TYPE + sizeof (MonoMarshalField) * count);
        info->num_fields = count;
        
        /* Try to find a size for this type in metadata */
@@ -11444,7 +10282,7 @@ mono_marshal_load_type_info (MonoClass* klass)
                if (mono_field_is_deleted (field))
                        continue;
                if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
-                       mono_metadata_field_info_with_mempool (klass->image->mempool, klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1, 
+                       mono_metadata_field_info_with_mempool (klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1, 
                                                  NULL, NULL, &info->fields [j].mspec);
 
                info->fields [j].field = field;
@@ -11488,6 +10326,8 @@ mono_marshal_load_type_info (MonoClass* klass)
                info->native_size &= ~(min_align - 1);
        }
 
+       info->min_align = min_align;
+
        /* Update the class's blittable info, if the layouts don't match */
        if (info->native_size != mono_class_value_size (klass, NULL))
                klass->blittable = FALSE;
@@ -11532,7 +10372,7 @@ mono_class_native_size (MonoClass *klass, guint32 *align)
        }
 
        if (align)
-               *align = klass->min_align;
+               *align = klass->marshal_info->min_align;
 
        return klass->marshal_info->native_size;
 }
@@ -11608,7 +10448,7 @@ mono_type_native_stack_size (MonoType *t, guint32 *align)
                MonoClass *klass = mono_class_from_mono_type (t);
 
                if (klass->enumtype)
-                       return mono_type_native_stack_size (klass->enum_basetype, align);
+                       return mono_type_native_stack_size (mono_class_enum_basetype (klass), align);
                else {
                        size = mono_class_native_size (klass, align);
                        *align = *align + 3;
@@ -11872,10 +10712,10 @@ mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface, gchar
        mono_mb_emit_managed_call (mb, method, NULL);
        mono_mb_emit_byte (mb, CEE_RET);
 
-       res = mono_mb_create_method (mb, csig, csig->param_count + 16);
-
        /* We can corlib internal methods */
-       res->skip_visibility = TRUE;
+       mb->skip_visibility = TRUE;
+
+       res = mono_mb_create_method (mb, csig, csig->param_count + 16);
 
        mono_mb_free (mb);
 
@@ -11921,675 +10761,17 @@ mono_win32_compat_ZeroMemory (gpointer dest, gsize length)
        memset (dest, 0, length);
 }
 
-/* Put COM Interop related stuff here */
-
-#ifndef DISABLE_COM
-
-/**
- * cominterop_get_ccw_object:
- * @ccw_entry: a pointer to the CCWEntry
- * @verify: verify ccw_entry is in fact a ccw
- *
- * Returns: the corresponding object for the CCW
- */
-static MonoObject*
-cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify)
+void
+mono_marshal_find_nonzero_bit_offset (guint8 *buf, int len, int *byte_offset, guint8 *bitmask)
 {
-       MonoCCW *ccw = NULL;
+       int i;
+       guint8 byte;
 
-       /* no CCW's exist yet */
-       if (!ccw_interface_hash)
-               return NULL;
+       for (i = 0; i < len; ++i)
+               if (buf [i])
+                       break;
 
-       if (verify) {
-               ccw = g_hash_table_lookup (ccw_interface_hash, ccw_entry);
-       }
-       else {
-               ccw = ccw_entry->ccw;
-               g_assert (ccw);
-       }
-       if (ccw)
-               return mono_gchandle_get_target (ccw->gc_handle);
-       else
-               return NULL;
-}
-
-static void
-cominterop_setup_marshal_context (EmitMarshalContext *m, MonoMethod *method)
-{
-       MonoMethodSignature *sig, *csig;
-       sig = mono_method_signature (method);
-       /* we copy the signature, so that we can modify it */
-       /* FIXME: which to use? */
-       csig = signature_dup (method->klass->image, sig);
-       /* csig = mono_metadata_signature_dup (sig); */
-       
-       /* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
-#ifdef PLATFORM_WIN32
-       csig->call_convention = MONO_CALL_STDCALL;
-#else
-       csig->call_convention = MONO_CALL_C;
-#endif
-       csig->hasthis = 0;
-       csig->pinvoke = 1;
-
-       m->image = method->klass->image;
-       m->piinfo = NULL;
-       m->retobj_var = 0;
-       m->sig = sig;
-       m->csig = csig;
-}
-
-/**
- * cominterop_get_ccw:
- * @object: a pointer to the object
- * @itf: interface type needed
- *
- * Returns: a value indicating if the object is a
- * Runtime Callable Wrapper (RCW) for a COM object
- */
-static gpointer
-cominterop_get_ccw (MonoObject* object, MonoClass* itf)
-{
-       int i;
-       MonoCCW *ccw = NULL;
-       MonoCCWInterface* ccw_entry = NULL;
-       gpointer *vtable = NULL;
-       static gpointer iunknown[3] = {NULL, NULL, NULL};
-       static gpointer idispatch[4] = {NULL, NULL, NULL, NULL};
-       MonoClass* iface = NULL;
-       MonoClass* klass = NULL;
-       EmitMarshalContext m;
-       int start_slot = 3;
-       int method_count = 0;
-       GList *ccw_list, *ccw_list_item;
-       MonoCustomAttrInfo *cinfo = NULL;
-
-       if (!object)
-               return NULL;
-
-       klass = mono_object_get_class (object);
-
-       if (!ccw_hash)
-               ccw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       if (!ccw_interface_hash)
-               ccw_interface_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
-       ccw_list = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-
-       ccw_list_item = ccw_list;
-       while (ccw_list_item) {
-               MonoCCW* ccw_iter = ccw_list_item->data;
-               if (mono_gchandle_get_target (ccw_iter->gc_handle) == object) {
-                       ccw = ccw_iter;
-                       break;
-               }
-               ccw_list_item = g_list_next(ccw_list_item);
-       }
-
-       if (!iunknown [0]) {
-               iunknown [0] = cominterop_ccw_queryinterface;
-               iunknown [1] = cominterop_ccw_addref;
-               iunknown [2] = cominterop_ccw_release;
-       }
-
-       if (!idispatch [0]) {
-               idispatch [0] = cominterop_ccw_get_type_info_count;
-               idispatch [1] = cominterop_ccw_get_type_info;
-               idispatch [2] = cominterop_ccw_get_ids_of_names;
-               idispatch [3] = cominterop_ccw_invoke;
-       }
-
-       if (!ccw) {
-               ccw = g_new0 (MonoCCW, 1);
-#ifdef PLATFORM_WIN32
-               ccw->free_marshaler = 0;
-#endif
-               ccw->vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-               ccw->ref_count = 0;
-               /* just alloc a weak handle until we are addref'd*/
-               ccw->gc_handle = mono_gchandle_new_weakref (object, FALSE);
-
-               if (!ccw_list) {
-                       ccw_list = g_list_alloc ();
-                       ccw_list->data = ccw;
-               }
-               else
-                       ccw_list = g_list_append (ccw_list, ccw);
-               g_hash_table_insert (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)), ccw_list);
-               /* register for finalization to clean up ccw */
-               mono_object_register_finalizer (object);
-       }
-
-       cinfo = mono_custom_attrs_from_class (itf);
-       if (cinfo) {
-               static MonoClass* coclass_attribute = NULL;
-               if (!coclass_attribute)
-                       coclass_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "CoClassAttribute");
-               if (mono_custom_attrs_has_attr (cinfo, coclass_attribute)) {
-                       g_assert(itf->interface_count && itf->interfaces[0]);
-                       itf = itf->interfaces[0];
-               }
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-       }
-
-       iface = itf;
-       if (iface == mono_defaults.iunknown_class) {
-               start_slot = 3;
-       }
-       else if (iface == mono_defaults.idispatch_class) {
-               start_slot = 7;
-       }
-       else {
-               method_count += iface->method.count;
-               start_slot = cominterop_get_com_slot_begin (iface);
-               iface = NULL;
-       }
-
-       ccw_entry = g_hash_table_lookup (ccw->vtable_hash, itf);
-
-       if (!ccw_entry) {
-               int vtable_index = method_count-1+start_slot;
-               mono_loader_lock ();
-               vtable = mono_image_alloc0 (klass->image, sizeof (gpointer)*(method_count+start_slot));
-               mono_loader_unlock ();
-               memcpy (vtable, iunknown, sizeof (iunknown));
-               if (start_slot == 7)
-                       memcpy (vtable+3, idispatch, sizeof (idispatch));
-
-               iface = itf;
-               for (i = iface->method.count-1; i >= 0;i--) {
-                       int param_index = 0;
-                       MonoMethodBuilder *mb;
-                       MonoMarshalSpec ** mspecs;
-                       MonoMethod *wrapper_method, *adjust_method;
-                       MonoMethod *method = iface->methods [i];
-                       MonoMethodSignature* sig_adjusted;
-                       MonoMethodSignature* sig = mono_method_signature (method);
-                       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-
-                       mb = mono_mb_new (iface, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
-                       adjust_method = cominterop_get_managed_wrapper_adjusted (method);
-                       sig_adjusted = mono_method_signature (adjust_method);
-                       
-                       mspecs = g_new (MonoMarshalSpec*, sig_adjusted->param_count + 1);
-                       mono_method_get_marshal_info (method, mspecs);
-
-                       
-                       /* move managed args up one */
-                       for (param_index = sig->param_count; param_index >= 1; param_index--) {
-                               int mspec_index = param_index+1;
-                               mspecs [mspec_index] = mspecs [param_index];
-
-                               if (mspecs[mspec_index] == NULL) {
-                                       if (sig_adjusted->params[param_index]->type == MONO_TYPE_OBJECT) {
-                                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
-                                       }
-                                       else if (sig_adjusted->params[param_index]->type == MONO_TYPE_STRING) {
-                                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
-                                       }
-                                       else if (sig_adjusted->params[param_index]->type == MONO_TYPE_CLASS) {
-                                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
-                                       }
-                               }
-                       }
-
-                       /* first arg is IntPtr for interface */
-                       mspecs [1] = NULL;
-
-                       /* move return spec to last param */
-                       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret)) {
-                               if (mspecs [0] == NULL) {
-                                       if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_OBJECT) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_STRUCT;
-                                       }
-                                       else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_STRING) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_BSTR;
-                                       }
-                                       else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_CLASS) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_INTERFACE;
-                                       }
-                               }
-
-                               mspecs [sig_adjusted->param_count] = mspecs [0];
-                               mspecs [0] = NULL;
-                       }
-
-                       cominterop_setup_marshal_context (&m, adjust_method);
-                       m.mb = mb;
-                       mono_marshal_emit_managed_wrapper (mb, sig_adjusted, mspecs, &m, adjust_method, NULL);
-                       mono_loader_lock ();
-                       mono_marshal_lock ();
-                       wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
-                       mono_marshal_unlock ();
-                       mono_loader_unlock ();
-
-                       /* skip visiblity since we call internal methods */
-                       wrapper_method->skip_visibility = TRUE;
-
-                       vtable [vtable_index--] = mono_compile_method (wrapper_method);
-
-                       
-                       for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
-                               if (mspecs [param_index])
-                                       mono_metadata_free_marshal_spec (mspecs [param_index]);
-                       g_free (mspecs);
-               }
-
-               ccw_entry = g_new0 (MonoCCWInterface, 1);
-               ccw_entry->ccw = ccw;
-               ccw_entry->vtable = vtable;
-               g_hash_table_insert (ccw->vtable_hash, itf, ccw_entry);
-               g_hash_table_insert (ccw_interface_hash, ccw_entry, ccw);
-       }
-
-       return ccw_entry;
-}
-
-static gboolean    
-mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
-{
-       g_assert (value);
-       g_free (value);
-       return TRUE;
-}
-
-/**
- * mono_marshal_free_ccw:
- * @object: the mono object
- *
- * Returns: whether the object had a CCW
- */
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
-       GList *ccw_list, *ccw_list_orig, *ccw_list_item;
-       /* no ccw's were created */
-       if (!ccw_hash || g_hash_table_size (ccw_hash) == 0)
-               return FALSE;
-
-       /* need to cache orig list address to remove from hash_table if empty */
-       mono_cominterop_lock ();
-       ccw_list = ccw_list_orig = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-       mono_cominterop_unlock ();
-
-       if (!ccw_list)
-               return FALSE;
-
-       ccw_list_item = ccw_list;
-       while (ccw_list_item) {
-               MonoCCW* ccw_iter = ccw_list_item->data;
-               MonoObject* handle_target = mono_gchandle_get_target (ccw_iter->gc_handle);
-
-               /* Looks like the GC NULLs the weakref handle target before running the
-                * finalizer. So if we get a NULL target, destroy the CCW as well. */
-               if (!handle_target || handle_target == object) {
-                       /* remove all interfaces */
-                       g_hash_table_foreach_remove (ccw_iter->vtable_hash, mono_marshal_free_ccw_entry, NULL);
-                       g_hash_table_destroy (ccw_iter->vtable_hash);
-
-                       /* get next before we delete */
-                       ccw_list_item = g_list_next(ccw_list_item);
-
-                       /* remove ccw from list */
-                       ccw_list = g_list_remove (ccw_list, ccw_iter);
-                       g_free (ccw_iter);
-               }
-               else
-                       ccw_list_item = g_list_next(ccw_list_item);
-       }
-
-       /* if list is empty remove original address from hash */
-       if (g_list_length (ccw_list) == 0)
-               g_hash_table_remove (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-
-
-       return TRUE;
-}
-
-/**
- * cominterop_get_managed_wrapper_adjusted:
- * @method: managed COM Interop method
- *
- * Returns: the generated method to call with signature matching
- * the unmanaged COM Method signature
- */
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method)
-{
-       static MonoMethod *get_hr_for_exception = NULL;
-       MonoMethod *res = NULL;
-       MonoMethodBuilder *mb;
-       MonoMarshalSpec **mspecs;
-       MonoMethodSignature *sig, *sig_native;
-       MonoExceptionClause *main_clause = NULL;
-       MonoMethodHeader *header;
-       int pos_leave;
-       int hr = 0;
-       int i;
-       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-       if (!get_hr_for_exception)
-               get_hr_for_exception = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetHRForException", -1);
-
-       sig = mono_method_signature (method);
-
-       /* create unmanaged wrapper */
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
-       sig_native = cominterop_method_signature (method);
-
-       mspecs = g_new0 (MonoMarshalSpec*, sig_native->param_count+1);
-
-       mono_method_get_marshal_info (method, mspecs);
-
-       /* move managed args up one */
-       for (i = sig->param_count; i >= 1; i--)
-               mspecs [i+1] = mspecs [i];
-
-       /* first arg is IntPtr for interface */
-       mspecs [1] = NULL;
-
-       /* move return spec to last param */
-       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
-               mspecs [sig_native->param_count] = mspecs [0];
-
-       mspecs [0] = NULL;
-
-       if (!preserve_sig) {
-               hr = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
-
-               /* try */
-               main_clause = g_new0 (MonoExceptionClause, 1);
-               main_clause->try_offset = mono_mb_get_label (mb);
-       }
-
-       /* load last param to store result if not preserve_sig and not void */
-       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
-               mono_mb_emit_ldarg (mb, sig_native->param_count-1);
-
-       /* the CCW -> object conversion */
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_icon (mb, FALSE);
-       mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-
-       for (i = 0; i < sig->param_count; i++)
-               mono_mb_emit_ldarg (mb, i+1);
-
-       mono_mb_emit_managed_call (mb, method, NULL);
-
-       if (!preserve_sig) {
-               /* store result if not preserve_sig and we have one */
-               if (!MONO_TYPE_IS_VOID (sig->ret))
-                       mono_mb_emit_byte (mb, mono_type_to_stind (sig->ret));
-
-               pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
-
-               /* Main exception catch */
-               main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
-               main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
-               main_clause->data.catch_class = mono_defaults.object_class;
-               
-               /* handler code */
-               main_clause->handler_offset = mono_mb_get_label (mb);
-               mono_mb_emit_managed_call (mb, get_hr_for_exception, NULL);
-               mono_mb_emit_stloc (mb, hr);
-               mono_mb_emit_branch (mb, CEE_LEAVE);
-               main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
-               /* end catch */
-
-               mono_mb_patch_branch (mb, pos_leave);
-
-               mono_mb_emit_ldloc (mb, hr);
-       }
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       mono_loader_lock ();
-       mono_marshal_lock ();
-       res = mono_mb_create_method (mb, sig_native, sig_native->param_count + 16);     
-       mono_marshal_unlock ();
-       mono_loader_unlock ();
-
-       mono_mb_free (mb);
-
-       for (i = sig_native->param_count; i >= 0; i--)
-               if (mspecs [i])
-                       mono_metadata_free_marshal_spec (mspecs [i]);
-       g_free (mspecs);
-
-       if (!preserve_sig) {
-               header = ((MonoMethodNormal *)res)->header;
-               header->num_clauses = 1;
-               header->clauses = main_clause;
-       }
-
-       return res;
-}
-
-/**
- * cominterop_mono_string_to_guid:
- *
- * Converts the standard string representation of a GUID 
- * to a 16 byte Microsoft GUID.
- */
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid) {
-       gunichar2 * chars = mono_string_chars (string);
-       int i = 0;
-       static guint8 indexes[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};
-
-       for (i = 0; i < sizeof(indexes); i++)
-               guid [i] = g_unichar_xdigit_value (chars [indexes [i]]) + (g_unichar_xdigit_value (chars [indexes [i] - 1]) << 4);
-}
-
-static gboolean
-cominterop_class_guid_equal (guint8* guid, MonoClass* klass)
-{
-       guint8 klass_guid [16];
-       if (cominterop_class_guid (klass, klass_guid))
-               return !memcmp (guid, klass_guid, sizeof (klass_guid));
-       return FALSE;
-}
-
-static int STDCALL 
-cominterop_ccw_addref (MonoCCWInterface* ccwe)
-{
-       gint32 ref_count = 0;
-       MonoCCW* ccw = ccwe->ccw;
-       g_assert (ccw);
-       g_assert (ccw->gc_handle);
-       g_assert (ccw->ref_count >= 0);
-       ref_count = InterlockedIncrement ((gint32*)&ccw->ref_count);
-       if (ref_count == 1) {
-               guint32 oldhandle = ccw->gc_handle;
-               g_assert (oldhandle);
-               /* since we now have a ref count, alloc a strong handle*/
-               ccw->gc_handle = mono_gchandle_new (mono_gchandle_get_target (oldhandle), FALSE);
-               mono_gchandle_free (oldhandle);
-       }
-       return ref_count;
-}
-
-static int STDCALL 
-cominterop_ccw_release (MonoCCWInterface* ccwe)
-{
-       gint32 ref_count = 0;
-       MonoCCW* ccw = ccwe->ccw;
-       g_assert (ccw);
-       g_assert (ccw->ref_count > 0);
-       ref_count = InterlockedDecrement ((gint32*)&ccw->ref_count);
-       if (ref_count == 0) {
-               /* allow gc of object */
-               guint32 oldhandle = ccw->gc_handle;
-               g_assert (oldhandle);
-#ifdef PLATFORM_WIN32
-               if (ccw->free_marshaler)
-                       ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (ccw->free_marshaler);
-#endif
-               ccw->gc_handle = mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle), FALSE);
-               mono_gchandle_free (oldhandle);
-       }
-       return ref_count;
-}
-
-#define MONO_S_OK 0x00000000L
-#define MONO_E_NOINTERFACE 0x80004002L
-#define MONO_E_NOTIMPL 0x80004001L
-
-#ifdef PLATFORM_WIN32
-static const IID MONO_IID_IMarshal = {0x3, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46}};
-#endif
-
-#ifdef PLATFORM_WIN32
-/* All ccw objects are free threaded */
-static int
-cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv)
-{
-#ifdef PLATFORM_WIN32
-       if (!ccw->free_marshaler) {
-               int ret = 0;
-               gpointer tunk;
-               tunk = cominterop_get_ccw (object, mono_defaults.iunknown_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (tunk);
-               ret = CoCreateFreeThreadedMarshaler (tunk, (LPUNKNOWN*)&ccw->free_marshaler);
-               cominterop_ccw_release(tunk);
-       }
-               
-       if (!ccw->free_marshaler)
-               return MONO_E_NOINTERFACE;
-
-       return ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (ccw->free_marshaler, (IID*)&MONO_IID_IMarshal, ppv);
-#else
-       return MONO_E_NOINTERFACE;
-#endif
-}
-#endif
-
-static int STDCALL 
-cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv)
-{
-       GPtrArray *ifaces;
-       MonoClass *itf = NULL;
-       int i;
-       MonoCCW* ccw = ccwe->ccw;
-       MonoClass* klass = NULL;
-       MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);
-       
-       g_assert (object);
-       klass = mono_object_class (object);
-
-       if (ppv)
-               *ppv = NULL;
-
-       if (!mono_domain_get ())
-               mono_thread_attach (mono_get_root_domain ());
-
-       /* handle IUnknown special */
-       if (cominterop_class_guid_equal (riid, mono_defaults.iunknown_class)) {
-               *ppv = cominterop_get_ccw (object, mono_defaults.iunknown_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-       /* handle IDispatch special */
-       if (cominterop_class_guid_equal (riid, mono_defaults.idispatch_class)) {
-               *ppv = cominterop_get_ccw (object, mono_defaults.idispatch_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-#ifdef PLATFORM_WIN32
-       /* handle IMarshal special */
-       if (0 == memcmp (riid, &MONO_IID_IMarshal, sizeof (IID))) {
-               return cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv);      
-       }
-#endif
-
-       ifaces = mono_class_get_implemented_interfaces (klass);
-       if (ifaces) {
-               for (i = 0; i < ifaces->len; ++i) {
-                       MonoClass *ic = NULL;
-                       ic = g_ptr_array_index (ifaces, i);
-                       if (cominterop_class_guid_equal (riid, ic)) {
-                               itf = ic;
-                               break;
-                       }
-               }
-               g_ptr_array_free (ifaces, TRUE);
-       }
-       if (itf) {
-               *ppv = cominterop_get_ccw (object, itf);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-       return MONO_E_NOINTERFACE;
-}
-
-static int STDCALL 
-cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
-                                                                                        gunichar2** rgszNames, guint32 cNames,
-                                                                                        guint32 lcid, gint32 *rgDispId)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
-                                                                  gpointer riid, guint32 lcid,
-                                                                  guint16 wFlags, gpointer pDispParams,
-                                                                  gpointer pVarResult, gpointer pExcepInfo,
-                                                                  guint32 *puArgErr)
-{
-       return MONO_E_NOTIMPL;
-}
-
-#else /* DISABLE_COM */
-
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
-       return FALSE;
-}
-
-#endif /* DISABLE_COM */
-
-void
-mono_marshal_find_nonzero_bit_offset (guint8 *buf, int len, int *byte_offset, guint8 *bitmask)
-{
-       int i;
-       guint8 byte;
-
-       for (i = 0; i < len; ++i)
-               if (buf [i])
-                       break;
-
-       g_assert (i < len);
+       g_assert (i < len);
 
        byte = buf [i];
        while (byte && !(byte & 1))
@@ -12606,7 +10788,6 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        MonoMethodBuilder *mb;
        MonoMethodSignature *sig, *csig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoImage *image;
        MonoClass *klass;
        GHashTable *cache;
@@ -12630,7 +10811,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
 
        /* dup & extend signature */
        csig = mono_metadata_signature_alloc (image, param_count);
-       sig_size = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
+       sig_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
        memcpy (csig, sig, sig_size);
        csig->param_count = param_count;
        csig->hasthis = 0;
@@ -12646,7 +10827,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        }
 
        /* setup exception param as byref+[out] */
-       csig->params [param_count - 1] = mono_metadata_type_dup (image->mempool,
+       csig->params [param_count - 1] = mono_metadata_type_dup (image,
                 &mono_defaults.exception_class->byval_arg);
        csig->params [param_count - 1]->byref = 1;
        csig->params [param_count - 1]->attrs = PARAM_ATTRIBUTE_OUT;
@@ -12668,9 +10849,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        mono_mb_emit_byte (mb, CEE_STIND_REF);
 
        /* try */
-       mono_loader_lock ();
        clause = mono_image_alloc0 (image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        clause->try_offset = mono_mb_get_label (mb);
 
        /* push method's args */
@@ -12725,6 +10904,8 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
 
        clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        mono_mb_patch_branch (mb, pos_leave);
        /* end-try */
 
@@ -12741,9 +10922,102 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        res = mono_mb_create_and_cache (cache, method, mb, csig, param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;
 }
+
+/*
+ * mono_marshal_free_dynamic_wrappers:
+ *
+ *   Free wrappers of the dynamic method METHOD.
+ */
+void
+mono_marshal_free_dynamic_wrappers (MonoMethod *method)
+{
+       g_assert (method->dynamic);
+
+       mono_marshal_lock ();
+       /* 
+        * FIXME: We currently leak the wrappers. Freeing them would be tricky as
+        * they could be shared with other methods ?
+        */
+       if (method->klass->image->runtime_invoke_direct_cache)
+               g_hash_table_remove (method->klass->image->runtime_invoke_direct_cache, method);
+       mono_marshal_unlock ();
+}
+
+/*
+ * mono_marshal_free_inflated_wrappers:
+ *
+ *   Free wrappers of the inflated method METHOD.
+ */
+
+static gboolean
+signature_method_pair_matches_signature (gpointer key, gpointer value, gpointer user_data)
+{
+       SignatureMethodPair *pair = (SignatureMethodPair*)key;
+       MonoMethodSignature *sig = (MonoMethodSignature*)user_data;
+
+       return mono_metadata_signature_equal (pair->sig, sig);
+}
+
+void
+mono_marshal_free_inflated_wrappers (MonoMethod *method)
+{
+       MonoMethodSignature *sig = method->signature;
+
+       g_assert (method->is_inflated);
+
+       /* Ignore calls occuring late during cleanup.  */
+       if (!marshal_mutex_initialized)
+               return;
+
+       mono_marshal_lock ();
+       /*
+        * FIXME: We currently leak the wrappers. Freeing them would be tricky as
+        * they could be shared with other methods ?
+        */
+
+        /*
+         * indexed by MonoMethodSignature
+         */
+          /* FIXME: This could remove unrelated wrappers as well */
+       if (sig && method->klass->image->delegate_begin_invoke_cache)
+               g_hash_table_remove (method->klass->image->delegate_begin_invoke_cache, sig);
+       if (sig && method->klass->image->delegate_end_invoke_cache)
+               g_hash_table_remove (method->klass->image->delegate_end_invoke_cache, sig);
+       if (sig && method->klass->image->delegate_invoke_cache)
+               g_hash_table_remove (method->klass->image->delegate_invoke_cache, sig);
+       if (sig && method->klass->image->runtime_invoke_cache)
+               g_hash_table_remove (method->klass->image->runtime_invoke_cache, sig);
+
+        /*
+         * indexed by SignatureMethodPair
+         */
+       if (sig && method->klass->image->delegate_abstract_invoke_cache)
+               g_hash_table_foreach_remove (method->klass->image->delegate_abstract_invoke_cache,
+                                            signature_method_pair_matches_signature, (gpointer)sig);
+
+        /*
+         * indexed by MonoMethod pointers
+         */
+       if (method->klass->image->runtime_invoke_direct_cache)
+               g_hash_table_remove (method->klass->image->runtime_invoke_direct_cache, method);
+       if (method->klass->image->managed_wrapper_cache)
+               g_hash_table_remove (method->klass->image->managed_wrapper_cache, method);
+       if (method->klass->image->native_wrapper_cache)
+               g_hash_table_remove (method->klass->image->native_wrapper_cache, method);
+       if (method->klass->image->remoting_invoke_cache)
+               g_hash_table_remove (method->klass->image->remoting_invoke_cache, method);
+       if (method->klass->image->synchronized_cache)
+               g_hash_table_remove (method->klass->image->synchronized_cache, method);
+       if (method->klass->image->unbox_wrapper_cache)
+               g_hash_table_remove (method->klass->image->unbox_wrapper_cache, method);
+       if (method->klass->image->cominterop_invoke_cache)
+               g_hash_table_remove (method->klass->image->cominterop_invoke_cache, method);
+       if (method->klass->image->cominterop_wrapper_cache)
+               g_hash_table_remove (method->klass->image->cominterop_wrapper_cache, method);
+       if (method->klass->image->thunk_invoke_cache)
+               g_hash_table_remove (method->klass->image->thunk_invoke_cache, method);
+
+       mono_marshal_unlock ();
+}