2008-09-07 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / aot-runtime.c
index a320578e57e3aa3170b0ea03d75ac62d63cb9218..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;
@@ -239,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 ();
@@ -303,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;
 }
 
@@ -312,33 +388,113 @@ 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, 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;
 
-       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)
@@ -346,33 +502,54 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
        } else if (image_index == 254) {
                /* Method on generic instance */
                MonoClass *klass;
-               guint32 token2;
-               MonoGenericContext context;
+               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, buf, &buf);
+               klass = decode_klass_ref (module, p, &p);
                if (!klass)
                        return NULL;
 
-               image_index = decode_value (buf, &buf);
-               token2 = 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;
 
-               g_assert (method);
-               *method = mono_get_method_full (image, token2, NULL, NULL);
-               g_assert (*method);
+               *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));
 
-               g_assert (klass->generic_class);
-               context.class_inst = klass->generic_class->context.class_inst;
-               context.method_inst = NULL;
+               // 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, &context);
+               *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
        } else {
                *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
 
@@ -381,11 +558,30 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        return NULL;
        }
 
-       *endbuf = buf;
+       *endbuf = p;
 
        return image;
 }
 
+/*
+ * 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;
+       return mono_get_method (image, token, NULL);
+}
+
 G_GNUC_UNUSED
 static void
 make_writable (guint8* addr, guint32 len)
@@ -1100,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);
 
@@ -1110,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) {
@@ -1135,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;
@@ -1143,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);
@@ -1303,27 +1524,13 @@ is_shared_got_patch (MonoJumpInfo *patch_info)
 }
 
 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;
        MonoImage *image;
-       guint8 *shared_p;
        int i;
 
-       if (is_shared_got_patch (ji)) {
-               *got_offset = decode_value (p, &p);
-
-               if (aot_module->got [*got_offset]) {
-                       /* Already loaded */
-                       //printf ("HIT!\n");
-                       *endbuf = p;
-                       return TRUE;
-               } else {
-                       shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
-               }
-       }
-
        switch (ji->type) {
        case MONO_PATCH_INFO_METHOD:
        case MONO_PATCH_INFO_METHOD_JUMP:
@@ -1357,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);
@@ -1436,29 +1572,18 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
                p += len + 1;
                break;
        }
-       case MONO_PATCH_INFO_METHODCONST: {
-               guint32 token;
-               MonoMethod *method;
-
+       case MONO_PATCH_INFO_METHODCONST:
                /* Shared */
-               image = decode_method_ref (aot_module, &token, &method, shared_p, &shared_p);
-               if (!image)
-                       goto cleanup;
-
-               if (method)
-                       ji->data.method = method;
-               else
-                       ji->data.method = mono_get_method (image, token, NULL);
+               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:
                /* Shared */
-               ji->data.klass = decode_klass_ref (aot_module, shared_p, &shared_p);
+               ji->data.klass = decode_klass_ref (aot_module, p, &p);
                if (!ji->data.klass)
                        goto cleanup;
                break;
@@ -1472,13 +1597,11 @@ 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:
                /* Shared */
-               ji->data.field = decode_field_info (aot_module, shared_p, &shared_p);
+               ji->data.field = decode_field_info (aot_module, p, &p);
                if (!ji->data.field)
                        goto cleanup;
                break;
@@ -1519,10 +1642,10 @@ decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji,
        case MONO_PATCH_INFO_LDTOKEN:
        case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
                /* Shared */
-               image = load_image (aot_module, decode_value (shared_p, &shared_p));
+               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 (shared_p, &shared_p));
+               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);
@@ -1534,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 ();
@@ -1548,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, 
@@ -1583,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)
@@ -1663,26 +1834,45 @@ mono_aot_get_method (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)
@@ -1836,6 +2026,9 @@ 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;
@@ -2174,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);
 
@@ -2181,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);
@@ -2329,7 +2526,8 @@ load_named_code (MonoAotModule *amodule, const char *name)
        symbol = g_strdup_printf ("%s", name);
        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);
 
@@ -2385,8 +2583,11 @@ load_named_code (MonoAotModule *amodule, const char *name)
                                } 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;
                                }
@@ -2500,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:
  *