Merge pull request #941 from upsilon/fix-layoutsettings-setter
[mono.git] / mono / metadata / marshal.c
index 31ba9e89f21a2652dabc119ba365b2f70924cb57..74c8be3c5f1c0daab762f3aa6d2cf4eabe79b66f 100644 (file)
@@ -38,6 +38,7 @@
 #include "mono/utils/mono-counters.h"
 #include "mono/utils/mono-tls.h"
 #include "mono/utils/mono-memory-model.h"
+#include "mono/utils/atomic.h"
 #include <string.h>
 #include <errno.h>
 
@@ -71,11 +72,12 @@ typedef struct _MonoRemotingMethods MonoRemotingMethods;
 /* 
  * This mutex protects the various marshalling related caches in MonoImage
  * and a few other data structures static to this file.
- * Note that when this lock is held it is not possible to take other runtime
- * locks like the loader lock.
+ *
+ * The marshal lock is a non-recursive complex lock that sits below the domain lock in the
+ * runtime locking latice. Which means it can take simple locks suck as the image lock.
  */
-#define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
-#define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
+#define mono_marshal_lock() mono_locks_acquire (&marshal_mutex, MarshalLock)
+#define mono_marshal_unlock() mono_locks_release (&marshal_mutex, MarshalLock)
 static CRITICAL_SECTION marshal_mutex;
 static gboolean marshal_mutex_initialized;
 
@@ -97,7 +99,7 @@ emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_obje
 static void 
 mono_struct_delete_old (MonoClass *klass, char *ptr);
 
-void *
+MONO_API void *
 mono_marshal_string_to_utf16 (MonoString *s);
 
 static void *
@@ -121,11 +123,14 @@ mono_string_from_byvalwstr (gunichar2 *data, int len);
 static void
 mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *eltype, guint32 elnum);
 
+static void
+mono_byvalarray_to_byte_array (MonoArray *arr, gpointer native_arr, guint32 elnum);
+
 static void
 mono_array_to_byvalarray (gpointer native_arr, MonoArray *arr, MonoClass *eltype, guint32 elnum);
 
-static MonoObject *
-mono_remoting_wrapper (MonoMethod *method, gpointer *params);
+static void
+mono_array_to_byte_byvalarray (gpointer native_arr, MonoArray *arr, guint32 elnum);
 
 static MonoAsyncResult *
 mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params);
@@ -142,9 +147,15 @@ mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push);
 static gboolean
 mono_marshal_check_domain_image (gint32 domain_id, MonoImage *image);
 
-void
+#ifndef DISABLE_REMOTING
+static MonoObject *
+mono_remoting_wrapper (MonoMethod *method, gpointer *params);
+
+MONO_API void
 mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype, MonoTransparentProxy *tproxy);
 
+#endif
+
 static MonoReflectionType *
 type_from_handle (MonoType *handle);
 
@@ -200,6 +211,13 @@ mono_signature_no_pinvoke (MonoMethod *method)
        return sig;
 }
 
+void
+mono_marshal_init_tls (void)
+{
+       mono_native_tls_alloc (&last_error_tls_id, NULL);
+       mono_native_tls_alloc (&load_type_info_tls_id, NULL);
+}
+
 void
 mono_marshal_init (void)
 {
@@ -209,8 +227,6 @@ mono_marshal_init (void)
                module_initialized = TRUE;
                InitializeCriticalSection (&marshal_mutex);
                marshal_mutex_initialized = TRUE;
-               mono_native_tls_alloc (&last_error_tls_id, NULL);
-               mono_native_tls_alloc (&load_type_info_tls_id, NULL);
 
                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);
@@ -229,7 +245,9 @@ mono_marshal_init (void)
                register_icall (mono_array_to_lparray, "mono_array_to_lparray", "ptr object", FALSE);
                register_icall (mono_free_lparray, "mono_free_lparray", "void object ptr", FALSE);
                register_icall (mono_byvalarray_to_array, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE);
+               register_icall (mono_byvalarray_to_byte_array, "mono_byvalarray_to_byte_array", "void object ptr int32", FALSE);
                register_icall (mono_array_to_byvalarray, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE);
+               register_icall (mono_array_to_byte_byvalarray, "mono_array_to_byte_byvalarray", "void ptr object int32", FALSE);
                register_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", "ptr object", FALSE);
                register_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE);
                register_icall (mono_marshal_asany, "mono_marshal_asany", "ptr object int32 int32", FALSE);
@@ -248,7 +266,6 @@ mono_marshal_init (void)
                register_icall (g_free, "g_free", "void ptr", FALSE);
                register_icall (mono_object_isinst, "mono_object_isinst", "object object ptr", FALSE);
                register_icall (mono_struct_delete_old, "mono_struct_delete_old", "void ptr ptr", FALSE);
-               register_icall (mono_remoting_wrapper, "mono_remoting_wrapper", "object ptr ptr", FALSE);
                register_icall (mono_delegate_begin_invoke, "mono_delegate_begin_invoke", "object object ptr", FALSE);
                register_icall (mono_delegate_end_invoke, "mono_delegate_end_invoke", "object object ptr", FALSE);
                register_icall (mono_marshal_xdomain_copy_value, "mono_marshal_xdomain_copy_value", "object object", FALSE);
@@ -258,11 +275,14 @@ mono_marshal_init (void)
                register_icall (mono_compile_method, "mono_compile_method", "ptr ptr", FALSE);
                register_icall (mono_context_get, "mono_context_get", "object", FALSE);
                register_icall (mono_context_set, "mono_context_set", "void object", FALSE);
-               register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
                register_icall (type_from_handle, "type_from_handle", "object ptr", FALSE);
                register_icall (mono_gc_wbarrier_generic_nostore, "wb_generic", "void ptr", FALSE);
                register_icall (mono_gchandle_get_target, "mono_gchandle_get_target", "object int32", TRUE);
 
+#ifndef DISABLE_REMOTING
+               register_icall (mono_remoting_wrapper, "mono_remoting_wrapper", "object ptr ptr", FALSE);
+               register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
+#endif
                mono_cominterop_init ();
        }
 }
@@ -278,9 +298,9 @@ mono_marshal_cleanup (void)
        marshal_mutex_initialized = FALSE;
 }
 
-MonoClass *byte_array_class;
+#ifndef DISABLE_REMOTING
+static MonoClass *byte_array_class;
 static MonoMethod *method_rs_serialize, *method_rs_deserialize, *method_exc_fixexc, *method_rs_appdomain_target;
-static MonoMethod *method_set_context, *method_get_context;
 static MonoMethod *method_set_call_context, *method_needs_context_sink, *method_rs_serialize_exc;
 
 static void
@@ -302,11 +322,6 @@ mono_remoting_marshal_init (void)
                klass = mono_defaults.exception_class;
                method_exc_fixexc = mono_class_get_method_from_name (klass, "FixRemotingException", -1);
        
-               klass = mono_defaults.thread_class;
-               method_get_context = mono_class_get_method_from_name (klass, "get_CurrentContext", -1);
-       
-               klass = mono_defaults.appdomain_class;
-               method_set_context = mono_class_get_method_from_name (klass, "InternalSetContext", -1);
                byte_array_class = mono_array_class_get (mono_defaults.byte_class, 1);
        
                klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "CallContext");
@@ -318,6 +333,7 @@ mono_remoting_marshal_init (void)
                module_initialized = TRUE;
        }
 }
+#endif
 
 gpointer
 mono_delegate_to_ftnptr (MonoDelegate *delegate)
@@ -382,40 +398,37 @@ delegate_hash_table_new (void) {
 static void 
 delegate_hash_table_remove (MonoDelegate *d)
 {
-#ifdef HAVE_MOVING_COLLECTOR
-       guint32 gchandle;
-#endif
+       guint32 gchandle = 0;
+
        mono_marshal_lock ();
        if (delegate_hash_table == NULL)
                delegate_hash_table = delegate_hash_table_new ();
-#ifdef HAVE_MOVING_COLLECTOR
-       gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
-#endif
+       if (mono_gc_is_moving ())
+               gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
        g_hash_table_remove (delegate_hash_table, d->delegate_trampoline);
        mono_marshal_unlock ();
-#ifdef HAVE_MOVING_COLLECTOR
-       mono_gchandle_free (gchandle);
-#endif
+       if (mono_gc_is_moving ())
+               mono_gchandle_free (gchandle);
 }
 
 static void
 delegate_hash_table_add (MonoDelegate *d)
 {
-#ifdef HAVE_MOVING_COLLECTOR
-       guint32 gchandle = mono_gchandle_new_weakref ((MonoObject*)d, FALSE);
+       guint32 gchandle;
        guint32 old_gchandle;
-#endif
+
        mono_marshal_lock ();
        if (delegate_hash_table == NULL)
                delegate_hash_table = delegate_hash_table_new ();
-#ifdef HAVE_MOVING_COLLECTOR
-       old_gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
-       g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, GUINT_TO_POINTER (gchandle));
-       if (old_gchandle)
-               mono_gchandle_free (old_gchandle);
-#else
-       g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, d);
-#endif
+       if (mono_gc_is_moving ()) {
+               gchandle = mono_gchandle_new_weakref ((MonoObject*)d, FALSE);
+               old_gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
+               g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, GUINT_TO_POINTER (gchandle));
+               if (old_gchandle)
+                       mono_gchandle_free (old_gchandle);
+       } else {
+               g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, d);
+       }
        mono_marshal_unlock ();
 }
 
@@ -447,7 +460,7 @@ parse_unmanaged_function_pointer_attr (MonoClass *klass, MonoMethodPInvoke *piin
                 * construct it.
                 */
                cinfo = mono_custom_attrs_from_class (klass);
-               if (cinfo) {
+               if (cinfo && !mono_runtime_get_no_exec ()) {
                        attr = (MonoReflectionUnmanagedFunctionPointerAttribute*)mono_custom_attrs_get_attr (cinfo, UnmanagedFunctionPointerAttribute);
                        if (attr) {
                                piinfo->piflags = (attr->call_conv << 8) | (attr->charset ? (attr->charset - 1) * 2 : 1) | attr->set_last_error;
@@ -461,9 +474,7 @@ parse_unmanaged_function_pointer_attr (MonoClass *klass, MonoMethodPInvoke *piin
 MonoDelegate*
 mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
 {
-#ifdef HAVE_MOVING_COLLECTOR
        guint32 gchandle;
-#endif
        MonoDelegate *d;
 
        if (ftn == NULL)
@@ -473,17 +484,17 @@ mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
        if (delegate_hash_table == NULL)
                delegate_hash_table = delegate_hash_table_new ();
 
-#ifdef HAVE_MOVING_COLLECTOR
-       gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, ftn));
-       mono_marshal_unlock ();
-       if (gchandle)
-               d = (MonoDelegate*)mono_gchandle_get_target (gchandle);
-       else
-               d = NULL;
-#else
-       d = g_hash_table_lookup (delegate_hash_table, ftn);
-       mono_marshal_unlock ();
-#endif
+       if (mono_gc_is_moving ()) {
+               gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, ftn));
+               mono_marshal_unlock ();
+               if (gchandle)
+                       d = (MonoDelegate*)mono_gchandle_get_target (gchandle);
+               else
+                       d = NULL;
+       } else {
+               d = g_hash_table_lookup (delegate_hash_table, ftn);
+               mono_marshal_unlock ();
+       }
        if (d == NULL) {
                /* This is a native function, so construct a delegate for it */
                MonoMethodSignature *sig;
@@ -545,10 +556,13 @@ mono_delegate_free_ftnptr (MonoDelegate *delegate)
        if (ptr) {
                uint32_t gchandle;
                void **method_data;
+               MonoMethod *method;
+
                ji = mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr));
                g_assert (ji);
 
-               method_data = ((MonoMethodWrapper*)ji->method)->method_data;
+               method = mono_jit_info_get_method (ji);
+               method_data = ((MonoMethodWrapper*)method)->method_data;
 
                /*the target gchandle is the first entry after size and the wrapper itself.*/
                gchandle = GPOINTER_TO_UINT (method_data [2]);
@@ -556,7 +570,7 @@ mono_delegate_free_ftnptr (MonoDelegate *delegate)
                if (gchandle)
                        mono_gchandle_free (gchandle);
 
-               mono_runtime_free_method (mono_object_domain (delegate), ji->method);
+               mono_runtime_free_method (mono_object_domain (delegate), method);
        }
 }
 
@@ -587,11 +601,13 @@ mono_array_to_savearray (MonoArray *array)
 gpointer
 mono_array_to_lparray (MonoArray *array)
 {
+#ifndef DISABLE_COM
        gpointer *nativeArray = NULL;
        int nativeArraySize = 0;
 
        int i = 0;
        MonoClass *klass;
+#endif
 
        if (!array)
                return NULL;
@@ -603,14 +619,12 @@ mono_array_to_lparray (MonoArray *array)
        case MONO_TYPE_VOID:
                g_assert_not_reached ();
                break;
-#ifndef DISABLE_COM
        case MONO_TYPE_CLASS:
                nativeArraySize = array->max_length;
                nativeArray = malloc(sizeof(gpointer) * nativeArraySize);
                for(i = 0; i < nativeArraySize; ++i)    
                        nativeArray[i] = ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal(((gpointer*)array->vector)[i]);
                return nativeArray;
-#endif
        case MONO_TYPE_U1:
        case MONO_TYPE_BOOLEAN:
        case MONO_TYPE_I1:
@@ -689,6 +703,12 @@ mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *elclas
                g_assert_not_reached ();
 }
 
+static void
+mono_byvalarray_to_byte_array (MonoArray *arr, gpointer native_arr, guint32 elnum)
+{
+       mono_byvalarray_to_array (arr, native_arr, mono_defaults.byte_class, elnum);
+}
+
 static void
 mono_array_to_byvalarray (gpointer native_arr, MonoArray *arr, MonoClass *elclass, guint32 elnum)
 {
@@ -712,6 +732,12 @@ mono_array_to_byvalarray (gpointer native_arr, MonoArray *arr, MonoClass *elclas
        }
 }
 
+static void
+mono_array_to_byte_byvalarray (gpointer native_arr, MonoArray *arr, guint32 elnum)
+{
+       mono_array_to_byvalarray (native_arr, arr, mono_defaults.byte_class, elnum);
+}
+
 void
 mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text)
 {
@@ -731,13 +757,12 @@ mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text)
                items_written = mono_stringbuilder_capacity (sb);
        
        if (!error) {
-               if (! sb->str || sb->str == sb->cached_str) {
+               if (! sb->str || sb->str == sb->cached_str)
                        MONO_OBJECT_SETREF (sb, str, mono_string_new_size (mono_domain_get (), items_written));
-                       sb->cached_str = NULL;
-               }
                
                memcpy (mono_string_chars (sb->str), ut, items_written * 2);
                sb->length = items_written;
+               sb->cached_str = NULL;
        } else 
                g_error_free (error);
 
@@ -1029,6 +1054,8 @@ mono_string_new_len_wrapper (const char *text, guint length)
        return mono_string_new_len (mono_domain_get (), text, length);
 }
 
+#ifndef DISABLE_JIT
+#ifndef DISABLE_REMOTING
 static int
 mono_mb_emit_proxy_check (MonoMethodBuilder *mb, int branch_code)
 {
@@ -1078,6 +1105,7 @@ mono_mb_emit_contextbound_check (MonoMethodBuilder *mb, int branch_code)
        mono_mb_emit_icon (mb, 0);
        return mono_mb_emit_branch (mb, branch_code);
 }
+#endif /* DISABLE_REMOTING */
 
 /*
  * mono_mb_emit_exception_marshal_directive:
@@ -1098,6 +1126,8 @@ mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, char *msg)
        mono_mb_emit_exception_full (mb, "System.Runtime.InteropServices", "MarshalDirectiveException", s);
 }
 
+#endif /* !DISABLE_JIT */
+
 guint
 mono_type_to_ldind (MonoType *type)
 {
@@ -1209,6 +1239,8 @@ handle_enum:
        return -1;
 }
 
+#ifndef DISABLE_JIT
+
 static void
 emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
 {
@@ -1337,9 +1369,8 @@ emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
                mono_mb_emit_ldloc (mb, 1);
                mono_mb_emit_byte (mb, CEE_LDIND_REF);
                mono_mb_emit_ldloc (mb, 0);
-               mono_mb_emit_ptr (mb, mono_defaults.byte_class);
                mono_mb_emit_icon (mb, mspec->data.array_data.num_elem);
-               mono_mb_emit_icall (mb, mono_byvalarray_to_array);
+               mono_mb_emit_icall (mb, mono_byvalarray_to_byte_array);
                break;
        }
        case MONO_MARSHAL_CONV_STR_BYVALSTR: 
@@ -1712,9 +1743,8 @@ emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
                mono_mb_emit_ldloc (mb, 1);
                mono_mb_emit_ldloc (mb, 0);     
                mono_mb_emit_byte (mb, CEE_LDIND_REF);
-               mono_mb_emit_ptr (mb, mono_defaults.byte_class);
                mono_mb_emit_icon (mb, mspec->data.array_data.num_elem);
-               mono_mb_emit_icall (mb, mono_array_to_byvalarray);
+               mono_mb_emit_icall (mb, mono_array_to_byte_byvalarray);
                mono_mb_patch_short_branch (mb, pos);
                break;
        }
@@ -1968,13 +1998,13 @@ emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_obje
                                break;
                        }
                        case MONO_TYPE_OBJECT: {
-                               mono_init_com_types ();
+#ifndef DISABLE_COM
                                if (to_object) {
                                        static MonoMethod *variant_clear = NULL;
                                        static MonoMethod *get_object_for_native_variant = NULL;
 
                                        if (!variant_clear)
-                                               variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
+                                               variant_clear = mono_class_get_method_from_name (mono_class_get_variant_class (), "Clear", 0);
                                        if (!get_object_for_native_variant)
                                                get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
                                        mono_mb_emit_ldloc (mb, 1);
@@ -1995,7 +2025,11 @@ emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_obje
                                        mono_mb_emit_byte(mb, CEE_LDIND_REF);
                                        mono_mb_emit_ldloc (mb, 1);
                                        mono_mb_emit_managed_call (mb, get_native_variant_for_object, NULL);
-                                       }
+                               }
+#else
+                               char *msg = g_strdup_printf ("COM support was disabled at compilation time.");
+                               mono_mb_emit_exception_marshal_directive (mb, msg);
+#endif
                                break;
                        }
 
@@ -2097,6 +2131,8 @@ mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder *mb)
        emit_thread_interrupt_checkpoint (mb);
 }
 
+#endif /* DISABLE_JIT */
+
 static MonoAsyncResult *
 mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
 {
@@ -2113,10 +2149,11 @@ mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
        if (mcast_delegate->prev != NULL)
                mono_raise_exception (mono_get_exception_argument (NULL, "The delegate must have only one target"));
 
+#ifndef DISABLE_REMOTING
        if (delegate->target && mono_object_class (delegate->target) == mono_defaults.transparent_proxy_class) {
 
                MonoTransparentProxy* tp = (MonoTransparentProxy *)delegate->target;
-               if (!tp->remote_class->proxy_class->contextbound || tp->rp->context != (MonoObject *) mono_context_get ()) {
+               if (!mono_class_is_contextbound (tp->remote_class->proxy_class) || tp->rp->context != (MonoObject *) mono_context_get ()) {
 
                        /* If the target is a proxy, make a direct call. Is proxy's work
                        // to make the call asynchronous.
@@ -2140,6 +2177,7 @@ mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
                        return ares;
                }
        }
+#endif
 
        klass = delegate->object.vtable->klass;
 
@@ -2155,6 +2193,8 @@ mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
        return mono_thread_pool_add ((MonoObject *)delegate, msg, async_callback, state);
 }
 
+#ifndef DISABLE_JIT
+
 static int
 mono_mb_emit_save_args (MonoMethodBuilder *mb, MonoMethodSignature *sig, gboolean save_this)
 {
@@ -2197,6 +2237,8 @@ mono_mb_emit_save_args (MonoMethodBuilder *mb, MonoMethodSignature *sig, gboolea
        return params_var;
 }
 
+#endif /* DISABLE_JIT */
+
 static char*
 mono_signature_to_name (MonoMethodSignature *sig, const char *prefix)
 {
@@ -2550,13 +2592,21 @@ mono_marshal_method_from_wrapper (MonoMethod *wrapper)
        case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
        case MONO_WRAPPER_XDOMAIN_INVOKE:
        case MONO_WRAPPER_SYNCHRONIZED:
+       case MONO_WRAPPER_UNBOX:
                res = mono_marshal_get_wrapper_info (wrapper);
                if (res == NULL)
                        return wrapper;
-               return res;
+               if (wrapper->is_inflated)
+                       /*
+                        * A method cannot be inflated and a wrapper at the same time, so the wrapper info
+                        * contains an uninflated method.
+                        */
+                       return mono_class_inflate_generic_method (res, mono_method_get_context (wrapper));
+               else
+                       return res;
        case MONO_WRAPPER_MANAGED_TO_NATIVE:
                info = mono_marshal_get_wrapper_info (wrapper);
-               if (info && (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT))
+               if (info && (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT || info->subtype == WRAPPER_SUBTYPE_PINVOKE))
                        return info->d.managed_to_native.method;
                else
                        return NULL;
@@ -2647,6 +2697,130 @@ get_wrapper_target_class (MonoImage *image)
        return klass;
 }
 
+/*
+ * Wrappers for generic methods should be instances of generic wrapper methods, i.e .the wrapper for Sort<int> should be
+ * an instance of the wrapper for Sort<T>. This is required for full-aot to work.
+ */
+
+/*
+ * check_generic_wrapper_cache:
+ *
+ *   Check CACHE for the wrapper of the generic instance ORIG_METHOD, and return it if it is found.
+ * KEY should be the key for ORIG_METHOD in the cache, while DEF_KEY should be the key of its
+ * generic method definition.
+ */
+static MonoMethod*
+check_generic_wrapper_cache (GHashTable *cache, MonoMethod *orig_method, gpointer key, gpointer def_key)
+{
+       MonoMethod *res;
+       MonoMethod *inst, *def;
+       MonoGenericContext *ctx;
+       MonoMethod *def_method;
+
+       g_assert (orig_method->is_inflated);
+       def_method = ((MonoMethodInflated*)orig_method)->declaring;
+       ctx = mono_method_get_context (orig_method);
+
+       /*
+        * Look for the instance
+        */
+       res = mono_marshal_find_in_cache (cache, key);
+       if (res)
+               return res;
+
+       /*
+        * Look for the definition
+        */
+       def = mono_marshal_find_in_cache (cache, def_key);
+       if (def) {
+               inst = mono_class_inflate_generic_method (def, ctx);
+               /* Cache it */
+               mono_memory_barrier ();
+               mono_marshal_lock ();
+               res = g_hash_table_lookup (cache, key);
+               if (!res) {
+                       g_hash_table_insert (cache, key, inst);
+                       res = inst;
+               }
+               mono_marshal_unlock ();
+               return res;
+       }
+       return NULL;
+}
+
+static MonoMethod*
+cache_generic_wrapper (GHashTable *cache, MonoMethod *orig_method, MonoMethod *def, MonoGenericContext *ctx, gpointer key)
+{
+       MonoMethod *inst, *res;
+
+       /*
+        * We use the same cache for the generic definition and the instances.
+        */
+       inst = mono_class_inflate_generic_method (def, ctx);
+       mono_memory_barrier ();
+       mono_marshal_lock ();
+       res = g_hash_table_lookup (cache, key);
+       if (!res) {
+               g_hash_table_insert (cache, key, inst);
+               res = inst;
+       }
+       mono_marshal_unlock ();
+       return res;
+}
+
+static MonoMethod*
+check_generic_delegate_wrapper_cache (GHashTable *cache, MonoMethod *orig_method, MonoMethod *def_method, MonoGenericContext *ctx)
+{
+       MonoMethod *res;
+       MonoMethod *inst, *def;
+
+       /*
+        * Look for the instance
+        */
+       res = mono_marshal_find_in_cache (cache, orig_method->klass);
+       if (res)
+               return res;
+
+       /*
+        * Look for the definition
+        */
+       def = mono_marshal_find_in_cache (cache, def_method->klass);
+       if (def) {
+               inst = mono_class_inflate_generic_method (def, ctx);
+               /* Cache it */
+               mono_memory_barrier ();
+               mono_marshal_lock ();
+               res = g_hash_table_lookup (cache, orig_method->klass);
+               if (!res) {
+                       g_hash_table_insert (cache, orig_method->klass, inst);
+                       res = inst;
+               }
+               mono_marshal_unlock ();
+               return res;
+       }
+       return NULL;
+}
+
+static MonoMethod*
+cache_generic_delegate_wrapper (GHashTable *cache, MonoMethod *orig_method, MonoMethod *def, MonoGenericContext *ctx)
+{
+       MonoMethod *inst, *res;
+
+       /*
+        * We use the same cache for the generic definition and the instances.
+        */
+       inst = mono_class_inflate_generic_method (def, ctx);
+       mono_memory_barrier ();
+       mono_marshal_lock ();
+       res = g_hash_table_lookup (cache, orig_method->klass);
+       if (!res) {
+               g_hash_table_insert (cache, orig_method->klass, inst);
+               res = inst;
+       }
+       mono_marshal_unlock ();
+       return res;
+}
+
 MonoMethod *
 mono_marshal_get_delegate_begin_invoke (MonoMethod *method)
 {
@@ -2656,32 +2830,65 @@ mono_marshal_get_delegate_begin_invoke (MonoMethod *method)
        GHashTable *cache;
        int params_var;
        char *name;
+       MonoGenericContext *ctx = NULL;
+       MonoMethod *orig_method = NULL;
 
        g_assert (method && method->klass->parent == mono_defaults.multicastdelegate_class &&
                  !strcmp (method->name, "BeginInvoke"));
 
+       /*
+        * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
+        */
+       if (method->is_inflated) {
+               orig_method = method;
+               ctx = &((MonoMethodInflated*)method)->context;
+               method = ((MonoMethodInflated*)method)->declaring;
+       }
+
        sig = mono_signature_no_pinvoke (method);
 
-       cache = get_cache (&method->klass->image->delegate_begin_invoke_cache,
-                                          (GHashFunc)mono_signature_hash, 
-                                          (GCompareFunc)mono_metadata_signature_equal);
-       if ((res = mono_marshal_find_in_cache (cache, sig)))
-               return res;
+       /*
+        * Check cache
+        */
+       if (ctx) {
+               cache = get_cache (&method->klass->image->delegate_begin_invoke_generic_cache, mono_aligned_addr_hash, NULL);
+               res = check_generic_delegate_wrapper_cache (cache, orig_method, method, ctx);
+               if (res)
+                       return res;
+       } else {
+               cache = get_cache (&method->klass->image->delegate_begin_invoke_cache,
+                                                  (GHashFunc)mono_signature_hash, 
+                                                  (GCompareFunc)mono_metadata_signature_equal);
+               if ((res = mono_marshal_find_in_cache (cache, sig)))
+                       return res;
+       }
 
        g_assert (sig->hasthis);
 
        name = mono_signature_to_name (sig, "begin_invoke");
-       mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE);
+       if (ctx)
+               mb = mono_mb_new (method->klass, name, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE);
+       else
+               mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE);
        g_free (name);
 
+#ifndef DISABLE_JIT
        params_var = mono_mb_emit_save_args (mb, sig, FALSE);
 
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_ldloc (mb, params_var);
        mono_mb_emit_icall (mb, mono_delegate_begin_invoke);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
+       if (ctx) {
+               MonoMethod *def;
+               def = mono_mb_create_and_cache (cache, method->klass, mb, sig, sig->param_count + 16);
+               res = cache_generic_delegate_wrapper (cache, orig_method, def, ctx);
+       } else {
+               res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
+       }
 
-       res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
        return res;
 }
@@ -2729,14 +2936,17 @@ mono_delegate_end_invoke (MonoDelegate *delegate, gpointer *params)
                return NULL;
        }
 
-       if (delegate->target && mono_object_class (delegate->target) == mono_defaults.transparent_proxy_class) {
+#ifndef DISABLE_REMOTING
+       if (delegate->target && mono_object_is_transparent_proxy (delegate->target)) {
                MonoTransparentProxy* tp = (MonoTransparentProxy *)delegate->target;
                msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
                mono_message_init (domain, msg, delegate->method_info, NULL);
                msg->call_type = CallType_EndInvoke;
                MONO_OBJECT_SETREF (msg, async_result, ares);
                res = mono_remoting_invoke ((MonoObject *)tp->rp, msg, &exc, &out_args);
-       } else {
+       } else
+#endif
+       {
                res = mono_thread_pool_finish (ares, &out_args, &exc);
        }
 
@@ -2756,6 +2966,8 @@ mono_delegate_end_invoke (MonoDelegate *delegate, gpointer *params)
        return res;
 }
 
+#ifndef DISABLE_JIT
+
 static void
 mono_mb_emit_restore_result (MonoMethodBuilder *mb, MonoType *return_type)
 {
@@ -2803,6 +3015,12 @@ mono_mb_emit_restore_result (MonoMethodBuilder *mb, MonoType *return_type)
                mono_mb_emit_op (mb, CEE_LDOBJ, klass);
                break;
        }
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR: {
+               MonoClass *klass = mono_class_from_mono_type (return_type);
+               mono_mb_emit_op (mb, CEE_UNBOX_ANY, klass);
+               break;
+       }
        default:
                g_warning ("type 0x%x not handled", return_type->type);
                g_assert_not_reached ();
@@ -2811,6 +3029,8 @@ mono_mb_emit_restore_result (MonoMethodBuilder *mb, MonoType *return_type)
        mono_mb_emit_byte (mb, CEE_RET);
 }
 
+#endif /* DISABLE_JIT */
+
 MonoMethod *
 mono_marshal_get_delegate_end_invoke (MonoMethod *method)
 {
@@ -2820,24 +3040,49 @@ mono_marshal_get_delegate_end_invoke (MonoMethod *method)
        GHashTable *cache;
        int params_var;
        char *name;
+       MonoGenericContext *ctx = NULL;
+       MonoMethod *orig_method = NULL;
 
        g_assert (method && method->klass->parent == mono_defaults.multicastdelegate_class &&
                  !strcmp (method->name, "EndInvoke"));
 
+       /*
+        * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
+        */
+       if (method->is_inflated) {
+               orig_method = method;
+               ctx = &((MonoMethodInflated*)method)->context;
+               method = ((MonoMethodInflated*)method)->declaring;
+       }
+
        sig = mono_signature_no_pinvoke (method);
 
-       cache = get_cache (&method->klass->image->delegate_end_invoke_cache,
-                                          (GHashFunc)mono_signature_hash, 
-                                          (GCompareFunc)mono_metadata_signature_equal);
-       if ((res = mono_marshal_find_in_cache (cache, sig)))
-               return res;
+       /*
+        * Check cache
+        */
+       if (ctx) {
+               cache = get_cache (&method->klass->image->delegate_end_invoke_generic_cache, mono_aligned_addr_hash, NULL);
+               res = check_generic_delegate_wrapper_cache (cache, orig_method, method, ctx);
+               if (res)
+                       return res;
+       } else {
+               cache = get_cache (&method->klass->image->delegate_end_invoke_cache,
+                                                  (GHashFunc)mono_signature_hash, 
+                                                  (GCompareFunc)mono_metadata_signature_equal);
+               if ((res = mono_marshal_find_in_cache (cache, sig)))
+                       return res;
+       }
 
        g_assert (sig->hasthis);
 
        name = mono_signature_to_name (sig, "end_invoke");
-       mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name, MONO_WRAPPER_DELEGATE_END_INVOKE);
+       if (ctx)
+               mb = mono_mb_new (method->klass, name, MONO_WRAPPER_DELEGATE_END_INVOKE);
+       else
+               mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name, MONO_WRAPPER_DELEGATE_END_INVOKE);
        g_free (name);
 
+#ifndef DISABLE_JIT
        params_var = mono_mb_emit_save_args (mb, sig, FALSE);
 
        mono_mb_emit_ldarg (mb, 0);
@@ -2849,14 +3094,23 @@ mono_marshal_get_delegate_end_invoke (MonoMethod *method)
                mono_mb_emit_byte (mb, CEE_RET);
        } else
                mono_mb_emit_restore_result (mb, sig->ret);
+#endif
 
-       res = mono_mb_create_and_cache (cache, sig,
-                                                                                mb, sig, sig->param_count + 16);
+       if (ctx) {
+               MonoMethod *def;
+               def = mono_mb_create_and_cache (cache, method->klass, mb, sig, sig->param_count + 16);
+               res = cache_generic_delegate_wrapper (cache, orig_method, def, ctx);
+       } else {
+               res = mono_mb_create_and_cache (cache, sig,
+                                                                               mb, sig, sig->param_count + 16);
+       }
        mono_mb_free (mb);
 
        return res;
 }
 
+#ifndef DISABLE_REMOTING
+
 static MonoObject *
 mono_remoting_wrapper (MonoMethod *method, gpointer *params)
 {
@@ -2873,7 +3127,7 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
        /* skip the this pointer */
        params++;
 
-       if (this->remote_class->proxy_class->contextbound && this->rp->context == (MonoObject *) mono_context_get ())
+       if (mono_class_is_contextbound (this->remote_class->proxy_class) && this->rp->context == (MonoObject *) mono_context_get ())
        {
                int i;
                MonoMethodSignature *sig = mono_method_signature (method);
@@ -2926,18 +3180,16 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
                return method;
 
        /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
-       if (method->klass->is_com_object || method->klass == mono_defaults.com_object_class) {
+#ifndef DISABLE_COM
+       if (mono_class_is_com_object (method->klass) || method->klass == mono_class_get_com_object_class ()) {
                MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
                g_assert (vtable); /*FIXME do proper error handling*/
 
-               if (!vtable->remote) {
-#ifndef DISABLE_COM
+               if (!mono_vtable_is_remote (vtable)) {
                        return mono_cominterop_get_invoke (method);
-#else
-                       g_assert_not_reached ();
-#endif
                }
        }
+#endif
 
        sig = mono_signature_no_pinvoke (method);
 
@@ -2949,6 +3201,8 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
                return res;
 
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
+
+#ifndef DISABLE_JIT
        mb->method->save_lmf = 1;
 
        params_var = mono_mb_emit_save_args (mb, sig, TRUE);
@@ -2964,6 +3218,7 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
        } else {
                 mono_mb_emit_restore_result (mb, sig->ret);
        }
+#endif
 
        res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
@@ -2971,6 +3226,9 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
        return res;
 }
 
+#endif /* DISABLE_REMOTING */
+
+
 /* mono_get_xdomain_marshal_type()
  * Returns the kind of marshalling that a type needs for cross domain calls.
  */
@@ -3109,6 +3367,9 @@ mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst)
        }
 }
 
+
+#if !(defined (DISABLE_JIT) || defined (DISABLE_REMOTING))
+
 static void
 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder *mb, MonoClass *pclass)
 {
@@ -3121,16 +3382,19 @@ mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder *mb, MonoClass *pcla
 {
        mono_mb_emit_icall (mb, mono_marshal_xdomain_copy_out_value);
 }
+#endif
 
+#ifndef DISABLE_REMOTING
 /* mono_marshal_supports_fast_xdomain()
  * Returns TRUE if the method can use the fast xdomain wrapper.
  */
 static gboolean
 mono_marshal_supports_fast_xdomain (MonoMethod *method)
 {
-       return !method->klass->contextbound &&
+       return !mono_class_is_contextbound (method->klass) &&
                   !((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && (strcmp (".ctor", method->name) == 0));
 }
+#endif
 
 static gint32
 mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push)
@@ -3149,6 +3413,7 @@ mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push)
        return current_domain->domain_id;
 }
 
+#if !(defined (DISABLE_JIT) || defined (DISABLE_REMOTING))
 static void
 mono_marshal_emit_switch_domain (MonoMethodBuilder *mb)
 {
@@ -3168,6 +3433,7 @@ mono_marshal_emit_load_domain_method (MonoMethodBuilder *mb, MonoMethod *method)
        mono_mb_emit_ptr (mb, method);
        mono_mb_emit_icall (mb, mono_compile_method);
 }
+#endif
 
 /* mono_marshal_check_domain_image ()
  * Returns TRUE if the image is loaded in the specified
@@ -3194,6 +3460,8 @@ mono_marshal_check_domain_image (gint32 domain_id, MonoImage *image)
        return tmp != NULL;
 }
 
+#ifndef DISABLE_REMOTING
+
 /* mono_marshal_get_xappdomain_dispatch ()
  * Generates a method that dispatches a method call from another domain into
  * the current domain.
@@ -3236,6 +3504,7 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_XDOMAIN_DISPATCH);
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        /* Locals */
 
        loc_serialized_exc = mono_mb_add_local (mb, &byte_array_class->byval_arg);
@@ -3445,6 +3714,7 @@ 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);
+#endif
 
        res = mono_remoting_mb_create_and_cache (method, mb, csig, csig->param_count + 16);
        mono_mb_free (mb);
@@ -3521,6 +3791,7 @@ mono_marshal_get_xappdomain_invoke (MonoMethod *method)
        
        /* Locals */
 
+#ifndef DISABLE_JIT
        if (complex_count > 0)
                loc_array = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
        loc_serialized_data = mono_mb_add_local (mb, &byte_array_class->byval_arg);
@@ -3770,6 +4041,7 @@ mono_marshal_get_xappdomain_invoke (MonoMethod *method)
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif /* DISABLE_JIT */
 
        res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
@@ -3827,6 +4099,7 @@ mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
        
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
 
+#ifndef DISABLE_JIT
        for (i = 0; i <= sig->param_count; i++)
                mono_mb_emit_ldarg (mb, i);
        
@@ -3853,6 +4126,7 @@ mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
        mono_mb_patch_branch (mb, pos);
        mono_mb_emit_managed_call (mb, method, mono_method_signature (method));
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
@@ -3860,6 +4134,8 @@ mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
        return res;
 }
 
+#endif /* DISABLE_REMOTING */
+
 typedef struct
 {
        MonoMethodSignature *sig;
@@ -3916,6 +4192,10 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
        gboolean callvirt = FALSE;
        gboolean closed_over_null = FALSE;
        gboolean static_method_with_first_arg_bound = FALSE;
+       MonoGenericContext *ctx = NULL;
+       MonoGenericContainer *container = NULL;
+       MonoMethod *orig_method = NULL;
+       WrapperInfo *info;
 
        /*
         * If the delegate target is null, and the target method is not static, a virtual 
@@ -3946,17 +4226,50 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                closed_over_null = sig->param_count == mono_method_signature (del->method)->param_count;
 
        if (del && del->method && mono_method_signature (del->method)->param_count == sig->param_count + 1 && (del->method->flags & METHOD_ATTRIBUTE_STATIC)) {
+               g_assert (!callvirt);
                invoke_sig = mono_method_signature (del->method);
-               target_method = del->method;
+               target_method = NULL;
                static_method_with_first_arg_bound = TRUE;
        }
 
-       if (callvirt || static_method_with_first_arg_bound) {
+       /*
+        * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
+        */
+       if (method->is_inflated && !callvirt && !static_method_with_first_arg_bound) {
+               orig_method = method;
+               ctx = &((MonoMethodInflated*)method)->context;
+               method = ((MonoMethodInflated*)method)->declaring;
+
+               container = mono_method_get_generic_container (method);
+               if (!container)
+                       container = method->klass->generic_container;
+               g_assert (container);
+
+               invoke_sig = sig = mono_signature_no_pinvoke (method);
+       }
+
+       /*
+        * Check cache
+        */
+       if (ctx) {
+               cache = get_cache (&method->klass->image->delegate_invoke_generic_cache, mono_aligned_addr_hash, NULL);
+               res = check_generic_delegate_wrapper_cache (cache, orig_method, method, ctx);
+               if (res)
+                       return res;
+       } else if (static_method_with_first_arg_bound) {
+               cache = get_cache (&method->klass->image->delegate_bound_static_invoke_cache,
+                                                  (GHashFunc)mono_signature_hash, 
+                                                  (GCompareFunc)mono_metadata_signature_equal);
+               /*
+                * The wrapper is based on sig+invoke_sig, but sig can be derived from invoke_sig.
+                */
+               res = mono_marshal_find_in_cache (cache, invoke_sig);
+               if (res)
+                       return res;
+       } else if (callvirt) {
                GHashTable **cache_ptr;
-               if (static_method_with_first_arg_bound)
-                       cache_ptr = &method->klass->image->delegate_bound_static_invoke_cache;
-               else
-                       cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
+
+               cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
 
                /* We need to cache the signature+method pair */
                mono_marshal_lock ();
@@ -3973,7 +4286,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                cache = get_cache (&method->klass->image->delegate_invoke_cache,
                                                   (GHashFunc)mono_signature_hash, 
                                                   (GCompareFunc)mono_metadata_signature_equal);
-               if ((res = mono_marshal_find_in_cache (cache, sig)))
+               res = mono_marshal_find_in_cache (cache, sig);
+               if (res)
                        return res;
        }
 
@@ -3982,10 +4296,21 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
        if (!static_method_with_first_arg_bound)
                invoke_sig = static_sig;
 
-       name = mono_signature_to_name (sig, "invoke");
-       mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name,  MONO_WRAPPER_DELEGATE_INVOKE);
+       if (static_method_with_first_arg_bound)
+               name = mono_signature_to_name (invoke_sig, "invoke_bound");
+       else if (closed_over_null)
+               name = mono_signature_to_name (invoke_sig, "invoke_closed_over_null");
+       else if (callvirt)
+               name = mono_signature_to_name (invoke_sig, "invoke_callvirt");
+       else
+               name = mono_signature_to_name (sig, "invoke");
+       if (ctx)
+               mb = mono_mb_new (method->klass, name, MONO_WRAPPER_DELEGATE_INVOKE);
+       else
+               mb = mono_mb_new (get_wrapper_target_class (method->klass->image), name, MONO_WRAPPER_DELEGATE_INVOKE);
        g_free (name);
 
+#ifndef DISABLE_JIT
        /* allocate local 0 (object) */
        local_target = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
        local_prev = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
@@ -4019,7 +4344,10 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
        mono_mb_emit_ldloc (mb, local_prev);
        for (i = 0; i < sig->param_count; i++)
                mono_mb_emit_ldarg (mb, i + 1);
-       mono_mb_emit_op (mb, CEE_CALLVIRT, method);
+       if (ctx)
+               mono_mb_emit_op (mb, CEE_CALLVIRT, mono_class_inflate_generic_method (method, &container->context));
+       else
+               mono_mb_emit_op (mb, CEE_CALLVIRT, method);
        if (sig->ret->type != MONO_TYPE_VOID)
                mono_mb_emit_byte (mb, CEE_POP);
 
@@ -4087,9 +4415,21 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
 
        mono_mb_emit_byte (mb, CEE_RET);
 
-       if (static_method_with_first_arg_bound || callvirt) {
-               // From mono_mb_create_and_cache
-               mb->skip_visibility = 1;
+       mb->skip_visibility = 1;
+#endif /* DISABLE_JIT */
+
+       if (ctx) {
+               MonoMethod *def;
+
+               def = mono_mb_create_and_cache (cache, method->klass, mb, sig, sig->param_count + 16);
+               res = cache_generic_delegate_wrapper (cache, orig_method, def, ctx);
+       } else if (static_method_with_first_arg_bound) {
+               res = mono_mb_create_and_cache (cache, invoke_sig, mb, sig, sig->param_count + 16);
+
+               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND);
+               mono_marshal_set_wrapper_info (res, info);
+       } else if (callvirt) {
+               // From mono_mb_create_and_cache
                newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
                /*We perform double checked locking, so must fence before publishing*/
                mono_memory_barrier ();
@@ -4102,15 +4442,20 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                        if (static_method_with_first_arg_bound)
                                new_key->sig = signature_dup (del->method->klass->image, key.sig);
                        g_hash_table_insert (cache, new_key, res);
-                       mono_marshal_set_wrapper_info (res, new_key);
+
+                       info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL);
+                       mono_marshal_set_wrapper_info (res, info);
+
                        mono_marshal_unlock ();
                } else {
                        mono_marshal_unlock ();
                        mono_free_method (newm);
                }
        } else {
-               mb->skip_visibility = 1;
                res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
+
+               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_NONE);
+               mono_marshal_set_wrapper_info (res, info);
        }
        mono_mb_free (mb);
 
@@ -4207,9 +4552,12 @@ mono_marshal_get_string_ctor_signature (MonoMethod *method)
 static MonoType*
 get_runtime_invoke_type (MonoType *t, gboolean ret)
 {
-       if (t->byref)
+       if (t->byref) {
+               if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t)))
+                       return t;
                /* Can't share this with 'I' as that needs another indirection */
-               return t;
+               return &mono_defaults.int_class->this_arg;
+       }
 
        if (MONO_TYPE_IS_REFERENCE (t))
                return &mono_defaults.object_class->byval_arg;
@@ -4220,18 +4568,21 @@ get_runtime_invoke_type (MonoType *t, gboolean ret)
 
 handle_enum:
        switch (t->type) {
+               /* Can't share these as the argument needs to be loaded using sign/zero extension */
+               /*
        case MONO_TYPE_U1:
                return &mono_defaults.sbyte_class->byval_arg;
        case MONO_TYPE_U2:
                return &mono_defaults.int16_class->byval_arg;
        case MONO_TYPE_U4:
                return &mono_defaults.int32_class->byval_arg;
+               */
        case MONO_TYPE_U8:
                return &mono_defaults.int64_class->byval_arg;
        case MONO_TYPE_BOOLEAN:
-               return &mono_defaults.sbyte_class->byval_arg;
+               return &mono_defaults.byte_class->byval_arg;
        case MONO_TYPE_CHAR:
-               return &mono_defaults.int16_class->byval_arg;
+               return &mono_defaults.uint16_class->byval_arg;
        case MONO_TYPE_U:
        case MONO_TYPE_PTR:
                return &mono_defaults.int_class->byval_arg;
@@ -4274,6 +4625,8 @@ runtime_invoke_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *
                return mono_metadata_signature_equal (sig1, sig2);
 }
 
+#ifndef DISABLE_JIT
+
 /*
  * emit_invoke_call:
  *
@@ -4303,7 +4656,12 @@ emit_invoke_call (MonoMethodBuilder *mb, MonoMethod *method,
 
        if (sig->hasthis) {
                if (method->string_ctor) {
-                       mono_mb_emit_ptr (mb, string_dummy);
+                       if (mono_gc_is_moving ()) {
+                               mono_mb_emit_ptr (mb, &string_dummy);
+                               mono_mb_emit_byte (mb, CEE_LDIND_REF);
+                       } else {
+                               mono_mb_emit_ptr (mb, string_dummy);
+                       }
                } else {
                        mono_mb_emit_ldarg (mb, 0);
                }
@@ -4548,6 +4906,7 @@ emit_runtime_invoke_body (MonoMethodBuilder *mb, MonoClass *target_klass, MonoMe
        mono_mb_emit_ldloc (mb, 0);
        mono_mb_emit_byte (mb, CEE_RET);
 }
+#endif
 
 /*
  * generates IL code for the runtime invoke function 
@@ -4617,20 +4976,10 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
                /* Can't share this as we push a string as this */
                need_direct_wrapper = TRUE;
        } else {
-               if (method->klass->valuetype && mono_method_signature (method)->hasthis) {
-                       /* 
-                        * Valuetype methods receive a managed pointer as the this argument.
-                        * Create a new signature to reflect this.
-                        */
-                       callsig = signature_dup_add_this (method->klass->image, mono_method_signature (method), method->klass);
-                       /* Can't share this as it would be shared with static methods taking an IntPtr argument */
-                       need_direct_wrapper = TRUE;
-               } else {
-                       if (method->dynamic)
-                               callsig = signature_dup (method->klass->image, mono_method_signature (method));
-                       else
-                               callsig = mono_method_signature (method);
-               }
+               if (method->dynamic)
+                       callsig = signature_dup (method->klass->image, mono_method_signature (method));
+               else
+                       callsig = mono_method_signature (method);
        }
 
        target_klass = get_wrapper_target_class (method->klass->image);
@@ -4651,9 +5000,15 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
 
                callsig = mono_marshal_get_runtime_invoke_sig (callsig);
 
-               cache = get_cache (&target_klass->image->runtime_invoke_cache, 
-                                                  (GHashFunc)mono_signature_hash, 
-                                                  (GCompareFunc)runtime_invoke_signature_equal);
+               if (method->klass->valuetype && mono_method_signature (method)->hasthis)
+                       /* These have a different csig */
+                       cache = get_cache (&target_klass->image->runtime_invoke_vtype_cache,
+                                                          (GHashFunc)mono_signature_hash,
+                                                          (GCompareFunc)runtime_invoke_signature_equal);
+               else
+                       cache = get_cache (&target_klass->image->runtime_invoke_cache,
+                                                          (GHashFunc)mono_signature_hash,
+                                                          (GCompareFunc)runtime_invoke_signature_equal);
 
                /* from mono_marshal_find_in_cache */
                mono_marshal_lock ();
@@ -4677,7 +5032,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
 
        csig->ret = &mono_defaults.object_class->byval_arg;
        if (method->klass->valuetype && mono_method_signature (method)->hasthis)
-               csig->params [0] = callsig->params [0];
+               csig->params [0] = get_runtime_invoke_type (&method->klass->this_arg, FALSE);
        else
                csig->params [0] = &mono_defaults.object_class->byval_arg;
        csig->params [1] = &mono_defaults.int_class->byval_arg;
@@ -4693,6 +5048,7 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
        mb = mono_mb_new (target_klass, name,  MONO_WRAPPER_RUNTIME_INVOKE);
        g_free (name);
 
+#ifndef DISABLE_JIT
        param_names [0] = "this";
        param_names [1] = "params";
        param_names [2] = "exc";
@@ -4700,9 +5056,12 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
        mono_mb_set_param_names (mb, param_names);
 
        emit_runtime_invoke_body (mb, target_klass, method, sig, callsig, virtual, need_direct_wrapper);
+#endif
 
        if (need_direct_wrapper) {
+#ifndef DISABLE_JIT
                mb->skip_visibility = 1;
+#endif
                res = mono_mb_create_and_cache (cache, method, mb, csig, sig->param_count + 16);
                info = mono_wrapper_info_create (res, virtual ? WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL : WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT);
                info->d.runtime_invoke.method = method;
@@ -4725,6 +5084,9 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
                                g_hash_table_insert (cache, callsig, res);
                                /* Can't insert it into wrapper_hash since the key is a signature */
                                g_hash_table_insert (method->klass->image->runtime_invoke_direct_cache, method, res);
+                               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL);
+                               info->d.runtime_invoke.sig = callsig;
+                               mono_marshal_set_wrapper_info (res, info);
                        } else {
                                mono_free_method (newm);
                        }
@@ -4780,6 +5142,7 @@ mono_marshal_get_runtime_invoke_dynamic (void)
        mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_RUNTIME_INVOKE);
        g_free (name);
 
+#ifndef DISABLE_JIT
        /* allocate local 0 (object) tmp */
        mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
        /* allocate local 1 (object) exc */
@@ -4849,21 +5212,23 @@ mono_marshal_get_runtime_invoke_dynamic (void)
        mono_mb_patch_branch (mb, pos);
        //mono_mb_emit_ldloc (mb, 0);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif /* DISABLE_JIT */
 
-       mono_loader_lock ();
+       mono_marshal_lock ();
        /* double-checked locking */
        if (!method) {
                method = mono_mb_create_method (mb, csig, 16);
                info = mono_wrapper_info_create (method, WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC);
                mono_marshal_set_wrapper_info (method, info);
        }
-       mono_loader_unlock ();
+       mono_marshal_unlock ();
 
        mono_mb_free (mb);
 
        return method;
 }
 
+#ifndef DISABLE_JIT
 static void
 mono_mb_emit_auto_layout_exception (MonoMethodBuilder *mb, MonoClass *klass)
 {
@@ -4872,7 +5237,9 @@ mono_mb_emit_auto_layout_exception (MonoMethodBuilder *mb, MonoClass *klass)
 
        mono_mb_emit_exception_marshal_directive (mb, msg);
 }
+#endif
 
+#ifndef DISABLE_REMOTING
 /*
  * mono_marshal_get_ldfld_remote_wrapper:
  * @klass: The return type
@@ -4907,6 +5274,7 @@ mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass)
        sig->params [2] = &mono_defaults.int_class->byval_arg;
        sig->ret = &mono_defaults.object_class->byval_arg;
 
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_ldarg (mb, 1);
        mono_mb_emit_ldarg (mb, 2);
@@ -4922,7 +5290,8 @@ mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass)
        emit_thread_interrupt_checkpoint (mb);
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
        mono_marshal_lock ();
        res = cached;
        mono_marshal_unlock ();
@@ -5005,6 +5374,7 @@ mono_marshal_get_ldfld_wrapper (MonoType *type)
        sig->params [3] = &mono_defaults.int_class->byval_arg;
        sig->ret = &klass->byval_arg;
 
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0);
        pos0 = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
 
@@ -5080,13 +5450,18 @@ mono_marshal_get_ldfld_wrapper (MonoType *type)
                        mono_mb_emit_byte (mb, CEE_LDIND_REF);
                }
                break;
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:
+               mono_mb_emit_op (mb, CEE_LDOBJ, klass);
+               break;
        default:
                g_warning ("type %x not implemented", type->type);
                g_assert_not_reached ();
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
-       
+#endif /* DISABLE_JIT */
+
        res = mono_mb_create_and_cache (cache, klass,
                                                                        mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
@@ -5154,6 +5529,7 @@ mono_marshal_get_ldflda_wrapper (MonoType *type)
        sig->params [3] = &mono_defaults.int_class->byval_arg;
        sig->ret = &mono_defaults.int_class->byval_arg;
 
+#ifndef DISABLE_JIT
        /* if typeof (this) != transparent_proxy goto pos0 */
        mono_mb_emit_ldarg (mb, 0);
        pos0 = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
@@ -5207,7 +5583,8 @@ mono_marshal_get_ldflda_wrapper (MonoType *type)
        mono_mb_emit_byte (mb, CEE_ADD);
 
        mono_mb_emit_byte (mb, CEE_RET);
-       
+#endif
+
        res = mono_mb_create_and_cache (cache, klass,
                                                                        mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
@@ -5250,6 +5627,7 @@ mono_marshal_get_stfld_remote_wrapper (MonoClass *klass)
        sig->params [3] = &mono_defaults.object_class->byval_arg;
        sig->ret = &mono_defaults.void_class->byval_arg;
 
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_ldarg (mb, 1);
        mono_mb_emit_ldarg (mb, 2);
@@ -5267,7 +5645,8 @@ mono_marshal_get_stfld_remote_wrapper (MonoClass *klass)
        emit_thread_interrupt_checkpoint (mb);
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
        mono_marshal_lock ();
        res = cached;
        mono_marshal_unlock ();
@@ -5350,6 +5729,7 @@ mono_marshal_get_stfld_wrapper (MonoType *type)
        sig->params [4] = &klass->byval_arg;
        sig->ret = &mono_defaults.void_class->byval_arg;
 
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0);
        pos = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
 
@@ -5402,6 +5782,8 @@ mono_marshal_get_stfld_wrapper (MonoType *type)
                mono_mb_emit_op (mb, CEE_STOBJ, klass);
                break;
        case MONO_TYPE_GENERICINST:
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:
                mono_mb_emit_op (mb, CEE_STOBJ, klass);
                break;
        default:
@@ -5410,13 +5792,15 @@ mono_marshal_get_stfld_wrapper (MonoType *type)
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
-       
+#endif
+
        res = mono_mb_create_and_cache (cache, klass,
                                                                        mb, sig, sig->param_count + 16);
        mono_mb_free (mb);
        
        return res;
 }
+#endif /* DISABLE_REMOTING */
 
 /*
  * generates IL code for the icall wrapper (the generated method
@@ -5444,16 +5828,20 @@ mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gcon
        else
                csig2 = signature_dup (mono_defaults.corlib, sig);
 
+#ifndef DISABLE_JIT
        if (sig->hasthis)
                mono_mb_emit_byte (mb, CEE_LDARG_0);
 
        for (i = 0; i < sig->param_count; i++)
                mono_mb_emit_ldarg (mb, i + sig->hasthis);
 
-       mono_mb_emit_native_call (mb, csig2, (gpointer) func);
+       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+       mono_mb_emit_op (mb, CEE_MONO_JIT_ICALL_ADDR, (gpointer)func);
+       mono_mb_emit_calli (mb, csig2);
        if (check_exceptions)
                emit_thread_interrupt_checkpoint (mb);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        csig = signature_dup (mono_defaults.corlib, sig);
        csig->pinvoke = 0;
@@ -5464,6 +5852,7 @@ mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gcon
        mono_mb_free (mb);
 
        info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_ICALL_WRAPPER);
+       info->d.icall.func = (gpointer)func;
        mono_marshal_set_wrapper_info (res, info);
        
        return res;
@@ -5475,6 +5864,11 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
                                         int conv_arg, MonoType **conv_arg_type, 
                                         MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       if (action == MARSHAL_ACTION_CONV_IN && t->type == MONO_TYPE_VALUETYPE)
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+       return conv_arg;
+#else
        MonoType *mtype;
        MonoClass *mklass;
        static MonoClass *ICustomMarshaler = NULL;
@@ -5768,8 +6162,9 @@ handle_exception:
        default:
                g_assert_not_reached ();
        }
-               
        return conv_arg;
+#endif
+
 }
 
 static int
@@ -5778,6 +6173,7 @@ emit_marshal_asany (EmitMarshalContext *m, int argnum, MonoType *t,
                                        int conv_arg, MonoType **conv_arg_type, 
                                        MarshalAction action)
 {
+#ifndef DISABLE_JIT
        MonoMethodBuilder *mb = m->mb;
 
        switch (action) {
@@ -5814,7 +6210,7 @@ emit_marshal_asany (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
@@ -5824,6 +6220,7 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                                        int conv_arg, MonoType **conv_arg_type, 
                                        MarshalAction action)
 {
+#ifndef DISABLE_JIT
        MonoMethodBuilder *mb = m->mb;
        MonoClass *klass, *date_time_class;
        int pos = 0, pos2;
@@ -6046,6 +6443,9 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                        break;
                }
 
+               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_IN) && !(t->attrs & PARAM_ATTRIBUTE_OUT))
+                       break;
+
                /* Check for null */
                mono_mb_emit_ldarg (mb, argnum);
                pos2 = mono_mb_emit_branch (mb, CEE_BRFALSE);
@@ -6097,7 +6497,7 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
@@ -6107,6 +6507,16 @@ emit_marshal_string (EmitMarshalContext *m, int argnum, MonoType *t,
                                         int conv_arg, MonoType **conv_arg_type, 
                                         MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       switch (action) {
+       case MARSHAL_ACTION_CONV_IN:
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               break;
+       case MARSHAL_ACTION_MANAGED_CONV_IN:
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               break;
+       }
+#else
        MonoMethodBuilder *mb = m->mb;
        MonoMarshalNative encoding = mono_marshal_get_string_encoding (m->piinfo, spec);
        MonoMarshalConv conv = mono_marshal_get_string_to_ptr_conv (m->piinfo, spec);
@@ -6264,15 +6674,20 @@ emit_marshal_string (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
+
 static int
 emit_marshal_safehandle (EmitMarshalContext *m, int argnum, MonoType *t, 
                         MonoMarshalSpec *spec, int conv_arg, 
                         MonoType **conv_arg_type, MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       if (action == MARSHAL_ACTION_CONV_IN)
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+#else
        MonoMethodBuilder *mb = m->mb;
 
        switch (action){
@@ -6423,15 +6838,20 @@ emit_marshal_safehandle (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                printf ("Unhandled case for MarshalAction: %d\n", action);
        }
-
+#endif
        return conv_arg;
 }
 
+
 static int
 emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t, 
                        MonoMarshalSpec *spec, int conv_arg, 
                        MonoType **conv_arg_type, MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       if (action == MARSHAL_ACTION_CONV_IN)
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+#else
        MonoMethodBuilder *mb = m->mb;
 
        switch (action){
@@ -6484,16 +6904,21 @@ emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                fprintf (stderr, "Unhandled case for MarshalAction: %d\n", action);
        }
-
+#endif
        return conv_arg;
 }
 
+
 static int
 emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                     MonoMarshalSpec *spec, 
                     int conv_arg, MonoType **conv_arg_type, 
                     MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       if (action == MARSHAL_ACTION_CONV_IN)
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+#else
        MonoMethodBuilder *mb = m->mb;
        MonoClass *klass = mono_class_from_mono_type (t);
        int pos, pos2, loc;
@@ -6538,7 +6963,7 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                        }
 #endif
 
-                       if (t->byref && !t->attrs & PARAM_ATTRIBUTE_IN && t->attrs & PARAM_ATTRIBUTE_OUT)
+                       if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && (t->attrs & PARAM_ATTRIBUTE_OUT))
                                break;
 
                        if (conv == -1) {
@@ -6967,10 +7392,14 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
+#ifndef DISABLE_JIT
+
+#ifndef DISABLE_COM
+
 static int
 emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
                     MonoMarshalSpec *spec, 
@@ -6981,8 +7410,6 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
        static MonoMethod *get_object_for_native_variant = NULL;
        static MonoMethod *get_native_variant_for_object = NULL;
 
-       mono_init_com_types ();
-       
        if (!get_object_for_native_variant)
                get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
        g_assert (get_object_for_native_variant);
@@ -6993,12 +7420,12 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
 
        switch (action) {
        case MARSHAL_ACTION_CONV_IN: {
-               conv_arg = mono_mb_add_local (mb, &mono_defaults.variant_class->byval_arg);
+               conv_arg = mono_mb_add_local (mb, &mono_class_get_variant_class ()->byval_arg);
                
                if (t->byref)
-                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
+                       *conv_arg_type = &mono_class_get_variant_class ()->this_arg;
                else
-                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
+                       *conv_arg_type = &mono_class_get_variant_class ()->byval_arg;
 
                if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
                        break;
@@ -7015,7 +7442,7 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
                static MonoMethod *variant_clear = NULL;
 
                if (!variant_clear)
-                       variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
+                       variant_clear = mono_class_get_method_from_name (mono_class_get_variant_class (), "Clear", 0);
                g_assert (variant_clear);
 
 
@@ -7048,9 +7475,9 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
                conv_arg = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
                if (t->byref)
-                       *conv_arg_type = &mono_defaults.variant_class->this_arg;
+                       *conv_arg_type = &mono_class_get_variant_class ()->this_arg;
                else
-                       *conv_arg_type = &mono_defaults.variant_class->byval_arg;
+                       *conv_arg_type = &mono_class_get_variant_class ()->byval_arg;
 
                if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
                        break;
@@ -7086,6 +7513,9 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
        return conv_arg;
 }
 
+#endif /* DISABLE_COM */
+#endif /* DISABLE_JIT */
+
 static gboolean
 mono_pinvoke_is_unicode (MonoMethodPInvoke *piinfo)
 {
@@ -7104,12 +7534,23 @@ mono_pinvoke_is_unicode (MonoMethodPInvoke *piinfo)
        }
 }
 
+
 static int
 emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                                        MonoMarshalSpec *spec, 
                                        int conv_arg, MonoType **conv_arg_type, 
                                        MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       switch (action) {
+       case MARSHAL_ACTION_CONV_IN:
+               *conv_arg_type = &mono_defaults.object_class->byval_arg;
+               break;
+       case MARSHAL_ACTION_MANAGED_CONV_IN:
+               *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               break;
+       }
+#else
        MonoMethodBuilder *mb = m->mb;
        MonoClass *klass = mono_class_from_mono_type (t);
        gboolean need_convert, need_free;
@@ -7782,16 +8223,82 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
+static MonoType*
+marshal_boolean_conv_in_get_local_type (MonoMarshalSpec *spec, guint8 *ldc_op /*out*/)
+{
+       if (spec == NULL) {
+               return &mono_defaults.int32_class->byval_arg;
+       } else {
+               switch (spec->native) {
+               case MONO_NATIVE_I1:
+               case MONO_NATIVE_U1:
+                       return &mono_defaults.byte_class->byval_arg;
+               case MONO_NATIVE_VARIANTBOOL:
+                       if (ldc_op) *ldc_op = CEE_LDC_I4_M1;
+                       return &mono_defaults.int16_class->byval_arg;
+               case MONO_NATIVE_BOOLEAN:
+                       return &mono_defaults.int32_class->byval_arg;
+               default:
+                       g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
+                       return &mono_defaults.int32_class->byval_arg;
+               }
+       }
+}
+
+static MonoClass*
+marshal_boolean_managed_conv_in_get_conv_arg_class (MonoMarshalSpec *spec, guint8 *ldop/*out*/)
+{
+       MonoClass* conv_arg_class = mono_defaults.int32_class;
+       if (spec) {
+               switch (spec->native) {
+               case MONO_NATIVE_I1:
+               case MONO_NATIVE_U1:
+                       conv_arg_class = mono_defaults.byte_class;
+                       if (ldop) *ldop = CEE_LDIND_I1;
+                       break;
+               case MONO_NATIVE_VARIANTBOOL:
+                       conv_arg_class = mono_defaults.int16_class;
+                       if (ldop) *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);
+               }
+       }
+       return conv_arg_class;
+}
+
 static int
 emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                      MonoMarshalSpec *spec, 
                      int conv_arg, MonoType **conv_arg_type, 
                      MarshalAction action)
 {
+#ifdef DISABLE_JIT
+       switch (action) {
+       case MARSHAL_ACTION_CONV_IN:
+               if (t->byref)
+                       *conv_arg_type = &mono_defaults.int_class->byval_arg;
+               else
+                       *conv_arg_type = marshal_boolean_conv_in_get_local_type (spec, NULL);
+               break;
+
+       case MARSHAL_ACTION_MANAGED_CONV_IN: {
+               MonoClass* conv_arg_class = marshal_boolean_managed_conv_in_get_conv_arg_class (spec, NULL);
+               if (t->byref)
+                       *conv_arg_type = &conv_arg_class->this_arg;
+               else
+                       *conv_arg_type = &conv_arg_class->byval_arg;
+               break;
+       }
+
+       }
+#else
        MonoMethodBuilder *mb = m->mb;
 
        switch (action) {
@@ -7800,27 +8307,7 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                int label_false;
                guint8 ldc_op = CEE_LDC_I4_1;
 
-               if (spec == NULL) {
-                       local_type = &mono_defaults.int32_class->byval_arg;
-               } else {
-                       switch (spec->native) {
-                       case MONO_NATIVE_I1:
-                       case MONO_NATIVE_U1:
-                               local_type = &mono_defaults.byte_class->byval_arg;
-                               break;
-                       case MONO_NATIVE_VARIANTBOOL:
-                               local_type = &mono_defaults.int16_class->byval_arg;
-                               ldc_op = CEE_LDC_I4_M1;
-                               break;
-                       case MONO_NATIVE_BOOLEAN:
-                               local_type = &mono_defaults.int32_class->byval_arg;
-                               break;
-                       default:
-                               g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
-                               local_type = &mono_defaults.int32_class->byval_arg;
-                               break;
-                       }
-               }
+               local_type = marshal_boolean_conv_in_get_local_type (spec, &ldc_op);
                if (t->byref)
                        *conv_arg_type = &mono_defaults.int_class->byval_arg;
                else
@@ -7878,26 +8365,9 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
                guint8 ldop = CEE_LDIND_I4;
                int label_null, label_false;
 
+               conv_arg_class = marshal_boolean_managed_conv_in_get_conv_arg_class (spec, &ldop);
                conv_arg = mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
 
-               if (spec) {
-                       switch (spec->native) {
-                       case MONO_NATIVE_I1:
-                       case MONO_NATIVE_U1:
-                               conv_arg_class = mono_defaults.byte_class;
-                               ldop = CEE_LDIND_I1;
-                               break;
-                       case MONO_NATIVE_VARIANTBOOL:
-                               conv_arg_class = mono_defaults.int16_class;
-                               ldop = CEE_LDIND_I2;
-                               break;
-                       case MONO_NATIVE_BOOLEAN:
-                               break;
-                       default:
-                               g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
-                       }
-               }
-
                if (t->byref)
                        *conv_arg_type = &conv_arg_class->this_arg;
                else
@@ -7969,7 +8439,7 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                g_assert_not_reached ();
        }
-
+#endif
        return conv_arg;
 }
 
@@ -7978,6 +8448,7 @@ emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
                  MonoMarshalSpec *spec, int conv_arg, 
                  MonoType **conv_arg_type, MarshalAction action)
 {
+#ifndef DISABLE_JIT
        MonoMethodBuilder *mb = m->mb;
 
        switch (action) {
@@ -8003,7 +8474,7 @@ emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                break;
        }
-
+#endif
        return conv_arg;
 }
 
@@ -8012,6 +8483,7 @@ emit_marshal_char (EmitMarshalContext *m, int argnum, MonoType *t,
                   MonoMarshalSpec *spec, int conv_arg, 
                   MonoType **conv_arg_type, MarshalAction action)
 {
+#ifndef DISABLE_JIT
        MonoMethodBuilder *mb = m->mb;
 
        switch (action) {
@@ -8030,7 +8502,7 @@ emit_marshal_char (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                break;
        }
-
+#endif
        return conv_arg;
 }
 
@@ -8039,6 +8511,7 @@ emit_marshal_scalar (EmitMarshalContext *m, int argnum, MonoType *t,
                     MonoMarshalSpec *spec, int conv_arg, 
                     MonoType **conv_arg_type, MarshalAction action)
 {
+#ifndef DISABLE_JIT
        MonoMethodBuilder *mb = m->mb;
 
        switch (action) {
@@ -8054,7 +8527,7 @@ emit_marshal_scalar (EmitMarshalContext *m, int argnum, MonoType *t,
        default:
                break;
        }
-
+#endif
        return conv_arg;
 }
 
@@ -8066,10 +8539,6 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
        /* Ensure that we have marshalling info for this param */
        mono_marshal_load_type_info (mono_class_from_mono_type (t));
 
-#ifdef DISABLE_JIT
-       /* Not JIT support, no need to generate correct IL */
-       return conv_arg;
-#else
        if (spec && spec->native == MONO_NATIVE_CUSTOM)
                return emit_marshal_custom (m, argnum, t, spec, conv_arg, conv_arg_type, action);
 
@@ -8086,10 +8555,10 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
                return emit_marshal_string (m, argnum, t, spec, conv_arg, conv_arg_type, action);
        case MONO_TYPE_CLASS:
        case MONO_TYPE_OBJECT:
+#ifndef DISABLE_COM
                if (spec && spec->native == MONO_NATIVE_STRUCT)
                        return emit_marshal_variant (m, argnum, t, spec, conv_arg, conv_arg_type, action);
 
-#ifndef DISABLE_COM
                if (spec && (spec->native == MONO_NATIVE_IUNKNOWN ||
                        spec->native == MONO_NATIVE_IDISPATCH ||
                        spec->native == MONO_NATIVE_INTERFACE))
@@ -8134,11 +8603,10 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
                else
                        return emit_marshal_object (m, argnum, t, spec, conv_arg, conv_arg_type, action);
        }
-#endif
-
        return conv_arg;
 }
 
+#ifndef DISABLE_JIT
 /**
  * mono_marshal_emit_native_wrapper:
  * @image: the image to use for looking up custom marshallers
@@ -8358,6 +8826,8 @@ mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoM
 
        mono_mb_emit_byte (mb, CEE_RET);
 }
+#endif /* DISABLE_JIT */
+
 
 G_GNUC_UNUSED static void
 code_for (MonoMethod *method) {
@@ -8415,13 +8885,14 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                pinvoke = TRUE;
 
        if (!piinfo->addr) {
-               if (pinvoke)
+               if (pinvoke) {
                        if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
                                exc_arg = "Method contains unsupported native code";
-                       else
+                       else if (!aot)
                                mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
-               else
+               } else {
                        piinfo->addr = mono_lookup_internal_call (method);
+               }
        }
 
        /* hack - redirect certain string constructors to CreateString */
@@ -8447,11 +8918,13 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                                /* create a wrapper to preserve .ctor in stack trace */
                                mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);
 
+#ifndef DISABLE_JIT
                                mono_mb_emit_byte (mb, CEE_LDARG_0);
                                for (i = 1; i <= csig->param_count; i++)
                                        mono_mb_emit_ldarg (mb, i);
                                mono_mb_emit_managed_call (mb, res, NULL);
                                mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
                                /* use native_wrapper_cache because internal calls are looked up there */
                                res = mono_mb_create_and_cache (cache, method,
@@ -8482,12 +8955,19 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
         * registered in the runtime doing the AOT compilation.
         */
        if (!piinfo->addr && !aot) {
+#ifndef DISABLE_JIT
                mono_mb_emit_exception (mb, exc_class, exc_arg);
+#endif
                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);
+
+               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_NONE);
+               info->d.managed_to_native.method = method;
+               mono_marshal_set_wrapper_info (res, info);
+
                return res;
        }
 
@@ -8502,6 +8982,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                if (method->string_ctor)
                        csig->ret = &mono_defaults.string_class->byval_arg;
 
+#ifndef DISABLE_JIT
                if (sig->hasthis) {
                        int pos;
 
@@ -8531,6 +9012,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                if (check_exceptions)
                        emit_thread_interrupt_checkpoint (mb);
                mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
                csig = signature_dup (method->klass->image, csig);
                csig->pinvoke = 0;
@@ -8549,10 +9031,12 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
        if (!aot)
                g_assert (piinfo->addr);
 
+#ifndef DISABLE_JIT
        mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
        mono_method_get_marshal_info (method, mspecs);
 
        mono_marshal_emit_native_wrapper (mb->method->klass->image, mb, sig, piinfo, mspecs, piinfo->addr, aot, check_exceptions, FALSE);
+#endif
 
        csig = signature_dup (method->klass->image, sig);
        csig->pinvoke = 0;
@@ -8560,14 +9044,16 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                                                                        mb, csig, csig->param_count + 16);
        mono_mb_free (mb);
 
-       info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_NONE);
+       info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_PINVOKE);
        info->d.managed_to_native.method = method;
        mono_marshal_set_wrapper_info (res, info);
 
+#ifndef DISABLE_JIT
        for (i = sig->param_count; i >= 0; i--)
                if (mspecs [i])
                        mono_metadata_free_marshal_spec (mspecs [i]);
        g_free (mspecs);
+#endif
 
        /* code_for (res); */
 
@@ -8602,7 +9088,9 @@ mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig
        mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_MANAGED_TO_NATIVE);
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        mono_marshal_emit_native_wrapper (image, mb, sig, piinfo, mspecs, func, FALSE, TRUE, FALSE);
+#endif
 
        csig = signature_dup (image, sig);
        csig->pinvoke = 0;
@@ -8657,7 +9145,9 @@ mono_marshal_get_native_func_wrapper_aot (MonoClass *klass)
        mb = mono_mb_new (invoke->klass, name, MONO_WRAPPER_MANAGED_TO_NATIVE);
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        mono_marshal_emit_native_wrapper (image, mb, sig, piinfo, mspecs, NULL, FALSE, TRUE, TRUE);
+#endif
 
        g_assert (!sig->hasthis);
        csig = signature_dup_add_this (image, sig, mono_defaults.int_class);
@@ -8679,7 +9169,7 @@ mono_marshal_get_native_func_wrapper_aot (MonoClass *klass)
 
        return res;
 }
-                           
+
 /*
  * mono_marshal_emit_managed_wrapper:
  *
@@ -8691,6 +9181,39 @@ mono_marshal_get_native_func_wrapper_aot (MonoClass *klass)
 void
 mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, uint32_t target_handle)
 {
+#ifdef DISABLE_JIT
+       MonoMethodSignature *sig, *csig;
+       int i;
+
+       sig = m->sig;
+       csig = m->csig;
+
+       /* we first do all conversions */
+       for (i = 0; i < sig->param_count; i ++) {
+               MonoType *t = sig->params [i];
+
+               switch (t->type) {
+               case MONO_TYPE_OBJECT:
+               case MONO_TYPE_CLASS:
+               case MONO_TYPE_VALUETYPE:
+               case MONO_TYPE_ARRAY:
+               case MONO_TYPE_SZARRAY:
+               case MONO_TYPE_STRING:
+               case MONO_TYPE_BOOLEAN:
+                       emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
+               }
+       }
+
+       if (!sig->ret->byref) {
+               switch (sig->ret->type) {
+               case MONO_TYPE_STRING:
+                       csig->ret = &mono_defaults.int_class->byval_arg;
+                       break;
+               default:
+                       break;
+               }
+       }
+#else
        MonoMethodSignature *sig, *csig;
        int i, *tmp_locals;
        gboolean closed = FALSE;
@@ -8872,9 +9395,9 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
 
        if (closed)
                g_free (sig);
+#endif
 }
 
-
 static void 
 mono_marshal_set_callconv_from_modopt (MonoMethod *method, MonoMethodSignature *csig)
 {
@@ -9005,9 +9528,10 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass,
                        MonoBoolean set_last_error = 0;
                        MonoBoolean best_fit_mapping = 0;
                        MonoBoolean throw_on_unmappable = 0;
+                       MonoError error;
 
-                       mono_reflection_create_custom_attr_data_args (mono_defaults.corlib, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo);
-
+                       mono_reflection_create_custom_attr_data_args (mono_defaults.corlib, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo, &error);
+                       g_assert (mono_error_ok (&error));
                        g_assert (mono_array_length (typed_args) == 1);
 
                        /* typed args */
@@ -9060,7 +9584,9 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass,
                info->d.native_to_managed.klass = delegate_klass;
                mono_marshal_set_wrapper_info (res, info);
        } else {
+#ifndef DISABLE_JIT
                mb->dynamic = 1;
+#endif
                res = mono_mb_create_method (mb, csig, sig->param_count + 16);
        }
        mono_mb_free (mb);
@@ -9117,7 +9643,9 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
 
                mono_marshal_emit_managed_wrapper (mb, sig, mspecs, &m, method, 0);
 
+#ifndef DISABLE_JIT
                mb->dynamic = 1;
+#endif
                method = mono_mb_create_method (mb, csig, sig->param_count + 16);
                mono_mb_free (mb);
 
@@ -9133,6 +9661,7 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);
 
        param_count = sig->param_count + sig->hasthis;
+#ifndef DISABLE_JIT
        for (i = 0; i < param_count; i++)
                mono_mb_emit_ldarg (mb, i);
 
@@ -9143,6 +9672,8 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
        mono_mb_emit_byte (mb, CEE_RET);
 
        mb->dynamic = 1;
+#endif
+
        method = mono_mb_create_method (mb, sig, param_count);
        mono_mb_free (mb);
 
@@ -9186,6 +9717,7 @@ mono_marshal_get_castclass_with_cache (void)
        sig->ret = &mono_defaults.object_class->byval_arg;
        sig->pinvoke = 0;
 
+#ifndef DISABLE_JIT
        /* allocate local 0 (pointer) obj_vtable */
        mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
 
@@ -9235,6 +9767,7 @@ mono_marshal_get_castclass_with_cache (void)
        mono_mb_patch_branch (mb, return_null_pos);
        mono_mb_emit_byte (mb, CEE_LDNULL);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif /* DISABLE_JIT */
 
        res = mono_mb_create_method (mb, sig, 8);
        info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE);
@@ -9275,6 +9808,7 @@ mono_marshal_get_isinst_with_cache (void)
        sig->ret = &mono_defaults.object_class->byval_arg;
        sig->pinvoke = 0;
 
+#ifndef DISABLE_JIT
        /* allocate local 0 (pointer) obj_vtable */
        mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
        /* allocate local 1 (pointer) cached_vtable */
@@ -9355,6 +9889,7 @@ mono_marshal_get_isinst_with_cache (void)
        mono_mb_patch_branch (mb, return_null_pos);
        mono_mb_emit_byte (mb, CEE_LDNULL);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_method (mb, sig, 8);
        info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_ISINST_WITH_CACHE);
@@ -9385,7 +9920,10 @@ mono_marshal_get_isinst (MonoClass *klass)
        static MonoMethodSignature *isint_sig = NULL;
        GHashTable *cache;
        MonoMethod *res;
-       int pos_was_ok, pos_failed, pos_end, pos_end2;
+       int pos_was_ok, pos_end;
+#ifndef DISABLE_REMOTING
+       int pos_end2, pos_failed;
+#endif
        char *name;
        MonoMethodBuilder *mb;
 
@@ -9406,16 +9944,17 @@ mono_marshal_get_isinst (MonoClass *klass)
        
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        /* check if the object is a proxy that needs special cast */
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
        mono_mb_emit_op (mb, CEE_MONO_CISINST, klass);
 
-       /* The result of MONO_ISINST can be:
+       /* The result of MONO_CISINST can be:
                0) the type check succeeded
                1) the type check did not succeed
                2) a CanCastTo call is needed */
-       
+#ifndef DISABLE_REMOTING
        mono_mb_emit_byte (mb, CEE_DUP);
        pos_was_ok = mono_mb_emit_branch (mb, CEE_BRFALSE);
 
@@ -9445,8 +9984,27 @@ mono_marshal_get_isinst (MonoClass *klass)
        mono_mb_patch_branch (mb, pos_end);
        mono_mb_patch_branch (mb, pos_end2);
        mono_mb_emit_byte (mb, CEE_RET);
+#else
+       pos_was_ok = mono_mb_emit_branch (mb, CEE_BRFALSE);
 
-       res = mono_mb_create_and_cache (cache, klass, mb, isint_sig, isint_sig->param_count + 16);
+       /* fail */
+
+       mono_mb_emit_byte (mb, CEE_LDNULL);
+       pos_end = mono_mb_emit_branch (mb, CEE_BR);
+
+       /* success */
+
+       mono_mb_patch_branch (mb, pos_was_ok);
+       mono_mb_emit_ldarg (mb, 0);
+
+       /* the end */
+
+       mono_mb_patch_branch (mb, pos_end);
+       mono_mb_emit_byte (mb, CEE_RET);
+#endif /* DISABLE_REMOTING */
+#endif /* DISABLE_JIT */
+
+       res = mono_mb_create_and_cache (cache, klass, mb, isint_sig, isint_sig->param_count + 16);
        mono_mb_free (mb);
 
        return res;
@@ -9468,7 +10026,9 @@ mono_marshal_get_castclass (MonoClass *klass)
        static MonoMethodSignature *castclass_sig = NULL;
        GHashTable *cache;
        MonoMethod *res;
+#ifndef DISABLE_REMOTING
        int pos_was_ok, pos_was_ok2;
+#endif
        char *name;
        MonoMethodBuilder *mb;
        WrapperInfo *info;
@@ -9490,17 +10050,18 @@ mono_marshal_get_castclass (MonoClass *klass)
        
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        /* check if the object is a proxy that needs special cast */
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
        mono_mb_emit_op (mb, CEE_MONO_CCASTCLASS, klass);
 
-       /* The result of MONO_ISINST can be:
+       /* The result of MONO_CCASTCLASS can be:
                0) the cast is valid
                1) cast of unknown proxy type
                or an exception if the cast is is invalid
        */
-       
+#ifndef DISABLE_REMOTING
        pos_was_ok = mono_mb_emit_branch (mb, CEE_BRFALSE);
 
        /* get the real proxy from the transparent proxy*/
@@ -9515,10 +10076,16 @@ mono_marshal_get_castclass (MonoClass *klass)
        /* success */
        mono_mb_patch_branch (mb, pos_was_ok);
        mono_mb_patch_branch (mb, pos_was_ok2);
+#else
+       /* MONO_CCASTCLASS leaves an int in the stack with the result, pop it. */
+       mono_mb_emit_byte (mb, CEE_POP);
+#endif /* DISABLE_REMOTING */
+
        mono_mb_emit_ldarg (mb, 0);
        
        /* the end */
        mono_mb_emit_byte (mb, CEE_RET);
+#endif /* DISABLE_JIT */
 
        res = mono_mb_create_and_cache (cache, klass, mb, castclass_sig, castclass_sig->param_count + 16);
        mono_mb_free (mb);
@@ -9529,6 +10096,7 @@ mono_marshal_get_castclass (MonoClass *klass)
        return res;
 }
 
+#ifndef DISABLE_REMOTING
 MonoMethod *
 mono_marshal_get_proxy_cancast (MonoClass *klass)
 {
@@ -9560,6 +10128,7 @@ mono_marshal_get_proxy_cancast (MonoClass *klass)
        
        mb->method->save_lmf = 1;
 
+#ifndef DISABLE_JIT
        /* get the real proxy from the transparent proxy*/
        mono_mb_emit_ldarg (mb, 0);
        mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
@@ -9600,6 +10169,7 @@ mono_marshal_get_proxy_cancast (MonoClass *klass)
        
        mono_mb_patch_branch (mb, pos_end);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_and_cache (cache, klass, mb, isint_sig, isint_sig->param_count + 16);
        mono_mb_free (mb);
@@ -9615,6 +10185,7 @@ mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype, MonoTransparentPro
        klass = mono_class_from_mono_type (rtype->type);
        mono_upgrade_remote_class (domain, (MonoObject*)tproxy, klass);
 }
+#endif /*DISABLE_REMOTING */
 
 /**
  * mono_marshal_get_struct_to_ptr:
@@ -9644,6 +10215,7 @@ mono_marshal_get_struct_to_ptr (MonoClass *klass)
 
        mb = mono_mb_new (klass, stoptr->name, MONO_WRAPPER_UNKNOWN);
 
+#ifndef DISABLE_JIT
        if (klass->blittable) {
                mono_mb_emit_byte (mb, CEE_LDARG_1);
                mono_mb_emit_byte (mb, CEE_LDARG_0);
@@ -9675,6 +10247,7 @@ mono_marshal_get_struct_to_ptr (MonoClass *klass)
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_method (mb, mono_signature_no_pinvoke (stoptr), 0);
        mono_mb_free (mb);
@@ -9723,6 +10296,7 @@ mono_marshal_get_ptr_to_struct (MonoClass *klass)
 
        mb = mono_mb_new (klass, "PtrToStructure", MONO_WRAPPER_UNKNOWN);
 
+#ifndef DISABLE_JIT
        if (klass->blittable) {
                mono_mb_emit_byte (mb, CEE_LDARG_1);
                mono_mb_emit_ldflda (mb, sizeof (MonoObject));
@@ -9750,6 +10324,7 @@ mono_marshal_get_ptr_to_struct (MonoClass *klass)
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_method (mb, ptostr, 0);
        mono_mb_free (mb);
@@ -9775,16 +10350,33 @@ mono_marshal_get_synchronized_inner_wrapper (MonoMethod *method)
        WrapperInfo *info;
        MonoMethodSignature *sig;
        MonoMethod *res;
+       MonoGenericContext *ctx = NULL;
+       MonoMethod *orig_method = NULL;
+       MonoGenericContainer *container = NULL;
+
+       if (method->is_inflated && !mono_method_get_context (method)->method_inst) {
+               orig_method = method;
+               ctx = &((MonoMethodInflated*)method)->context;
+               method = ((MonoMethodInflated*)method)->declaring;
+               container = mono_method_get_generic_container (method);
+               if (!container)
+                       container = method->klass->generic_container;
+               g_assert (container);
+       }
 
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_UNKNOWN);
+#ifndef DISABLE_JIT
        mono_mb_emit_exception_full (mb, "System", "ExecutionEngineException", "Shouldn't be called.");
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
        sig = signature_dup (method->klass->image, mono_method_signature (method));
        res = mono_mb_create_method (mb, sig, 0);
        mono_mb_free (mb);
        info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_SYNCHRONIZED_INNER);
        info->d.synchronized_inner.method = method;
        mono_marshal_set_wrapper_info (res, info);
+       if (ctx)
+               res = mono_class_inflate_generic_method (res, ctx);
        return res;
 }
 
@@ -9802,27 +10394,54 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        MonoMethod *res;
        GHashTable *cache;
        int i, pos, this_local, ret_local = 0;
+       MonoGenericContext *ctx = NULL;
+       MonoMethod *orig_method = NULL;
+       MonoGenericContainer *container = NULL;
 
        g_assert (method);
 
        if (method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED)
                return method;
 
-       cache = get_cache (&method->klass->image->synchronized_cache, mono_aligned_addr_hash, NULL);
-       if ((res = mono_marshal_find_in_cache (cache, method)))
-               return res;
+       /* FIXME: Support generic methods too */
+       if (method->is_inflated && !mono_method_get_context (method)->method_inst) {
+               orig_method = method;
+               ctx = &((MonoMethodInflated*)method)->context;
+               method = ((MonoMethodInflated*)method)->declaring;
+               container = mono_method_get_generic_container (method);
+               if (!container)
+                       container = method->klass->generic_container;
+               g_assert (container);
+       }
+
+       /*
+        * Check cache
+        */
+       if (ctx) {
+               cache = get_cache (&method->klass->image->synchronized_generic_cache, mono_aligned_addr_hash, NULL);
+               res = check_generic_wrapper_cache (cache, orig_method, orig_method, method);
+               if (res)
+                       return res;
+       } else {
+               cache = get_cache (&method->klass->image->synchronized_cache, mono_aligned_addr_hash, NULL);
+               if ((res = mono_marshal_find_in_cache (cache, method)))
+                       return res;
+       }
 
        sig = signature_dup (method->klass->image, mono_method_signature (method));
        sig->pinvoke = 0;
 
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_SYNCHRONIZED);
 
+#ifndef DISABLE_JIT
        /* result */
        if (!MONO_TYPE_IS_VOID (sig->ret))
                ret_local = mono_mb_add_local (mb, sig->ret);
+#endif
 
        if (method->klass->valuetype && !(method->flags & MONO_METHOD_ATTR_STATIC)) {
                mono_class_set_failure (method->klass, MONO_EXCEPTION_TYPE_LOAD, NULL);
+#ifndef DISABLE_JIT
                /* This will throw the type load exception when the wrapper is compiled */
                mono_mb_emit_byte (mb, CEE_LDNULL);
                mono_mb_emit_op (mb, CEE_ISINST, method->klass);
@@ -9831,6 +10450,7 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                if (!MONO_TYPE_IS_VOID (sig->ret))
                        mono_mb_emit_ldloc (mb, ret_local);
                mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
                res = mono_mb_create_and_cache (cache, method,
                                                                                mb, sig, sig->param_count + 16);
@@ -9839,13 +10459,15 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                return res;
        }
 
+#ifndef DISABLE_JIT
        /* this */
        this_local = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
        clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
        clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
+#endif
 
-       mono_loader_lock ();
+       mono_marshal_lock ();
 
        if (!enter_method) {
                MonoMethodDesc *desc;
@@ -9861,13 +10483,14 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
                mono_method_desc_free (desc);
 
                desc = mono_method_desc_new ("Type:GetTypeFromHandle", FALSE);
-               gettypefromhandle_method = mono_method_desc_search_in_class (desc, mono_defaults.monotype_class->parent);
+               gettypefromhandle_method = mono_method_desc_search_in_class (desc, mono_defaults.systemtype_class);
                g_assert (gettypefromhandle_method);
                mono_method_desc_free (desc);
        }
 
-       mono_loader_unlock ();
+       mono_marshal_unlock ();
 
+#ifndef DISABLE_JIT
        /* Push this or the type object */
        if (method->flags & METHOD_ATTRIBUTE_STATIC) {
                /* We have special handling for this in the JIT */
@@ -9894,7 +10517,10 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        for (i = 0; i < sig->param_count; i++)
                mono_mb_emit_ldarg (mb, i + (sig->hasthis == TRUE));
 
-       mono_mb_emit_managed_call (mb, method, NULL);
+       if (ctx)
+               mono_mb_emit_managed_call (mb, mono_class_inflate_generic_method (method, &container->context), NULL);
+       else
+               mono_mb_emit_managed_call (mb, method, NULL);
 
        if (!MONO_TYPE_IS_VOID (sig->ret))
                mono_mb_emit_stloc (mb, ret_local);
@@ -9917,9 +10543,16 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        mono_mb_emit_byte (mb, CEE_RET);
 
        mono_mb_set_clauses (mb, 1, clause);
+#endif
 
-       res = mono_mb_create_and_cache (cache, method,
-                                                                       mb, sig, sig->param_count + 16);
+       if (ctx) {
+               MonoMethod *def;
+               def = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
+               res = cache_generic_wrapper (cache, orig_method, def, ctx, orig_method);
+       } else {
+               res = mono_mb_create_and_cache (cache, method,
+                                                                               mb, sig, sig->param_count + 16);
+       }
        mono_mb_free (mb);
 
        return res;     
@@ -9946,6 +10579,7 @@ mono_marshal_get_unbox_wrapper (MonoMethod *method)
 
        g_assert (sig->hasthis);
        
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0); 
        mono_mb_emit_icon (mb, sizeof (MonoObject));
        mono_mb_emit_byte (mb, CEE_ADD);
@@ -9953,6 +10587,7 @@ mono_marshal_get_unbox_wrapper (MonoMethod *method)
                mono_mb_emit_ldarg (mb, i + 1);
        mono_mb_emit_managed_call (mb, method, NULL);
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_and_cache (cache, method,
                                                                                 mb, sig, sig->param_count + 16);
@@ -10002,13 +10637,15 @@ get_virtual_stelemref_kind (MonoClass *element_class)
                return STELEMREF_INTERFACE;
 #endif
        /*Arrays are sealed but are covariant on their element type, We can't use any of the fast paths.*/
-       if (element_class->marshalbyref || element_class->rank || mono_class_has_variant_generic_params (element_class))
+       if (mono_class_is_marshalbyref (element_class) || element_class->rank || mono_class_has_variant_generic_params (element_class))
                return STELEMREF_COMPLEX;
        if (element_class->flags & TYPE_ATTRIBUTE_SEALED)
                return STELEMREF_SEALED_CLASS;
        return STELEMREF_CLASS;
 }
 
+#ifndef DISABLE_JIT
+
 static void
 load_array_element_address (MonoMethodBuilder *mb)
 {
@@ -10040,6 +10677,7 @@ load_value_class (MonoMethodBuilder *mb, int vklass)
        mono_mb_emit_byte (mb, CEE_LDIND_I);
        mono_mb_emit_stloc (mb, vklass);
 }
+#endif
 
 #if 0
 static void
@@ -10060,14 +10698,13 @@ record_slot_vstore (MonoObject *array, size_t index, MonoObject *value)
  *     - Maybe mve some MonoClass field into the vtable to reduce the number of loads
  *     - Add a case for arrays of arrays.
  */
-MonoMethod*
-mono_marshal_get_virtual_stelemref (MonoClass *array_class)
+static MonoMethod*
+get_virtual_stelemref_wrapper (int kind)
 {
        static MonoMethod *cached_methods [STELEMREF_KIND_COUNT] = { NULL }; /*object iface sealed regular*/
        static MonoMethodSignature *signature;
        MonoMethodBuilder *mb;
        MonoMethod *res;
-       int kind;
        char *name;
        const char *param_names [16];
        guint32 b1, b2, b3;
@@ -10075,9 +10712,6 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
        int array_slot_addr;
        WrapperInfo *info;
 
-       g_assert (array_class->rank == 1);
-       kind = get_virtual_stelemref_kind (array_class->element_class);
-
        if (cached_methods [kind])
                return cached_methods [kind];
 
@@ -10085,10 +10719,6 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
        mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_STELEMREF);
        g_free (name);
 
-       param_names [0] = "index";
-       param_names [1] = "value";
-       mono_mb_set_param_names (mb, param_names);
-
        if (!signature) {
                MonoMethodSignature *sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
 
@@ -10100,6 +10730,11 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
                signature = sig;
        }
 
+#ifndef DISABLE_JIT
+       param_names [0] = "index";
+       param_names [1] = "value";
+       mono_mb_set_param_names (mb, param_names);
+
        /*For now simply call plain old stelemref*/
        switch (kind) {
        case STELEMREF_OBJECT:
@@ -10422,6 +11057,7 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
                mono_mb_emit_byte (mb, CEE_RET);
                g_assert (0);
        }
+#endif /* DISABLE_JIT */
 
        res = mono_mb_create_method (mb, signature, 4);
        res->flags |= METHOD_ATTRIBUTE_VIRTUAL;
@@ -10443,6 +11079,30 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
        return cached_methods [kind];
 }
 
+MonoMethod*
+mono_marshal_get_virtual_stelemref (MonoClass *array_class)
+{
+       int kind;
+
+       g_assert (array_class->rank == 1);
+       kind = get_virtual_stelemref_kind (array_class->element_class);
+
+       return get_virtual_stelemref_wrapper (kind);
+}
+
+MonoMethod**
+mono_marshal_get_virtual_stelemref_wrappers (int *nwrappers)
+{
+       MonoMethod **res;
+       int i;
+
+       *nwrappers = STELEMREF_KIND_COUNT;
+       res = g_malloc0 (STELEMREF_KIND_COUNT * sizeof (MonoMethod*));
+       for (i = 0; i < STELEMREF_KIND_COUNT; ++i)
+               res [i] = get_virtual_stelemref_wrapper (i);
+       return res;
+}
+
 /*
  * The wrapper info for the wrapper is a WrapperInfo structure.
  */
@@ -10472,7 +11132,8 @@ mono_marshal_get_stelemref ()
        sig->params [0] = &mono_defaults.object_class->byval_arg;
        sig->params [1] = &mono_defaults.int_class->byval_arg; /* this is a natural sized int */
        sig->params [2] = &mono_defaults.object_class->byval_arg;
-               
+
+#ifndef DISABLE_JIT
        aklass = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
        vklass = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
        array_slot_addr = mono_mb_add_local (mb, &mono_defaults.object_class->this_arg);
@@ -10583,6 +11244,8 @@ mono_marshal_get_stelemref ()
        mono_mb_emit_exception (mb, "ArrayTypeMismatchException", NULL);
        
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
        ret = mono_mb_create_method (mb, sig, 4);
        mono_mb_free (mb);
 
@@ -10592,6 +11255,84 @@ mono_marshal_get_stelemref ()
        return ret;
 }
 
+/*
+ * mono_marshal_get_gsharedvt_in_wrapper:
+ *
+ *   This wrapper handles calls from normal code to gsharedvt code.
+ *
+ * The wrapper info for the wrapper is a WrapperInfo structure.
+ */
+MonoMethod*
+mono_marshal_get_gsharedvt_in_wrapper (void)
+{
+       static MonoMethod* ret = NULL;
+       MonoMethodSignature *sig;
+       MonoMethodBuilder *mb;
+       WrapperInfo *info;
+
+       if (ret)
+               return ret;
+       
+       mb = mono_mb_new (mono_defaults.object_class, "gsharedvt_in", MONO_WRAPPER_UNKNOWN);
+       
+       sig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
+       sig->ret = &mono_defaults.void_class->byval_arg;
+
+#ifndef DISABLE_JIT
+       /*
+        * The body is generated by the JIT, we use a wrapper instead of a trampoline so EH works.
+        */
+       mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
+       ret = mono_mb_create_method (mb, sig, 4);
+       mono_mb_free (mb);
+
+       info = mono_wrapper_info_create (ret, WRAPPER_SUBTYPE_GSHAREDVT_IN);
+       mono_marshal_set_wrapper_info (ret, info);
+
+       return ret;
+}
+
+/*
+ * mono_marshal_get_gsharedvt_out_wrapper:
+ *
+ *   This wrapper handles calls from gsharedvt code to normal code.
+ *
+ * The wrapper info for the wrapper is a WrapperInfo structure.
+ */
+MonoMethod*
+mono_marshal_get_gsharedvt_out_wrapper (void)
+{
+       static MonoMethod* ret = NULL;
+       MonoMethodSignature *sig;
+       MonoMethodBuilder *mb;
+       WrapperInfo *info;
+
+       if (ret)
+               return ret;
+       
+       mb = mono_mb_new (mono_defaults.object_class, "gsharedvt_out", MONO_WRAPPER_UNKNOWN);
+       
+       sig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
+       sig->ret = &mono_defaults.void_class->byval_arg;
+
+#ifndef DISABLE_JIT
+       /*
+        * The body is generated by the JIT, we use a wrapper instead of a trampoline so EH works.
+        */
+       mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
+       ret = mono_mb_create_method (mb, sig, 4);
+       mono_mb_free (mb);
+
+       info = mono_wrapper_info_create (ret, WRAPPER_SUBTYPE_GSHAREDVT_OUT);
+       mono_marshal_set_wrapper_info (ret, info);
+
+       return ret;
+}
+
 typedef struct {
        int rank;
        int elem_size;
@@ -10648,6 +11389,7 @@ mono_marshal_get_array_address (int rank, int elem_size)
 
        mb = mono_mb_new (mono_defaults.object_class, "ElementAddr", MONO_WRAPPER_MANAGED_TO_MANAGED);
        
+#ifndef DISABLE_JIT
        bounds = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
        ind = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
        realidx = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
@@ -10727,6 +11469,8 @@ mono_marshal_get_array_address (int rank, int elem_size)
        mono_mb_emit_exception (mb, "IndexOutOfRangeException", NULL);
 
        g_free (branch_positions);
+#endif /* DISABLE_JIT */
+
        ret = mono_mb_create_method (mb, sig, 4);
        mono_mb_free (mb);
 
@@ -10766,6 +11510,76 @@ mono_marshal_get_array_address (int rank, int elem_size)
        return ret;
 }
 
+/*
+ * mono_marshal_get_array_accessor_wrapper:
+ *
+ *   Return a wrapper which just calls METHOD, which should be an Array Get/Set/Address method.
+ */
+MonoMethod *
+mono_marshal_get_array_accessor_wrapper (MonoMethod *method)
+{
+       MonoMethodSignature *sig;
+       MonoMethodBuilder *mb;
+       MonoMethod *res;
+       GHashTable *cache;
+       int i;
+       MonoGenericContext *ctx = NULL;
+       MonoMethod *orig_method = NULL;
+       MonoGenericContainer *container = NULL;
+       WrapperInfo *info;
+
+       /*
+        * These wrappers are needed to avoid the JIT replacing the calls to these methods with intrinsics
+        * inside runtime invoke wrappers, thereby making the wrappers not unshareable.
+        * FIXME: Use generic methods.
+        */
+       /*
+        * Check cache
+        */
+       if (ctx) {
+               cache = NULL;
+               g_assert_not_reached ();
+       } else {
+               cache = get_cache (&method->klass->image->array_accessor_cache, mono_aligned_addr_hash, NULL);
+               if ((res = mono_marshal_find_in_cache (cache, method)))
+                       return res;
+       }
+
+       sig = signature_dup (method->klass->image, mono_method_signature (method));
+       sig->pinvoke = 0;
+
+       mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_UNKNOWN);
+
+#ifndef DISABLE_JIT
+       /* Call the method */
+       if (sig->hasthis)
+               mono_mb_emit_ldarg (mb, 0);
+       for (i = 0; i < sig->param_count; i++)
+               mono_mb_emit_ldarg (mb, i + (sig->hasthis == TRUE));
+
+       if (ctx)
+               mono_mb_emit_managed_call (mb, mono_class_inflate_generic_method (method, &container->context), NULL);
+       else
+               mono_mb_emit_managed_call (mb, method, NULL);
+       mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
+       if (ctx) {
+               MonoMethod *def;
+               def = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
+               res = cache_generic_wrapper (cache, orig_method, def, ctx, orig_method);
+       } else {
+               res = mono_mb_create_and_cache (cache, method,
+                                                                               mb, sig, sig->param_count + 16);
+               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_ARRAY_ACCESSOR);
+               info->d.array_accessor.method = method;
+               mono_marshal_set_wrapper_info (res, info);
+       }
+       mono_mb_free (mb);
+
+       return res;     
+}
+
 void*
 mono_marshal_alloc (gulong size)
 {
@@ -10871,7 +11685,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *s
        element_size = mono_array_element_size (src->obj.vtable->klass);
 
        /* no references should be involved */
-       source_addr = mono_array_addr_with_size (src, element_size, start_index);
+       source_addr = mono_array_addr_with_size_fast (src, element_size, start_index);
 
        memcpy (dest, source_addr, length * element_size);
 }
@@ -10900,127 +11714,11 @@ ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer s
        element_size = mono_array_element_size (dest->obj.vtable->klass);
          
        /* no references should be involved */
-       dest_addr = mono_array_addr_with_size (dest, element_size, start_index);
+       dest_addr = mono_array_addr_with_size_fast (dest, element_size, start_index);
 
        memcpy (dest_addr, src, length * element_size);
 }
 
-#if NO_UNALIGNED_ACCESS
-#define RETURN_UNALIGNED(type, addr) \
-       { \
-               type val; \
-               memcpy(&val, p + offset, sizeof(val)); \
-               return val; \
-       }
-#define WRITE_UNALIGNED(type, addr, val) \
-       memcpy(addr, &val, sizeof(type))
-#else
-#define RETURN_UNALIGNED(type, addr) \
-       return *(type*)(p + offset);
-#define WRITE_UNALIGNED(type, addr, val) \
-       (*(type *)(addr) = (val))
-#endif
-
-gpointer
-ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr, gint32 offset)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       RETURN_UNALIGNED(gpointer, p + offset);
-}
-
-unsigned char
-ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr, gint32 offset)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       return *(unsigned char*)(p + offset);
-}
-
-gint16
-ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr, gint32 offset)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       RETURN_UNALIGNED(gint16, p + offset);
-}
-
-gint32
-ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr, gint32 offset)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       RETURN_UNALIGNED(gint32, p + offset);
-}
-
-gint64
-ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr, gint32 offset)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       RETURN_UNALIGNED(gint64, p + offset);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr, gint32 offset, unsigned char val)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       *(unsigned char*)(p + offset) = val;
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr, gint32 offset, gpointer val)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       WRITE_UNALIGNED(gpointer, p + offset, val);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr, gint32 offset, gint16 val)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       WRITE_UNALIGNED(gint16, p + offset, val);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr, gint32 offset, gint32 val)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       WRITE_UNALIGNED(gint32, p + offset, val);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr, gint32 offset, gint64 val)
-{
-       char *p = ptr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       WRITE_UNALIGNED(gint64, p + offset, val);
-}
-
 MonoString *
 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr)
 {
@@ -11106,7 +11804,9 @@ ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rty
 
        layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
 
-       if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+       if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
+               return sizeof (gpointer);
+       } else if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
                gchar *msg;
                MonoException *exc;
 
@@ -11190,7 +11890,8 @@ ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer s
 
        MONO_ARCH_SAVE_REGS;
 
-       MONO_CHECK_ARG_NULL (src);
+       if (src == NULL)
+               return NULL;
        MONO_CHECK_ARG_NULL (type);
 
        klass = mono_class_from_mono_type (type->type);
@@ -11463,7 +12164,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr,
 void*
 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray *arrayobj, int index)
 {
-       return mono_array_addr_with_size (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
+       return mono_array_addr_with_size_fast (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
 }
 
 MonoDelegate*
@@ -11518,12 +12219,8 @@ mono_marshal_load_type_info (MonoClass* klass)
        if (!klass->inited)
                mono_class_init (klass);
 
-       mono_loader_lock ();
-
-       if (klass->marshal_info) {
-               mono_loader_unlock ();
+       if (klass->marshal_info)
                return klass->marshal_info;
-       }
 
        /*
         * This function can recursively call itself, so we keep the list of classes which are
@@ -11599,7 +12296,7 @@ mono_marshal_load_type_info (MonoClass* klass)
                case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
                        size = mono_marshal_type_size (field->type, info->fields [j].mspec, 
                                                       &align, TRUE, klass->unicode);
-                       min_align = packing;
+                       min_align = MAX (align, min_align);
                        info->fields [j].offset = field->offset - sizeof (MonoObject);
                        info->native_size = MAX (info->native_size, info->fields [j].offset + size);
                        break;
@@ -11613,9 +12310,12 @@ mono_marshal_load_type_info (MonoClass* klass)
                 * If the provided Size is equal or larger than the calculated size, and there
                 * was no Pack attribute, we set min_align to 1 to avoid native_size being increased
                 */
-               if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
+               if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
                        if (native_size && native_size == info->native_size && klass->packing_size == 0)
                                min_align = 1;
+                       else
+                               min_align = MIN (min_align, packing);
+               }
        }
 
        if (info->native_size & (min_align - 1)) {
@@ -11638,12 +12338,13 @@ mono_marshal_load_type_info (MonoClass* klass)
        loads_list = g_slist_remove (loads_list, klass);
        mono_native_tls_set_value (load_type_info_tls_id, loads_list);
 
-       /*We do double-checking locking on marshal_info */
-       mono_memory_barrier ();
-
-       klass->marshal_info = info;
-
-       mono_loader_unlock ();
+       mono_marshal_lock ();
+       if (!klass->marshal_info) {
+               /*We do double-checking locking on marshal_info */
+               mono_memory_barrier ();
+               klass->marshal_info = info;
+       }
+       mono_marshal_unlock ();
 
        return klass->marshal_info;
 }
@@ -12004,6 +12705,7 @@ mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface, gchar
        csig = signature_dup (method->klass->image, sig);
        csig->generic_param_count = 0;
 
+#ifndef DISABLE_JIT
        mono_mb_emit_ldarg (mb, 0);
        for (i = 0; i < csig->param_count; i++)
                mono_mb_emit_ldarg (mb, i + 1);
@@ -12012,6 +12714,7 @@ mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface, gchar
 
        /* We can corlib internal methods */
        mb->skip_visibility = TRUE;
+#endif
 
        res = mono_mb_create_method (mb, csig, csig->param_count + 16);
 
@@ -12138,6 +12841,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        if (MONO_TYPE_ISSTRUCT (sig->ret))
                csig->ret = &mono_defaults.object_class->byval_arg;
 
+#ifndef DISABLE_JIT
        /* local 0 (temp for exception object) */
        mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
 
@@ -12220,6 +12924,7 @@ mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method)
        }
 
        mono_mb_emit_byte (mb, CEE_RET);
+#endif
 
        res = mono_mb_create_and_cache (cache, method, mb, csig, param_count + 16);
        mono_mb_free (mb);
@@ -12248,8 +12953,6 @@ mono_marshal_free_dynamic_wrappers (MonoMethod *method)
         */
        if (image->runtime_invoke_direct_cache)
                g_hash_table_remove (image->runtime_invoke_direct_cache, method);
-       if (image->delegate_bound_static_invoke_cache)
-               g_hash_table_foreach_remove (image->delegate_bound_static_invoke_cache, signature_method_pair_matches_method, method);
        if (image->delegate_abstract_invoke_cache)
                g_hash_table_foreach_remove (image->delegate_abstract_invoke_cache, signature_method_pair_matches_method, method);
 
@@ -12257,12 +12960,6 @@ mono_marshal_free_dynamic_wrappers (MonoMethod *method)
                mono_marshal_unlock ();
 }
 
-/*
- * mono_marshal_free_inflated_wrappers:
- *
- *   Free wrappers of the inflated method METHOD.
- */
-
 static gboolean
 signature_method_pair_matches_signature (gpointer key, gpointer value, gpointer user_data)
 {
@@ -12272,6 +12969,11 @@ signature_method_pair_matches_signature (gpointer key, gpointer value, gpointer
        return mono_metadata_signature_equal (pair->sig, sig);
 }
 
+/*
+ * mono_marshal_free_inflated_wrappers:
+ *
+ *   Free wrappers of the inflated method METHOD.
+ */
 void
 mono_marshal_free_inflated_wrappers (MonoMethod *method)
 {
@@ -12301,6 +13003,8 @@ mono_marshal_free_inflated_wrappers (MonoMethod *method)
                g_hash_table_remove (method->klass->image->delegate_invoke_cache, sig);
        if (sig && method->klass->image->runtime_invoke_cache)
                g_hash_table_remove (method->klass->image->runtime_invoke_cache, sig);
+       if (sig && method->klass->image->runtime_invoke_vtype_cache)
+               g_hash_table_remove (method->klass->image->runtime_invoke_vtype_cache, sig);
 
         /*
          * indexed by SignatureMethodPair
@@ -12309,10 +13013,6 @@ mono_marshal_free_inflated_wrappers (MonoMethod *method)
                g_hash_table_foreach_remove (method->klass->image->delegate_abstract_invoke_cache,
                                             signature_method_pair_matches_signature, (gpointer)sig);
 
-       if (sig && method->klass->image->delegate_bound_static_invoke_cache)
-                g_hash_table_foreach_remove (method->klass->image->delegate_bound_static_invoke_cache,
-                                             signature_method_pair_matches_signature, (gpointer)sig);
-
         /*
          * indexed by MonoMethod pointers
          */