2008-09-07 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / aot-runtime.c
index 09b798c8f000af6d7b03936815bed74983505a41..b34b45530a9169c625319c038fc39437b86b04da 100644 (file)
@@ -57,7 +57,7 @@
 
 #ifdef PLATFORM_WIN32
 #define SHARED_EXT ".dll"
-#elif (defined(__ppc__) || defined(__ppc64__)) && defined(__MACH__)
+#elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
 #define SHARED_EXT ".dylib"
 #else
 #define SHARED_EXT ".so"
@@ -75,6 +75,10 @@ typedef struct MonoAotModule {
        guint32 got_size;
        GHashTable *name_cache;
        GHashTable *wrappers;
+       /* Maps wrapper names to their method index */
+       GHashTable *wrappers_by_name;
+       /* Maps wrapper methods to their code */
+       GHashTable *wrappers_to_code;
        MonoAssemblyName *image_names;
        char **image_guids;
        MonoAssembly *assembly;
@@ -108,6 +112,7 @@ typedef struct MonoAotModule {
        guint8 *trampolines;
        guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
        gpointer *globals;
+       MonoDl *sofile;
 } MonoAotModule;
 
 static GHashTable *aot_modules;
@@ -115,6 +120,12 @@ static GHashTable *aot_modules;
 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
 static CRITICAL_SECTION aot_mutex;
 
+/* 
+ * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
+ * AOT modules registered by mono_aot_register_module ().
+ */
+static GHashTable *static_aot_modules;
+
 /*
  * Disabling this will make a copy of the loaded code and use the copy instead 
  * of the original. This will place the caller and the callee close to each 
@@ -232,63 +243,135 @@ decode_value (guint8 *ptr, guint8 **rptr)
        return len;
 }
 
+static MonoMethod*
+decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
+
+static MonoClass*
+decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
+
+static MonoGenericInst*
+decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
+{
+       int type_argc, i;
+       MonoType **type_argv;
+       MonoGenericInst *inst;
+       guint8 *p = buf;
+
+       type_argc = decode_value (p, &p);
+       type_argv = g_new0 (MonoType*, type_argc);
+
+       for (i = 0; i < type_argc; ++i) {
+               MonoClass *pclass = decode_klass_ref (module, p, &p);
+               if (!pclass) {
+                       g_free (type_argv);
+                       return NULL;
+               }
+               type_argv [i] = &pclass->byval_arg;
+       }
+
+       inst = mono_metadata_get_generic_inst (type_argc, type_argv);
+       g_free (type_argv);
+
+       *endbuf = p;
+
+       return inst;
+}
+
 static MonoClass*
 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
 {
        MonoImage *image;
        MonoClass *klass, *eklass;
        guint32 token, rank;
+       guint8 *p = buf;
 
-       token = decode_value (buf, &buf);
+       token = decode_value (p, &p);
        if (token == 0) {
-               *endbuf = buf;
+               *endbuf = p;
                return NULL;
        }
        if (mono_metadata_token_table (token) == 0) {
-               image = load_image (module, decode_value (buf, &buf));
+               image = load_image (module, decode_value (p, &p));
                if (!image)
                        return NULL;
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
        } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
                if (token == MONO_TOKEN_TYPE_SPEC) {
-                       MonoClass *gclass;
-                       int i;
-                       MonoGenericContext ctx;
-                       MonoGenericInst inst;
-                       MonoType *type;
-
-                       gclass = decode_klass_ref (module, buf, &buf);
-                       g_assert (gclass->generic_container);
-
-                       memset (&ctx, 0, sizeof (ctx));
-                       memset (&inst, 0, sizeof (inst));
-                       ctx.class_inst = &inst;
-                       inst.type_argc = decode_value (buf, &buf);
-                       inst.type_argv = g_new0 (MonoType*, inst.type_argc);
-                       for (i = 0; i < inst.type_argc; ++i) {
-                               MonoClass *pclass = decode_klass_ref (module, buf, &buf);
-                               if (!pclass) {
-                                       g_free (inst.type_argv);
+                       MonoTypeEnum type = decode_value (p, &p);
+
+                       if (type == MONO_TYPE_GENERICINST) {
+                               MonoClass *gclass;
+                               MonoGenericContext ctx;
+                               MonoType *type;
+
+                               gclass = decode_klass_ref (module, p, &p);
+                               g_assert (gclass->generic_container);
+
+                               memset (&ctx, 0, sizeof (ctx));
+                               ctx.class_inst = decode_generic_inst (module, p, &p);
+                               if (!ctx.class_inst)
                                        return NULL;
+                               type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
+                               klass = mono_class_from_mono_type (type);
+                               mono_metadata_free_type (type);
+                       } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
+                               MonoType *t;
+                               gboolean is_method;
+                               MonoGenericContainer *container;
+
+                               // FIXME: Maybe use types directly to avoid
+                               // the overhead of creating MonoClass-es
+
+                               // FIXME: Memory management
+                               t = g_new0 (MonoType, 1);
+                               t->type = type;
+                               t->data.generic_param = g_new0 (MonoGenericParam, 1);
+                               t->data.generic_param->num = decode_value (p, &p);
+                               t->data.generic_param->name = "T";
+
+                               is_method = decode_value (p, &p);
+                               if (is_method) {
+                                       MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
+
+                                       if (!method_def) {
+                                               g_free (t->data.generic_param);
+                                               g_free (t);
+                                               return NULL;
+                                       }
+
+                                       container = mono_method_get_generic_container (method_def);
+                               } else {
+                                       MonoClass *class_def = decode_klass_ref (module, p, &p);
+                                       
+                                       if (!class_def) {
+                                               g_free (t->data.generic_param);
+                                               g_free (t);
+                                               return NULL;
+                                       }
+
+                                       container = class_def->generic_container;
                                }
-                               inst.type_argv [i] = &pclass->byval_arg;
+
+                               g_assert (container);
+                               t->data.generic_param->owner = container;
+
+                               klass = mono_class_from_mono_type (t);
+                       } else {
+                               g_assert_not_reached ();
                        }
-                       type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
-                       klass = mono_class_from_mono_type (type);
-                       mono_metadata_free_type (type);
                } else {
-                       image = load_image (module, decode_value (buf, &buf));
+                       image = load_image (module, decode_value (p, &p));
                        if (!image)
                                return NULL;
                        klass = mono_class_get (image, token);
                }
        } else if (token == MONO_TOKEN_TYPE_DEF) {
                /* Array */
-               image = load_image (module, decode_value (buf, &buf));
+               image = load_image (module, decode_value (p, &p));
                if (!image)
                        return NULL;
-               rank = decode_value (buf, &buf);
-               eklass = decode_klass_ref (module, buf, &buf);
+               rank = decode_value (p, &p);
+               eklass = decode_klass_ref (module, p, &p);
                klass = mono_array_class_get (eklass, rank);
        } else {
                g_assert_not_reached ();
@@ -296,7 +379,7 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
        g_assert (klass);
        mono_class_init (klass);
 
-       *endbuf = buf;
+       *endbuf = p;
        return klass;
 }
 
@@ -305,40 +388,198 @@ decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
 {
        MonoClass *klass = decode_klass_ref (module, buf, &buf);
        guint32 token;
+       guint8 *p = buf;
 
        if (!klass)
                return NULL;
 
-       token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
+       token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
 
-       *endbuf = buf;
+       *endbuf = p;
 
        return mono_class_get_field (klass, token);
 }
 
+/*
+ * decode_method_ref:
+ *
+ *   Decode a method reference, and return its image and token. This avoids loading
+ * metadata for the method if the caller does not need it. If the method has no token,
+ * then it is loaded from metadata and METHOD is set to the method instance.
+ */
 static inline MonoImage*
-decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
+decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, guint8 *buf, guint8 **endbuf)
 {
        guint32 image_index, value;
        MonoImage *image;
+       guint8 *p = buf;
+
+       if (method)
+               *method = NULL;
 
-       value = decode_value (buf, &buf);
+       value = decode_value (p, &p);
        image_index = value >> 24;
-       *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
 
-       if (image_index == 255) {
+       if (image_index == 253) {
+               /* Wrapper */
+               guint32 wrapper_type;
+
+               wrapper_type = decode_value (p, &p);
+
+               /* Doesn't matter */
+               image = mono_defaults.corlib;
+
+               switch (wrapper_type) {
+               case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
+                       MonoMethod *m = decode_method_ref_2 (module, p, &p);
+
+                       if (!m)
+                               return NULL;
+                       mono_class_init (m->klass);
+                       *method = mono_marshal_get_remoting_invoke_with_check (m);
+                       break;
+               }
+               case MONO_WRAPPER_PROXY_ISINST: {
+                       MonoClass *klass = decode_klass_ref (module, p, &p);
+                       if (!klass)
+                               return NULL;
+                       *method = mono_marshal_get_proxy_cancast (klass);
+                       break;
+               }
+               case MONO_WRAPPER_LDFLD:
+               case MONO_WRAPPER_LDFLDA:
+               case MONO_WRAPPER_STFLD:
+               case MONO_WRAPPER_ISINST: {
+                       MonoClass *klass = decode_klass_ref (module, p, &p);
+                       if (!klass)
+                               return NULL;
+                       if (wrapper_type == MONO_WRAPPER_LDFLD)
+                               *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
+                       else if (wrapper_type == MONO_WRAPPER_LDFLDA)
+                               *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
+                       else if (wrapper_type == MONO_WRAPPER_STFLD)
+                               *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
+                       else if (wrapper_type == MONO_WRAPPER_ISINST)
+                               *method = mono_marshal_get_isinst (klass);
+                       else
+                               g_assert_not_reached ();
+                       break;
+               }
+               case MONO_WRAPPER_LDFLD_REMOTE:
+                       *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
+                       break;
+               case MONO_WRAPPER_STFLD_REMOTE:
+                       *method = mono_marshal_get_stfld_remote_wrapper (NULL);
+                       break;
+               case MONO_WRAPPER_ALLOC: {
+                       int atype = decode_value (p, &p);
+
+                       *method = mono_gc_get_managed_allocator_by_type (atype);
+                       break;
+               }
+               case MONO_WRAPPER_STELEMREF:
+                       *method = mono_marshal_get_stelemref ();
+                       break;
+               case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
+                       MonoMethod *m = decode_method_ref_2 (module, p, &p);
+
+                       if (!m)
+                               return NULL;
+                       *method = mono_marshal_get_static_rgctx_invoke (m);
+                       break;
+               }
+               default:
+                       g_assert_not_reached ();
+               }
+       } else if (image_index == 255) {
                /* Methodspec */
-               image_index = decode_value (buf, &buf);
-               *token = decode_value (buf, &buf);
+               image_index = decode_value (p, &p);
+               *token = decode_value (p, &p);
+
+               image = load_image (module, image_index);
+               if (!image)
+                       return NULL;
+       } else if (image_index == 254) {
+               /* Method on generic instance */
+               MonoClass *klass;
+               MonoGenericContext ctx;
+               gboolean has_class_inst, has_method_inst;
+
+               /* 
+                * These methods do not have a token which resolves them, so we 
+                * resolve them immediately.
+                */
+               klass = decode_klass_ref (module, p, &p);
+               if (!klass)
+                       return NULL;
+
+               image_index = decode_value (p, &p);
+               *token = decode_value (p, &p);
+
+               image = load_image (module, image_index);
+               if (!image)
+                       return NULL;
+
+               *method = mono_get_method_full (image, *token, NULL, NULL);
+               if (!(*method))
+                       return NULL;
+
+               memset (&ctx, 0, sizeof (ctx));
+
+               if (FALSE && klass->generic_class) {
+                       ctx.class_inst = klass->generic_class->context.class_inst;
+                       ctx.method_inst = NULL;
+                       *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
+               }                       
+
+               memset (&ctx, 0, sizeof (ctx));
+
+               // FIXME: Memory management
+               has_class_inst = decode_value (p, &p);
+               if (has_class_inst) {
+                       ctx.class_inst = decode_generic_inst (module, p, &p);
+                       if (!ctx.class_inst)
+                               return NULL;
+               }
+               has_method_inst = decode_value (p, &p);
+               if (has_method_inst) {
+                       ctx.method_inst = decode_generic_inst (module, p, &p);
+                       if (!ctx.method_inst)
+                               return NULL;
+               }
+
+               *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
+       } else {
+               *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
+
+               image = load_image (module, image_index);
+               if (!image)
+                       return NULL;
        }
 
-       *endbuf = buf;
+       *endbuf = p;
+
+       return image;
+}
 
-       image = load_image (module, image_index);
+/*
+ * decode_method_ref_2:
+ *
+ *   Similar to decode_method_ref, but resolve and return the method itself.
+ */
+static MonoMethod*
+decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
+{
+       MonoMethod *method;
+       guint32 token;
+       MonoImage *image = decode_method_ref (module, &token, &method, buf, endbuf);
+
+       if (method)
+               return method;
        if (!image)
                return NULL;
-       else
-               return image;
+       return mono_get_method (image, token, NULL);
 }
 
 G_GNUC_UNUSED
@@ -532,7 +773,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        if (mono_compile_aot)
                return;
 
-       if (assembly->aot_module)
+       if (assembly->image->aot_module)
                /* 
                 * Already loaded. This can happen because the assembly loading code might invoke
                 * the assembly load hooks multiple times for the same assembly.
@@ -542,23 +783,46 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        if (assembly->image->dynamic)
                return;
 
-       TlsSetValue (globals_tls_id, NULL);
+       mono_aot_lock ();
+       if (static_aot_modules)
+               globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
+       else
+               globals = NULL;
+       mono_aot_unlock ();
 
-       if (use_aot_cache)
-               assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
-       else {
-               char *err;
-               aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
+       if (globals) {
+               /* Statically linked AOT module */
+               sofile = NULL;
+               aot_name = g_strdup_printf ("%s", assembly->aname.name);
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
+       } else {
+               TlsSetValue (globals_tls_id, NULL);
+
+               if (use_aot_cache)
+                       sofile = load_aot_module_from_cache (assembly, &aot_name);
+               else {
+                       char *err;
+                       aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
 
-               assembly->aot_module = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
+                       sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
 
-               if (!assembly->aot_module) {
-                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
-                       g_free (err);
+                       if (!sofile) {
+                               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
+                               g_free (err);
+                       }
                }
+
+               /*
+                * If the image was compiled in no-dlsym mode, it contains no global symbols,
+                * instead it contains an ELF ctor function which is called by dlopen () which 
+                * in turn calls mono_aot_register_globals () to register a table which contains
+                * the name and address of the globals.
+                */
+               globals = TlsGetValue (globals_tls_id);
+               TlsSetValue (globals_tls_id, NULL);
        }
 
-       if (!assembly->aot_module) {
+       if (!sofile && !globals) {
                if (mono_aot_only) {
                        fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
                        exit (1);
@@ -567,17 +831,6 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                return;
        }
 
-       /* 
-        * If the image was compiled in no-dlsym mode, it contains no global symbols,
-        * instead it contains an ELF ctor functions which is called by dlopen () which 
-        * in turn calls mono_aot_register_globals () to register a table which contains
-        * the name and address of the globals.
-        */
-       globals = TlsGetValue (globals_tls_id);
-       TlsSetValue (globals_tls_id, NULL);
-
-       sofile = assembly->aot_module;
-
        find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
        find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
        find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
@@ -623,8 +876,9 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                        exit (1);
                }
                g_free (aot_name);
-               mono_dl_close (sofile);
-               assembly->aot_module = NULL;
+               if (sofile)
+                       mono_dl_close (sofile);
+               assembly->image->aot_module = NULL;
                return;
        }
 
@@ -642,6 +896,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        amodule->got_size = *got_size_ptr;
        amodule->got [0] = assembly->image;
        amodule->globals = globals;
+       amodule->sofile = sofile;
 
        sscanf (opt_flags, "%d", &amodule->opts);               
 
@@ -748,11 +1003,13 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
        aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
 
-       g_hash_table_insert (aot_modules, assembly->image, amodule);
+       g_hash_table_insert (aot_modules, assembly, amodule);
        mono_aot_unlock ();
 
        mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
 
+       assembly->image->aot_module = amodule;
+
        /*
         * Since we store methoddef and classdef tokens when referring to methods/classes in
         * referenced assemblies, we depend on the exact versions of the referenced assemblies.
@@ -785,6 +1042,39 @@ mono_aot_register_globals (gpointer *globals)
        TlsSetValue (globals_tls_id, globals);
 }
 
+/*
+ * mono_aot_register_module:
+ *
+ *   This should be called by embedding code to register AOT modules statically linked
+ * into the executable. AOT_INFO should be the value of the 
+ * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
+ */
+void
+mono_aot_register_module (gpointer *aot_info)
+{
+       gpointer *globals;
+       char *aname;
+
+       globals = aot_info;
+       g_assert (globals);
+
+       /* Determine the assembly name */
+       find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
+       g_assert (aname);
+
+       /* This could be called before startup */
+       if (aot_modules)
+               mono_aot_lock ();
+
+       if (!static_aot_modules)
+               static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
+
+       g_hash_table_insert (static_aot_modules, aname, globals);
+
+       if (aot_modules)
+               mono_aot_unlock ();
+}
+
 void
 mono_aot_init (void)
 {
@@ -820,12 +1110,12 @@ decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guin
        info->no_special_static_fields = (flags >> 7) & 0x1;
 
        if (info->has_cctor) {
-               MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
+               MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, buf, &buf);
                if (!cctor_image)
                        return FALSE;
        }
        if (info->has_finalize) {
-               info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
+               info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, buf, &buf);
                if (!info->finalize_image)
                        return FALSE;
        }
@@ -844,44 +1134,30 @@ gpointer
 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
 {
        int i;
-       MonoAotModule *aot_module;
        MonoClass *klass = vtable->klass;
+       MonoAotModule *aot_module = klass->image->aot_module;
        guint8 *info, *p;
        MonoCachedClassInfo class_info;
        gboolean err;
        guint32 token;
        MonoImage *image;
 
-       if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
-               return NULL;
-
-       mono_aot_lock ();
-
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image);
-       if (!aot_module) {
-               mono_aot_unlock ();
+       if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
                return NULL;
-       }
 
        info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
        p = info;
 
        err = decode_cached_class_info (aot_module, &class_info, p, &p);
-       if (!err) {
-               mono_aot_unlock ();
+       if (!err)
                return NULL;
-       }
 
        for (i = 0; i < slot; ++i)
-               decode_method_ref (aot_module, &token, p, &p);
+               decode_method_ref (aot_module, &token, NULL, p, &p);
 
-       image = decode_method_ref (aot_module, &token, p, &p);
-       if (!image) {
-               mono_aot_unlock ();
+       image = decode_method_ref (aot_module, &token, NULL, p, &p);
+       if (!image)
                return NULL;
-       }
-
-       mono_aot_unlock ();
 
        return mono_aot_get_method_from_token (domain, image, token);
 }
@@ -889,30 +1165,18 @@ mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int sl
 gboolean
 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
 {
-       MonoAotModule *aot_module;
+       MonoAotModule *aot_module = klass->image->aot_module;
        guint8 *p;
        gboolean err;
 
-       if (klass->rank || !klass->image->assembly->aot_module)
+       if (klass->rank || !aot_module)
                return FALSE;
 
-       mono_aot_lock ();
-
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image);
-       if (!aot_module) {
-               mono_aot_unlock ();
-               return FALSE;
-       }
-
        p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
 
        err = decode_cached_class_info (aot_module, res, p, &p);
-       if (!err) {
-               mono_aot_unlock ();
+       if (!err)
                return FALSE;
-       }
-
-       mono_aot_unlock ();
 
        return TRUE;
 }
@@ -930,7 +1194,7 @@ mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
 gboolean
 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
 {
-       MonoAotModule *aot_module;
+       MonoAotModule *aot_module = image->aot_module;
        guint16 *table, *entry;
        guint16 table_size;
        guint32 hash;
@@ -941,17 +1205,11 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
        guint32 cols [MONO_TYPEDEF_SIZE];
        GHashTable *nspace_table;
 
-       if (!aot_modules)
+       if (!aot_module || !aot_module->class_name_table)
                return FALSE;
 
        mono_aot_lock ();
 
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image);
-       if (!aot_module || !aot_module->class_name_table) {
-               mono_aot_unlock ();
-               return FALSE;
-       }
-
        *klass = NULL;
 
        /* First look in the cache */
@@ -1038,8 +1296,10 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
        int i, buf_len;
        MonoJitInfo *jinfo;
        guint code_len, used_int_regs;
+       gboolean has_generic_jit_info;
        guint8 *p;
        MonoMethodHeader *header;
+       int generic_info_size;
 
        header = mono_method_get_header (method);
 
@@ -1048,11 +1308,16 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
        p = ex_info;
        code_len = decode_value (p, &p);
        used_int_regs = decode_value (p, &p);
+       has_generic_jit_info = decode_value (p, &p);
+       if (has_generic_jit_info)
+               generic_info_size = sizeof (MonoGenericJitInfo);
+       else
+               generic_info_size = 0;
 
        /* Exception table */
        if (header && header->num_clauses) {
                jinfo = 
-                       mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
+                       mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
                jinfo->num_clauses = header->num_clauses;
 
                for (i = 0; i < header->num_clauses; ++i) {
@@ -1073,7 +1338,7 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
                }
        }
        else
-               jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
+               jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + generic_info_size);
 
        jinfo->code_size = code_len;
        jinfo->used_regs = used_int_regs;
@@ -1081,6 +1346,24 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
        jinfo->code_start = code;
        jinfo->domain_neutral = 0;
 
+       if (has_generic_jit_info) {
+               MonoGenericJitInfo *gi;
+
+               jinfo->has_generic_jit_info = 1;
+
+               gi = mono_jit_info_get_generic_jit_info (jinfo);
+               g_assert (gi);
+
+               gi->has_this = decode_value (p, &p);
+               gi->this_reg = decode_value (p, &p);
+               gi->this_offset = decode_value (p, &p);
+
+               /* This currently contains no data */
+               gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
+
+               jinfo->method = decode_method_ref_2 (aot_module, p, &p);
+       }
+
        /* Load debug info */
        buf_len = decode_value (p, &p);
        mono_debug_add_aot_method (domain, method, code, p, buf_len);
@@ -1091,22 +1374,18 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
 MonoJitInfo *
 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
 {
-       MonoAssembly *ass = image->assembly;
-       MonoDl *module = ass->aot_module;
        int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
        guint32 token;
-       MonoAotModule *aot_module;
+       MonoAotModule *aot_module = image->aot_module;
        MonoMethod *method;
        MonoJitInfo *jinfo;
        guint8 *code, *ex_info;
        guint32 *table, *ptr;
        gboolean found;
 
-       if (!module)
+       if (!aot_module)
                return NULL;
 
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image);
-
        if (domain != mono_get_root_domain ())
                /* FIXME: */
                return NULL;
@@ -1222,8 +1501,30 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
        return jinfo;
 }
 
+/* Keep it in sync with the version in aot-compiler.c */
+static inline gboolean
+is_shared_got_patch (MonoJumpInfo *patch_info)
+{
+       switch (patch_info->type) {
+       case MONO_PATCH_INFO_VTABLE:
+       case MONO_PATCH_INFO_CLASS:
+       case MONO_PATCH_INFO_IID:
+       case MONO_PATCH_INFO_ADJUSTED_IID:
+       case MONO_PATCH_INFO_FIELD:
+       case MONO_PATCH_INFO_SFLDA:
+       case MONO_PATCH_INFO_DECLSEC:
+       case MONO_PATCH_INFO_LDTOKEN:
+       case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
+       case MONO_PATCH_INFO_RVA:
+       case MONO_PATCH_INFO_METHODCONST:
+               return TRUE;
+       default:
+               return FALSE;
+       }
+}
+
 static gboolean
-decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
+decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
 {
        guint8 *p = buf;
        gpointer *table;
@@ -1232,22 +1533,26 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
 
        switch (ji->type) {
        case MONO_PATCH_INFO_METHOD:
-       case MONO_PATCH_INFO_METHODCONST:
        case MONO_PATCH_INFO_METHOD_JUMP:
-       case MONO_PATCH_INFO_ICALL_ADDR: {
+       case MONO_PATCH_INFO_ICALL_ADDR:
+       case MONO_PATCH_INFO_METHOD_RGCTX: {
                guint32 token;
+               MonoMethod *method;
 
-               image = decode_method_ref (aot_module, &token, p, &p);
+               image = decode_method_ref (aot_module, &token, &method, p, &p);
                if (!image)
                        goto cleanup;
 
 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
-               if (!mono_aot_only && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
+               if (!method && !mono_aot_only && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
                        ji->data.target = mono_create_jit_trampoline_from_token (image, token);
                        ji->type = MONO_PATCH_INFO_ABS;
                }
                else {
-                       ji->data.method = mono_get_method (image, token, NULL);
+                       if (method)
+                               ji->data.method = method;
+                       else
+                               ji->data.method = mono_get_method (image, token, NULL);
                        g_assert (ji->data.method);
                        mono_class_init (ji->data.method->klass);
                }
@@ -1259,77 +1564,6 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
 
                break;
        }
-       case MONO_PATCH_INFO_WRAPPER: {
-               guint32 wrapper_type;
-
-               wrapper_type = decode_value (p, &p);
-
-               ji->type = MONO_PATCH_INFO_METHOD;
-
-               switch (wrapper_type) {
-               case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
-                       guint32 image_index, token, value;
-
-                       value = decode_value (p, &p);
-                       image_index = value >> 24;
-                       token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
-
-                       image = load_image (aot_module, image_index);
-                       if (!image)
-                               goto cleanup;
-                       ji->data.method = mono_get_method (image, token, NULL);
-                       g_assert (ji->data.method);
-                       mono_class_init (ji->data.method->klass);
-
-                       ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
-                       break;
-               }
-               case MONO_WRAPPER_PROXY_ISINST: {
-                       MonoClass *klass = decode_klass_ref (aot_module, p, &p);
-                       if (!klass)
-                               goto cleanup;
-                       ji->data.method = mono_marshal_get_proxy_cancast (klass);
-                       break;
-               }
-               case MONO_WRAPPER_LDFLD:
-               case MONO_WRAPPER_LDFLDA:
-               case MONO_WRAPPER_STFLD:
-               case MONO_WRAPPER_ISINST: {
-                       MonoClass *klass = decode_klass_ref (aot_module, p, &p);
-                       if (!klass)
-                               goto cleanup;
-                       if (wrapper_type == MONO_WRAPPER_LDFLD)
-                               ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
-                       else if (wrapper_type == MONO_WRAPPER_LDFLDA)
-                               ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
-                       else if (wrapper_type == MONO_WRAPPER_STFLD)
-                               ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
-                       else if (wrapper_type == MONO_WRAPPER_ISINST)
-                               ji->data.method = mono_marshal_get_isinst (klass);
-                       else
-                               g_assert_not_reached ();
-                       break;
-               }
-               case MONO_WRAPPER_LDFLD_REMOTE:
-                       ji->data.method = mono_marshal_get_ldfld_remote_wrapper (NULL);
-                       break;
-               case MONO_WRAPPER_STFLD_REMOTE:
-                       ji->data.method = mono_marshal_get_stfld_remote_wrapper (NULL);
-                       break;
-               case MONO_WRAPPER_ALLOC: {
-                       int atype = decode_value (p, &p);
-
-                       ji->data.method = mono_gc_get_managed_allocator_by_type (atype);
-                       break;
-               }
-               case MONO_WRAPPER_STELEMREF:
-                       ji->data.method = mono_marshal_get_stelemref ();
-                       break;
-               default:
-                       g_assert_not_reached ();
-               }
-               break;
-       }
        case MONO_PATCH_INFO_INTERNAL_METHOD:
        case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
                guint32 len = decode_value (p, &p);
@@ -1338,21 +1572,20 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
                p += len + 1;
                break;
        }
+       case MONO_PATCH_INFO_METHODCONST:
+               /* Shared */
+               ji->data.method = decode_method_ref_2 (aot_module, p, &p);
+               if (!ji->data.method)
+                       goto cleanup;
+               break;
        case MONO_PATCH_INFO_VTABLE:
        case MONO_PATCH_INFO_CLASS:
        case MONO_PATCH_INFO_IID:
        case MONO_PATCH_INFO_ADJUSTED_IID:
-               *got_offset = decode_value (p, &p);
-
-               if (aot_module->got [*got_offset]) {
-                       /* Already loaded */
-                       //printf ("HIT!\n");
-               } else {
-                       guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
-                       ji->data.klass = decode_klass_ref (aot_module, tmp, &tmp);
-                       if (!ji->data.klass)
-                               goto cleanup;
-               }
+               /* Shared */
+               ji->data.klass = decode_klass_ref (aot_module, p, &p);
+               if (!ji->data.klass)
+                       goto cleanup;
                break;
        case MONO_PATCH_INFO_CLASS_INIT:
        case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
@@ -1364,22 +1597,13 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
                ji->data.image = load_image (aot_module, decode_value (p, &p));
                if (!ji->data.image)
                        goto cleanup;
-               if (ji->data.image == aot_module->assembly->image)
-                       *got_offset = 0;
                break;
        case MONO_PATCH_INFO_FIELD:
        case MONO_PATCH_INFO_SFLDA:
-               *got_offset = decode_value (p, &p);
-
-               if (aot_module->got [*got_offset]) {
-                       /* Already loaded */
-                       //printf ("HIT2!\n");
-               } else {
-                       guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
-                       ji->data.field = decode_field_info (aot_module, tmp, &tmp);
-                       if (!ji->data.field)
-                               goto cleanup;
-               }
+               /* Shared */
+               ji->data.field = decode_field_info (aot_module, p, &p);
+               if (!ji->data.field)
+                       goto cleanup;
                break;
        case MONO_PATCH_INFO_SWITCH:
                ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
@@ -1417,17 +1641,11 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
        case MONO_PATCH_INFO_DECLSEC:
        case MONO_PATCH_INFO_LDTOKEN:
        case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
-               *got_offset = decode_value (p, &p);
-
-               if (aot_module->got [*got_offset]) {
-                       /* Already loaded */
-               } else {
-                       guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
-                       image = load_image (aot_module, decode_value (tmp, &tmp));
-                       if (!image)
-                               goto cleanup;
-                       ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
-               }
+               /* Shared */
+               image = load_image (aot_module, decode_value (p, &p));
+               if (!image)
+                       goto cleanup;
+               ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
                break;
        case MONO_PATCH_INFO_EXC_NAME:
                ji->data.klass = decode_klass_ref (aot_module, p, &p);
@@ -1439,7 +1657,25 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
                ji->data.offset = decode_value (p, &p);
                break;
        case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
+       case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
+               break;
+       case MONO_PATCH_INFO_RGCTX_FETCH: {
+               gboolean res;
+               MonoJumpInfoRgctxEntry *entry;
+
+               entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
+               entry->method = decode_method_ref_2 (aot_module, p, &p);
+               entry->in_mrgctx = decode_value (p, &p);
+               entry->info_type = decode_value (p, &p);
+               entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
+               entry->data->type = decode_value (p, &p);
+               
+               res = decode_patch (aot_module, mp, entry->data, p, &p);
+               if (!res)
+                       goto cleanup;
+               ji->data.rgctx_entry = entry;
                break;
+       }
        default:
                g_warning ("unhandled type %d", ji->type);
                g_assert_not_reached ();
@@ -1453,6 +1689,36 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
        return FALSE;
 }
 
+static gboolean
+decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
+{
+       guint8 *p = buf;
+       guint8 *shared_p;
+       gboolean res;
+
+       if (is_shared_got_patch (ji)) {
+               *got_offset = decode_value (p, &p);
+
+               if (aot_module->got [*got_offset]) {
+                       /* Already loaded */
+                       //printf ("HIT!\n");
+               } else {
+                       shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
+
+                       res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
+                       if (!res)
+                               return FALSE;
+               }
+       } else {
+               res = decode_patch (aot_module, mp, ji, p, &p);
+               if (!res)
+                       return FALSE;
+       }
+
+       *endbuf = p;
+       return TRUE;
+}
+
 static MonoJumpInfo*
 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
                                 guint32 got_index, guint32 **got_slots, 
@@ -1488,7 +1754,7 @@ load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
        for (pindex = 0; pindex < n_patches; ++pindex) {
                MonoJumpInfo *ji = &patches [pindex];
 
-               if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
+               if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
                        goto cleanup;
 
                if ((*got_slots) [pindex] == 0xffffffff)
@@ -1505,17 +1771,36 @@ load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
        return NULL;
 }
 
-static gpointer
-mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
+static void
+register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
+{
+       /*
+        * Jump addresses cannot be patched by the trampoline code since it
+        * does not have access to the caller's address. Instead, we collect
+        * the addresses of the GOT slots pointing to a method, and patch
+        * them after the method has been compiled.
+        */
+       MonoJitDomainInfo *info = jit_domain_info (domain);
+       GSList *list;
+               
+       mono_domain_lock (domain);
+       if (!info->jump_target_got_slot_hash)
+               info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
+       list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
+       list = g_slist_prepend (list, got_slot);
+       g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
+       mono_domain_unlock (domain);
+}
+
+gpointer
+mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
 {
        MonoClass *klass = method->klass;
-       MonoAssembly *ass = klass->image->assembly;
-       MonoDl *module = ass->aot_module;
        guint32 method_index = mono_metadata_token_index (method->token) - 1;
        guint8 *code, *info;
-       MonoAotModule *aot_module;
+       MonoAotModule *aot_module = klass->image->aot_module;
 
-       if (!module)
+       if (!aot_module)
                return NULL;
 
        if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
@@ -1526,8 +1811,6 @@ mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
                (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
                (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
                return NULL;
-       
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image);
 
        g_assert (klass->inited);
 
@@ -1551,26 +1834,45 @@ mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
                if (!method->wrapper_type || !aot_module->wrapper_info)
                        return NULL;
 
+               mono_aot_lock ();
+
+               /* Avoid doing a hash table lookup using strings if possible */
+               if (!aot_module->wrappers_to_code)
+                       aot_module->wrappers_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
+               code = g_hash_table_lookup (aot_module->wrappers_to_code, method);
+               if (code) {
+                       mono_aot_unlock ();
+                       return code;
+               }
+
                /* Try to find the wrapper among the wrapper info */
                full_name = mono_method_full_name (method, TRUE);
-               p = (char*)aot_module->wrapper_info;
-               while (*p) {
-                       char *end;
-
-                       end = p + strlen (p) + 1;
-                       end = ALIGN_PTR_TO (end, 4);
-                       method_index = *(guint32*)end;
-                       end += 4;
-                       if (strcmp (full_name, p) == 0)
-                               break;
-                       p = end;
+
+               /* Create a hash table to avoid repeated linear searches */
+               if (!aot_module->wrappers_by_name) {
+                       aot_module->wrappers_by_name = g_hash_table_new (g_str_hash, g_str_equal);
+                       p = (char*)aot_module->wrapper_info;
+                       while (*p) {
+                               char *end;
+
+                               end = p + strlen (p) + 1;
+                               end = ALIGN_PTR_TO (end, 4);
+                               method_index = *(guint32*)end;
+                               end += 4;
+                               /* The + 1 is used to distinguish a 0 index from a not-found one */
+                               g_hash_table_insert (aot_module->wrappers_by_name, p, GUINT_TO_POINTER (method_index + 1));
+                               p = end;
+                       }
                }
-               g_free (full_name);
+               method_index = GPOINTER_TO_UINT (g_hash_table_lookup (aot_module->wrappers_by_name, full_name));
+               mono_aot_unlock ();
 
-               if (!(*p))
-                       /* Not found */
+               g_free (full_name);
+               if (!method_index)
                        return NULL;
 
+               method_index --;
+
                /* Needed by find_jit_info */
                mono_aot_lock ();
                if (!aot_module->wrappers)
@@ -1591,8 +1893,10 @@ mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
        code = &aot_module->code [aot_module->code_offsets [method_index]];
        info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
 
+       mono_aot_lock ();
        if (!aot_module->methods_loaded)
                aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
+       mono_aot_unlock ();
 
        if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
                return code;
@@ -1630,7 +1934,9 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
                        jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
                }
 
+               mono_domain_lock (domain);
                code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
+               mono_domain_unlock (domain);
                memcpy (code2, code, jinfo->code_size);
                mono_arch_flush_icache (code2, jinfo->code_size);
                code = code2;
@@ -1668,15 +1974,16 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
                if (patches == NULL)
                        goto cleanup;
 
-               /* Do this outside the lock to avoid deadlocks */
-               mono_aot_unlock ();
                non_got_patches = FALSE;
                for (pindex = 0; pindex < n_patches; ++pindex) {
                        MonoJumpInfo *ji = &patches [pindex];
 
                        if (is_got_patch (ji->type)) {
-                               if (!aot_module->got [got_slots [pindex]])
+                               if (!aot_module->got [got_slots [pindex]]) {
                                        aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
+                                       if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
+                                               register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
+                               }
                                ji->type = MONO_PATCH_INFO_NONE;
                        }
                        else
@@ -1692,7 +1999,6 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
                        make_writable (code, jinfo->code_size);
                        mono_arch_patch_code (method, domain, code, patch_info, TRUE);
                }
-               mono_aot_lock ();
 
                g_free (got_slots);
 
@@ -1700,6 +2006,8 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
                        mono_mempool_destroy (mp);
        }
 
+       mono_aot_lock ();
+
        mono_jit_stats.methods_aot++;
 
        if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
@@ -1718,6 +2026,11 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
 
        init_plt (aot_module);
 
+       if (method->wrapper_type)
+               g_hash_table_insert (aot_module->wrappers_to_code, method, code);
+
+       mono_aot_unlock ();
+
        return code;
 
  cleanup:
@@ -1732,41 +2045,28 @@ mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod
        return NULL;
 }
 
+/**
+ * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
+ * method.
+ */
 gpointer
-mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
-{
-       gpointer code;
-
-       mono_aot_lock ();
-       code = mono_aot_get_method_inner (domain, method);
-       mono_aot_unlock ();
-
-       return code;
-}
-
-static gpointer
-mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
+mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
 {
-       MonoAssembly *ass = image->assembly;
        MonoMemPool *mp;
        int i, method_index, pindex, got_index, n_patches, used_strings;
        gboolean keep_patches = TRUE;
        guint8 *p;
-       MonoDl *module = ass->aot_module;
        guint8 *code = NULL;
        guint8 *info;
-       MonoAotModule *aot_module;
-
-       *klass = NULL;
+       MonoAotModule *aot_module = image->aot_module;
+       MonoClass *klass;
 
-       if (!module)
+       if (!aot_module)
                return NULL;
 
        if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
                return NULL;
 
-       aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image);
-
        if (domain != mono_get_root_domain ())
                return NULL;
 
@@ -1781,8 +2081,10 @@ mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guin
        code = &aot_module->code [aot_module->code_offsets [method_index]];
        info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
 
+       mono_aot_lock ();
        if (!aot_module->methods_loaded)
                aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
+       mono_aot_unlock ();
 
        if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
                return code;
@@ -1798,7 +2100,7 @@ mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guin
        }
 
        p = info;
-       *klass = decode_klass_ref (aot_module, p, &p);
+       klass = decode_klass_ref (aot_module, p, &p);
 
        if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
                MonoMethod *method = mono_get_method (image, token, NULL);
@@ -1839,21 +2141,19 @@ mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guin
                if (patches == NULL)
                        goto cleanup;
 
-               /* Do this outside the lock to avoid deadlocks */
-               mono_aot_unlock ();
-
                for (pindex = 0; pindex < n_patches; ++pindex) {
                        MonoJumpInfo *ji = &patches [pindex];
 
                        if (is_got_patch (ji->type)) {
-                               if (!aot_module->got [got_slots [pindex]])
+                               if (!aot_module->got [got_slots [pindex]]) {
                                        aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
+                                       if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
+                                               register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
+                               }
                                ji->type = MONO_PATCH_INFO_NONE;
                        }
                }
 
-               mono_aot_lock ();
-
                g_free (got_slots);
 
                if (!keep_patches)
@@ -1862,10 +2162,17 @@ mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guin
 
        mono_jit_stats.methods_aot++;
 
+       mono_aot_lock ();
+
        aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
 
        init_plt (aot_module);
 
+       mono_aot_unlock ();
+
+       if (klass)
+               mono_runtime_class_init (mono_class_vtable (domain, klass));
+
        return code;
 
  cleanup:
@@ -1877,29 +2184,6 @@ mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guin
        return NULL;
 }
 
-/**
- * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
- * method.
- */
-gpointer
-mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
-{
-       gpointer res;   
-       MonoClass *klass;
-
-       mono_aot_lock ();
-       res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
-       mono_aot_unlock ();
-
-       if (!res)
-               return NULL;
-
-       if (klass)
-               mono_runtime_class_init (mono_class_vtable (domain, klass));
-
-       return res;
-}
-
 typedef struct {
        guint8 *addr;
        gboolean res;
@@ -2083,6 +2367,7 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
        MonoJumpInfo ji;
        MonoAotModule *module = (MonoAotModule*)aot_module;
        gboolean res;
+       MonoMemPool *mp;
 
        //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
 
@@ -2090,12 +2375,15 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
 
        ji.type = decode_value (p, &p);
 
-       res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
+       mp = mono_mempool_new_size (512);
+       res = decode_patch (module, mp, &ji, p, &p);
        // FIXME: Error handling (how ?)
        g_assert (res);
 
        target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
 
+       mono_mempool_destroy (mp);
+
        /* Patch the PLT entry with target which might be the actual method not a trampoline */
        plt_entry = mono_aot_get_plt_entry (code);
        g_assert (plt_entry);
@@ -2220,7 +2508,7 @@ mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
        return ((guint32*)plt_entry) [4];
 #else
        g_assert_not_reached ();
-       return NULL;
+       return 0;
 #endif
 }
 
@@ -2232,21 +2520,21 @@ load_named_code (MonoAotModule *amodule, const char *name)
        int n_patches, got_index, pindex;
        MonoMemPool *mp;
        gpointer code;
-       MonoAssembly *assembly = amodule->assembly;
 
        /* Load the code */
 
        symbol = g_strdup_printf ("%s", name);
-       find_symbol (assembly->aot_module, amodule->globals, symbol, (gpointer *)&code);
+       find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
        g_free (symbol);
-       g_assert (code);
+       if (!code)
+               g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
 
        /* Load info */
 
        symbol = g_strdup_printf ("%s_p", name);
-       find_symbol (assembly->aot_module, amodule->globals, symbol, (gpointer *)&p);
+       find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
        g_free (symbol);
        if (!p)
                /* Nothing to patch */
@@ -2287,12 +2575,19 @@ load_named_code (MonoAotModule *amodule, const char *name)
 #ifdef __x86_64__
                                } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
                                        target = mono_amd64_throw_exception;
+#endif
+#ifdef __arm__
+                               } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
+                                       target = mono_arm_throw_exception;
 #endif
                                } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
                                        int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
                                        target = (gpointer)mono_get_trampoline_func (tramp_type2);
+                               } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
+                                       guint32 slot = atoi (ji->data.name + strlen ("specific_trampoline_lazy_fetch_"));
+                                       target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
                                } else {
-                                       fprintf (stderr, "%s\n", ji->data.name);
+                                       fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
                                        g_assert_not_reached ();
                                        target = NULL;
                                }
@@ -2321,21 +2616,14 @@ gpointer
 mono_aot_get_named_code (const char *name)
 {
        MonoImage *image;
-       MonoAssembly *assembly;
        MonoAotModule *amodule;
 
        image = mono_defaults.corlib;
        g_assert (image);
 
-       mono_aot_lock ();
-
-       assembly = image->assembly;
-       g_assert (assembly);
-       amodule = (MonoAotModule*) g_hash_table_lookup (aot_modules, image);
+       amodule = image->aot_module;
        g_assert (amodule);
 
-       mono_aot_unlock ();
-
        return load_named_code (amodule, name);
 }
 
@@ -2356,8 +2644,7 @@ mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampo
 
        mono_aot_lock ();
 
-       g_assert (image->assembly);
-       amodule = (MonoAotModule*) g_hash_table_lookup (aot_modules, image);
+       amodule = image->aot_module;
        g_assert (amodule);
 
        if (amodule->trampoline_index == amodule->num_trampolines)
@@ -2400,18 +2687,12 @@ mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampo
 gpointer
 mono_aot_get_unbox_trampoline (MonoMethod *method)
 {
-       MonoClass *klass = method->klass;
        guint32 method_index = mono_metadata_token_index (method->token) - 1;
        MonoAotModule *amodule;
        char *symbol;
        gpointer code;
 
-       mono_aot_lock ();
-
-       amodule = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image);
-
-       mono_aot_unlock ();
-
+       amodule = method->klass->image->aot_module;
        g_assert (amodule);
 
        symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
@@ -2420,6 +2701,18 @@ mono_aot_get_unbox_trampoline (MonoMethod *method)
        return code;
 }
 
+gpointer
+mono_aot_get_lazy_fetch_trampoline (guint32 slot)
+{
+       char *symbol;
+       gpointer code;
+
+       symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
+       code = load_named_code (mono_defaults.corlib->aot_module, symbol);
+       g_free (symbol);
+       return code;
+}
+
 /*
  * mono_aot_get_n_pagefaults:
  *