Special case Interlocked.CompareExchange<T> in the aot runtime.
[mono.git] / mono / mini / aot-runtime.c
index 530ac947ed97a185fd47659908a1dae87ab1f8e2..4004e1e8dcd6aac20065c198598e9be827865ac3 100644 (file)
@@ -51,7 +51,7 @@
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/monitor.h>
 #include <mono/metadata/threads-types.h>
-#include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-mmap.h>
 #include "mono/utils/mono-compiler.h"
 #include <mono/utils/mono-counters.h>
@@ -65,6 +65,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
@@ -74,8 +76,6 @@
 
 typedef struct MonoAotModule {
        char *aot_name;
-       /* Optimization flags used to compile the module */
-       guint32 opts;
        /* Pointer to the Global Offset Table */
        gpointer *got;
        GHashTable *name_cache;
@@ -97,26 +97,23 @@ typedef struct MonoAotModule {
        guint8 *code_end;
        guint8 *plt;
        guint8 *plt_end;
+       guint8 *blob;
        gint32 *code_offsets;
        /* This contains <offset, index> pairs sorted by offset */
        /* This is needed because LLVM emitted methods can be in any order */
        gint32 *sorted_code_offsets;
-       guint8 *method_info;
+       gint32 sorted_code_offsets_len;
        guint32 *method_info_offsets;
-       guint8 *got_info;
        guint32 *got_info_offsets;
-       guint8 *ex_info;
        guint32 *ex_info_offsets;
-       guint8 *class_info;
        guint32 *class_info_offsets;
        guint32 *methods_loaded;
        guint16 *class_name_table;
        guint32 *extra_method_table;
        guint32 *extra_method_info_offsets;
-       guint8 *extra_method_info;
        guint8 *unwind_info;
 
-       /* Points to the the GNU .eh_frame_hdr section, if it exists */
+       /* Points to the GNU .eh_frame_hdr section, if it exists */
        guint8 *eh_frame_hdr;
 
        /* Points to the trampolines */
@@ -164,11 +161,14 @@ static gint32 mono_last_aot_method = -1;
 
 static gboolean make_unreadable = FALSE;
 static guint32 name_table_accesses = 0;
+static guint32 n_pagefaults = 0;
 
 /* Used to speed-up find_aot_module () */
 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);
 
@@ -176,37 +176,48 @@ init_plt (MonoAotModule *info);
 /*                 AOT RUNTIME                       */
 /*****************************************************/
 
+/*
+ * load_image:
+ *
+ *   Load one of the images referenced by AMODULE. Returns NULL if the image is not
+ * found, and sets the loader error if SET_ERROR is TRUE.
+ */
 static MonoImage *
-load_image (MonoAotModule *module, int index)
+load_image (MonoAotModule *amodule, int index, gboolean set_error)
 {
        MonoAssembly *assembly;
        MonoImageOpenStatus status;
 
-       g_assert (index < module->image_table_len);
+       g_assert (index < amodule->image_table_len);
 
-       if (module->image_table [index])
-               return module->image_table [index];
-       if (module->out_of_date)
+       if (amodule->image_table [index])
+               return amodule->image_table [index];
+       if (amodule->out_of_date)
                return NULL;
 
-       assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
+       assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
        if (!assembly) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
-               module->out_of_date = TRUE;
+               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;
+
+               if (set_error) {
+                       char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
+                       mono_loader_set_error_assembly_load (full_name, FALSE);
+                       g_free (full_name);
+               }
                return NULL;
        }
 
-       if (strcmp (assembly->image->guid, module->image_guids [index])) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name);
-               module->out_of_date = TRUE;
+       if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", amodule->aot_name, amodule->image_names [index].name);
+               amodule->out_of_date = TRUE;
                return NULL;
        }
 
-       module->image_table [index] = assembly->image;
+       amodule->image_table [index] = assembly->image;
        return assembly->image;
 }
 
-
 static inline gint32
 decode_value (guint8 *ptr, guint8 **rptr)
 {
@@ -237,8 +248,50 @@ decode_value (guint8 *ptr, guint8 **rptr)
        return len;
 }
 
+/*
+ * mono_aot_get_method:
+ *
+ *   Decode an offset table emitted by emit_offset_table (), returning the INDEXth
+ * entry.
+ */
+static guint32
+mono_aot_get_offset (guint32 *table, int index)
+{
+       int i, group, ngroups, index_entry_size;
+       int start_offset, offset, noffsets, group_size;
+       guint8 *data_start, *p;
+       guint32 *index32 = NULL;
+       guint16 *index16 = NULL;
+       
+       noffsets = table [0];
+       group_size = table [1];
+       ngroups = table [2];
+       index_entry_size = table [3];
+       group = index / group_size;
+
+       if (index_entry_size == 2) {
+               index16 = (guint16*)&table [4];
+               data_start = (guint8*)&index16 [ngroups];
+               p = data_start + index16 [group];
+       } else {
+               index32 = (guint32*)&table [4];
+               data_start = (guint8*)&index32 [ngroups];
+               p = data_start + index32 [group];
+       }
+
+       /* offset will contain the value of offsets [group * group_size] */
+       offset = start_offset = decode_value (p, &p);
+       for (i = group * group_size + 1; i <= index; ++i) {
+               offset += decode_value (p, &p);
+       }
+
+       //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
+
+       return offset;
+}
+
 static MonoMethod*
-decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
+decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
 
 static MonoClass*
 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
@@ -308,7 +361,7 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                return NULL;
        }
        if (mono_metadata_token_table (token) == 0) {
-               image = load_image (module, decode_value (p, &p));
+               image = load_image (module, decode_value (p, &p), TRUE);
                if (!image)
                        return NULL;
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
@@ -322,6 +375,8 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                                MonoType *type;
 
                                gclass = decode_klass_ref (module, p, &p);
+                               if (!gclass)
+                                       return NULL;
                                g_assert (gclass->generic_container);
 
                                memset (&ctx, 0, sizeof (ctx));
@@ -341,7 +396,7 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                                if (is_method) {
                                        MonoMethod *method_def;
                                        g_assert (type == MONO_TYPE_MVAR);
-                                       method_def = decode_method_ref_2 (module, p, &p);
+                                       method_def = decode_resolve_method_ref (module, p, &p);
                                        if (!method_def)
                                                return NULL;
 
@@ -372,14 +427,14 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                                g_assert_not_reached ();
                        }
                } else {
-                       image = load_image (module, decode_value (p, &p));
+                       image = load_image (module, decode_value (p, &p), TRUE);
                        if (!image)
                                return NULL;
                        klass = mono_class_get (image, token);
                }
        } else if (token == MONO_TOKEN_TYPE_DEF) {
                /* Array */
-               image = load_image (module, decode_value (p, &p));
+               image = load_image (module, decode_value (p, &p), TRUE);
                if (!image)
                        return NULL;
                rank = decode_value (p, &p);
@@ -389,7 +444,6 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                g_assert_not_reached ();
        }
        g_assert (klass);
-       mono_class_init (klass);
 
        *endbuf = p;
        return klass;
@@ -412,95 +466,71 @@ 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_method_ref_2 on P could return the same method as 
- * METHOD. This is an optimization to avoid calling decode_method_ref_2 () 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;
 
                switch (wrapper_type) {
                case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
-                       MonoMethod *m = decode_method_ref_2 (module, p, &p);
+                       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:
@@ -509,40 +539,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:
+                       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_method_ref_2 (module, p, &p);
+                       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: {
@@ -559,31 +592,46 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        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);
+                       ref->method = mono_monitor_get_fast_path (orig_method);
                        break;
                }
                case MONO_WRAPPER_RUNTIME_INVOKE: {
                        /* Direct wrapper */
-                       MonoMethod *m = decode_method_ref_2 (module, p, &p);
+                       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: {
+                       int subtype = decode_value (p, &p);
+
+                       if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
+                               int rank = decode_value (p, &p);
+                               int elem_size = decode_value (p, &p);
+
+                               ref->method = mono_marshal_get_array_address (rank, elem_size);
+                       } else {
+                               g_assert_not_reached ();
+                       }
                        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);
+               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;
@@ -594,18 +642,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);
+               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));
 
@@ -613,92 +664,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);
+               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_method_ref_2:
+ * decode_resolve_method_ref_with_target:
  *
  *   Similar to decode_method_ref, but resolve and return the method itself.
  */
 static MonoMethod*
-decode_method_ref_2 (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);
 }
 
-G_GNUC_UNUSED
-static void
-make_writable (guint8* addr, guint32 len)
+static MonoMethod*
+decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
 {
-       guint8 *page_start;
-       int pages, err;
-
-       if (mono_aot_only)
-               g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
-
-       page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
-       pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
-
-       err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
-       g_assert (err == 0);
+       return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
 }
 
 static void
@@ -831,21 +879,55 @@ static void
 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
 {
        if (globals) {
-               int i = 0;
+               int global_index;
+               guint16 *table, *entry;
+               guint16 table_size;
+               guint32 hash;           
+
+               /* The first entry points to the hash */
+               table = globals [0];
+               globals ++;
+
+               table_size = table [0];
+               table ++;
+
+               hash = mono_metadata_str_hash (name) % table_size;
 
-               *value = NULL;
-               for (i = 0; globals [i]; i+= 2) {
-                       if (strcmp (globals [i], name) == 0) {
-                               *value = globals [i + 1];
+               entry = &table [hash * 2];
+
+               /* Search the hash for the index into the globals table */
+               global_index = -1;
+               while (entry [0] != 0) {
+                       guint32 index = entry [0] - 1;
+                       guint32 next = entry [1];
+
+                       //printf ("X: %s %s\n", (char*)globals [index * 2], name);
+
+                       if (!strcmp (globals [index * 2], name)) {
+                               global_index = index;
+                               break;
+                       }
+
+                       if (next != 0) {
+                               entry = &table [next * 2];
+                       } else {
                                break;
                        }
                }
+
+               if (global_index != -1)
+                       *value = globals [global_index * 2 + 1];
+               else
+                       *value = NULL;
        } else {
-               mono_dl_symbol (module, name, value);
+               char *err = mono_dl_symbol (module, name, value);
+
+               if (err)
+                       g_free (err);
        }
 }
 
-#ifdef HAVE_DL_ITERATE_PHDR
+#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)
 {
@@ -880,6 +962,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        MonoAotFileInfo *file_info = NULL;
        int i;
        gpointer *got_addr;
+       guint8 *blob;
 
        if (mono_compile_aot)
                return;
@@ -961,14 +1044,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
        g_assert (file_info);
 
-       {
-               char *full_aot_str;
-
-               find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
-
-               if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
-                       full_aot = TRUE;
-       }
+       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);
@@ -979,15 +1055,36 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                usable = FALSE;
        }
 
+       /* This is no longer needed, LLVM and non-LLVM runtimes should be compatible.
        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);
                usable = FALSE;
        }
+       */
+
+       if (mini_get_debug_options ()->mdb_optimizations && !(file_info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is not compiled for debugging.\n", aot_name);
+               usable = FALSE;
+       }
+
+       find_symbol (sofile, globals, "blob", (gpointer*)&blob);
+
+       if (usable && ((MonoAotFileInfo*)file_info)->gc_name_index != -1) {
+               char *gc_name = (char*)&blob [((MonoAotFileInfo*)file_info)->gc_name_index];
+               const char *current_gc_name = mono_gc_get_gc_name ();
+
+               if (strcmp (current_gc_name, gc_name) != 0) {
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against GC %s, while the current runtime uses GC %s.\n", aot_name, gc_name, current_gc_name);
+                       usable = FALSE;
+               }
+       }
 
        if (!usable) {
                if (mono_aot_only) {
                        fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
                        exit (1);
+               } else {
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable.\n", aot_name);
                }
                g_free (aot_name);
                if (sofile)
@@ -1007,8 +1104,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        amodule->globals = globals;
        amodule->sofile = sofile;
        amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
-       sscanf (opt_flags, "%d", &amodule->opts);               
+       amodule->blob = blob;
 
        /* Read image table */
        {
@@ -1052,20 +1148,15 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        }
 
        /* Read method and method_info tables */
-       find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
+       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, "method_info", (gpointer*)&amodule->method_info);
        find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
-       find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
-       find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
        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", (gpointer *)&amodule->extra_method_info);
        find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
-       find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
        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]));
@@ -1081,17 +1172,19 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        if (make_unreadable) {
 #ifndef TARGET_WIN32
                guint8 *addr;
-               guint8 *page_start;
-               int pages, err, len;
+               guint8 *page_start, *page_end;
+               int err, len;
 
                addr = amodule->mem_begin;
                len = amodule->mem_end - amodule->mem_begin;
 
                /* Round down in both directions to avoid modifying data which is not ours */
                page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
-               pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
-               err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
-               g_assert (err == 0);
+               page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
+               if (page_end > page_start) {
+                       err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
+                       g_assert (err == 0);
+               }
 #endif
        }
 
@@ -1107,7 +1200,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
 
        assembly->image->aot_module = amodule;
  
-#ifdef HAVE_DL_ITERATE_PHDR
+#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 
@@ -1123,14 +1216,24 @@ 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.
         * MS calls this 'hard binding'. This means we have to load all referenced assemblies
         * 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);
+               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);
@@ -1202,10 +1305,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)
@@ -1220,16 +1334,20 @@ decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guin
        info->has_references = (flags >> 5) & 0x1;
        info->has_static_refs = (flags >> 6) & 0x1;
        info->no_special_static_fields = (flags >> 7) & 0x1;
+       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);
@@ -1247,52 +1365,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->class_info [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->class_info [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;
 
@@ -1312,7 +1429,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;
@@ -1323,7 +1440,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 ();
@@ -1331,9 +1448,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) {
@@ -1342,8 +1459,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);
@@ -1355,7 +1472,7 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
                        full_name = g_strdup_printf ("%s.%s", name_space, name);
                }
        }
-       hash = mono_aot_str_hash (full_name) % table_size;
+       hash = mono_metadata_str_hash (full_name) % table_size;
        if (full_name != full_name_buf)
                g_free (full_name);
 
@@ -1383,10 +1500,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 ();
@@ -1438,9 +1555,12 @@ typedef struct
  * file belong to CODE, and construct a MonoJitInfo structure from it.
  * LOCKING: Acquires the domain lock.
  */
-static void
+static G_GNUC_UNUSED MonoJitInfo*
 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain, 
-                                MonoMethod *method, guint8 *code, MonoJitInfo *jinfo)
+                                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;
@@ -1448,8 +1568,12 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
        guint32 eh_frame_ptr;
        int fde_count;
        gint32 *table;
-       int pos, left, right, offset, offset1, offset2;
+       int i, j, pos, left, right, offset, offset1, offset2;
        guint32 unw_len, code_len;
+       MonoJitExceptionInfo *ei;
+       guint32 ei_len, nested_len, nindex;
+       gpointer *type_info;
+       MonoJitInfo *jinfo;
 
        g_assert (amodule->eh_frame_hdr);
 
@@ -1491,12 +1615,37 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
        }
 
        g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
-       if (pos < fde_count)
+       if (pos + 1 < fde_count)
                g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
 
        eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
 
-       unwind_info = mono_unwind_get_ops_from_fde (eh_frame, &unw_len, &code_len);
+       unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, &type_info, this_reg, this_offset);
+
+       /* Count number of nested clauses */
+       nested_len = 0;
+       for (i = 0; i < ei_len; ++i) {
+               gint32 cindex1 = *(gint32*)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 = *(gint32*)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.
+        */
+       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);
@@ -1505,6 +1654,55 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
        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 + nested_len;
+
+       for (i = 0; i < ei_len; ++i) {
+               /*
+                * orig_jinfo contains the original IL exception info saved by the AOT
+                * 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];
+               MonoJitExceptionInfo *jei = &jinfo->clauses [i];
+               MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
+
+               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;
+       }
+
+       /* 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 = *(gint32*)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 = *(gint32*)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;
 }
 
 /*
@@ -1513,24 +1711,27 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
 static MonoJitInfo*
 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, 
                                                         MonoMethod *method, guint8* ex_info, guint8 *addr,
-                                                        guint8 *code)
+                                                        guint8 *code, guint32 code_len)
 {
-       int i, buf_len;
+       int i, buf_len, num_clauses;
        MonoJitInfo *jinfo;
-       guint code_len, used_int_regs, flags;
-       gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
+       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;
        guint8 *p;
-       int generic_info_size;
+       int generic_info_size, try_holes_info_size, num_holes, this_reg, this_offset;
 
        /* Load the method info from the AOT file */
 
        p = ex_info;
-       code_len = decode_value (p, &p);
        flags = decode_value (p, &p);
        has_generic_jit_info = (flags & 1) != 0;
        has_dwarf_unwind_info = (flags & 2) != 0;
        has_clauses = (flags & 4) != 0;
        has_seq_points = (flags & 8) != 0;
+       from_llvm = (flags & 16) != 0;
+       has_try_block_holes = (flags & 32) != 0;
+
        if (has_dwarf_unwind_info) {
                guint32 offset;
 
@@ -1545,21 +1746,66 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
        else
                generic_info_size = 0;
 
+       if (has_try_block_holes) {
+               num_holes = decode_value (p, &p);
+               try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
+       } else {
+               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;
+
+       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 = &clauses [i];
+
+                       ei->flags = decode_value (p, &p);
+
+                       if (decode_value (p, &p))
+                               ei->data.catch_class = decode_klass_ref (amodule, p, &p);
 
+                       /* 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_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);
+                       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 < num_clauses; ++i) {
+               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);
 
-                       if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
+                       if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
                                ei->data.filter = code + decode_value (p, &p);
                        else {
                                if (decode_value (p, &p))
@@ -1570,16 +1816,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);
-       }
 
-       if (code_len == 0) {
-               /* LLVM compiled method */
-               /* The info is in the .eh_frame section */
-               decode_eh_frame (amodule, domain, method, code, jinfo);
-       } else {
                jinfo->code_size = code_len;
                jinfo->used_regs = used_int_regs;
                jinfo->method = method;
@@ -1596,30 +1833,60 @@ 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);
 
-               jinfo->method = decode_method_ref_2 (amodule, p, &p);
+               jinfo->method = decode_resolve_method_ref (amodule, p, &p);
+       }
+
+       if (has_try_block_holes) {
+               MonoTryBlockHoleTableJitInfo *table;
+
+               jinfo->has_try_block_holes = 1;
+
+               table = mono_jit_info_get_try_block_hole_table_info (jinfo);
+               g_assert (table);
+
+               table->num_holes = (guint16)num_holes;
+               for (i = 0; i < num_holes; ++i) {
+                       MonoTryBlockHoleJitInfo *hole = &table->holes [i];
+                       hole->clause = decode_value (p, &p);
+                       hole->length = decode_value (p, &p);
+                       hole->offset = decode_value (p, &p);
+               }
        }
 
        if (has_seq_points) {
-               GPtrArray *seq_points;
-               int il_offset, native_offset, last_il_offset, last_native_offset;
+               MonoSeqPointInfo *seq_points;
+               int il_offset, native_offset, last_il_offset, last_native_offset, j;
 
                int len = decode_value (p, &p);
 
-               seq_points = g_ptr_array_new ();
+               seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
+               seq_points->len = len;
                last_il_offset = last_native_offset = 0;
-               for (i = 0; i < len; i += 2) {
+               for (i = 0; i < len; ++i) {
+                       SeqPoint *sp = &seq_points->seq_points [i];
                        il_offset = last_il_offset + decode_value (p, &p);
                        native_offset = last_native_offset + decode_value (p, &p);
 
-                       g_ptr_array_add (seq_points, GINT_TO_POINTER (il_offset));
-                       g_ptr_array_add (seq_points, GINT_TO_POINTER (native_offset));
+                       sp->il_offset = il_offset;
+                       sp->native_offset = native_offset;
+                       
+                       sp->next_len = decode_value (p, &p);
+                       sp->next = g_new (int, sp->next_len);
+                       for (j = 0; j < sp->next_len; ++j)
+                               sp->next [j] = decode_value (p, &p);
 
                        last_il_offset = il_offset;
                        last_native_offset = native_offset;
@@ -1667,6 +1934,7 @@ mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
                amodule = g_hash_table_lookup (ji_to_amodule, ji);
                g_assert (amodule);
                g_assert (code >= amodule->code && code <= amodule->code_end);
+               mono_aot_unlock ();
        }
 
        p = amodule->unwind_info + ji->used_regs;
@@ -1674,16 +1942,70 @@ mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
        return p;
 }
 
-static int
+static G_GNUC_UNUSED int
 compare_ints (const void *a, const void *b)
 {
        return *(gint32*)a - *(gint32*)b;
 }
 
+static void
+msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
+{
+       int mid = (lo + hi) / 2;
+       int i, t_lo, t_hi;
+
+       if (lo >= hi)
+               return;
+
+       if (hi - lo < 32) {
+               for (i = lo; i < hi; ++i)
+                       if (array [(i * 2)] > array [(i * 2) + 2])
+                               break;
+               if (i == hi)
+                       /* Already sorted */
+                       return;
+       }
+
+       msort_code_offsets_internal (array, lo, mid, scratch);
+       msort_code_offsets_internal (array, mid + 1, hi, scratch);
+
+       if (array [mid * 2] < array [(mid + 1) * 2])
+               return;
+
+       /* Merge */
+       t_lo = lo;
+       t_hi = mid + 1;
+       for (i = lo; i <= hi; i ++) {
+               if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
+                       scratch [(i * 2)] = array [t_lo * 2];
+                       scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
+                       t_lo ++;
+               } else {
+                       scratch [(i * 2)] = array [t_hi * 2];
+                       scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
+                       t_hi ++;
+               }
+       }
+       for (i = lo; i <= hi; ++i) {
+               array [(i * 2)] = scratch [i * 2];
+               array [(i * 2) + 1] = scratch [(i * 2) + 1];
+       }
+}
+
+static void
+msort_code_offsets (gint32 *array, int len)
+{
+       gint32 *scratch;
+
+       scratch = g_new (gint32, len * 2);
+       msort_code_offsets_internal (array, 0, len - 1, scratch);
+       g_free (scratch);
+}
+
 MonoJitInfo *
 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
 {
-       int pos, left, right, offset, offset1, offset2;
+       int pos, left, right, offset, offset1, offset2, code_len;
        int method_index, table_len, is_wrapper;
        guint32 token;
        MonoAotModule *amodule = image->aot_module;
@@ -1693,7 +2015,7 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
        guint32 *table;
        int nmethods = amodule->info.nmethods;
        gint32 *code_offsets;
-       int i;
+       int offsets_len, i;
 
        if (!amodule)
                return NULL;
@@ -1706,31 +2028,40 @@ 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) {
-                       code_offsets [(i * 2)] = amodule->code_offsets [i];
-                       code_offsets [(i *2) + 1] = i;
+                       /* Skip the -1 entries to speed up sorting */
+                       if (amodule->code_offsets [i] == 0xffffffff)
+                               continue;
+                       code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
+                       code_offsets [(offsets_len *2) + 1] = i;
+                       offsets_len ++;
                }
-               /* FIXME: Use a merge sort as this is mostly sorted */
-               qsort (code_offsets, nmethods, sizeof (gint32) * 2, compare_ints);
-               for (i = 0; i < nmethods -1; ++i)
+               /* Use a merge sort as this is mostly sorted */
+               msort_code_offsets (code_offsets, offsets_len);
+               //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
+               for (i = 0; i < offsets_len -1; ++i)
                        g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
 
                if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
                        /* Somebody got in before us */
                        g_free (code_offsets);
+               amodule->sorted_code_offsets_len = offsets_len;
        }
 
        code_offsets = amodule->sorted_code_offsets;
+       offsets_len = amodule->sorted_code_offsets_len;
 
        /* Binary search in the sorted_code_offsets table */
        left = 0;
-       right = nmethods;
+       right = offsets_len;
        while (TRUE) {
                pos = (left + right) / 2;
 
                offset1 = code_offsets [(pos * 2)];
-               if (pos + 1 == nmethods)
+               if (pos + 1 == offsets_len)
                        offset2 = amodule->code_end - amodule->code;
                else
                        offset2 = code_offsets [(pos + 1) * 2];
@@ -1744,10 +2075,20 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
        }
 
        g_assert (offset >= code_offsets [(pos * 2)]);
-       if (pos + 1 < nmethods)
+       if (pos + 1 < offsets_len)
                g_assert (offset < code_offsets [((pos + 1) * 2)]);
        method_index = code_offsets [(pos * 2) + 1];
 
+       code = &amodule->code [amodule->code_offsets [method_index]];
+       ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
+
+       if (pos == offsets_len - 1)
+               code_len = amodule->code_end - code;
+       else
+               code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
+
+       g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
+
        /* Might be a wrapper/extra method */
        if (amodule->extra_methods) {
                mono_aot_lock ();
@@ -1783,10 +2124,10 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
                                        break;
                        }
 
-                       p = amodule->extra_method_info + table [(pos * 2) + 1];
+                       p = amodule->blob + table [(pos * 2) + 1];
                        is_wrapper = decode_value (p, &p);
                        g_assert (!is_wrapper);
-                       method = decode_method_ref_2 (amodule, p, &p);
+                       method = decode_resolve_method_ref (amodule, p, &p);
                        g_assert (method);
                } else {
                        token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
@@ -1798,13 +2139,8 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
        g_assert (method);
 
        //printf ("F: %s\n", mono_method_full_name (method, TRUE));
-
-       code = &amodule->code [amodule->code_offsets [method_index]];
-       ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
-
-       g_assert ((guint8*)code <= (guint8*)addr);
        
-       jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code);
+       jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
 
        g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
        g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
@@ -1828,23 +2164,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);
                }
@@ -1860,7 +2195,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        }
        case MONO_PATCH_INFO_METHODCONST:
                /* Shared */
-               ji->data.method = decode_method_ref_2 (aot_module, p, &p);
+               ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
                if (!ji->data.method)
                        goto cleanup;
                break;
@@ -1880,7 +2215,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
                        goto cleanup;
                break;
        case MONO_PATCH_INFO_IMAGE:
-               ji->data.image = load_image (aot_module, decode_value (p, &p));
+               ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!ji->data.image)
                        goto cleanup;
                break;
@@ -1894,7 +2229,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_SWITCH:
                ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
                ji->data.table->table_size = decode_value (p, &p);
-               table = g_new (gpointer, ji->data.table->table_size);
+               table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
                ji->data.table->table = (MonoBasicBlock**)table;
                for (i = 0; i < ji->data.table->table_size; i++)
                        table [i] = (gpointer)(gssize)decode_value (p, &p);
@@ -1915,13 +2250,12 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
 
                val [0] = decode_value (p, &p);
                val [1] = decode_value (p, &p);
-               // FIXME: Is this correct ?
                v = ((guint64)val [1] << 32) | ((guint64)val [0]);
                *(double*)ji->data.target = *(double*)&v;
                break;
        }
        case MONO_PATCH_INFO_LDSTR:
-               image = load_image (aot_module, decode_value (p, &p));
+               image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!image)
                        goto cleanup;
                ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
@@ -1931,7 +2265,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_LDTOKEN:
        case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
                /* Shared */
-               image = load_image (aot_module, decode_value (p, &p));
+               image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!image)
                        goto cleanup;
                ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
@@ -1956,13 +2290,14 @@ 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;
                MonoJumpInfoRgctxEntry *entry;
 
                entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
-               entry->method = decode_method_ref_2 (aot_module, p, &p);
+               entry->method = decode_resolve_method_ref (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));
@@ -1979,7 +2314,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
                MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
 
-               imt_tramp->method = decode_method_ref_2 (aot_module, p, &p);
+               imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
                imt_tramp->vt_offset = decode_value (p, &p);
                
                ji->data.imt_tramp = imt_tramp;
@@ -2025,7 +2360,7 @@ load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
                        /* Already loaded */
                        //printf ("HIT!\n");
                } else {
-                       shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset];
+                       shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
 
                        ji->type = decode_value (shared_p, &shared_p);
 
@@ -2083,14 +2418,14 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
        MonoMemPool *mp;
        int i, pindex, n_patches, used_strings;
        gboolean keep_patches = TRUE;
-       guint8 *p, *ex_info;
+       guint8 *p;
        MonoJitInfo *jinfo = NULL;
        guint8 *code, *info;
 
        if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
                return NULL;
 
-       if ((domain != mono_get_root_domain ()) && (!(amodule->opts & MONO_OPT_SHARED)))
+       if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
                /* Non shared AOT code can't be used in other appdomains */
                return NULL;
 
@@ -2111,7 +2446,8 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
        }
 
        code = &amodule->code [amodule->code_offsets [method_index]];
-       info = &amodule->method_info [amodule->method_info_offsets [method_index]];
+
+       info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
 
        mono_aot_lock ();
        if (!amodule->methods_loaded)
@@ -2125,10 +2461,15 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
                if (mono_jit_stats.methods_aot >= mono_last_aot_method)
                                return NULL;
                else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
-                       if (method)
-                               printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name);
-                       else
+                       if (!method)
+                               method = mono_get_method (image, token, NULL);
+                       if (method) {
+                               char *name = mono_method_full_name (method, TRUE);
+                               printf ("LAST AOT METHOD: %s.\n", name);
+                               g_free (name);
+                       } else {
                                printf ("LAST AOT METHOD: %p %d\n", code, method_index);
+                       }
                }
        }
 
@@ -2141,7 +2482,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
                klass = decode_klass_ref (amodule, p, &p);
        }
 
-       if (amodule->opts & MONO_OPT_SHARED)
+       if (amodule->info.opts & MONO_OPT_SHARED)
                used_strings = decode_value (p, &p);
        else
                used_strings = 0;
@@ -2151,7 +2492,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
                mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
        }
 
-       if (amodule->opts & MONO_OPT_SHARED)    
+       if (amodule->info.opts & MONO_OPT_SHARED)       
                keep_patches = FALSE;
 
        n_patches = decode_value (p, &p);
@@ -2198,10 +2539,8 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
 
                full_name = mono_method_full_name (method, TRUE);
 
-               if (!jinfo) {
-                       ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
-                       jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code, code);
-               }
+               if (!jinfo)
+                       jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
 
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info);
                g_free (full_name);
@@ -2240,7 +2579,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
 
  cleanup:
        /* FIXME: The space in domain->mp is wasted */  
-       if (amodule->opts & MONO_OPT_SHARED)
+       if (amodule->info.opts & MONO_OPT_SHARED)
                /* No need to cache patches */
                mono_mempool_destroy (mp);
 
@@ -2251,11 +2590,10 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
 }
 
 static guint32
-find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
+find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
 {
        guint32 table_size, entry_size, hash;
        guint32 *table, *entry;
-       char *name = NULL;
        guint32 index;
        static guint32 n_extra_decodes;
 
@@ -2266,10 +2604,6 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
        table = amodule->extra_method_table + 1;
        entry_size = 3;
 
-       if (method->wrapper_type) {
-               name = mono_aot_wrapper_name (method);
-       }
-
        hash = mono_aot_method_hash (method) % table_size;
 
        entry = &table [hash * entry_size];
@@ -2286,7 +2620,7 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
                guint8 *p;
                int is_wrapper_name;
 
-               p = amodule->extra_method_info + key;
+               p = amodule->blob + key;
                is_wrapper_name = decode_value (p, &p);
                if (is_wrapper_name) {
                        int wrapper_type = decode_value (p, &p);
@@ -2294,15 +2628,16 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
                                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_method_ref_2 (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);
@@ -2327,11 +2662,10 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
                        }
 
                        /* 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)
@@ -2340,7 +2674,6 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
                        break;
        }
 
-       g_free (name);
        return index;
 }
 
@@ -2363,12 +2696,18 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
        guint32 index;
        GPtrArray *modules;
        int i;
+       char *name = NULL;
+
+       if (method->wrapper_type)
+               name = mono_aot_wrapper_name (method);
 
        /* Try the method's module first */
        *out_amodule = method->klass->image->aot_module;
-       index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
-       if (index != 0xffffff)
+       index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
+       if (index != 0xffffff) {
+               g_free (name);
                return index;
+       }
 
        /* 
         * Try all other modules.
@@ -2388,7 +2727,7 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
                MonoAotModule *amodule = g_ptr_array_index (modules, i);
 
                if (amodule != method->klass->image->aot_module)
-                       index = find_extra_method_in_amodule (amodule, method);
+                       index = find_extra_method_in_amodule (amodule, method, name);
                if (index != 0xffffff) {
                        *out_amodule = amodule;
                        break;
@@ -2397,6 +2736,7 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
        
        g_ptr_array_free (modules, TRUE);
 
+       g_free (name);
        return index;
 }
 
@@ -2438,7 +2778,11 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
        g_assert (klass->inited);
 
        /* Find method index */
-       if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
+       if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
+               /* 
+                * For generic methods, we store the fully shared instance in place of the
+                * original method.
+                */
                method = mono_method_get_declaring_generic_method (method);
                method_index = mono_metadata_token_index (method->token) - 1;
        } else if (method->is_inflated || !method->token) {
@@ -2460,8 +2804,11 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
 
                        code = mono_aot_get_method (domain, m);
                        if (code) {
-                               if (mono_method_needs_static_rgctx_invoke (m, FALSE))
-                                       code = mono_create_static_rgctx_trampoline (m, code);
+                               if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
+                                       code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
+                                       /* The call above returns an ftnptr */
+                                       code = mono_get_addr_from_ftnptr (code);
+                               }
 
                                return code;
                        }
@@ -2499,6 +2846,40 @@ 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);
+
+                       /* 
+                        * 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;
+                       printf ("HIT!\n");
+               }
+
+               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);
+               }
+
                if (method_index == 0xffffff) {
                        if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
                                char *full_name;
@@ -2614,11 +2995,25 @@ find_aot_module (guint8 *code)
        return user_data.module;
 }
 
+void
+mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
+{
+       /*
+        * Since AOT code is only used in the root domain, 
+        * mono_domain_get () != mono_get_root_domain () means the calling method
+        * is AppDomain:InvokeInDomain, so this is the same check as in 
+        * mono_method_same_domain () but without loading the metadata for the method.
+        */
+       if (mono_domain_get () == mono_get_root_domain ())
+               mono_arch_patch_plt_entry (code, got, regs, addr);
+}
+
 /*
  * mono_aot_plt_resolve:
  *
  *   This function is called by the entries in the PLT to resolve the actual method that
  * needs to be called. It returns a trampoline to the method and patches the PLT entry.
+ * Returns NULL if the something cannot be loaded.
  */
 gpointer
 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
@@ -2632,14 +3027,17 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
 
        //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
 
-       p = &module->got_info [plt_info_offset];
+       p = &module->blob [plt_info_offset];
 
        ji.type = decode_value (p, &p);
 
        mp = mono_mempool_new_size (512);
        res = decode_patch (module, mp, &ji, p, &p);
-       // FIXME: Error handling (how ?)
-       g_assert (res);
+
+       if (!res) {
+               mono_mempool_destroy (mp);
+               return NULL;
+       }
 
        /* 
         * Avoid calling resolve_patch_target in the full-aot case if possible, since
@@ -2655,8 +3053,21 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
                target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
        }
 
-       // FIXME: Clean this up, but how ?
-       if (ji.type != MONO_PATCH_INFO_ABS && ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_CLASS_INIT && ji.type != MONO_PATCH_INFO_GENERIC_CLASS_INIT && ji.type != MONO_PATCH_INFO_ICALL_ADDR && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && !no_ftnptr) {
+       /*
+        * The trampoline expects us to return a function descriptor on platforms which use
+        * it, but resolve_patch_target returns a direct function pointer for some type of
+        * patches, so have to translate between the two.
+        * FIXME: Clean this up, but how ?
+        */
+       if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) {
+               /* These should already have a function descriptor */
+#ifdef PPC_USES_FUNCTION_DESCRIPTOR
+               /* Our function descriptors have a 0 environment, gcc created ones don't */
+               if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
+                       g_assert (((gpointer*)target) [2] == 0);
+#endif
+               /* Empty */
+       } else if (!no_ftnptr) {
 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
                g_assert (((gpointer*)target) [2] != 0);
 #endif
@@ -2668,7 +3079,7 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
        /* 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);
-       mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
+       mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
 
        return target;
 #else
@@ -2687,15 +3098,7 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
 static void
 init_plt (MonoAotModule *amodule)
 {
-#ifndef MONO_CROSS_COMPILE
-
-#ifdef MONO_ARCH_AOT_SUPPORTED
-#ifdef __i386__
-       guint8 *buf = amodule->plt;
-#elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
        int i;
-       gpointer plt_0;
-#endif
        gpointer tramp;
 
        if (amodule->plt_inited)
@@ -2703,30 +3106,16 @@ init_plt (MonoAotModule *amodule)
 
        tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
 
-#ifdef __i386__
-       /* Initialize the first PLT entry */
-       make_writable (amodule->plt, amodule->plt_end - amodule->plt);
-       x86_jump_code (buf, tramp);
-#elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
        /*
         * Initialize the PLT entries in the GOT to point to the default targets.
         */
 
        tramp = mono_create_ftnptr (mono_domain_get (), tramp);
-       plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
-        /* The first entry points to the AOT trampoline */
-        ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
         for (i = 1; i < amodule->info.plt_size; ++i)
-                /* All the default entries point to the first entry */
-                ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
-#else
-       g_assert_not_reached ();
-#endif
+                /* All the default entries point to the AOT trampoline */
+                ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
 
        amodule->plt_inited = TRUE;
-#endif
-
-#endif /* MONO_CROSS_COMPILE */
 }
 
 /*
@@ -2738,39 +3127,13 @@ guint8*
 mono_aot_get_plt_entry (guint8 *code)
 {
        MonoAotModule *aot_module = find_aot_module (code);
-#if defined(__arm__) || defined(__mono_ppc__)
-       guint32 ins;
-#endif
 
        if (!aot_module)
                return NULL;
 
-#if defined(__i386__) || defined(__x86_64__)
-       if (code [-5] == 0xe8) {
-               guint32 disp = *(guint32*)(code - 4);
-               guint8 *target = code + disp;
-
-               if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
-                       return target;
-       }
-#elif defined(__arm__)
-       ins = ((guint32*)(gpointer)code) [-1];
-
-       /* Should be a 'bl' */
-       if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
-               gint32 disp = ((gint32)ins) & 0xffffff;
-               guint8 *target = code - 4 + 8 + (disp * 4);
-
-               if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
-                       return target;
-       }               
-#elif defined(__mono_ppc__)
-       /* Should be a bl */
-       ins = ((guint32*)(gpointer)code) [-1];
-
-       if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
-               gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
-               guint8 *target = code - 4 + (disp * 4);
+#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;
@@ -2795,19 +3158,8 @@ mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
        g_assert (plt_entry);
 
        /* The offset is embedded inside the code after the plt entry */
-#if defined(__i386__)
-       return *(guint32*)(plt_entry + 5);
-#elif defined(__x86_64__)
-       return *(guint32*)(plt_entry + 6);
-#elif defined(__arm__)
-       /* The offset is stored as the 4th word of the plt entry */
-       return ((guint32*)plt_entry) [3];          
-#elif defined(__mono_ppc__)
-#ifdef PPC_USES_FUNCTION_DESCRIPTOR
-       return ((guint32*)plt_entry) [8];
-#else
-       return ((guint32*)plt_entry) [6];
-#endif
+#ifdef MONO_ARCH_AOT_SUPPORTED
+       return mono_arch_get_plt_info_offset (plt_entry, regs, code);
 #else
        g_assert_not_reached ();
        return 0;
@@ -2830,6 +3182,21 @@ mono_create_ftnptr_malloc (guint8 *code)
 #endif
 }
 
+/*
+ * mono_aot_register_jit_icall:
+ *
+ *   Register a JIT icall which is called by trampolines in full-aot mode. This should
+ * be called from mono_arch_init () during startup.
+ */
+void
+mono_aot_register_jit_icall (const char *name, gpointer addr)
+{
+       /* No need for locking */
+       if (!aot_jit_icall_hash)
+               aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
+       g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
+}
+
 /*
  * load_function:
  *
@@ -2863,6 +3230,8 @@ load_function (MonoAotModule *amodule, const char *name)
                /* Nothing to patch */
                return code;
 
+       p = amodule->blob + *(guint32*)p;
+
        /* Similar to mono_aot_load_method () */
 
        n_patches = decode_value (p, &p);
@@ -2896,24 +3265,6 @@ load_function (MonoAotModule *amodule, const char *name)
                                        target = mono_exception_from_token;
                                } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
                                        target = mono_get_throw_exception ();
-#ifdef __x86_64__
-                               } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
-                                       target = mono_amd64_throw_exception;
-#endif
-#ifdef __x86_64__
-                               } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
-                                       target = mono_amd64_get_original_ip;
-#endif
-#ifdef __arm__
-                               } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
-                                       target = mono_arm_throw_exception;
-                               } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
-                                       target = mono_arm_throw_exception_by_token;
-#endif
-#ifdef __mono_ppc__
-                               } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
-                                       target = mono_ppc_throw_exception;
-#endif
                                } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
                                        int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
                                        target = (gpointer)mono_get_trampoline_func (tramp_type2);
@@ -2937,6 +3288,11 @@ 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_")) {
+                                       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);
                                } else {
                                        fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
                                        g_assert_not_reached ();
@@ -2962,11 +3318,11 @@ load_function (MonoAotModule *amodule, const char *name)
 }
 
 /*
- * Return the piece of code identified by NAME from the mscorlib AOT file.
+ * Return the trampoline identified by NAME from the mscorlib AOT file.
  * On ppc64, this returns a function descriptor.
  */
 gpointer
-mono_aot_get_named_code (const char *name)
+mono_aot_get_trampoline (const char *name)
 {
        MonoImage *image;
        MonoAotModule *amodule;
@@ -3045,8 +3401,8 @@ 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);
-               generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
+               symbol = mono_get_generic_trampoline_name (tramp_type);
+               generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
                g_free (symbol);
        }
 
@@ -3112,10 +3468,11 @@ 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);
-       return code;
+       /* The caller expects an ftnptr */
+       return mono_create_ftnptr (mono_domain_get (), code);
 }
 
 gpointer
@@ -3148,6 +3505,98 @@ mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem
 
        return code;
 }
+/*
+ * mono_aot_set_make_unreadable:
+ *
+ *   Set whenever to make all mmaped memory unreadable. In conjuction with a
+ * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
+ */
+void
+mono_aot_set_make_unreadable (gboolean unreadable)
+{
+       static int inited;
+
+       make_unreadable = unreadable;
+
+       if (make_unreadable && !inited) {
+               mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
+       }               
+}
+
+typedef struct {
+       MonoAotModule *module;
+       guint8 *ptr;
+} FindMapUserData;
+
+static void
+find_map (gpointer key, gpointer value, gpointer user_data)
+{
+       MonoAotModule *module = (MonoAotModule*)value;
+       FindMapUserData *data = (FindMapUserData*)user_data;
+
+       if (!data->module)
+               if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
+                       data->module = module;
+}
+
+static MonoAotModule*
+find_module_for_addr (void *ptr)
+{
+       FindMapUserData data;
+
+       if (!make_unreadable)
+               return NULL;
+
+       data.module = NULL;
+       data.ptr = (guint8*)ptr;
+
+       mono_aot_lock ();
+       g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
+       mono_aot_unlock ();
+
+       return data.module;
+}
+
+/*
+ * mono_aot_is_pagefault:
+ *
+ *   Should be called from a SIGSEGV signal handler to find out whenever @ptr is
+ * within memory allocated by this module.
+ */
+gboolean
+mono_aot_is_pagefault (void *ptr)
+{
+       if (!make_unreadable)
+               return FALSE;
+
+       /* 
+        * Not signal safe, but SIGSEGV's are synchronous, and
+        * this is only turned on by a MONO_DEBUG option.
+        */
+       return find_module_for_addr (ptr) != NULL;
+}
+
+/*
+ * mono_aot_handle_pagefault:
+ *
+ *   Handle a pagefault caused by an unreadable page by making it readable again.
+ */
+void
+mono_aot_handle_pagefault (void *ptr)
+{
+#ifndef PLATFORM_WIN32
+       guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
+       int res;
+
+       mono_aot_lock ();
+       res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
+       g_assert (res == 0);
+
+       n_pagefaults ++;
+       mono_aot_unlock ();
+#endif
+}
 
 #else
 /* AOT disabled */
@@ -3205,6 +3654,11 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
        return NULL;
 }
 
+void
+mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
+{
+}
+
 gpointer
 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
 {
@@ -3234,7 +3688,7 @@ mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
 }
 
 gpointer
-mono_aot_get_named_code (const char *name)
+mono_aot_get_trampoline (const char *name)
 {
        g_assert_not_reached ();
        return NULL;
@@ -3268,4 +3722,9 @@ mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
        return NULL;
 }
 
+void
+mono_aot_register_jit_icall (const char *name, gpointer addr)
+{
+}
+
 #endif