2009-02-13 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / marshal.c
index accad66705e57c07c0dd5cac253e2608c3b2d07c..6a74315eeb73f20ac75412bbae0794f4fd2588d3 100644 (file)
@@ -4,11 +4,16 @@
  * Author:
  *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2002 Ximian, Inc.  http://www.ximian.com
+ * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  *
  */
 
 #include "config.h"
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
 #include "object.h"
 #include "loader.h"
 #include "cil-coff.h"
@@ -28,6 +33,7 @@
 #include "mono/metadata/string-icalls.h"
 #include "mono/metadata/attrdefs.h"
 #include "mono/metadata/gc-internal.h"
+#include "mono/metadata/cominterop.h"
 #include "mono/utils/mono-counters.h"
 #include <string.h>
 #include <errno.h>
@@ -44,13 +50,6 @@ typedef enum {
        MONO_MARSHAL_SERIALIZE          /* Value needs to be serialized into the new domain */
 } MonoXDomainMarshalType;
 
-typedef enum {
-       MONO_COM_DEFAULT,
-       MONO_COM_MS
-} MonoCOMProvider;
-
-static MonoCOMProvider com_provider = MONO_COM_DEFAULT;
-
 enum {
 #include "mono/cil/opcode.def"
        LAST = 0xff
@@ -76,11 +75,6 @@ typedef struct _MonoRemotingMethods MonoRemotingMethods;
 #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 guint32 last_error_tls_id;
 
 static guint32 load_type_info_tls_id;
@@ -103,12 +97,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);
 
@@ -148,9 +136,6 @@ 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);
 
 /* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
@@ -200,368 +185,17 @@ mono_signature_no_pinvoke (MonoMethod *method)
        return sig;
 }
 
-/* Begin COM Interop related stuff until seperate file */
-
-
-/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
-#ifdef  PLATFORM_WIN32
-#define STDCALL __stdcall
-#else
-#define STDCALL
-#endif
-
-/* Upon creation of a CCW, only allocate a weak handle and set the
- * reference count to 0. If the unmanaged client code decides to addref and
- * hold onto the CCW, I then allocate a strong handle. Once the reference count
- * goes back to 0, convert back to a weak handle.
- */
-typedef struct {
-       guint32 ref_count;
-       guint32 gc_handle;
-       GHashTable* vtable_hash;
-#ifdef  PLATFORM_WIN32
-       gpointer free_marshaler;
-#endif
-} MonoCCW;
-
-/* This type is the actual pointer passed to unmanaged code
- * to represent a COM interface.
- */
-typedef struct {
-       gpointer vtable;
-       MonoCCW* ccw;
-} MonoCCWInterface;
-
-#ifndef DISABLE_COM
-
-/* IUnknown */
-static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);
-
-/* IDispatch */
-static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);
-
-static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);
-
-static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
-                                                                                        gunichar2** rgszNames, guint32 cNames,
-                                                                                        guint32 lcid, gint32 *rgDispId);
-
-static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
-                                                                  gpointer riid, guint32 lcid,
-                                                                  guint16 wFlags, gpointer pDispParams,
-                                                                  gpointer pVarResult, gpointer pExcepInfo,
-                                                                  guint32 *puArgErr);
-
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method);
-
-static gpointer
-cominterop_get_ccw (MonoObject* object, MonoClass* itf);
-
-static MonoObject*
-cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);
-
-/**
- * cominterop_method_signature:
- * @method: a method
- *
- * Returns: the corresponding unmanaged method signature for a managed COM 
- * method.
- */
-static MonoMethodSignature*
-cominterop_method_signature (MonoMethod* method)
-{
-       MonoMethodSignature *res;
-       MonoImage *image = method->klass->image;
-       MonoMethodSignature *sig = mono_method_signature (method);
-       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-       int sigsize;
-       int i;
-       int param_count = sig->param_count + 1; // convert this arg into IntPtr arg
-
-       if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
-               param_count++;
-
-       res = mono_metadata_signature_alloc (image, param_count);
-       sigsize = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
-       memcpy (res, sig, sigsize);
-
-       // now move args forward one
-       for (i = sig->param_count-1; i >= 0; i--)
-               res->params[i+1] = sig->params[i];
-
-       // first arg is interface pointer
-       res->params[0] = &mono_defaults.int_class->byval_arg;
-
-       if (preserve_sig) {
-               res->ret = sig->ret;
-       }
-       else {
-               // last arg is return type
-               if (!MONO_TYPE_IS_VOID (sig->ret)) {
-                       res->params[param_count-1] = mono_metadata_type_dup (image->mempool, sig->ret);
-                       res->params[param_count-1]->byref = 1;
-                       res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
-               }
-
-               // return type is always int32 (HRESULT)
-               res->ret = &mono_defaults.int32_class->byval_arg;
-       }
-
-       // no pinvoke
-       res->pinvoke = FALSE;
-
-       // no hasthis
-       res->hasthis = 0;
-
-       // set param_count
-       res->param_count = param_count;
-
-       // STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
-#ifdef PLATFORM_WIN32
-       res->call_convention = MONO_CALL_STDCALL;
-#else
-       res->call_convention = MONO_CALL_C;
-#endif
-
-       return res;
-}
-
-/**
- * cominterop_get_function_pointer:
- * @itf: a pointer to the COM interface
- * @slot: the vtable slot of the method pointer to return
- *
- * Returns: the unmanaged vtable function pointer from the interface
- */
-static gpointer
-cominterop_get_function_pointer (gpointer itf, int slot)
-{
-       gpointer func;
-       func = *((*(gpointer**)itf)+slot);
-       return func;
-}
-
-/**
- * cominterop_object_is_com_object:
- * @obj: a pointer to the object
- *
- * Returns: a value indicating if the object is a
- * Runtime Callable Wrapper (RCW) for a COM object
- */
-static gboolean
-cominterop_object_is_rcw (MonoObject *obj)
-{
-       MonoClass *klass = NULL;
-       MonoRealProxy* real_proxy = NULL;
-       if (!obj)
-               return FALSE;
-       klass = mono_object_class (obj);
-       if (klass != mono_defaults.transparent_proxy_class)
-               return FALSE;
-
-       real_proxy = ((MonoTransparentProxy*)obj)->rp;
-       if (!real_proxy)
-               return FALSE;
-
-       klass = mono_object_class (real_proxy);
-       return (klass && klass == mono_defaults.com_interop_proxy_class);
-}
-
-static int
-cominterop_get_com_slot_begin (MonoClass* klass)
-{
-       static MonoClass *interface_type_attribute = NULL;
-       MonoCustomAttrInfo *cinfo = NULL;
-       MonoInterfaceTypeAttribute* itf_attr = NULL; 
-
-       if (!interface_type_attribute)
-               interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
-       cinfo = mono_custom_attrs_from_class (klass);
-       if (cinfo) {
-               itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr (cinfo, interface_type_attribute);
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-       }
-
-       if (itf_attr && itf_attr->intType == 1)
-               return 3; /* 3 methods in IUnknown*/
-       else
-               return 7; /* 7 methods in IDispatch*/
-}
-
-/**
- * cominterop_get_method_interface:
- * @method: method being called
- *
- * Returns: the MonoClass* representing the interface on which
- * the method is defined.
- */
-static MonoClass*
-cominterop_get_method_interface (MonoMethod* method)
-{
-       MonoClass *ic = method->klass;
-
-       /* if method is on a class, we need to look up interface method exists on */
-       if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
-               GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass);
-               if (ifaces) {
-                       int i;
-                       for (i = 0; i < ifaces->len; ++i) {
-                               int offset;
-                               ic = g_ptr_array_index (ifaces, i);
-                               offset = mono_class_interface_offset (method->klass, ic);
-                               if (method->slot >= offset && method->slot < offset + ic->method.count)
-                                       break;
-                               ic = NULL;
-                       }
-                       g_ptr_array_free (ifaces, TRUE);
-               }
-       }
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       return ic;
-}
-
-/**
- * cominterop_get_com_slot_for_method:
- * @method: a method
- *
- * Returns: the method's slot in the COM interface vtable
- */
-static int
-cominterop_get_com_slot_for_method (MonoMethod* method)
-{
-       guint32 slot = method->slot;
-       MonoClass *ic = method->klass;
-
-       /* if method is on a class, we need to look up interface method exists on */
-       if (!MONO_CLASS_IS_INTERFACE(ic)) {
-               int offset = 0;
-               ic = cominterop_get_method_interface (method);
-               offset = mono_class_interface_offset (method->klass, ic);
-               g_assert(offset >= 0);
-               slot -= offset;
-       }
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       return slot + cominterop_get_com_slot_begin (ic);
-}
-
-
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid);
-
-static gboolean
-cominterop_class_guid (MonoClass* klass, guint8* guid)
-{
-       static MonoClass *GuidAttribute = NULL;
-       MonoCustomAttrInfo *cinfo;
-
-       /* Handle the GuidAttribute */
-       if (!GuidAttribute)
-               GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");
-
-       cinfo = mono_custom_attrs_from_class (klass);   
-       if (cinfo) {
-               MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr (cinfo, GuidAttribute);
-
-               if (!attr)
-                       return FALSE;
-               if (!cinfo->cached)
-                       mono_custom_attrs_free (cinfo);
-
-               cominterop_mono_string_to_guid (attr->guid, guid);
-               return TRUE;
-       }
-       return FALSE;
-}
-
-/**
- * cominterop_get_interface:
- * @obj: managed wrapper object containing COM object
- * @ic: interface type to retrieve for COM object
- *
- * Returns: the COM interface requested
- */
-static gpointer
-cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
-{
-       gpointer itf = NULL;
-
-       g_assert (ic);
-       g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
-       mono_cominterop_lock ();
-       if (obj->itf_hash)
-               itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
-       mono_cominterop_unlock ();
-
-       if (!itf) {
-               guint8 iid [16];
-               int found = cominterop_class_guid (ic, iid);
-               int hr;
-               g_assert(found);
-               hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
-               if (hr < 0 && throw_exception) {
-                       static MonoMethod* throw_exception_for_hr = NULL;
-                       MonoException* ex;
-                       void* params[1] = {&hr};
-                       if (!throw_exception_for_hr)
-                               throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
-                       ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
-                       mono_raise_exception (ex);
-               }
-
-               if (hr >= 0 && itf) {
-                       mono_cominterop_lock ();
-                       if (!obj->itf_hash)
-                               obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-                       g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
-                       mono_cominterop_unlock ();
-               }
-
-       }
-       if (throw_exception)
-               g_assert (itf);
-
-       return itf;
-}
-
-static int
-cominterop_get_hresult_for_exception (MonoException* exc)
-{
-       int hr = 0;
-       return hr;
-}
-
-#endif /* DISABLE_COM */
-
 void
 mono_marshal_init (void)
 {
        static gboolean module_initialized = FALSE;
 
        if (!module_initialized) {
-               char* com_provider_env = NULL;
                module_initialized = TRUE;
                InitializeCriticalSection (&marshal_mutex);
-               InitializeCriticalSection (&cominterop_mutex);
                last_error_tls_id = TlsAlloc ();
                load_type_info_tls_id = TlsAlloc ();
 
-               com_provider_env = getenv ("MONO_COM");
-               if (com_provider_env && !strcmp(com_provider_env, "MS"))
-                       com_provider = MONO_COM_MS;
-
                register_icall (ves_icall_System_Threading_Thread_ResetAbort, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE);
                register_icall (mono_marshal_string_to_utf16, "mono_marshal_string_to_utf16", "ptr obj", FALSE);
                register_icall (mono_marshal_string_to_utf16_copy, "mono_marshal_string_to_utf16_copy", "ptr obj", FALSE);
@@ -570,9 +204,6 @@ 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);
@@ -610,25 +241,19 @@ mono_marshal_init (void)
                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 */
+
+               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);
 }
 
 MonoClass *byte_array_class;
@@ -1128,137 +753,6 @@ mono_string_to_ansibstr (MonoString *string_obj)
        return NULL;
 }
 
-typedef gpointer (*SysAllocStringLenFunc)(gunichar* str, guint32 len);
-typedef guint32 (*SysStringLenFunc)(gpointer bstr);
-typedef void (*SysFreeStringFunc)(gunichar* str);
-
-static SysAllocStringLenFunc sys_alloc_string_len_ms = NULL;
-static SysStringLenFunc sys_string_len_ms = NULL;
-static SysFreeStringFunc sys_free_string_ms = NULL;
-
-static gboolean
-init_com_provider_ms (void)
-{
-       static gboolean initialized = FALSE;
-       char *error_msg;
-       MonoDl *module = NULL;
-       const char* scope = "liboleaut32.so";
-
-       if (initialized)
-               return TRUE;
-
-       module = mono_dl_open(scope, MONO_DL_LAZY, &error_msg);
-       if (error_msg) {
-               g_warning ("Error loading COM support library '%s': %s", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-       error_msg = mono_dl_symbol (module, "SysAllocStringLen", (gpointer*)&sys_alloc_string_len_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysAllocStringLen", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       error_msg = mono_dl_symbol (module, "SysStringLen", (gpointer*)&sys_string_len_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysStringLen", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       error_msg = mono_dl_symbol (module, "SysFreeString", (gpointer*)&sys_free_string_ms);
-       if (error_msg) {
-               g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysFreeString", scope, error_msg);
-               g_assert_not_reached ();
-               return FALSE;
-       }
-
-       initialized = TRUE;
-       return TRUE;
-}
-
-gpointer
-mono_string_to_bstr (MonoString *string_obj)
-{
-       if (!string_obj)
-               return NULL;
-#ifdef PLATFORM_WIN32
-       return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               int slen = mono_string_length (string_obj);
-               /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
-               char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
-               if (ret == NULL)
-                       return NULL;
-               memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
-               * ((guint32 *) ret) = slen * sizeof(gunichar2);
-               ret [4 + slen * sizeof(gunichar2)] = 0;
-               ret [5 + slen * sizeof(gunichar2)] = 0;
-
-               return ret + 4;
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               gpointer ret = NULL;
-               gunichar* str = NULL;
-               guint32 len;
-               len = mono_string_length (string_obj);
-               str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
-                       NULL, NULL, NULL);
-               ret = sys_alloc_string_len_ms (str, len);
-               g_free(str);
-               return ret;
-       } else {
-               g_assert_not_reached ();
-       }
-#endif
-}
-
-MonoString *
-mono_string_from_bstr (gpointer bstr)
-{
-       if (!bstr)
-               return NULL;
-#ifdef PLATFORM_WIN32
-       return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               MonoString* str = NULL;
-               glong written = 0;
-               gunichar2* utf16 = NULL;
-
-               utf16 = g_ucs4_to_utf16 (bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
-               str = mono_string_new_utf16 (mono_domain_get (), utf16, written);
-               g_free (utf16);
-               return str;
-       } else {
-               g_assert_not_reached ();
-       }
-
-#endif
-}
-
-void
-mono_free_bstr (gpointer bstr)
-{
-       if (!bstr)
-               return;
-#ifdef PLATFORM_WIN32
-       SysFreeString ((BSTR)bstr);
-#else
-       if (com_provider == MONO_COM_DEFAULT) {
-               g_free (((char *)bstr) - 4);
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
-               sys_free_string_ms (bstr);
-       } else {
-               g_assert_not_reached ();
-       }
-
-#endif
-}
-
 /**
  * mono_string_to_byvalstr:
  * @dst: Where to store the null-terminated utf8 decoded string.
@@ -1369,25 +863,6 @@ mono_mb_emit_contextbound_check (MonoMethodBuilder *mb, int branch_code)
        return mono_mb_emit_branch (mb, branch_code);
 }
 
-#ifndef DISABLE_COM
-
-static void
-mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
-{
-       // get function pointer from 1st arg, the COM interface pointer
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
-       mono_mb_emit_icall (mb, cominterop_get_function_pointer);
-
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
-       mono_mb_emit_calli (mb, sig);
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
-}
-
-#endif /* DISABLE_COM */
-
 static void
 mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, const char *msg)
 {
@@ -1436,7 +911,7 @@ handle_enum:
                return CEE_LDIND_R8;
        case MONO_TYPE_VALUETYPE:
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                }
                return CEE_LDOBJ;
@@ -1490,7 +965,7 @@ handle_enum:
                return CEE_STIND_R8;
        case MONO_TYPE_VALUETYPE:
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                }
                return CEE_STOBJ;
@@ -1731,77 +1206,10 @@ 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);
-               break;
-       }
-#endif /* DISABLE_COM */
+       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: {
                /*
@@ -2107,91 +1515,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: {
@@ -2359,7 +1685,7 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
                                int src_var, dst_var;
 
                                if (ftype->data.klass->enumtype) {
-                                       ftype = ftype->data.klass->enum_basetype;
+                                       ftype = mono_class_enum_basetype (ftype->data.klass);
                                        goto handle_enum;
                                }
 
@@ -2501,6 +1827,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)
 {
@@ -2797,7 +2129,13 @@ get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
        return *var;
 }
 
-static inline MonoMethod*
+GHashTable*
+mono_marshal_get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
+{
+       return get_cache (var, hash_func, equal_func);
+}
+
+MonoMethod*
 mono_marshal_find_in_cache (GHashTable *cache, gpointer key)
 {
        MonoMethod *res;
@@ -2821,7 +2159,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)
@@ -3200,317 +2538,6 @@ 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 *res;
-       MonoMethodBuilder *mb_native;
-       MonoMarshalSpec **mspecs;
-       MonoMethodSignature *sig, *sig_native;
-       MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
-       int i;
-
-       sig = mono_method_signature (method);
-
-       // create unmanaged wrapper
-       mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
-       sig_native = cominterop_method_signature (method);
-
-       mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
-       memset (mspecs, 0, sizeof(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;
-
-       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];
-
-               mspecs[0] = NULL;
-       }
-
-       for (i = 1; i < sig_native->param_count; i++) {
-               int mspec_index = i + 1;
-               if (mspecs[mspec_index] == NULL) {
-                       // default object to VARIANT
-                       if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
-                       }
-                       else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
-                       }
-                       else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
-                               mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
-                       }
-               }
-       }
-
-       if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
-               // move return spec to last param
-               if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {                       
-                       // default object to VARIANT
-                       if (sig->ret->type == MONO_TYPE_OBJECT) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_STRUCT;
-                       }
-                       else if (sig->ret->type == MONO_TYPE_STRING) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_BSTR;
-                       }
-                       else if (sig->ret->type == MONO_TYPE_CLASS) {
-                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                               mspecs[0]->native = MONO_NATIVE_INTERFACE;
-                       }
-               }
-       }
-
-       mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, FALSE, TRUE);
-
-       res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);      
-
-       mono_mb_free (mb_native);
-
-       for (i = sig_native->param_count; i >= 0; i--)
-               if (mspecs [i])
-                       mono_metadata_free_marshal_spec (mspecs [i]);
-       g_free (mspecs);
-
-       return res;
-}
-
-/**
- * cominterop_get_native_wrapper:
- * @method: managed method
- *
- * Returns: the generated method to call
- */
-static MonoMethod *
-cominterop_get_native_wrapper (MonoMethod *method)
-{
-       MonoMethod *res;
-       GHashTable *cache;
-       MonoMethodBuilder *mb;
-       MonoMethodSignature *sig, *csig;
-
-       g_assert (method);
-
-       cache = get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
-
-       mono_init_com_types ();
-
-       if (!method->klass->vtable)
-               mono_class_setup_vtable (method->klass);
-       
-       if (!method->klass->methods)
-               mono_class_setup_methods (method->klass);
-
-       sig = mono_method_signature (method);
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
-       /* if method klass is import, that means method
-        * is really a com call. let interop system emit it.
-       */
-       if (MONO_CLASS_IS_IMPORT(method->klass)) {
-               /* FIXME: we have to call actual class .ctor
-                * instead of just __ComObject .ctor.
-                */
-               if (!strcmp(method->name, ".ctor")) {
-                       static MonoMethod *ctor = NULL;
-
-                       if (!ctor)
-                               ctor = mono_class_get_method_from_name (mono_defaults.com_object_class, ".ctor", 0);
-                       mono_mb_emit_ldarg (mb, 0);
-                       mono_mb_emit_managed_call (mb, ctor, NULL);
-                       mono_mb_emit_byte (mb, CEE_RET);
-               }
-               else {
-                       static MonoMethod * ThrowExceptionForHR = NULL;
-                       MonoMethod *adjusted_method;
-                       int retval = 0;
-                       int ptr_this;
-                       int i;
-                       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-                       // add local variables
-                       ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-                       if (!MONO_TYPE_IS_VOID (sig->ret))
-                               retval =  mono_mb_add_local (mb, sig->ret);
-
-                       // get the type for the interface the method is defined on
-                       // and then get the underlying COM interface for that type
-                       mono_mb_emit_ldarg (mb, 0);
-                       mono_mb_emit_ptr (mb, method);
-                       mono_mb_emit_icall (mb, cominterop_get_method_interface);
-                       mono_mb_emit_icon (mb, TRUE);
-                       mono_mb_emit_icall (mb, cominterop_get_interface);
-                       mono_mb_emit_stloc (mb, ptr_this);
-
-                       // arg 1 is unmanaged this pointer
-                       mono_mb_emit_ldloc (mb, ptr_this);
-
-                       // load args
-                       for (i = 1; i <= sig->param_count; i++)
-                               mono_mb_emit_ldarg (mb, i);
-
-                       // push managed return value as byref last argument
-                       if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
-                               mono_mb_emit_ldloc_addr (mb, retval);
-                       
-                       adjusted_method = cominterop_get_native_wrapper_adjusted (method);
-                       mono_mb_emit_managed_call (mb, adjusted_method, NULL);
-
-                       if (!preserve_sig) {
-                               if (!ThrowExceptionForHR)
-                                       ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
-                               mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);
-
-                               // load return value managed is expecting
-                               if (!MONO_TYPE_IS_VOID (sig->ret))
-                                       mono_mb_emit_ldloc (mb, retval);
-                       }
-
-                       mono_mb_emit_byte (mb, CEE_RET);
-               }
-               
-               
-       }
-       /* Does this case ever get hit? */
-       else {
-               char *msg = g_strdup ("non imported interfaces on \
-                       imported classes is not yet implemented.");
-               mono_mb_emit_exception (mb, "NotSupportedException", msg);
-       }
-       csig = signature_dup (method->klass->image, sig);
-       csig->pinvoke = 0;
-       res = mono_mb_create_and_cache (cache, method,
-                                                                       mb, csig, csig->param_count + 16);
-       mono_mb_free (mb);
-       return res;
-}
-
-/**
- * cominterop_get_invoke:
- * @method: managed method
- *
- * Returns: the generated method that calls the underlying __ComObject
- * rather than the proxy object.
- */
-static MonoMethod *
-cominterop_get_invoke (MonoMethod *method)
-{
-       MonoMethodSignature *sig;
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       int i, temp_obj;
-       GHashTable* cache = get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);
-
-       g_assert (method);
-
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
-
-       sig = mono_signature_no_pinvoke (method);
-
-       /* we cant remote methods without this pointer */
-       if (!sig->hasthis)
-               return method;
-
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);
-
-       /* get real proxy object, which is a ComInteropProxy in this case*/
-       temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
-       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-       /* load the RCW from the ComInteropProxy*/
-       mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
-       mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
-       /* load args and make the call on the RCW */
-       for (i = 1; i <= sig->param_count; i++)
-               mono_mb_emit_ldarg (mb, i);
-
-       if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
-               MonoMethod * native_wrapper = cominterop_get_native_wrapper(method);
-               mono_mb_emit_managed_call (mb, native_wrapper, NULL);
-       }
-       else {
-               if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
-                       mono_mb_emit_op (mb, CEE_CALLVIRT, method);
-               else
-                       mono_mb_emit_op (mb, CEE_CALL, method);
-       }
-
-       if (!strcmp(method->name, ".ctor"))     {
-               static MonoClass *com_interop_proxy_class = NULL;
-               static MonoMethod *cache_proxy = NULL;
-
-               if (!com_interop_proxy_class)
-                       com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
-               if (!cache_proxy)
-                       cache_proxy = mono_class_get_method_from_name (com_interop_proxy_class, "CacheProxy", 0);
-
-               mono_mb_emit_ldarg (mb, 0);
-               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
-               mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_managed_call (mb, cache_proxy, NULL);
-       }
-
-       emit_thread_interrupt_checkpoint (mb);
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
-       mono_mb_free (mb);
-
-       return res;
-}
-
-#endif /* DISABLE_COM */
-
-/* Maps a managed object to its unmanaged representation 
- * i.e. it's COM Callable Wrapper (CCW). 
- * Key: MonoObject*
- * Value: MonoCCW*
- */
-static GHashTable* ccw_hash = NULL;
-
-/* Maps a CCW interface to it's containing CCW. 
- * Note that a CCW support many interfaces.
- * Key: MonoCCW*
- * Value: MonoCCWInterface*
- */
-static GHashTable* ccw_interface_hash = NULL;
-
-/* Maps the IUnknown value of a RCW to
- * it's MonoComInteropProxy*.
- * Key: void*
- * Value: gchandle
- */
-static GHashTable* rcw_hash = NULL;
-
 MonoMethod *
 mono_marshal_get_remoting_invoke (MonoMethod *method)
 {
@@ -3527,7 +2554,7 @@ mono_marshal_get_remoting_invoke (MonoMethod *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);
+               return mono_cominterop_get_invoke (method);
 #else
                g_assert_not_reached ();
 #endif
@@ -3802,7 +2829,6 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
        MonoClass *ret_class = NULL;
        int loc_array=0, loc_return=0, loc_serialized_exc=0;
        MonoExceptionClause *main_clause;
-       MonoMethodHeader *header;
        int pos, pos_leave;
        gboolean copy_return;
 
@@ -3843,9 +2869,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 */
@@ -4039,13 +3063,11 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
 
        mono_mb_emit_byte (mb, CEE_RET);
 
+       mono_mb_set_clauses (mb, 1, main_clause);
+
        res = mono_remoting_mb_create_and_cache (method, mb, csig, csig->param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = main_clause;
-
        return res;
 }
 
@@ -4381,7 +3403,7 @@ 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
@@ -4641,8 +3663,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
 
        if (callvirt) {
                // From mono_mb_create_and_cache
+               mb->skip_visibility = 1;
                newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
-               newm->skip_visibility = 1;
                /*We perform double checked locking, so must fence before publishing*/
                mono_memory_barrier ();
                mono_marshal_lock ();
@@ -4660,8 +3682,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                        mono_free_method (newm);
                }
        } else {
+               mb->skip_visibility = 1;
                res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
-               res->skip_visibility = 1;
        }
        mono_mb_free (mb);
 
@@ -4761,10 +3783,7 @@ get_runtime_invoke_type (MonoType *t)
        case MONO_TYPE_U:
                return &mono_defaults.int_class->byval_arg;
        case MONO_TYPE_VALUETYPE:
-               if (t->data.klass->enumtype)
-                       return mono_type_get_underlying_type (t);
-               else
-                       return t;
+               return t;
        default:
                if (MONO_TYPE_IS_REFERENCE (t))
                        return &mono_defaults.object_class->byval_arg;
@@ -4811,7 +3830,6 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
 {
        MonoMethodSignature *sig, *csig, *callsig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoMethodBuilder *mb;
        GHashTable *cache = NULL;
        MonoClass *target_klass;
@@ -4822,6 +3840,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
        int i, pos, posna;
        char *name;
        gboolean need_direct_wrapper = FALSE;
+       int *tmp_nullable_locals;
 
        g_assert (method);
 
@@ -4843,7 +3862,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
        res = mono_marshal_find_in_cache (cache, method);
        if (res)
                return res;
-
+               
        if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
                (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
                /* 
@@ -4872,6 +3891,12 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                }
        }
 
+       /* 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;
+       }
+
        /*
         * We try to share runtime invoke wrappers between different methods but have to
         * be careful about methods whose klass has a type cctor, since putting the wrapper
@@ -4973,6 +3998,8 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
                }
        }
 
+       tmp_nullable_locals = g_new0 (int, sig->param_count);
+
        for (i = 0; i < sig->param_count; i++) {
                MonoType *t = sig->params [i];
                int type;
@@ -4985,6 +4012,16 @@ mono_marshal_get_runtime_invoke (MonoMethod *method)
 
                if (t->byref) {
                        mono_mb_emit_byte (mb, CEE_LDIND_I);
+                       /* A Nullable<T> type don't have a boxed form, it's either null or a boxed T.
+                        * So to make this work we unbox it to a local variablee and push a reference to that.
+                        */
+                       if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+                               tmp_nullable_locals [i] = mono_mb_add_local (mb, &mono_class_from_mono_type (t)->byval_arg);
+
+                               mono_mb_emit_op (mb, CEE_UNBOX_ANY, mono_class_from_mono_type (t));
+                               mono_mb_emit_stloc (mb, tmp_nullable_locals [i]);
+                               mono_mb_emit_ldloc_addr (mb, tmp_nullable_locals [i]);
+                       }
                        continue;
                }
 
@@ -5025,7 +4062,7 @@ handle_enum:
                        /* fall through */
                case MONO_TYPE_VALUETYPE:
                        if (type == MONO_TYPE_VALUETYPE && t->data.klass->enumtype) {
-                               type = t->data.klass->enum_basetype->type;
+                               type = mono_class_enum_basetype (t->data.klass)->type;
                                goto handle_enum;
                        }
                        mono_mb_emit_byte (mb, CEE_LDIND_I);
@@ -5093,12 +4130,30 @@ handle_enum:
        }
 
        mono_mb_emit_stloc (mb, 0);
+
+       /* Convert back nullable-byref arguments */
+       for (i = 0; i < sig->param_count; i++) {
+               MonoType *t = sig->params [i];
+
+               /* 
+                * Box the result and put it back into the array, the caller will have
+                * to obtain it from there.
+                */
+               if (t->byref && t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+                       mono_mb_emit_ldarg (mb, 1);                     
+                       mono_mb_emit_icon (mb, sizeof (gpointer) * i);
+                       mono_mb_emit_byte (mb, CEE_ADD);
+
+                       mono_mb_emit_ldloc (mb, tmp_nullable_locals [i]);
+                       mono_mb_emit_op (mb, CEE_BOX, mono_class_from_mono_type (t));
+
+                       mono_mb_emit_byte (mb, CEE_STIND_REF);
+               }
+       }
                        
        pos = mono_mb_emit_branch (mb, CEE_LEAVE);
 
-       mono_loader_lock ();
        clause = mono_image_alloc0 (target_klass->image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        clause->flags = MONO_EXCEPTION_CLAUSE_FILTER;
        clause->try_len = mono_mb_get_label (mb);
 
@@ -5139,12 +4194,15 @@ handle_enum:
 
        clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        /* return result */
        mono_mb_patch_branch (mb, pos);
        mono_mb_emit_ldloc (mb, 0);
        mono_mb_emit_byte (mb, CEE_RET);
 
        if (need_direct_wrapper) {
+               mb->skip_visibility = 1;
                res = mono_mb_create_and_cache (cache, method, mb, csig, sig->param_count + 16);
        } else {
                /* taken from mono_mb_create_and_cache */
@@ -5175,10 +4233,6 @@ handle_enum:
 
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;     
 }
 
@@ -5228,9 +4282,9 @@ mono_marshal_get_static_rgctx_invoke (MonoMethod *method)
        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->skip_visibility = TRUE;
        res->flags = method->flags;
 
        mono_mb_free (mb);
@@ -5834,51 +4888,6 @@ mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gcon
        return res;
 }
 
-typedef struct {
-       MonoMethodBuilder *mb;
-       MonoMethodSignature *sig;
-       MonoMethodPInvoke *piinfo;
-       int *orig_conv_args; /* Locals containing the original values of byref args */
-       int retobj_var;
-       MonoClass *retobj_class;
-       MonoMethodSignature *csig; /* Might need to be changed due to MarshalAs directives */
-       MonoImage *image; /* The image to use for looking up custom marshallers */
-} EmitMarshalContext;
-
-typedef enum {
-       /*
-        * This is invoked to convert arguments from the current types to
-        * the underlying types expected by the platform routine.  If required,
-        * the methods create a temporary variable with the proper type, and return
-        * the location for it (either the passed argument, or the newly allocated
-        * local slot).
-        */
-       MARSHAL_ACTION_CONV_IN,
-
-       /*
-        * This operation is called to push the actual value that was optionally
-        * converted on the first stage
-        */
-       MARSHAL_ACTION_PUSH,
-
-       /*
-        * Convert byref arguments back or free resources allocated during the
-        * CONV_IN stage
-        */
-       MARSHAL_ACTION_CONV_OUT,
-
-       /*
-        * The result from the unmanaged call is at the top of the stack when
-        * this action is invoked.    The result should be stored in the
-        * third local variable slot. 
-        */
-       MARSHAL_ACTION_CONV_RESULT,
-
-       MARSHAL_ACTION_MANAGED_CONV_IN,
-       MARSHAL_ACTION_MANAGED_CONV_OUT,
-       MARSHAL_ACTION_MANAGED_CONV_RESULT
-} MarshalAction;
-
 static int
 emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
                                         MonoMarshalSpec *spec, 
@@ -6029,7 +5038,7 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
                        mono_mb_emit_ldloc (mb, conv_arg);
                        mono_mb_emit_op (mb, CEE_CALLVIRT, marshal_native_to_managed);
                        mono_mb_emit_byte (mb, CEE_STIND_REF);
-               } else {
+               } else if (t->attrs & PARAM_ATTRIBUTE_OUT) {
                        mono_mb_emit_ldstr (mb, g_strdup (spec->data.custom_data.cookie));
 
                        mono_mb_emit_op (mb, CEE_CALL, get_instance);
@@ -7254,325 +6263,6 @@ 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,
-                    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, 
@@ -8423,6 +7113,70 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                mono_mb_emit_stloc (mb, 3);
                break;
 
+       case MARSHAL_ACTION_MANAGED_CONV_IN: {
+               gint variant_bool = 0;
+               guint8 ldop = CEE_LDIND_I4;
+               if (!t->byref)
+                       break;
+
+               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;
+                               ldop = CEE_LDIND_I1;
+                               break;
+                       case MONO_NATIVE_VARIANTBOOL:
+                               *conv_arg_type = &mono_defaults.int16_class->this_arg;
+                               variant_bool = 1;
+                               ldop = CEE_LDIND_I2;
+                               break;
+                       case MONO_NATIVE_BOOLEAN:
+                               break;
+                       default:
+                               g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
+                       }
+               }
+               
+               mono_mb_emit_ldarg (mb, argnum);
+               mono_mb_emit_byte (mb, ldop);   
+
+               if (variant_bool)
+                       mono_mb_emit_byte (mb, CEE_NEG);
+               mono_mb_emit_stloc (mb, conv_arg);
+               break;
+       }
+
+       case MARSHAL_ACTION_MANAGED_CONV_OUT: {
+               guint8 stop = CEE_STIND_I4;
+               if (!t->byref)
+                       break;
+               if (spec) {
+                       switch (spec->native) {
+                       case MONO_NATIVE_I1:
+                       case MONO_NATIVE_U1:
+                               stop = CEE_STIND_I1;
+                               break;
+                       case MONO_NATIVE_VARIANTBOOL:
+                               stop = CEE_STIND_I2;
+                               break;
+                       default:
+                               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);
+               mono_mb_emit_byte (mb, stop);
+               break;
+       }
+
        default:
                g_assert_not_reached ();
        }
@@ -8439,7 +7193,7 @@ emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
 
        switch (action) {
        case MARSHAL_ACTION_CONV_IN:
-               if (MONO_TYPE_ISSTRUCT (t->data.type)) {
+               if (MONO_TYPE_ISSTRUCT (t->data.type) && !mono_class_from_mono_type (t->data.type)->blittable) {
                        char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
                        mono_mb_emit_exception_marshal_directive (m->mb, msg);
                }
@@ -8543,7 +7297,7 @@ 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);
 #endif
 
                if (mono_defaults.safehandle_class != NULL &&
@@ -8596,7 +7350,7 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
  *
  * generates IL code for the pinvoke wrapper, the generated code calls @func.
  */
-static void
+void
 mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean aot, gboolean check_exceptions)
 {
        EmitMarshalContext m;
@@ -8714,7 +7468,7 @@ mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoM
                        case MONO_TYPE_VALUETYPE:
                                klass = sig->ret->data.klass;
                                if (klass->enumtype) {
-                                       type = sig->ret->data.klass->enum_basetype->type;
+                                       type = mono_class_enum_basetype (sig->ret->data.klass)->type;
                                        goto handle_enum;
                                }
                                emit_marshal (&m, 0, sig->ret, spec, 0, NULL, MARSHAL_ACTION_CONV_RESULT);
@@ -8816,13 +7570,16 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
        g_assert (method != NULL);
        g_assert (mono_method_signature (method)->pinvoke);
 
-       cache = method->klass->image->native_wrapper_cache;
+       if (aot)
+               cache = get_cache (&method->klass->image->native_wrapper_aot_cache, mono_aligned_addr_hash, NULL);
+       else
+               cache = get_cache (&method->klass->image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
        if ((res = mono_marshal_find_in_cache (cache, method)))
                return res;
 
        if (MONO_CLASS_IS_IMPORT (method->klass)) {
 #ifndef DISABLE_COM
-               return cominterop_get_native_wrapper (method);
+               return mono_cominterop_get_native_wrapper (method);
 #else
                g_assert_not_reached ();
 #endif
@@ -8986,7 +7743,7 @@ mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig
        GHashTable *cache;
        char *name;
 
-       cache = image->native_wrapper_cache;
+       cache = get_cache (&image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
        if ((res = mono_marshal_find_in_cache (cache, func)))
                return res;
 
@@ -9008,7 +7765,7 @@ mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig
 }
                            
 /* FIXME: moving GC */
-static void
+void
 mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject* this)
 {
        MonoMethodSignature *sig, *csig;
@@ -9044,6 +7801,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                case MONO_TYPE_ARRAY:
                case MONO_TYPE_SZARRAY:
                case MONO_TYPE_STRING:
+               case MONO_TYPE_BOOLEAN:
                        tmp_locals [i] = emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
 
                        break;
@@ -9092,6 +7850,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
+               case MONO_TYPE_CHAR:
                case MONO_TYPE_I2:
                case MONO_TYPE_U2:
                case MONO_TYPE_I4:
@@ -9137,6 +7896,7 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                        case MONO_TYPE_VALUETYPE:
                        case MONO_TYPE_OBJECT:
                        case MONO_TYPE_STRING:
+                       case MONO_TYPE_BOOLEAN:
                                emit_marshal (m, i, t, mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_MANAGED_CONV_OUT);
                                break;
                        }
@@ -9780,7 +8540,6 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        static MonoMethod *enter_method, *exit_method, *gettypefromhandle_method;
        MonoMethodSignature *sig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoMethodBuilder *mb;
        MonoMethod *res;
        GHashTable *cache;
@@ -9825,11 +8584,11 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        /* this */
        this_local = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
-       mono_loader_lock ();
        clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
-       mono_loader_unlock ();
        clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
 
+       mono_loader_lock ();
+
        if (!enter_method) {
                MonoMethodDesc *desc;
 
@@ -9849,6 +8608,8 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                mono_method_desc_free (desc);
        }
 
+       mono_loader_unlock ();
+
        /* Push this or the type object */
        if (method->flags & METHOD_ATTRIBUTE_STATIC) {
                /* We have special handling for this in the JIT */
@@ -9897,14 +8658,12 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                mono_mb_emit_ldloc (mb, ret_local);
        mono_mb_emit_byte (mb, CEE_RET);
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        res = mono_mb_create_and_cache (cache, method,
                                                                        mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;     
 }
 
@@ -10253,46 +9012,6 @@ mono_marshal_get_array_address (int rank, int elem_size)
        return ret;
 }
 
-MonoMethod*
-mono_marshal_get_write_barrier (void)
-{
-       static MonoMethod* ret = NULL;
-       MonoMethodSignature *sig;
-       MonoMethodBuilder *mb;
-       int max_stack = 2;
-
-       if (ret)
-               return ret;
-       
-       mb = mono_mb_new (mono_defaults.object_class, "writebarrier", MONO_WRAPPER_WRITE_BARRIER);
-
-       sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
-
-       /* void writebarrier (MonoObject** addr, MonoObject* obj) */
-       sig->ret = &mono_defaults.void_class->byval_arg;
-       sig->params [0] = &mono_defaults.object_class->this_arg;
-       sig->params [1] = &mono_defaults.object_class->byval_arg;
-
-       /* just the store right now: add an hook for the GC to use, maybe something
-        * that can be used for stelemref as well
-        * We need a write barrier variant to be used with struct copies as well, though
-        * there are also other approaches possible, like writing a wrapper specific to
-        * the struct or to the reference pattern in the struct...
-        * Depending on the GC, we may want variants that take the object we store to
-        * when it is available.
-        */
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_ldarg (mb, 1);
-       mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_store);
-       /*mono_mb_emit_byte (mb, CEE_STIND_REF);*/
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       ret = mono_mb_create_method (mb, sig, max_stack);
-       mono_mb_free (mb);
-       return ret;
-}
-
 void*
 mono_marshal_alloc (gulong size)
 {
@@ -10607,226 +9326,16 @@ ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16 *pt
        }
 }
 
-MonoString *
-ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
+guint32 
+ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
 {
        MONO_ARCH_SAVE_REGS;
 
-       return mono_string_from_bstr(ptr);
+       return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
 }
 
-gpointer
-ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return mono_string_to_bstr(ptr);
-}
-
-typedef struct
-{
-       int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
-       int (STDCALL *AddRef)(gpointer pUnk);
-       int (STDCALL *Release)(gpointer pUnk);
-} MonoIUnknown;
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       mono_free_bstr (ptr);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->AddRef(pUnk);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->QueryInterface(pUnk, riid, ppv);
-}
-
-int
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
-{
-       g_assert (pUnk);
-       return (*(MonoIUnknown**)pUnk)->Release(pUnk);
-}
-
-#ifndef DISABLE_COM
-
-static void*
-cominterop_get_idispatch_for_object (MonoObject* object)
-{
-       if (!object)
-               return NULL;
-
-       if (cominterop_object_is_rcw (object)) {
-               return cominterop_get_interface (((MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp)->com_object, 
-                       mono_defaults.idispatch_class, TRUE);
-       }
-       else {
-               return cominterop_get_ccw (object, mono_defaults.idispatch_class);
-       }
-}
-
-#endif
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       if (!object)
-               return NULL;
-
-       if (cominterop_object_is_rcw (object)) {
-               MonoClass *klass = NULL;
-               MonoRealProxy* real_proxy = NULL;
-               if (!object)
-                       return NULL;
-               klass = mono_object_class (object);
-               if (klass != mono_defaults.transparent_proxy_class) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               real_proxy = ((MonoTransparentProxy*)object)->rp;
-               if (!real_proxy) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               klass = mono_object_class (real_proxy);
-               if (klass != mono_defaults.com_interop_proxy_class) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               if (!((MonoComInteropProxy*)real_proxy)->com_object) {
-                       g_assert_not_reached ();
-                       return NULL;
-               }
-
-               return ((MonoComInteropProxy*)real_proxy)->com_object->iunknown;
-       }
-       else {
-               return cominterop_get_ccw (object, mono_defaults.iunknown_class);
-       }
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-MonoObject*
-ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk)
-{
-#ifndef DISABLE_COM
-       MonoObject* object = NULL;
-
-       if (!pUnk)
-               return NULL;
-
-       /* see if it is a CCW */
-       object = cominterop_get_ccw_object ((MonoCCWInterface*)pUnk, TRUE);
-
-       return object;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       return cominterop_get_idispatch_for_object (object);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type)
-{
-#ifndef DISABLE_COM
-       MonoClass* klass = NULL;
-       void* itf = NULL;
-       g_assert (type);
-       g_assert (type->type);
-       klass = mono_type_get_class (type->type);
-       g_assert (klass);
-       itf = cominterop_get_ccw (object, klass);
-       g_assert (itf);
-       return itf;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-
-MonoBoolean
-ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       return (MonoBoolean)cominterop_object_is_rcw (object);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-gint32
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
-       MonoComInteropProxy* proxy = NULL;
-       gint32 ref_count = 0;
-
-       g_assert (object);
-       g_assert (cominterop_object_is_rcw (object));
-
-       proxy = (MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp;
-       g_assert (proxy);
-
-       ref_count = InterlockedDecrement (&proxy->ref_count);
-       g_assert (ref_count >= 0);
-
-       if (ref_count == 0)
-               ves_icall_System_ComObject_ReleaseInterfaces (proxy->com_object);
-
-       return ref_count;
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-guint32
-ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m)
-{
-       MONO_ARCH_SAVE_REGS;
-
-#ifndef DISABLE_COM
-       return cominterop_get_com_slot_for_method (m->method);
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-guint32 
-ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
-}
-
-guint32 
-ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
+guint32 
+ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
 {
        MonoClass *klass;
        MonoType *type;
@@ -11197,112 +9706,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_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_finalizer, NULL);
-               ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj->iunknown);
-               obj->itf_hash = obj->iunknown = NULL;
-               mono_cominterop_unlock ();
-       }
-}
-
-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:
  *
@@ -11399,7 +9802,7 @@ mono_marshal_load_type_info (MonoClass* klass)
                if (mono_field_is_deleted (field))
                        continue;
                if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
-                       mono_metadata_field_info_with_mempool (klass->image->mempool, klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1, 
+                       mono_metadata_field_info_with_mempool (klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1, 
                                                  NULL, NULL, &info->fields [j].mspec);
 
                info->fields [j].field = field;
@@ -11443,6 +9846,8 @@ mono_marshal_load_type_info (MonoClass* klass)
                info->native_size &= ~(min_align - 1);
        }
 
+       info->min_align = min_align;
+
        /* Update the class's blittable info, if the layouts don't match */
        if (info->native_size != mono_class_value_size (klass, NULL))
                klass->blittable = FALSE;
@@ -11487,7 +9892,7 @@ mono_class_native_size (MonoClass *klass, guint32 *align)
        }
 
        if (align)
-               *align = klass->min_align;
+               *align = klass->marshal_info->min_align;
 
        return klass->marshal_info->native_size;
 }
@@ -11563,7 +9968,7 @@ mono_type_native_stack_size (MonoType *t, guint32 *align)
                MonoClass *klass = mono_class_from_mono_type (t);
 
                if (klass->enumtype)
-                       return mono_type_native_stack_size (klass->enum_basetype, align);
+                       return mono_type_native_stack_size (mono_class_enum_basetype (klass), align);
                else {
                        size = mono_class_native_size (klass, align);
                        *align = *align + 3;
@@ -11827,10 +10232,10 @@ mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface, gchar
        mono_mb_emit_managed_call (mb, method, NULL);
        mono_mb_emit_byte (mb, CEE_RET);
 
-       res = mono_mb_create_method (mb, csig, csig->param_count + 16);
-
        /* We can corlib internal methods */
-       res->skip_visibility = TRUE;
+       mb->skip_visibility = TRUE;
+
+       res = mono_mb_create_method (mb, csig, csig->param_count + 16);
 
        mono_mb_free (mb);
 
@@ -11876,664 +10281,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;
-                                       }
-                               }
-                       }
-
-                       /* first arg is IntPtr for interface */
-                       mspecs [1] = NULL;
-
-                       /* move return spec to last param */
-                       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret)) {
-                               if (mspecs [0] == NULL) {
-                                       if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_OBJECT) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_STRUCT;
-                                       }
-                                       else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_STRING) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_BSTR;
-                                       }
-                                       else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_CLASS) {
-                                               mspecs[0] = g_new0 (MonoMarshalSpec, 1);
-                                               mspecs[0]->native = MONO_NATIVE_INTERFACE;
-                                       }
-                               }
-
-                               mspecs [sig_adjusted->param_count] = mspecs [0];
-                               mspecs [0] = NULL;
-                       }
-
-                       cominterop_setup_marshal_context (&m, adjust_method);
-                       m.mb = mb;
-                       mono_marshal_emit_managed_wrapper (mb, sig_adjusted, mspecs, &m, adjust_method, NULL);
-                       mono_loader_lock ();
-                       mono_marshal_lock ();
-                       wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
-                       mono_marshal_unlock ();
-                       mono_loader_unlock ();
-
-                       /* skip visiblity since we call internal methods */
-                       wrapper_method->skip_visibility = TRUE;
-
-                       vtable [vtable_index--] = mono_compile_method (wrapper_method);
-
-                       
-                       for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
-                               if (mspecs [param_index])
-                                       mono_metadata_free_marshal_spec (mspecs [param_index]);
-                       g_free (mspecs);
-               }
-
-               ccw_entry = g_new0 (MonoCCWInterface, 1);
-               ccw_entry->ccw = ccw;
-               ccw_entry->vtable = vtable;
-               g_hash_table_insert (ccw->vtable_hash, itf, ccw_entry);
-               g_hash_table_insert (ccw_interface_hash, ccw_entry, ccw);
-       }
-
-       return ccw_entry;
-}
-
-static gboolean    
-mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
-{
-       g_assert (value);
-       g_free (value);
-       return TRUE;
-}
-
-/**
- * mono_marshal_free_ccw:
- * @object: the mono object
- *
- * Returns: whether the object had a CCW
- */
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
-       GList *ccw_list, *ccw_list_orig, *ccw_list_item;
-       /* no ccw's were created */
-       if (!ccw_hash || g_hash_table_size (ccw_hash) == 0)
-               return FALSE;
-
-       /* need to cache orig list address to remove from hash_table if empty */
-       mono_cominterop_lock ();
-       ccw_list = ccw_list_orig = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-       mono_cominterop_unlock ();
-
-       if (!ccw_list)
-               return FALSE;
-
-       ccw_list_item = ccw_list;
-       while (ccw_list_item) {
-               MonoCCW* ccw_iter = ccw_list_item->data;
-               MonoObject* handle_target = mono_gchandle_get_target (ccw_iter->gc_handle);
-
-               /* Looks like the GC NULLs the weakref handle target before running the
-                * finalizer. So if we get a NULL target, destroy the CCW as well. */
-               if (!handle_target || handle_target == object) {
-                       /* remove all interfaces */
-                       g_hash_table_foreach_remove (ccw_iter->vtable_hash, mono_marshal_free_ccw_entry, NULL);
-                       g_hash_table_destroy (ccw_iter->vtable_hash);
-
-                       /* get next before we delete */
-                       ccw_list_item = g_list_next(ccw_list_item);
-
-                       /* remove ccw from list */
-                       ccw_list = g_list_remove (ccw_list, ccw_iter);
-                       g_free (ccw_iter);
-               }
-               else
-                       ccw_list_item = g_list_next(ccw_list_item);
-       }
-
-       /* if list is empty remove original address from hash */
-       if (g_list_length (ccw_list) == 0)
-               g_hash_table_remove (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-
-
-       return TRUE;
-}
-
-/**
- * cominterop_get_managed_wrapper_adjusted:
- * @method: managed COM Interop method
- *
- * Returns: the generated method to call with signature matching
- * the unmanaged COM Method signature
- */
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method)
-{
-       static MonoMethod *get_hr_for_exception = NULL;
-       MonoMethod *res = NULL;
-       MonoMethodBuilder *mb;
-       MonoMarshalSpec **mspecs;
-       MonoMethodSignature *sig, *sig_native;
-       MonoExceptionClause *main_clause = NULL;
-       MonoMethodHeader *header;
-       int pos_leave;
-       int hr = 0;
-       int i;
-       gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-       if (!get_hr_for_exception)
-               get_hr_for_exception = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetHRForException", -1);
-
-       sig = mono_method_signature (method);
-
-       /* create unmanaged wrapper */
-       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
-       sig_native = cominterop_method_signature (method);
-
-       mspecs = g_new0 (MonoMarshalSpec*, sig_native->param_count+1);
-
-       mono_method_get_marshal_info (method, mspecs);
-
-       /* move managed args up one */
-       for (i = sig->param_count; i >= 1; i--)
-               mspecs [i+1] = mspecs [i];
-
-       /* first arg is IntPtr for interface */
-       mspecs [1] = NULL;
-
-       /* move return spec to last param */
-       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
-               mspecs [sig_native->param_count] = mspecs [0];
-
-       mspecs [0] = NULL;
-
-       if (!preserve_sig) {
-               hr = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
-
-               /* try */
-               main_clause = g_new0 (MonoExceptionClause, 1);
-               main_clause->try_offset = mono_mb_get_label (mb);
-       }
-
-       /* load last param to store result if not preserve_sig and not void */
-       if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
-               mono_mb_emit_ldarg (mb, sig_native->param_count-1);
-
-       /* the CCW -> object conversion */
-       mono_mb_emit_ldarg (mb, 0);
-       mono_mb_emit_icon (mb, FALSE);
-       mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-
-       for (i = 0; i < sig->param_count; i++)
-               mono_mb_emit_ldarg (mb, i+1);
-
-       mono_mb_emit_managed_call (mb, method, NULL);
-
-       if (!preserve_sig) {
-               /* store result if not preserve_sig and we have one */
-               if (!MONO_TYPE_IS_VOID (sig->ret))
-                       mono_mb_emit_byte (mb, mono_type_to_stind (sig->ret));
-
-               pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
-
-               /* Main exception catch */
-               main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
-               main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
-               main_clause->data.catch_class = mono_defaults.object_class;
-               
-               /* handler code */
-               main_clause->handler_offset = mono_mb_get_label (mb);
-               mono_mb_emit_managed_call (mb, get_hr_for_exception, NULL);
-               mono_mb_emit_stloc (mb, hr);
-               mono_mb_emit_branch (mb, CEE_LEAVE);
-               main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
-               /* end catch */
-
-               mono_mb_patch_branch (mb, pos_leave);
-
-               mono_mb_emit_ldloc (mb, hr);
-       }
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       mono_loader_lock ();
-       mono_marshal_lock ();
-       res = mono_mb_create_method (mb, sig_native, sig_native->param_count + 16);     
-       mono_marshal_unlock ();
-       mono_loader_unlock ();
-
-       mono_mb_free (mb);
-
-       for (i = sig_native->param_count; i >= 0; i--)
-               if (mspecs [i])
-                       mono_metadata_free_marshal_spec (mspecs [i]);
-       g_free (mspecs);
-
-       if (!preserve_sig) {
-               header = ((MonoMethodNormal *)res)->header;
-               header->num_clauses = 1;
-               header->clauses = main_clause;
-       }
-
-       return res;
-}
-
-/**
- * cominterop_mono_string_to_guid:
- *
- * Converts the standard string representation of a GUID 
- * to a 16 byte Microsoft GUID.
- */
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid) {
-       gunichar2 * chars = mono_string_chars (string);
-       int i = 0;
-       static guint8 indexes[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};
-
-       for (i = 0; i < sizeof(indexes); i++)
-               guid [i] = g_unichar_xdigit_value (chars [indexes [i]]) + (g_unichar_xdigit_value (chars [indexes [i] - 1]) << 4);
-}
-
-static gboolean
-cominterop_class_guid_equal (guint8* guid, MonoClass* klass)
-{
-       guint8 klass_guid [16];
-       if (cominterop_class_guid (klass, klass_guid))
-               return !memcmp (guid, klass_guid, sizeof (klass_guid));
-       return FALSE;
-}
-
-static int STDCALL 
-cominterop_ccw_addref (MonoCCWInterface* ccwe)
-{
-       gint32 ref_count = 0;
-       MonoCCW* ccw = ccwe->ccw;
-       g_assert (ccw);
-       g_assert (ccw->gc_handle);
-       g_assert (ccw->ref_count >= 0);
-       ref_count = InterlockedIncrement ((gint32*)&ccw->ref_count);
-       if (ref_count == 1) {
-               guint32 oldhandle = ccw->gc_handle;
-               g_assert (oldhandle);
-               /* since we now have a ref count, alloc a strong handle*/
-               ccw->gc_handle = mono_gchandle_new (mono_gchandle_get_target (oldhandle), FALSE);
-               mono_gchandle_free (oldhandle);
-       }
-       return ref_count;
-}
-
-static int STDCALL 
-cominterop_ccw_release (MonoCCWInterface* ccwe)
-{
-       gint32 ref_count = 0;
-       MonoCCW* ccw = ccwe->ccw;
-       g_assert (ccw);
-       g_assert (ccw->ref_count > 0);
-       ref_count = InterlockedDecrement ((gint32*)&ccw->ref_count);
-       if (ref_count == 0) {
-               /* allow gc of object */
-               guint32 oldhandle = ccw->gc_handle;
-               g_assert (oldhandle);
-#ifdef PLATFORM_WIN32
-               if (ccw->free_marshaler)
-                       ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (ccw->free_marshaler);
-#endif
-               ccw->gc_handle = mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle), FALSE);
-               mono_gchandle_free (oldhandle);
-       }
-       return ref_count;
-}
-
-#define MONO_S_OK 0x00000000L
-#define MONO_E_NOINTERFACE 0x80004002L
-#define MONO_E_NOTIMPL 0x80004001L
-
-#ifdef PLATFORM_WIN32
-static const IID MONO_IID_IMarshal = {0x3, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46}};
-#endif
-
-#ifdef PLATFORM_WIN32
-/* All ccw objects are free threaded */
-static int
-cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv)
-{
-#ifdef PLATFORM_WIN32
-       if (!ccw->free_marshaler) {
-               int ret = 0;
-               gpointer tunk;
-               tunk = cominterop_get_ccw (object, mono_defaults.iunknown_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (tunk);
-               ret = CoCreateFreeThreadedMarshaler (tunk, (LPUNKNOWN*)&ccw->free_marshaler);
-               cominterop_ccw_release(tunk);
-       }
-               
-       if (!ccw->free_marshaler)
-               return MONO_E_NOINTERFACE;
-
-       return ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (ccw->free_marshaler, (IID*)&MONO_IID_IMarshal, ppv);
-#else
-       return MONO_E_NOINTERFACE;
-#endif
-}
-#endif
-
-static int STDCALL 
-cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv)
-{
-       GPtrArray *ifaces;
-       MonoClass *itf = NULL;
-       int i;
-       MonoCCW* ccw = ccwe->ccw;
-       MonoClass* klass = NULL;
-       MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);
-       
-       g_assert (object);
-       klass = mono_object_class (object);
-
-       if (ppv)
-               *ppv = NULL;
-
-       if (!mono_domain_get ())
-               mono_thread_attach (mono_get_root_domain ());
-
-       /* handle IUnknown special */
-       if (cominterop_class_guid_equal (riid, mono_defaults.iunknown_class)) {
-               *ppv = cominterop_get_ccw (object, mono_defaults.iunknown_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-       /* handle IDispatch special */
-       if (cominterop_class_guid_equal (riid, mono_defaults.idispatch_class)) {
-               *ppv = cominterop_get_ccw (object, mono_defaults.idispatch_class);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-#ifdef PLATFORM_WIN32
-       /* handle IMarshal special */
-       if (0 == memcmp (riid, &MONO_IID_IMarshal, sizeof (IID))) {
-               return cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv);      
-       }
-#endif
-
-       ifaces = mono_class_get_implemented_interfaces (klass);
-       if (ifaces) {
-               for (i = 0; i < ifaces->len; ++i) {
-                       MonoClass *ic = NULL;
-                       ic = g_ptr_array_index (ifaces, i);
-                       if (cominterop_class_guid_equal (riid, ic)) {
-                               itf = ic;
-                               break;
-                       }
-               }
-               g_ptr_array_free (ifaces, TRUE);
-       }
-       if (itf) {
-               *ppv = cominterop_get_ccw (object, itf);
-               /* remember to addref on QI */
-               cominterop_ccw_addref (*ppv);
-               return MONO_S_OK;
-       }
-
-       return MONO_E_NOINTERFACE;
-}
-
-static int STDCALL 
-cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
-                                                                                        gunichar2** rgszNames, guint32 cNames,
-                                                                                        guint32 lcid, gint32 *rgDispId)
-{
-       return MONO_E_NOTIMPL;
-}
-
-static int STDCALL 
-cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
-                                                                  gpointer riid, guint32 lcid,
-                                                                  guint16 wFlags, gpointer pDispParams,
-                                                                  gpointer pVarResult, gpointer pExcepInfo,
-                                                                  guint32 *puArgErr)
-{
-       return MONO_E_NOTIMPL;
-}
-
-#else /* DISABLE_COM */
-
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
-       return FALSE;
-}
-
-#endif /* DISABLE_COM */
-
 void
 mono_marshal_find_nonzero_bit_offset (guint8 *buf, int len, int *byte_offset, guint8 *bitmask)
 {
@@ -12561,7 +10308,6 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        MonoMethodBuilder *mb;
        MonoMethodSignature *sig, *csig;
        MonoExceptionClause *clause;
-       MonoMethodHeader *header;
        MonoImage *image;
        MonoClass *klass;
        GHashTable *cache;
@@ -12601,7 +10347,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        }
 
        /* setup exception param as byref+[out] */
-       csig->params [param_count - 1] = mono_metadata_type_dup (image->mempool,
+       csig->params [param_count - 1] = mono_metadata_type_dup (image,
                 &mono_defaults.exception_class->byval_arg);
        csig->params [param_count - 1]->byref = 1;
        csig->params [param_count - 1]->attrs = PARAM_ATTRIBUTE_OUT;
@@ -12623,9 +10369,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 */
@@ -12680,6 +10424,8 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
 
        clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
 
+       mono_mb_set_clauses (mb, 1, clause);
+
        mono_mb_patch_branch (mb, pos_leave);
        /* end-try */
 
@@ -12696,9 +10442,25 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        res = mono_mb_create_and_cache (cache, method, mb, csig, param_count + 16);
        mono_mb_free (mb);
 
-       header = ((MonoMethodNormal *)res)->header;
-       header->num_clauses = 1;
-       header->clauses = clause;
-
        return res;
 }
+
+/*
+ * mono_marshal_free_dynamic_wrappers:
+ *
+ *   Free wrappers of the dynamic method METHOD.
+ */
+void
+mono_marshal_free_dynamic_wrappers (MonoMethod *method)
+{
+       g_assert (method->dynamic);
+
+       mono_marshal_lock ();
+       /* 
+        * FIXME: We currently leak the wrappers. Freeing them would be tricky as
+        * they could be shared with other methods ?
+        */
+       if (method->klass->image->runtime_invoke_direct_cache)
+               g_hash_table_remove (method->klass->image->runtime_invoke_direct_cache, method);
+       mono_marshal_unlock ();
+}