Merge remote branch 'upstream/master'
[mono.git] / mono / mini / aot-runtime.c
index 789f9ce907d049e55d5e1aad8d4e8fafc7ed6955..115384028f003b2d2bca9677e26e655fb197ce92 100644 (file)
@@ -51,6 +51,7 @@
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/monitor.h>
 #include <mono/metadata/threads-types.h>
+#include <mono/metadata/mono-endian.h>
 #include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-mmap.h>
 #include "mono/utils/mono-compiler.h"
@@ -65,6 +66,8 @@
 #define SHARED_EXT ".dll"
 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
 #define SHARED_EXT ".dylib"
+#elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
+#define SHARED_EXT ".dylib"
 #else
 #define SHARED_EXT ".so"
 #endif
@@ -110,9 +113,10 @@ typedef struct MonoAotModule {
        guint32 *extra_method_table;
        guint32 *extra_method_info_offsets;
        guint8 *unwind_info;
+       guint8 *thumb_end;
 
-       /* Points to the GNU .eh_frame_hdr section, if it exists */
-       guint8 *eh_frame_hdr;
+       /* Points to the mono EH data created by LLVM */
+       guint8 *mono_eh_frame;
 
        /* Points to the trampolines */
        guint8 *trampolines [MONO_AOT_TRAMP_NUM];
@@ -165,6 +169,8 @@ static guint32 n_pagefaults = 0;
 static gsize aot_code_low_addr = (gssize)-1;
 static gsize aot_code_high_addr = 0;
 
+static GHashTable *aot_jit_icall_hash;
+
 static void
 init_plt (MonoAotModule *info);
 
@@ -191,7 +197,7 @@ load_image (MonoAotModule *amodule, int index, gboolean set_error)
        if (amodule->out_of_date)
                return NULL;
 
-       assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
+       assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
        if (!assembly) {
                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
                amodule->out_of_date = TRUE;
@@ -462,77 +468,53 @@ decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
        return mono_class_get_field (klass, token);
 }
 
-/*
- * can_method_ref_match_method:
- *
- *   Determine if calling decode_resolve_method_ref on P could return the same method as 
- * METHOD. This is an optimization to avoid calling decode_resolve_method_ref () which
- * would create MonoMethods which are not needed etc.
- */
-static gboolean
-can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
-{
-       guint8 *p = buf;
-       guint32 image_index, value;
-
-       /* Keep this in sync with decode_method_ref () */
-       value = decode_value (p, &p);
-       image_index = value >> 24;
-
-       if (image_index == MONO_AOT_METHODREF_WRAPPER) {
-               guint32 wrapper_type;
-
-               if (!method->wrapper_type)
-                       return FALSE;
-
-               wrapper_type = decode_value (p, &p);
-
-               if (method->wrapper_type != wrapper_type)
-                       return FALSE;
-       } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
-               return FALSE;
-       } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
-               if (method->wrapper_type)
-                       return FALSE;
-       }
-
-       return TRUE;
-}
+/* Stores information returned by decode_method_ref () */
+typedef struct {
+       MonoImage *image;
+       guint32 token;
+       MonoMethod *method;
+       gboolean no_aot_trampoline;
+} MethodRef;
 
 /*
- * decode_method_ref:
+ * decode_method_ref_with_target:
  *
- *   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.
+ *   Decode a method reference, storing the image/token into a MethodRef structure.
+ * 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 ref->method is set to the method instance.
+ * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method couldn't resolve to TARGET, and return FALSE.
  */
-static MonoImage*
-decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
+static gboolean
+decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
 {
        guint32 image_index, value;
        MonoImage *image = NULL;
        guint8 *p = buf;
 
-       if (method)
-               *method = NULL;
-       if (no_aot_trampoline)
-               *no_aot_trampoline = FALSE;
+       memset (ref, 0, sizeof (MethodRef));
 
        value = decode_value (p, &p);
        image_index = value >> 24;
 
        if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
-               if (no_aot_trampoline)
-                       *no_aot_trampoline = TRUE;
+               ref->no_aot_trampoline = TRUE;
                value = decode_value (p, &p);
                image_index = value >> 24;
        }
 
+       if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
+               if (target && target->wrapper_type)
+                       return FALSE;
+       }
+
        if (image_index == MONO_AOT_METHODREF_WRAPPER) {
                guint32 wrapper_type;
 
                wrapper_type = decode_value (p, &p);
 
+               if (target && target->wrapper_type != wrapper_type)
+                       return FALSE;
+
                /* Doesn't matter */
                image = mono_defaults.corlib;
 
@@ -541,16 +523,16 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        MonoMethod *m = decode_resolve_method_ref (module, p, &p);
 
                        if (!m)
-                               return NULL;
+                               return FALSE;
                        mono_class_init (m->klass);
-                       *method = mono_marshal_get_remoting_invoke_with_check (m);
+                       ref->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);
+                               return FALSE;
+                       ref->method = mono_marshal_get_proxy_cancast (klass);
                        break;
                }
                case MONO_WRAPPER_LDFLD:
@@ -559,43 +541,43 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                case MONO_WRAPPER_ISINST: {
                        MonoClass *klass = decode_klass_ref (module, p, &p);
                        if (!klass)
-                               return NULL;
+                               return FALSE;
                        if (wrapper_type == MONO_WRAPPER_LDFLD)
-                               *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
+                               ref->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);
+                               ref->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);
+                               ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
                        else if (wrapper_type == MONO_WRAPPER_ISINST)
-                               *method = mono_marshal_get_isinst (klass);
+                               ref->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);
+                       ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
                        break;
                case MONO_WRAPPER_STFLD_REMOTE:
-                       *method = mono_marshal_get_stfld_remote_wrapper (NULL);
+                       ref->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);
+                       ref->method = mono_gc_get_managed_allocator_by_type (atype);
                        break;
                }
                case MONO_WRAPPER_WRITE_BARRIER:
-                       *method = mono_gc_get_write_barrier ();
+                       ref->method = mono_gc_get_write_barrier ();
                        break;
                case MONO_WRAPPER_STELEMREF:
-                       *method = mono_marshal_get_stelemref ();
+                       ref->method = mono_marshal_get_stelemref ();
                        break;
                case MONO_WRAPPER_SYNCHRONIZED: {
                        MonoMethod *m = decode_resolve_method_ref (module, p, &p);
 
                        if (!m)
-                               return NULL;
-                       *method = mono_marshal_get_synchronized_wrapper (m);
+                               return FALSE;
+                       ref->method = mono_marshal_get_synchronized_wrapper (m);
                        break;
                }
                case MONO_WRAPPER_UNKNOWN: {
@@ -603,16 +585,37 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        MonoMethod *orig_method;
                        int subtype = decode_value (p, &p);
 
-                       if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
-                               desc = mono_method_desc_new ("Monitor:Enter", FALSE);
-                       else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
-                               desc = mono_method_desc_new ("Monitor:Exit", FALSE);
-                       else
-                               g_assert_not_reached ();
-                       orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
-                       g_assert (orig_method);
-                       mono_method_desc_free (desc);
-                       *method = mono_monitor_get_fast_path (orig_method);
+                       if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE || subtype == MONO_AOT_WRAPPER_STRUCTURE_TO_PTR) {
+                               MonoClass *klass = decode_klass_ref (module, p, &p);
+                               
+                               if (!klass)
+                                       return FALSE;
+
+                               g_assert (target);
+                               if (klass != target->klass)
+                                       return FALSE;
+
+                               if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE) {
+                                       if (strcmp (target->name, "PtrToStructure"))
+                                               return FALSE;
+                                       ref->method = mono_marshal_get_ptr_to_struct (klass);
+                               } else {
+                                       if (strcmp (target->name, "StructureToPtr"))
+                                               return FALSE;
+                                       ref->method = mono_marshal_get_struct_to_ptr (klass);
+                               }
+                       } else {
+                               if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
+                                       desc = mono_method_desc_new ("Monitor:Enter", FALSE);
+                               else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
+                                       desc = mono_method_desc_new ("Monitor:Exit", FALSE);
+                               else
+                                       g_assert_not_reached ();
+                               orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
+                               g_assert (orig_method);
+                               mono_method_desc_free (desc);
+                               ref->method = mono_monitor_get_fast_path (orig_method);
+                       }
                        break;
                }
                case MONO_WRAPPER_RUNTIME_INVOKE: {
@@ -620,8 +623,8 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        MonoMethod *m = decode_resolve_method_ref (module, p, &p);
 
                        if (!m)
-                               return NULL;
-                       *method = mono_marshal_get_runtime_invoke (m, FALSE);
+                               return FALSE;
+                       ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
                        break;
                }
                case MONO_WRAPPER_MANAGED_TO_MANAGED: {
@@ -631,25 +634,41 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                                int rank = decode_value (p, &p);
                                int elem_size = decode_value (p, &p);
 
-                               *method = mono_marshal_get_array_address (rank, elem_size);
+                               ref->method = mono_marshal_get_array_address (rank, elem_size);
                        } else {
                                g_assert_not_reached ();
                        }
                        break;
                }
+               case MONO_WRAPPER_MANAGED_TO_NATIVE: {
+                       MonoMethod *m = decode_resolve_method_ref (module, p, &p);
+
+                       if (!m)
+                               return FALSE;
+
+                       /* This should only happen when looking for an extra method */
+                       g_assert (target);
+                       if (mono_marshal_method_from_wrapper (target) == m)
+                               ref->method = target;
+                       else
+                               return FALSE;
+                       break;
+               }
                default:
                        g_assert_not_reached ();
                }
        } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
+               if (target)
+                       return FALSE;
                /* Can't decode these */
                g_assert_not_reached ();
        } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
                image_index = decode_value (p, &p);
-               *token = decode_value (p, &p);
+               ref->token = decode_value (p, &p);
 
                image = load_image (module, image_index, TRUE);
                if (!image)
-                       return NULL;
+                       return FALSE;
        } else if (image_index == MONO_AOT_METHODREF_GINST) {
                MonoClass *klass;
                MonoGenericContext ctx;
@@ -660,18 +679,21 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                 */
                klass = decode_klass_ref (module, p, &p);
                if (!klass)
-                       return NULL;
+                       return FALSE;
+
+               if (target && target->klass != klass)
+                       return FALSE;
 
                image_index = decode_value (p, &p);
-               *token = decode_value (p, &p);
+               ref->token = decode_value (p, &p);
 
                image = load_image (module, image_index, TRUE);
                if (!image)
-                       return NULL;
+                       return FALSE;
 
-               *method = mono_get_method_full (image, *token, NULL, NULL);
-               if (!(*method))
-                       return NULL;
+               ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
+               if (!ref->method)
+                       return FALSE;
 
                memset (&ctx, 0, sizeof (ctx));
 
@@ -679,75 +701,89 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        ctx.class_inst = klass->generic_class->context.class_inst;
                        ctx.method_inst = NULL;
  
-                       *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
+                       ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
                }                       
 
                memset (&ctx, 0, sizeof (ctx));
 
                if (!decode_generic_context (module, &ctx, p, &p))
-                       return NULL;
+                       return FALSE;
 
-               *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
+               ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
        } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
                MonoClass *klass;
                int method_type;
 
                klass = decode_klass_ref (module, p, &p);
                if (!klass)
-                       return NULL;
+                       return FALSE;
                method_type = decode_value (p, &p);
-               *token = 0;
                switch (method_type) {
                case 0:
-                       *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
+                       ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
                        break;
                case 1:
-                       *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
+                       ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
                        break;
                case 2:
-                       *method = mono_class_get_method_from_name (klass, "Get", -1);
+                       ref->method = mono_class_get_method_from_name (klass, "Get", -1);
                        break;
                case 3:
-                       *method = mono_class_get_method_from_name (klass, "Address", -1);
+                       ref->method = mono_class_get_method_from_name (klass, "Address", -1);
                        break;
                case 4:
-                       *method = mono_class_get_method_from_name (klass, "Set", -1);
+                       ref->method = mono_class_get_method_from_name (klass, "Set", -1);
                        break;
                default:
                        g_assert_not_reached ();
                }
        } else {
                g_assert (image_index < MONO_AOT_METHODREF_MIN);
-               *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
+               ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
 
                image = load_image (module, image_index, TRUE);
                if (!image)
-                       return NULL;
+                       return FALSE;
        }
 
        *endbuf = p;
 
-       return image;
+       ref->image = image;
+
+       return TRUE;
+}
+
+static gboolean
+decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
+{
+       return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
 }
 
 /*
- * decode_resolve_method_ref:
+ * decode_resolve_method_ref_with_target:
  *
  *   Similar to decode_method_ref, but resolve and return the method itself.
  */
 static MonoMethod*
-decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
+decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
 {
-       MonoMethod *method;
-       guint32 token;
-       MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
+       MethodRef ref;
+       gboolean res;
 
-       if (method)
-               return method;
-       if (!image)
+       res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
+       if (!res)
+               return NULL;
+       if (ref.method)
+               return ref.method;
+       if (!ref.image)
                return NULL;
-       method = mono_get_method (image, token, NULL);
-       return method;
+       return mono_get_method (ref.image, ref.token, NULL);
+}
+
+static MonoMethod*
+decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
+{
+       return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
 }
 
 static void
@@ -928,24 +964,64 @@ find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *valu
        }
 }
 
-#if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
-static int
-dl_callback (struct dl_phdr_info *info, size_t size, void *data)
+static gboolean
+check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
 {
-       int j;
-       MonoAotModule *amodule = data;
+       char *build_info;
+       char *msg = NULL;
+       gboolean usable = TRUE;
+       gboolean full_aot;
+       guint8 *blob;
+
+       if (strcmp (assembly->image->guid, info->assembly_guid)) {
+               msg = g_strdup_printf ("doesn't match assembly");
+               usable = FALSE;
+       }
+
+       build_info = mono_get_runtime_build_info ();
+       if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
+               msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
+               usable = FALSE;
+       }
+       g_free (build_info);
+
+       full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
+
+       if (mono_aot_only && !full_aot) {
+               msg = g_strdup_printf ("not compiled with --aot=full");
+               usable = FALSE;
+       }
+       if (!mono_aot_only && full_aot) {
+               msg = g_strdup_printf ("compiled with --aot=full");
+               usable = FALSE;
+       }
+#ifdef TARGET_ARM
+       /* mono_arch_find_imt_method () requires this */
+       if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
+               msg = g_strdup_printf ("compiled against LLVM");
+               usable = FALSE;
+       }
+#endif
+       if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
+               msg = g_strdup_printf ("not compiled for debugging");
+               usable = FALSE;
+       }
+
+       blob = info->blob;
 
-       if (!strcmp (amodule->aot_name, info->dlpi_name)) {
-               for (j = 0; j < info->dlpi_phnum; j++) {
-                       if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
-                               amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
+       if (info->gc_name_index != -1) {
+               char *gc_name = (char*)&blob [info->gc_name_index];
+               const char *current_gc_name = mono_gc_get_gc_name ();
+
+               if (strcmp (current_gc_name, gc_name) != 0) {
+                       msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
+                       usable = FALSE;
                }
-               return 1;
-       } else {
-               return 0;
        }
+
+       *out_msg = msg;
+       return usable;
 }
-#endif
 
 static void
 load_aot_module (MonoAssembly *assembly, gpointer user_data)
@@ -954,15 +1030,13 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        MonoAotModule *amodule;
        MonoDl *sofile;
        gboolean usable = TRUE;
-       char *saved_guid = NULL;
-       char *aot_version = NULL;
-       char *runtime_version, *build_info;
-       char *opt_flags = NULL;
+       char *version_symbol = NULL;
+       char *msg = NULL;
        gpointer *globals;
-       gboolean full_aot = FALSE;
-       MonoAotFileInfo *file_info = NULL;
-       int i;
-       gpointer *got_addr;
+       MonoAotFileInfo *info = NULL;
+       int i, version;
+       guint8 *blob;
+       gboolean do_load_image = TRUE;
 
        if (mono_compile_aot)
                return;
@@ -1017,54 +1091,32 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                return;
        }
 
-       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);
-       find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
-       find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
-
-       if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
-               usable = FALSE;
-       }
-       else {
-               if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
-                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
-                       usable = FALSE;
-               }
-       }
-
-       build_info = mono_get_runtime_build_info ();
-       if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against runtime version '%s' while this runtime has version '%s'.\n", aot_name, runtime_version, build_info);
-               usable = FALSE;
-       }
-       g_free (build_info);
-
-       find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
-       g_assert (file_info);
+       find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
+       find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
 
-       full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
-
-       if (mono_aot_only && !full_aot) {
-               fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
-               exit (1);
-       }
-       if (!mono_aot_only && full_aot) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
-               usable = FALSE;
+       if (version_symbol) {
+               /* Old file format */
+               version = atoi (version_symbol);
+       } else {
+               g_assert (info);
+               version = info->version;
        }
 
-       if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
+       if (version != MONO_AOT_FILE_VERSION) {
+               msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
                usable = FALSE;
+       } else {
+               usable = check_usable (assembly, info, &msg);
        }
 
        if (!usable) {
                if (mono_aot_only) {
-                       fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
+                       fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
                        exit (1);
+               } else {
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable: %s.\n", aot_name, msg);
                }
+               g_free (msg);
                g_free (aot_name);
                if (sofile)
                        mono_dl_close (sofile);
@@ -1072,24 +1124,27 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                return;
        }
 
+       blob = info->blob;
+
        amodule = g_new0 (MonoAotModule, 1);
        amodule->aot_name = aot_name;
        amodule->assembly = assembly;
 
-       memcpy (&amodule->info, file_info, sizeof (*file_info));
+       memcpy (&amodule->info, info, sizeof (*info));
 
-       amodule->got = *got_addr;
+       amodule->got = amodule->info.got;
        amodule->got [0] = assembly->image;
        amodule->globals = globals;
        amodule->sofile = sofile;
        amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
+       amodule->blob = blob;
 
        /* Read image table */
        {
                guint32 table_len, i;
                char *table = NULL;
 
-               find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
+               table = info->image_table;
                g_assert (table);
 
                table_len = *(guint32*)table;
@@ -1125,28 +1180,30 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                }
        }
 
-       /* Read method and method_info tables */
-       find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
-       find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
-       find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
-       find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
-       find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
-       find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob);
-       find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
-       find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
-       find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
-       find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
-       find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
-       find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
-       find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
-       find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
-       find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
-       find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
-
+       amodule->code_offsets = info->code_offsets;
+       amodule->code = info->methods;
+#ifdef TARGET_ARM
+       /* Mask out thumb interop bit */
+       amodule->code = (void*)((mgreg_t)amodule->code & ~1);
+#endif
+       amodule->code_end = info->methods_end;
+       amodule->method_info_offsets = info->method_info_offsets;
+       amodule->ex_info_offsets = info->ex_info_offsets;
+       amodule->class_info_offsets = info->class_info_offsets;
+       amodule->class_name_table = info->class_name_table;
+       amodule->extra_method_table = info->extra_method_table;
+       amodule->extra_method_info_offsets = info->extra_method_info_offsets;
+       amodule->got_info_offsets = info->got_info_offsets;
+       amodule->unwind_info = info->unwind_info;
+       amodule->mem_end = info->mem_end;
        amodule->mem_begin = amodule->code;
-
-       find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
-       find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
+       amodule->plt = info->plt;
+       amodule->plt_end = info->plt_end;
+       amodule->mono_eh_frame = info->mono_eh_frame;
+       amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
+       amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
+       amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
+       amodule->thumb_end = info->thumb_end;
 
        if (make_unreadable) {
 #ifndef TARGET_WIN32
@@ -1178,11 +1235,6 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
 
        assembly->image->aot_module = amodule;
-#if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
-       /* Lookup the address of the .eh_frame_hdr () section if available */
-       dl_iterate_phdr (dl_callback, amodule);
-#endif 
 
        if (mono_aot_only) {
                if (mono_defaults.corlib) {
@@ -1195,6 +1247,15 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                }
        }
 
+       if (mono_gc_is_moving ()) {
+               MonoJumpInfo ji;
+
+               memset (&ji, 0, sizeof (ji));
+               ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
+
+               amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
+       }
+
        /*
         * 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.
@@ -1202,8 +1263,20 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
         * non-lazily, since we can't handle out-of-date errors later.
         * The cached class info also depends on the exact assemblies.
         */
-       for (i = 0; i < amodule->image_table_len; ++i)
-               load_image (amodule, i, FALSE);
+#if defined(__native_client__)
+       /* TODO: Don't 'load_image' on mscorlib due to a */
+       /* recursive loading problem.  This should be    */
+       /* removed if mscorlib is loaded from disk.      */
+       if (strncmp(assembly->aname.name, "mscorlib", 8)) {
+               do_load_image = TRUE;
+       } else {
+               do_load_image = FALSE;
+       }
+#endif
+       if (do_load_image) {
+               for (i = 0; i < amodule->image_table_len; ++i)
+                       load_image (amodule, i, FALSE);
+       }
 
        if (amodule->out_of_date) {
                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
@@ -1275,10 +1348,21 @@ mono_aot_init (void)
                use_aot_cache = TRUE;
 }
 
+void
+mono_aot_cleanup (void)
+{
+       if (aot_jit_icall_hash)
+               g_hash_table_destroy (aot_jit_icall_hash);
+       if (aot_modules)
+               g_hash_table_destroy (aot_modules);
+}
+
 static gboolean
 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
 {
        guint32 flags;
+       MethodRef ref;
+       gboolean res;
 
        info->vtable_size = decode_value (buf, &buf);
        if (info->vtable_size == -1)
@@ -1296,14 +1380,17 @@ decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guin
        info->is_generic_container = (flags >> 8) & 0x1;
 
        if (info->has_cctor) {
-               MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
-               if (!cctor_image)
+               res = decode_method_ref (module, &ref, buf, &buf);
+               if (!res)
                        return FALSE;
+               info->cctor_token = ref.token;
        }
        if (info->has_finalize) {
-               info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
-               if (!info->finalize_image)
+               res = decode_method_ref (module, &ref, buf, &buf);
+               if (!res)
                        return FALSE;
+               info->finalize_image = ref.image;
+               info->finalize_token = ref.token;
        }
 
        info->instance_size = decode_value (buf, &buf);
@@ -1321,52 +1408,51 @@ mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int sl
 {
        int i;
        MonoClass *klass = vtable->klass;
-       MonoAotModule *aot_module = klass->image->aot_module;
+       MonoAotModule *amodule = klass->image->aot_module;
        guint8 *info, *p;
        MonoCachedClassInfo class_info;
        gboolean err;
-       guint32 token;
-       MonoImage *image;
-       gboolean no_aot_trampoline;
+       MethodRef ref;
+       gboolean res;
 
-       if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
+       if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
                return NULL;
 
-       info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
+       info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
        p = info;
 
-       err = decode_cached_class_info (aot_module, &class_info, p, &p);
+       err = decode_cached_class_info (amodule, &class_info, p, &p);
        if (!err)
                return NULL;
 
        for (i = 0; i < slot; ++i)
-               decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
+               decode_method_ref (amodule, &ref, p, &p);
 
-       image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
-       if (!image)
+       res = decode_method_ref (amodule, &ref, p, &p);
+       if (!res)
                return NULL;
-       if (no_aot_trampoline)
+       if (ref.no_aot_trampoline)
                return NULL;
 
-       if (mono_metadata_token_index (token) == 0)
+       if (mono_metadata_token_index (ref.token) == 0)
                return NULL;
 
-       return mono_aot_get_method_from_token (domain, image, token);
+       return mono_aot_get_method_from_token (domain, ref.image, ref.token);
 }
 
 gboolean
 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
 {
-       MonoAotModule *aot_module = klass->image->aot_module;
+       MonoAotModule *amodule = klass->image->aot_module;
        guint8 *p;
        gboolean err;
 
-       if (klass->rank || !aot_module)
+       if (klass->rank || !amodule)
                return FALSE;
 
-       p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
+       p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
 
-       err = decode_cached_class_info (aot_module, res, p, &p);
+       err = decode_cached_class_info (amodule, res, p, &p);
        if (!err)
                return FALSE;
 
@@ -1386,7 +1472,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 = image->aot_module;
+       MonoAotModule *amodule = image->aot_module;
        guint16 *table, *entry;
        guint16 table_size;
        guint32 hash;
@@ -1397,7 +1483,7 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
        guint32 cols [MONO_TYPEDEF_SIZE];
        GHashTable *nspace_table;
 
-       if (!aot_module || !aot_module->class_name_table)
+       if (!amodule || !amodule->class_name_table)
                return FALSE;
 
        mono_aot_lock ();
@@ -1405,9 +1491,9 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
        *klass = NULL;
 
        /* First look in the cache */
-       if (!aot_module->name_cache)
-               aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
-       nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
+       if (!amodule->name_cache)
+               amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
+       nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
        if (nspace_table) {
                *klass = g_hash_table_lookup (nspace_table, name);
                if (*klass) {
@@ -1416,8 +1502,8 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
                }
        }
 
-       table_size = aot_module->class_name_table [0];
-       table = aot_module->class_name_table + 1;
+       table_size = amodule->class_name_table [0];
+       table = amodule->class_name_table + 1;
 
        if (name_space [0] == '\0')
                full_name = g_strdup_printf ("%s", name);
@@ -1457,10 +1543,10 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
                                /* Add to cache */
                                if (*klass) {
                                        mono_aot_lock ();
-                                       nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
+                                       nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
                                        if (!nspace_table) {
                                                nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
-                                               g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
+                                               g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
                                        }
                                        g_hash_table_insert (nspace_table, (char*)name2, *klass);
                                        mono_aot_unlock ();
@@ -1481,73 +1567,52 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
        return TRUE;
 }
 
-#define DW_EH_PE_omit  0xff
-#define DW_EH_PE_uleb128 0x01
-#define DW_EH_PE_udata2        0x02
-#define DW_EH_PE_udata4        0x03
-#define DW_EH_PE_udata8        0x04
-#define DW_EH_PE_sleb128 0x09
-#define DW_EH_PE_sdata2        0x0A
-#define DW_EH_PE_sdata4        0x0B
-#define DW_EH_PE_sdata8        0x0C
-
-#define DW_EH_PE_absptr        0x00
-#define DW_EH_PE_pcrel 0x10
-#define DW_EH_PE_datarel 0x30
-#define DW_EH_PE_omit  0xff
-
-typedef struct
-{
-       guint8 version;
-       guint8 eh_frame_ptr_enc;
-       guint8 fde_count_enc;
-       guint8 table_enc;
-       guint8 rest;
-} eh_frame_hdr;
-
 /*
- * decode_eh_frame:
+ * decode_mono_eh_frame:
  *
- *   Decode the exception handling information in the .eh_frame section of the AOT
- * file belong to CODE, and construct a MonoJitInfo structure from it.
+ *   Decode the EH information emitted by our modified LLVM compiler and construct a
+ * MonoJitInfo structure from it.
  * LOCKING: Acquires the domain lock.
  */
-static G_GNUC_UNUSED MonoJitInfo*
-decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain, 
-                                MonoMethod *method, guint8 *code, MonoJitInfo *orig_jinfo,
-                                int extra_size)
+static MonoJitInfo*
+decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain, 
+                                                  MonoMethod *method, guint8 *code, 
+                                                  MonoJitExceptionInfo *clauses, int num_clauses,
+                                                  int extra_size, GSList **nesting,
+                                                  int *this_reg, int *this_offset)
 {
-       eh_frame_hdr *hdr;
        guint8 *p;
-       guint8 *eh_frame, *unwind_info;
-       guint32 eh_frame_ptr;
-       int fde_count;
+       guint8 *fde, *cie, *code_start, *code_end;
+       int version, fde_count;
        gint32 *table;
-       int i, pos, left, right, offset, offset1, offset2;
-       guint32 unw_len, code_len;
+       int i, j, pos, left, right, offset, offset1, offset2, code_len;
        MonoJitExceptionInfo *ei;
-       guint32 ei_len;
+       guint32 fde_len, ei_len, nested_len, nindex;
        gpointer *type_info;
        MonoJitInfo *jinfo;
+       MonoLLVMFDEInfo info;
 
-       g_assert (amodule->eh_frame_hdr);
+       g_assert (amodule->mono_eh_frame);
 
-       // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
-       hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
-       g_assert (hdr->version == 1);
-       g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
-       g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
-       g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
+       p = amodule->mono_eh_frame;
+
+       /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
+
+       /* Header */
+       version = *p;
+       g_assert (version == 1);
+       p ++;
+       p = ALIGN_PTR_TO (p, 4);
 
-       p = &(hdr->rest);
-       eh_frame_ptr = *(guint32*)p;
-       p += 4;
        fde_count = *(guint32*)p;
        p += 4;
        table = (gint32*)p;
 
+       /* There is +1 entry in the table */
+       cie = p + ((fde_count + 1) * 8);
+
        /* Binary search in the table to find the entry for code */
-       offset = code - amodule->eh_frame_hdr;
+       offset = code - amodule->mono_eh_frame;
 
        left = 0;
        right = fde_count;
@@ -1569,33 +1634,58 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
                        break;
        }
 
-       g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
-       if (pos < fde_count)
-               g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
+       code_start = amodule->mono_eh_frame + table [(pos * 2)];
+       /* This won't overflow because there is +1 entry in the table */
+       code_end = amodule->mono_eh_frame + table [(pos * 2) + 2];
+       code_len = code_end - code_start;
+
+       g_assert (code >= code_start && code < code_end);
+
+       fde = amodule->mono_eh_frame + table [(pos * 2) + 1];   
+       /* This won't overflow because there is +1 entry in the table */
+       fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
+
+       mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
+       ei = info.ex_info;
+       ei_len = info.ex_info_len;
+       type_info = info.type_info;
+       *this_reg = info.this_reg;
+       *this_offset = info.this_offset;
 
-       eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
+       /* Count number of nested clauses */
+       nested_len = 0;
+       for (i = 0; i < ei_len; ++i) {
+               /* This might be unaligned */
+               gint32 cindex1 = read32 (type_info [i]);
+               GSList *l;
+
+               for (l = nesting [cindex1]; l; l = l->next) {
+                       gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
 
-       unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, &type_info);
+                       for (j = 0; j < ei_len; ++j) {
+                               gint32 cindex2 = read32 (type_info [j]);
+
+                               if (cindex2 == nesting_cindex)
+                                       nested_len ++;
+                       }
+               }
+       }
 
        /*
         * LLVM might represent one IL region with multiple regions, so have to
         * allocate a new JI.
         */
-       if (ei_len) {
-               jinfo = 
-                       mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * ei_len) + extra_size);
-       } else {
-               jinfo = orig_jinfo;
-       }
+       jinfo = 
+               mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
 
        jinfo->code_size = code_len;
-       jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
+       jinfo->used_regs = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
        jinfo->method = method;
        jinfo->code_start = code;
        jinfo->domain_neutral = 0;
        /* This signals that used_regs points to a normal cached unwind info */
        jinfo->from_aot = 0;
-       jinfo->num_clauses = ei_len;
+       jinfo->num_clauses = ei_len + nested_len;
 
        for (i = 0; i < ei_len; ++i) {
                /*
@@ -1603,19 +1693,50 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
                 * compiler, we have to combine that with the information produced by LLVM
                 */
                /* The type_info entries contain IL clause indexes */
-               int clause_index = *(gint32*)type_info [i];
+               int clause_index = read32 (type_info [i]);
                MonoJitExceptionInfo *jei = &jinfo->clauses [i];
-               MonoJitExceptionInfo *orig_jei = &orig_jinfo->clauses [clause_index];
+               MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
 
-               g_assert (clause_index < orig_jinfo->num_clauses);
+               g_assert (clause_index < num_clauses);
                jei->flags = orig_jei->flags;
                jei->data.catch_class = orig_jei->data.catch_class;
 
                jei->try_start = ei [i].try_start;
                jei->try_end = ei [i].try_end;
                jei->handler_start = ei [i].handler_start;
+
+               /* Make sure we transition to thumb when a handler starts */
+               if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
+                       jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
        }
 
+       /* See exception_cb () in mini-llvm.c as to why this is needed */
+       nindex = ei_len;
+       for (i = 0; i < ei_len; ++i) {
+               gint32 cindex1 = read32 (type_info [i]);
+               GSList *l;
+
+               for (l = nesting [cindex1]; l; l = l->next) {
+                       gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
+
+                       for (j = 0; j < ei_len; ++j) {
+                               gint32 cindex2 = read32 (type_info [j]);
+
+                               if (cindex2 == nesting_cindex) {
+                                       /* 
+                                        * The try interval comes from the nested clause, everything else from the
+                                        * nesting clause.
+                                        */
+                                       memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
+                                       jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
+                                       jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
+                                       nindex ++;
+                               }
+                       }
+               }
+       }
+       g_assert (nindex == ei_len + nested_len);
+
        return jinfo;
 }
 
@@ -1627,13 +1748,13 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                                                         MonoMethod *method, guint8* ex_info, guint8 *addr,
                                                         guint8 *code, guint32 code_len)
 {
-       int i, buf_len;
+       int i, buf_len, num_clauses;
        MonoJitInfo *jinfo;
        guint used_int_regs, flags;
        gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
-       gboolean from_llvm;
+       gboolean from_llvm, has_gc_map;
        guint8 *p;
-       int generic_info_size, try_holes_info_size, num_holes;
+       int generic_info_size, try_holes_info_size, num_holes, this_reg = 0, this_offset = 0;
 
        /* Load the method info from the AOT file */
 
@@ -1645,6 +1766,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
        has_seq_points = (flags & 8) != 0;
        from_llvm = (flags & 16) != 0;
        has_try_block_holes = (flags & 32) != 0;
+       has_gc_map = (flags & 64) != 0;
 
        if (has_dwarf_unwind_info) {
                guint32 offset;
@@ -1667,25 +1789,55 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                num_holes = try_holes_info_size = 0;
        }
        /* Exception table */
-       if (has_clauses) {
-               int num_clauses = decode_value (p, &p);
+       if (has_clauses)
+               num_clauses = decode_value (p, &p);
+       else
+               num_clauses = 0;
 
-               jinfo = 
-                       mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
-               jinfo->num_clauses = num_clauses;
+       if (from_llvm) {
+               MonoJitExceptionInfo *clauses;
+               GSList **nesting;
+
+               /*
+                * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
+                * section.
+                */
+               clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
+               nesting = g_new0 (GSList*, num_clauses);
 
                for (i = 0; i < num_clauses; ++i) {
-                       MonoJitExceptionInfo *ei = &jinfo->clauses [i];
+                       MonoJitExceptionInfo *ei = &clauses [i];
 
                        ei->flags = decode_value (p, &p);
 
-                       if (from_llvm) {
-                               if (decode_value (p, &p))
-                                       ei->data.catch_class = decode_klass_ref (amodule, p, &p);
+                       if (decode_value (p, &p))
+                               ei->data.catch_class = decode_klass_ref (amodule, p, &p);
 
-                               /* The rest of the info is in the DWARF EH section */
-                               continue;
+                       /* Read the list of nesting clauses */
+                       while (TRUE) {
+                               int nesting_index = decode_value (p, &p);
+                               if (nesting_index == -1)
+                                       break;
+                               nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
                        }
+               }
+
+               jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, generic_info_size + try_holes_info_size, nesting, &this_reg, &this_offset);
+               jinfo->from_llvm = 1;
+
+               g_free (clauses);
+               for (i = 0; i < num_clauses; ++i)
+                       g_slist_free (nesting [i]);
+               g_free (nesting);
+       } else {
+               jinfo = 
+                       mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
+               jinfo->num_clauses = num_clauses;
+
+               for (i = 0; i < jinfo->num_clauses; ++i) {
+                       MonoJitExceptionInfo *ei = &jinfo->clauses [i];
+
+                       ei->flags = decode_value (p, &p);
 
                        ei->exvar_offset = decode_value (p, &p);
 
@@ -1700,17 +1852,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                        ei->try_end = code + decode_value (p, &p);
                        ei->handler_start = code + decode_value (p, &p);
                }
-       }
-       else {
-               jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size + try_holes_info_size);
-       }
 
-       if (from_llvm) {
-               /* LLVM compiled method */
-               /* The info is in the .eh_frame section */
-               jinfo = decode_eh_frame (amodule, domain, method, code, jinfo, generic_info_size);
-               jinfo->from_llvm = 1;
-       } else {
                jinfo->code_size = code_len;
                jinfo->used_regs = used_int_regs;
                jinfo->method = method;
@@ -1727,9 +1869,15 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                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);
+               if (from_llvm) {
+                       gi->has_this = this_reg != -1;
+                       gi->this_reg = this_reg;
+                       gi->this_offset = this_offset;
+               } else {
+                       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);
@@ -1788,6 +1936,16 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
        /* Load debug info */
        buf_len = decode_value (p, &p);
        mono_debug_add_aot_method (domain, method, code, p, buf_len);
+       p += buf_len;
+
+       if (has_gc_map) {
+               int map_size = decode_value (p, &p);
+               /* The GC map requires 4 bytes of alignment */
+               while ((guint64)(gsize)p % 4)
+                       p ++;           
+               jinfo->gc_info = p;
+               p += map_size;
+       }
 
        if (amodule != jinfo->method->klass->image->aot_module) {
                mono_aot_lock ();
@@ -1796,7 +1954,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                g_hash_table_insert (ji_to_amodule, jinfo, amodule);
                mono_aot_unlock ();             
        }
-       
+
        return jinfo;
 }
 
@@ -1916,7 +2074,6 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
 
        /* Compute a sorted table mapping code offsets to method indexes. */
        if (!amodule->sorted_code_offsets) {
-
                code_offsets = g_new0 (gint32, nmethods * 2);
                offsets_len = 0;
                for (i = 0; i < nmethods; ++i) {
@@ -2052,23 +2209,22 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_METHOD_JUMP:
        case MONO_PATCH_INFO_ICALL_ADDR:
        case MONO_PATCH_INFO_METHOD_RGCTX: {
-               guint32 token;
-               MonoMethod *method;
-               gboolean no_aot_trampoline;
+               MethodRef ref;
+               gboolean res;
 
-               image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
-               if (!image)
+               res = decode_method_ref (aot_module, &ref, p, &p);
+               if (!res)
                        goto cleanup;
 
-               if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
-                       ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
+               if (!ref.method && !mono_aot_only && !ref.no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (ref.token) == MONO_TABLE_METHOD)) {
+                       ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
                        ji->type = MONO_PATCH_INFO_ABS;
                }
                else {
-                       if (method)
-                               ji->data.method = method;
+                       if (ref.method)
+                               ji->data.method = ref.method;
                        else
-                               ji->data.method = mono_get_method (image, token, NULL);
+                               ji->data.method = mono_get_method (ref.image, ref.token, NULL);
                        g_assert (ji->data.method);
                        mono_class_init (ji->data.method->klass);
                }
@@ -2179,6 +2335,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
        case MONO_PATCH_INFO_MONITOR_ENTER:
        case MONO_PATCH_INFO_MONITOR_EXIT:
+       case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
                break;
        case MONO_PATCH_INFO_RGCTX_FETCH: {
                gboolean res;
@@ -2337,6 +2494,12 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
 
        info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
 
+       if (amodule->thumb_end && code < amodule->thumb_end) {
+               /* Convert this into a thumb address */
+               g_assert ((amodule->code_offsets [method_index] & 0x1) == 0);
+               code = &amodule->code [amodule->code_offsets [method_index] + 1];
+       }
+
        mono_aot_lock ();
        if (!amodule->methods_loaded)
                amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
@@ -2516,15 +2679,16 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const
                                index = value;
                                break;
                        }
-               } else if (can_method_ref_match_method (amodule, p, method)) {
+               } else {
+                       guint8 *orig_p = p;
+
                        mono_aot_lock ();
                        if (!amodule->method_ref_to_method)
                                amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
                        m = g_hash_table_lookup (amodule->method_ref_to_method, p);
                        mono_aot_unlock ();
                        if (!m) {
-                               guint8 *orig_p = p;
-                               m = decode_resolve_method_ref (amodule, p, &p);
+                               m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
                                if (m) {
                                        mono_aot_lock ();
                                        g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
@@ -2549,11 +2713,10 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const
                        }
 
                        /* Methods decoded needlessly */
-                       /*
-                       if (m)
-                               printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
-                       */
-                       n_extra_decodes ++;
+                       if (m) {
+                               //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
+                               n_extra_decodes ++;
+                       }
                }
 
                if (next != 0)
@@ -2734,6 +2897,38 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
                                return code;
                }
 
+               /* Same for CompareExchange<T> */
+               if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib && !strcmp (method->klass->name_space, "System.Threading") && !strcmp (method->klass->name, "Interlocked") && !strcmp (method->name, "CompareExchange")) {
+                       MonoMethod *m;
+                       MonoGenericContext ctx;
+                       MonoType *args [16];
+                       gpointer iter = NULL;
+
+                       while ((m = mono_class_get_methods (method->klass, &iter))) {
+                               if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, "CompareExchange"))
+                                       break;
+                       }
+                       g_assert (m);
+
+                       memset (&ctx, 0, sizeof (ctx));
+                       args [0] = &mono_defaults.object_class->byval_arg;
+                       ctx.method_inst = mono_metadata_get_generic_inst (1, args);
+
+                       m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
+
+                       /* Avoid recursion */
+                       if (method == m)
+                               return NULL;
+
+                       /* 
+                        * Get the code for the <object> instantiation which should be emitted into
+                        * the mscorlib aot image by the AOT compiler.
+                        */
+                       code = mono_aot_get_method (domain, m);
+                       if (code)
+                               return code;
+               }
+
                if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
                        /* Partial sharing */
                        method_index = find_extra_method (mini_get_shared_method (method), &amodule);
@@ -2985,23 +3180,28 @@ init_plt (MonoAotModule *amodule)
 guint8*
 mono_aot_get_plt_entry (guint8 *code)
 {
-       MonoAotModule *aot_module = find_aot_module (code);
+       MonoAotModule *amodule = find_aot_module (code);
+       guint8 *target = NULL;
 
-       if (!aot_module)
+       if (!amodule)
                return NULL;
 
-#ifdef MONO_ARCH_AOT_SUPPORTED
-       {
-               guint8 *target = mono_arch_get_call_target (code);
-
-               if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
-                       return target;
+#ifdef TARGET_ARM
+       if (amodule->thumb_end && code < amodule->thumb_end) {
+               return mono_arm_get_thumb_plt_entry (code);
        }
+#endif
+
+#ifdef MONO_ARCH_AOT_SUPPORTED
+       target = mono_arch_get_call_target (code);
 #else
        g_assert_not_reached ();
 #endif
 
-       return NULL;
+       if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
+               return target;
+       else
+               return NULL;
 }
 
 /*
@@ -3041,8 +3241,6 @@ mono_create_ftnptr_malloc (guint8 *code)
 #endif
 }
 
-static GHashTable *aot_jit_icall_hash;
-
 /*
  * mono_aot_register_jit_icall:
  *
@@ -3149,24 +3347,8 @@ load_function (MonoAotModule *amodule, const char *name)
                                        target = mono_create_ftnptr_malloc (target);
                                } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
                                        target = mono_thread_get_and_clear_pending_exception;
-                               } else if (strstr (ji->data.name, "generic_trampoline_monitor_enter")) {
-                                       char *symbol;
-
-                                       symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_MONITOR_ENTER);
-                                       target = mono_aot_get_trampoline (symbol);
-                                       g_free (symbol);
-                               } else if (strstr (ji->data.name, "generic_trampoline_monitor_exit")) {
-                                       char *symbol;
-
-                                       symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_MONITOR_EXIT);
-                                       target = mono_aot_get_trampoline (symbol);
-                                       g_free (symbol);
-                               } else if (strstr (ji->data.name, "generic_trampoline_generic_class_init")) {
-                                       char *symbol;
-
-                                       symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
-                                       target = mono_aot_get_trampoline (symbol);
-                                       g_free (symbol);
+                               } else if (strstr (ji->data.name, "generic_trampoline_")) {
+                                       target = mono_aot_get_trampoline (ji->data.name);
                                } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
                                        /* Registered by mono_arch_init () */
                                        target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
@@ -3278,7 +3460,7 @@ mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampo
        if (!generic_trampolines [tramp_type]) {
                char *symbol;
 
-               symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
+               symbol = mono_get_generic_trampoline_name (tramp_type);
                generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
                g_free (symbol);
        }
@@ -3345,7 +3527,7 @@ mono_aot_get_lazy_fetch_trampoline (guint32 slot)
        char *symbol;
        gpointer code;
 
-       symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
+       symbol = mono_get_rgctx_fetch_trampoline_name (slot);
        code = load_function (mono_defaults.corlib->aot_module, symbol);
        g_free (symbol);
        /* The caller expects an ftnptr */