2009-08-12 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / marshal.c
index fe98f30558bdb92f468fd7940c3f7bc4503edc4e..fac7cb83b23e6ba1f4928c9ea68dec2a2b2f10f2 100644 (file)
@@ -33,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>
@@ -49,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
@@ -80,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);
@@ -108,12 +98,6 @@ 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);
 
@@ -153,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;
@@ -187,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;
@@ -205,413 +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, 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;
-}
-
-static gboolean
-cominterop_com_visible (MonoClass* klass)
-{
-       static MonoClass *ComVisibleAttribute = NULL;
-       MonoCustomAttrInfo *cinfo;
-       GPtrArray *ifaces;
-       MonoBoolean visible = 0;
-
-       /* Handle the ComVisibleAttribute */
-       if (!ComVisibleAttribute)
-               ComVisibleAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ComVisibleAttribute");
-
-       cinfo = mono_custom_attrs_from_class (klass);
-       if (cinfo) {
-               MonoReflectionComVisibleAttribute *attr = (MonoReflectionComVisibleAttribute*)mono_custom_attrs_get_attr (cinfo, ComVisibleAttribute);
-
-               if (attr)
-                       visible = attr->visible;
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-               if (visible)
-                       return TRUE;
-       }
-
-       ifaces = mono_class_get_implemented_interfaces (klass);
-       if (ifaces) {
-               int i;
-               for (i = 0; i < ifaces->len; ++i) {
-                       MonoClass *ic = NULL;
-                       ic = g_ptr_array_index (ifaces, i);
-                       if (MONO_CLASS_IS_IMPORT (ic))
-                               visible = TRUE;
-
-               }
-               g_ptr_array_free (ifaces, TRUE);
-       }
-       return visible;
-
-}
-
-static void cominterop_raise_hr_exception (int hr)
-{
-       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);
-}
-
-/**
- * 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) {
-                       cominterop_raise_hr_exception (hr);     
-               }
-
-               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);
@@ -620,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);
@@ -659,26 +245,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;
@@ -727,6 +309,7 @@ mono_delegate_to_ftnptr (MonoDelegate *delegate)
 {
        MonoMethod *method, *wrapper;
        MonoClass *klass;
+       MonoObject **target_loc;
 
        if (!delegate)
                return NULL;
@@ -739,12 +322,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);
@@ -758,6 +361,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) {
@@ -767,6 +372,7 @@ delegate_hash_table_new (void) {
 static void 
 delegate_hash_table_remove (MonoDelegate *d)
 {
+       MonoObject **target_loc;
 #ifdef HAVE_MOVING_COLLECTOR
        guint32 gchandle;
 #endif
@@ -777,26 +383,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 ();
 }
 
@@ -808,6 +435,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 ();
@@ -916,13 +546,81 @@ 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;
 
-       /* 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 ();
+       }
+
        return array->vector;
 }
 
+void
+mono_free_lparray (MonoArray *array, gpointer* nativeArray)
+{
+       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;
+       }               
+}
+
 static void
 mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *elclass, guint32 elnum)
 {
@@ -1178,151 +876,20 @@ 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)
+/**
+ * mono_string_to_byvalstr:
+ * @dst: Where to store the null-terminated utf8 decoded string.
+ * @src: the MonoString to copy.
+ * @size: the maximum number of bytes to copy.
+ *
+ * Copies the MonoString pointed to by @src as a utf8 string
+ * into @dst, it copies at most @size bytes into the destination.
+ */
+void
+mono_string_to_byvalstr (gpointer dst, MonoString *src, int size)
 {
-       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.
- * @src: the MonoString to copy.
- * @size: the maximum number of bytes to copy.
- *
- * Copies the MonoString pointed to by @src as a utf8 string
- * into @dst, it copies at most @size bytes into the destination.
- */
-void
-mono_string_to_byvalstr (gpointer dst, MonoString *src, int size)
-{
-       char *s;
-       int len;
+       char *s;
+       int len;
 
        g_assert (dst != NULL);
        g_assert (size > 0);
@@ -1419,25 +986,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)
 {
@@ -1781,76 +1329,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: {
@@ -1926,6 +1407,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:
@@ -2157,91 +1640,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: {
@@ -2551,6 +1952,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)
 {
@@ -2847,7 +2254,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;
@@ -2871,7 +2284,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)
@@ -3250,384 +2663,65 @@ 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) && !mono_class_vtable (mono_domain_get (), method->klass)->remote) {
+#ifndef DISABLE_COM
+               return mono_cominterop_get_invoke (method);
+#else
+               g_assert_not_reached ();
+#endif
+       }
 
-       mono_method_get_marshal_info (method, mspecs);
+       sig = mono_signature_no_pinvoke (method);
 
-       // move managed args up one
-       for (i = sig->param_count; i >= 1; i--)
-               mspecs[i+1] = mspecs[i];
+       /* we cant remote methods without this pointer */
+       if (!sig->hasthis)
+               return method;
 
-       // first arg is IntPtr for interface
-       mspecs[1] = NULL;
+       if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
+               return res;
 
-       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];
+       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
+       mb->method->save_lmf = 1;
 
-               mspecs[0] = NULL;
-       }
+       params_var = mono_mb_emit_save_args (mb, sig, TRUE);
 
-       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;
-                       }
-                       else if (sig_native->params[i]->type == MONO_NATIVE_BOOLEAN) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
-                       }
-               }
-       }
+       mono_mb_emit_ptr (mb, method);
+       mono_mb_emit_ldloc (mb, params_var);
+       mono_mb_emit_icall (mb, mono_remoting_wrapper);
+       emit_thread_interrupt_checkpoint (mb);
 
-       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;
-                       }
-                       else if (sig->ret->type == MONO_NATIVE_BOOLEAN) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
-                       }
-               }
+       if (sig->ret->type == MONO_TYPE_VOID) {
+               mono_mb_emit_byte (mb, CEE_POP);
+               mono_mb_emit_byte (mb, CEE_RET);
+       } else {
+                mono_mb_emit_restore_result (mb, sig->ret);
        }
 
-       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);
+       res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
+       mono_mb_free (mb);
 
        return res;
 }
 
-/**
- * cominterop_get_native_wrapper:
- * @method: managed method
- *
- * Returns: the generated method to call
+/* mono_get_xdomain_marshal_type()
+ * Returns the kind of marshalling that a type needs for cross domain calls.
  */
-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;
-
-       params_var = mono_mb_emit_save_args (mb, sig, TRUE);
-
-       mono_mb_emit_ptr (mb, method);
-       mono_mb_emit_ldloc (mb, params_var);
-       mono_mb_emit_icall (mb, mono_remoting_wrapper);
-       emit_thread_interrupt_checkpoint (mb);
-
-       if (sig->ret->type == MONO_TYPE_VOID) {
-               mono_mb_emit_byte (mb, CEE_POP);
-               mono_mb_emit_byte (mb, CEE_RET);
-       } else {
-                mono_mb_emit_restore_result (mb, sig->ret);
-       }
-
-       res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
-       mono_mb_free (mb);
-
-       return res;
-}
-
-/* mono_get_xdomain_marshal_type()
- * Returns the kind of marshalling that a type needs for cross domain calls.
- */
-static MonoXDomainMarshalType
-mono_get_xdomain_marshal_type (MonoType *t)
+static MonoXDomainMarshalType
+mono_get_xdomain_marshal_type (MonoType *t)
 {
        switch (t->type) {
        case MONO_TYPE_VOID:
@@ -3900,9 +2994,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 */
@@ -4436,13 +3528,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
@@ -4735,7 +3829,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 --)
@@ -4795,11 +3889,23 @@ add_string_ctor_signature (MonoMethod *method)
 }
 
 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;
@@ -4810,16 +3916,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) {
+                       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;
        }
 }
@@ -4835,9 +3944,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;
 }
@@ -4846,20 +3955,63 @@ 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;
@@ -4870,7 +4022,7 @@ 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;
@@ -4887,11 +4039,17 @@ 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;
@@ -4909,6 +4067,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) {
                        /* 
@@ -4924,11 +4084,13 @@ 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
@@ -4955,11 +4117,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);
                }
        }
 
@@ -4984,7 +4142,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);
@@ -5004,7 +4162,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);
 
@@ -5023,6 +4181,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);
@@ -5088,7 +4251,7 @@ 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;
                        }
 
@@ -5112,7 +4275,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);
@@ -5124,7 +4289,6 @@ handle_enum:
                g_assert_not_reached ();
        }
 
-
        switch (sig->ret->type) {
        case MONO_TYPE_VOID:
                if (!method->string_ctor)
@@ -5158,6 +4322,9 @@ 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 ();
        }
@@ -5186,9 +4353,7 @@ handle_enum:
                        
        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);
 
@@ -5216,15 +4381,11 @@ 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);
-
-       /* Delay the abort exception */
-       mono_mb_emit_icall (mb, ves_icall_System_Threading_Thread_ResetAbort);
+       mono_mb_emit_icall (mb, runtime_invoke_reset_abort);
 
-       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;
@@ -5271,62 +4432,6 @@ handle_enum:
        return res;     
 }
 
-/*
- * mono_marshal_get_static_rgctx_invoke:
- * @method: a method
- *
- * 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.
- */
-MonoMethod *
-mono_marshal_get_static_rgctx_invoke (MonoMethod *method)
-{
-       static gboolean inited = FALSE;
-       static int num_wrappers = 0;
-
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       MonoClass *target_klass = method->klass;
-       MonoMethodSignature *sig = mono_method_signature (method);
-       int i;
-       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 (!inited) {
-               mono_counters_register ("static rgctx invoke wrappers",
-                               MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_wrappers);
-               inited = TRUE;
-       }
-       ++num_wrappers;
-
-       name = mono_signature_to_name (mono_method_signature (method), "static_rgctx_invoke");
-       mb = mono_mb_new (target_klass, name, MONO_WRAPPER_STATIC_RGCTX_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);
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       mb->skip_visibility = TRUE;
-       res = mono_mb_create_and_cache (cache, method, mb, mono_method_signature (method),
-               sig->param_count + sig->hasthis + 4);
-       res->flags = method->flags;
-
-       mono_mb_free (mb);
-
-       return res;
-}
-
 static void
 mono_mb_emit_auto_layout_exception (MonoMethodBuilder *mb, MonoClass *klass)
 {
@@ -5923,51 +5028,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, 
@@ -6904,17 +5964,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)) {
@@ -7343,334 +6405,15 @@ 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 */
-       mono_init_com_types ();
-
-       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);      
-
-               /* we dont need any conversions for out parameters */
-               if (t->byref && 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);
-               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);
-
-                       if (klass && klass != mono_defaults.object_class)
-                               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_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_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);
-               }
-               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: {
-               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);
-               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);
-
-               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;
-
-               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;
+       static MonoMethod *get_object_for_native_variant = NULL;
+       static MonoMethod *get_native_variant_for_object = NULL;
 
        mono_init_com_types ();
        
@@ -8035,6 +6778,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:
@@ -8459,9 +7211,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 {
@@ -8472,7 +7224,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);
@@ -8480,29 +7235,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;
@@ -8513,25 +7288,21 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                break;
 
        case MARSHAL_ACTION_MANAGED_CONV_IN: {
-               gint variant_bool = 0;
+               MonoClass* conv_arg_class = mono_defaults.int32_class;
                guint8 ldop = CEE_LDIND_I4;
-               if (!t->byref)
-                       break;
+               int label_null, label_false;
 
                conv_arg = mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
 
-               *conv_arg_type = &mono_defaults.int32_class->byval_arg;
-
                if (spec) {
                        switch (spec->native) {
                        case MONO_NATIVE_I1:
                        case MONO_NATIVE_U1:
-                               *conv_arg_type = &mono_defaults.byte_class->this_arg;
+                               conv_arg_class = mono_defaults.byte_class;
                                ldop = CEE_LDIND_I1;
                                break;
                        case MONO_NATIVE_VARIANTBOOL:
-                               *conv_arg_type = &mono_defaults.int16_class->this_arg;
-                               variant_bool = 1;
+                               conv_arg_class = mono_defaults.int16_class;
                                ldop = CEE_LDIND_I2;
                                break;
                        case MONO_NATIVE_BOOLEAN:
@@ -8540,18 +7311,38 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                                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);
-               mono_mb_emit_byte (mb, ldop);   
+               
+               /* 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);
+               }
 
-               if (variant_bool)
-                       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);
                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) {
@@ -8562,17 +7353,30 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                                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);
-               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, 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;
        }
 
@@ -8696,10 +7500,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);
                
@@ -8749,7 +7557,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;
@@ -8978,7 +7786,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
 
        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
@@ -9163,12 +7971,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;
@@ -9180,6 +7996,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);
@@ -9192,7 +8018,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:
@@ -9212,16 +8038,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];
@@ -9240,9 +8068,7 @@ 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;
@@ -9324,6 +8150,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);
 }
 
 
@@ -9367,7 +8196,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;
@@ -9388,10 +8217,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);
@@ -9401,13 +8230,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;
 
@@ -9448,9 +8276,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 {
@@ -9983,9 +8811,7 @@ 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 ();
@@ -10727,260 +9553,28 @@ 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)
+guint32 
+ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
 {
        MONO_ARCH_SAVE_REGS;
 
-       return mono_string_to_bstr(ptr);
+       return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
 }
 
-typedef struct
+guint32 
+ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
 {
-       int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
-       int (STDCALL *AddRef)(gpointer pUnk);
-       int (STDCALL *Release)(gpointer pUnk);
-} MonoIUnknown;
+       MonoClass *klass;
+       MonoType *type;
+       guint32 layout;
 
-void
-ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
-{
        MONO_ARCH_SAVE_REGS;
 
-       mono_free_bstr (ptr);
-}
+       MONO_CHECK_ARG_NULL (rtype);
 
-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
-
-#define MONO_S_OK 0x00000000L
-#define MONO_E_NOINTERFACE 0x80004002L
-#define MONO_E_NOTIMPL 0x80004001L
-
-static gboolean cominterop_can_support_dispatch (MonoClass* klass)
-{
-       if (!(klass->flags & TYPE_ATTRIBUTE_PUBLIC) )
-               return FALSE;
-
-       if (!cominterop_com_visible (klass))
-               return FALSE;
-
-       return TRUE;
-}
-
-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 {
-               MonoClass* klass = mono_object_class (object);
-               if (!cominterop_can_support_dispatch (klass) )
-                       cominterop_raise_hr_exception (MONO_E_NOINTERFACE);
-               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;
-
-       mono_init_com_types ();
-
-       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
-       mono_init_com_types ();
-
-       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)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
-}
-
-guint32 
-ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
-{
-       MonoClass *klass;
-       MonoType *type;
-       guint32 layout;
-
-       MONO_ARCH_SAVE_REGS;
-
-       MONO_CHECK_ARG_NULL (rtype);
-
-       type = rtype->type;
-       klass = mono_class_from_mono_type (type);
-       layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
+       type = rtype->type;
+       klass = mono_class_from_mono_type (type);
+       layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
 
        if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
                gchar *msg;
@@ -11339,157 +9933,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:
  *
@@ -11560,7 +10003,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 */
@@ -12065,666 +10508,6 @@ 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)
-{
-       MonoCCW *ccw = NULL;
-
-       /* no CCW's exist yet */
-       if (!ccw_interface_hash)
-               return NULL;
-
-       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;
-                                       }
-                                       else if (sig_adjusted->params[param_index]->type == MONO_NATIVE_BOOLEAN) {
-                                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
-                                       }
-                               }
-                       }
-
-                       /* 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;
-                                       }
-                                       else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_NATIVE_BOOLEAN) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
-                                       }
-                               }
-
-                               mspecs [sig_adjusted->param_count] = mspecs [0];
-                               mspecs [0] = NULL;
-                       }
-
-                       /* skip visiblity since we call internal methods */
-                       mb->skip_visibility = TRUE;
-
-                       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 ();
-
-                       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;
-       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_set_clauses (mb, 1, main_clause);
-
-               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);
-
-       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;
-}
-
-#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)) {
-               if (!cominterop_can_support_dispatch (klass))
-                       return MONO_E_NOINTERFACE;
-               
-               *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)
 {
@@ -12775,7 +10558,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;
@@ -12813,9 +10596,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 */
@@ -12910,3 +10691,80 @@ mono_marshal_free_dynamic_wrappers (MonoMethod *method)
                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 ();
+}