2010-07-08 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / aot-runtime.c
index abd9a4dc4622403cbdd3aafbaf06a6c6e3559a1e..7a7d4530b1ef305dd657a2dbffb857196d60fcd8 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>
@@ -100,6 +100,7 @@ typedef struct MonoAotModule {
        /* This contains <offset, index> pairs sorted by offset */
        /* This is needed because LLVM emitted methods can be in any order */
        gint32 *sorted_code_offsets;
+       gint32 sorted_code_offsets_len;
        guint32 *method_info_offsets;
        guint32 *got_info_offsets;
        guint32 *ex_info_offsets;
@@ -113,10 +114,6 @@ typedef struct MonoAotModule {
        /* Points to the GNU .eh_frame_hdr section, if it exists */
        guint8 *eh_frame_hdr;
 
-       /* Points to the .ARM.exidx section, if it exists */
-       guint8 *arm_exidx;
-       guint32 arm_exidx_size;
-
        /* Points to the trampolines */
        guint8 *trampolines [MONO_AOT_TRAMP_NUM];
        /* The first unused trampoline of each kind */
@@ -162,6 +159,7 @@ 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;
@@ -289,7 +287,7 @@ mono_aot_get_offset (guint32 *table, int index)
 }
 
 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);
@@ -394,7 +392,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;
 
@@ -467,8 +465,8 @@ decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
 /*
  * 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
+ *   Determine if calling decode_resolve_method_ref on P could return the same method as 
+ * METHOD. This is an optimization to avoid calling decode_resolve_method_ref () which
  * would create MonoMethods which are not needed etc.
  */
 static gboolean
@@ -540,7 +538,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
 
                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;
@@ -586,11 +584,14 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        *method = mono_gc_get_managed_allocator_by_type (atype);
                        break;
                }
+               case MONO_WRAPPER_WRITE_BARRIER:
+                       *method = mono_gc_get_write_barrier ();
+                       break;
                case MONO_WRAPPER_STELEMREF:
                        *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;
@@ -616,7 +617,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                }
                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;
@@ -730,12 +731,12 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
 }
 
 /*
- * decode_method_ref_2:
+ * decode_resolve_method_ref:
  *
  *   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 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
 {
        MonoMethod *method;
        guint32 token;
@@ -749,23 +750,6 @@ decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
        return method;
 }
 
-G_GNUC_UNUSED
-static void
-make_writable (guint8* addr, guint32 len)
-{
-       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);
-}
-
 static void
 create_cache_structure (void)
 {
@@ -937,15 +921,14 @@ find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *valu
                else
                        *value = NULL;
        } else {
-               mono_dl_symbol (module, name, value);
+               char *err = mono_dl_symbol (module, name, value);
+
+               if (err)
+                       g_free (err);
        }
 }
 
-#ifndef SHT_ARM_EXIDX
-#define SHT_ARM_EXIDX 0x70000001
-#endif
-
-#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)
 {
@@ -956,10 +939,6 @@ dl_callback (struct dl_phdr_info *info, size_t size, void *data)
                for (j = 0; j < info->dlpi_phnum; j++) {
                        if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
                                amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
-                       if (info->dlpi_phdr [j].p_type == SHT_ARM_EXIDX) {
-                               amodule->arm_exidx = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
-                               amodule->arm_exidx_size = info->dlpi_phdr [j].p_filesz;
-                       }
                }
                return 1;
        } else {
@@ -1172,17 +1151,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
        }
 
@@ -1198,7 +1179,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 
@@ -1531,9 +1512,12 @@ typedef struct
  * file belong to CODE, and construct a MonoJitInfo structure from it.
  * LOCKING: Acquires the domain lock.
  */
-static G_GNUC_UNUSED 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;
@@ -1541,10 +1525,12 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
        guint32 eh_frame_ptr;
        int fde_count;
        gint32 *table;
-       int i, 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;
+       guint32 ei_len, nested_len, nindex;
+       gpointer *type_info;
+       MonoJitInfo *jinfo;
 
        g_assert (amodule->eh_frame_hdr);
 
@@ -1586,12 +1572,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_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, NULL);
+       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);
@@ -1600,290 +1611,56 @@ 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;
 
-       g_assert (ei_len == jinfo->num_clauses);
-       for (i = 0; i < jinfo->num_clauses; ++i) {
+       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;
        }
-}
-#ifdef TARGET_ARM
-
-/* The offsets in the table are 31 bits long, have to extend them to 32 */
-#define EXTEND_PREL31(val) ((((gint32)(val)) << 1) >> 1)
-
-static inline guint32
-decode_uleb128 (guint8 *buf, guint8 **endbuf)
-{
-       guint8 *p = buf;
-       guint32 res = 0;
-       int shift = 0;
-
-       while (TRUE) {
-               guint8 b = *p;
-               p ++;
-
-               res = res | (((int)(b & 0x7f)) << shift);
-               if (!(b & 0x80))
-                       break;
-               shift += 7;
-       }
-
-       *endbuf = p;
-
-       return res;
-}
-
-static GSList*
-decode_arm_eh_ops (guint8 *unwind_ops, int nops)
-{
-       int i, vsp_reg, vsp_offset;
-       GSList *ops;
-       gint32 *reg_offsets;
-
-       /*
-        * Have to convert the ARM unwind info into DWARF unwind info.
-        * The ARM unwind info specifies a simple set of instructions which need to be
-        * executed during unwinding. It manipulates a virtual stack pointer (vsp). The
-        * connection with DWARF unwind info is the following: after all ARM unwind
-        * opcodes have been executed, the stack should be completely unwound, i.e.
-        * vsp == DWARF CFA. This allows us to construct the DWARF opcodes corresponding
-        * to the ARM opcodes.
-        * The ARM unwind info is not instruction precise, i. e. it can't handle
-        * async exceptions etc.
-        */
-       /* The reg used to compute the initial value of vsp */
-       vsp_reg = ARMREG_SP;
-       /* The offset between vsp_reg and the CFA */
-       vsp_offset = 0;
-
-       /* The register save offsets from the initial value of vsp */
-       reg_offsets = g_new0 (gint32, 16);
-       for (i = 0; i < 16; ++i)
-               reg_offsets [i] = -1;
-
-       /* section 9.3 in the ehabi doc */
-       for (i = 0; i < nops; ++i) {
-               guint8 op = unwind_ops [i];
-
-               if ((op >> 6) == 0) {
-                       /* vsp = vsp + (xxxxxx << 2) + 4. */
-                       vsp_offset += ((op & 0x3f) << 2) + 4;
-               } else if ((op >> 6) == 1) {
-                       /* vsp = vsp - (xxxxxx << 2) - 4. */
-                       vsp_offset -= ((op & 0x3f) << 2) + 4;
-               } else if (op == 0xb2) {
-                       /* vsp = vsp = vsp + 0x204 + (uleb128 << 2) */
-                       guint8 *p = unwind_ops + i + 1;
-                       guint32 v = decode_uleb128 (p, &p);
-
-                       vsp_offset += 0x204 + (v << 2);
-                       i = (p - unwind_ops) - 1;
-               } else if (op >= 0x80 && op <= 0x8f) {
-                       /* pop registers */
-                       guint8 op2;
-                       GSList *regs;
-                       int j;
-
-                       g_assert (i + 1 < nops);
-                       op2 = unwind_ops [i + 1];
-
-                       regs = NULL;
-                       for (j = 0; j < 8; ++j)
-                               if (op2 & (0x1 << j))
-                                       regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
-                       for (j = 0; j < 4; ++j)
-                               if (op & (0x1 << j))
-                                       regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R12 + j));
-                       g_assert (regs);
-
-                       for (j = 0; j < g_slist_length (regs); ++j)
-                               reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
-
-                       vsp_offset += g_slist_length (regs) * 4;
-
-                       g_slist_free (regs);
-
-                       i ++;
-               } else if (op >= 0xa8 && op <= 0xaf) {
-                       GSList *regs;
-                       int j;
-
-                       /* pop r4-r[4 + nnn], r14 */
-
-                       regs = NULL;
-                       for (j = 0; j <= (op & 0x7); ++j)
-                               regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
-                       regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R14));
-
-                       for (j = 0; j < g_slist_length (regs); ++j)
-                               reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
-
-                       vsp_offset += g_slist_length (regs) * 4;
-
-                       g_slist_free (regs);
-               } else if (op == 0xb0) {
-                       /* finish */
-                       break;
-               } else if (op >= 0x90 && op <= 0x9f && op != 0x9d && op != 0x9f) {
-                       /* vsp = <reg> */
-                       vsp_reg = op & 0xf;
-                       vsp_offset = 0;
-               } else {
-                       int j;
-
-                       for (j = 0; j < nops; ++j)
-                               printf ("%x ", unwind_ops [j]);
-                       printf (" / %d\n", i);
-                       g_assert_not_reached ();
-               }
-       }
-
-       ops = NULL;
-
-       /* vsp_reg + vsp_offset = CFA */
-       mono_add_unwind_op_def_cfa (ops, (guint8*)NULL, (guint8*)NULL, vsp_reg, vsp_offset);
-
-       for (i = 0; i < 16; ++i) {
-               if (reg_offsets [i] != -1)
-                       /* The reg is saved at vsp_reg + reg_offset [i] == CFA - (vsp_offset - reg_offset [i]) */
-                       mono_add_unwind_op_offset (ops, (guint8*)NULL, (guint8*)NULL, i, - (vsp_offset - reg_offsets [i]));
-       }
-
-       return ops;
-}
-
-/*
- * decode_arm_exidx:
- *
- *   Decode the exception handling information in the .ARM.exidx section of the AOT
- * file belong to CODE, and construct a MonoJitInfo structure from it.
- * LOCKING: Acquires the domain lock.
- */
-static void
-decode_arm_exidx (MonoAotModule *amodule, MonoDomain *domain, 
-                                 MonoMethod *method, guint8 *code, guint32 code_len, MonoJitInfo *jinfo)
-{
-       guint32 *table;
-       guint8 *base, *code1, *code2;
-       int i, pos, left, right, offset, offset1, offset2, count, nwords, nops;
-       guint32 entry;
-       guint8 unwind_ops [64];
-       GSList *ops;
-       guint8 *unwind_info;
-       guint32 unw_len;
-
-       g_assert (amodule->arm_exidx);
-
-       table = (guint32*)amodule->arm_exidx;
-
-       /* 
-        * The table format is described in:
-        * infocenter.arm.com/help/topic/com.arm.doc.../IHI0038A_ehabi.pdf
-        */
-
-       base = amodule->arm_exidx;
-       count = amodule->arm_exidx_size / 8;
-
-       /* Binary search in the table to find the entry for code */
-       offset = code - base;
-
-       left = 0;
-       right = count;
-       while (TRUE) {
-               pos = (left + right) / 2;
-
-               if (left == right)
-                       break;
-
-               offset1 = EXTEND_PREL31 (table [(pos * 2)]);
-               code1 = (guint8*)&(table [pos * 2]) + offset1;
-               if (pos + 1 == count)
-                       /* FIXME: */
-                       offset2 = amodule->code_end - amodule->code;
-               else
-                       offset2 = EXTEND_PREL31 (table [(pos + 1) * 2]);
-               code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
-
-               if (code < code1)
-                       right = pos;
-               else if (code >= code2)
-                       left = pos + 1;
-               else
-                       break;
-       }
-
-       if (code >= code1) {
-               /* 
-                * The linker might merge duplicate unwind table entries, so
-                * offset1 and offset2 might point to another method, but this is not a problem.
-                */
-               code1 = (guint8*)&(table [pos * 2]) + offset1;
-               code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
-
-               g_assert (code >= code1);
-               if (pos < count)
-                       g_assert (code < code2);
-
-               entry = table [(pos * 2) + 1];
-
-               /* inline entry, compact model, personality routine 0 */
-               if ((entry & 0xff000000) == 0x80000000) {
-                       nops = 3;
-                       unwind_ops [0] = (entry & 0x00ff0000) >> 16;
-                       unwind_ops [1] = (entry & 0x0000ff00) >> 8;
-                       unwind_ops [2] = (entry & 0x000000ff) >> 0;
 
-                       ops = decode_arm_eh_ops (unwind_ops, nops);
-               } else if ((entry & 0x80000000) == 0) {
-                       /* non-inline entry */
-                       guint8 *data = (guint8*)&table [(pos * 2) + 1] + EXTEND_PREL31 (entry);
+       /* 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;
 
-                       entry = ((guint32*)data) [0];
+               for (l = nesting [cindex1]; l; l = l->next) {
+                       gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
 
-                       /* compact model, personality routine 1 */
-                       g_assert ((entry & 0xff000000) == 0x81000000);
+                       for (j = 0; j < ei_len; ++j) {
+                               gint32 cindex2 = *(gint32*)type_info [j];
 
-                       nwords = (entry & 0x00ff0000) >> 16;
-                       nops = nwords * 4 + 2;
-                       g_assert (nops < 64);
-
-                       unwind_ops [0] = (entry & 0x0000ff00) >> 8;
-                       unwind_ops [1] = (entry & 0x000000ff) >> 0;
-
-                       for (i = 0; i < nwords; ++i) {
-                               entry = ((guint32*)data) [1 + i];
-                               unwind_ops [(i * 4) + 2] = (entry & 0xff000000) >> 24;
-                               unwind_ops [(i * 4) + 2 + 1] = (entry & 0x00ff0000) >> 16;
-                               unwind_ops [(i * 4) + 2 + 2] = (entry & 0x0000ff00) >> 8;
-                               unwind_ops [(i * 4) + 2 + 3] = (entry & 0x000000ff) >> 0;
+                               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 ++;
+                               }
                        }
-
-                       ops = decode_arm_eh_ops (unwind_ops, nops);
-               } else {
-                       NOT_IMPLEMENTED;
                }
-
-               unwind_info = mono_unwind_ops_encode (ops, &unw_len);
-       } else {
-               /* The method has no unwind info */
-               unwind_info = NULL;
-               unw_len = 0;
        }
+       g_assert (nindex == ei_len + nested_len);
 
-       jinfo->code_size = code_len;
-       jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
-       jinfo->method = method;
-       jinfo->code_start = code;
-       jinfo->domain_neutral = 0;
-       /* This signals that used_regs points to a normal cached unwind info */
-       jinfo->from_aot = 0;
+       return jinfo;
 }
-#endif
 
 /*
  * LOCKING: Acquires the domain lock.
@@ -1893,13 +1670,13 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                                                         MonoMethod *method, guint8* ex_info, guint8 *addr,
                                                         guint8 *code, guint32 code_len)
 {
-       int i, buf_len;
+       int i, buf_len, num_clauses;
        MonoJitInfo *jinfo;
        guint used_int_regs, flags;
-       gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
+       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 */
 
@@ -1910,6 +1687,8 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
        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;
 
@@ -1924,21 +1703,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))
@@ -1949,21 +1773,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 (from_llvm) {
-               /* LLVM compiled method */
-               /* The info is in the .eh_frame section */
-#ifdef TARGET_ARM
-               decode_arm_exidx (amodule, domain, method, code, code_len, jinfo);
-#else
-               decode_eh_frame (amodule, domain, method, code, jinfo);
-#endif
-               jinfo->from_llvm = 1;
-       } else {
                jinfo->code_size = code_len;
                jinfo->used_regs = used_int_regs;
                jinfo->method = method;
@@ -1980,14 +1790,37 @@ 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) {
@@ -1997,6 +1830,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                int len = decode_value (p, &p);
 
                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) {
                        SeqPoint *sp = &seq_points->seq_points [i];
@@ -2057,6 +1891,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;
@@ -2064,12 +1899,66 @@ 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)
 {
@@ -2083,7 +1972,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;
@@ -2096,31 +1985,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];
@@ -2134,14 +2032,14 @@ 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 == nmethods - 1)
+       if (pos == offsets_len - 1)
                code_len = amodule->code_end - code;
        else
                code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
@@ -2186,7 +2084,7 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
                        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);
@@ -2255,7 +2153,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;
@@ -2289,7 +2187,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);
@@ -2356,7 +2254,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
                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));
@@ -2373,7 +2271,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;
@@ -2520,10 +2418,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);
+                       }
                }
        }
 
@@ -2690,7 +2593,7 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const
                        mono_aot_unlock ();
                        if (!m) {
                                guint8 *orig_p = p;
-                               m = decode_method_ref_2 (amodule, p, &p);
+                               m = decode_resolve_method_ref (amodule, p, &p);
                                if (m) {
                                        mono_aot_lock ();
                                        g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
@@ -2832,7 +2735,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) {
@@ -2896,6 +2803,11 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
                                return code;
                }
 
+               if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
+                       /* Partial sharing */
+                       method_index = find_extra_method (mini_get_shared_method (method), &amodule);
+               }
+
                if (method_index == 0xffffff) {
                        if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
                                char *full_name;
@@ -3011,6 +2923,19 @@ 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:
  *
@@ -3082,7 +3007,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
@@ -3101,15 +3026,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)
@@ -3117,30 +3034,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 */
 }
 
 /*
@@ -3152,39 +3055,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;
@@ -3209,19 +3086,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;
@@ -3244,6 +3110,23 @@ mono_create_ftnptr_malloc (guint8 *code)
 #endif
 }
 
+static GHashTable *aot_jit_icall_hash;
+
+/*
+ * 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:
  *
@@ -3312,24 +3195,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);
@@ -3353,6 +3218,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 ();
@@ -3378,11 +3248,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;
@@ -3461,8 +3331,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);
        }
 
@@ -3528,7 +3398,7 @@ mono_aot_get_lazy_fetch_trampoline (guint32 slot)
        char *symbol;
        gpointer code;
 
-       symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
+       symbol = mono_get_rgctx_fetch_trampoline_name (slot);
        code = load_function (mono_defaults.corlib->aot_module, symbol);
        g_free (symbol);
        /* The caller expects an ftnptr */
@@ -3565,6 +3435,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 */
@@ -3622,6 +3584,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)
 {
@@ -3651,7 +3618,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;
@@ -3685,4 +3652,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