Enable --aot (but not --aot=full) on osx/x86
[mono.git] / mono / mini / aot-compiler.c
index 2306c2b1477e159683969a19712e49af61788e94..a5cc9c625428cadd555ff68473446378559b65ca 100644 (file)
 
 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
 
+#if defined(__linux__) || defined(__native_client_codegen__)
+#define RODATA_SECT ".rodata"
+#else
+#define RODATA_SECT ".text"
+#endif
+
 #define TV_DECLARE(name) gint64 name
 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
@@ -76,6 +82,8 @@
 #define SHARED_EXT ".dll"
 #elif defined(__ppc__) && defined(__MACH__)
 #define SHARED_EXT ".dylib"
+#elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
+#define SHARED_EXT ".dylib"
 #else
 #define SHARED_EXT ".so"
 #endif
@@ -123,8 +131,8 @@ typedef struct MonoAotCompile {
        GHashTable *method_depth;
        MonoCompile **cfgs;
        int cfgs_size;
-       GHashTable *patch_to_plt_offset;
-       GHashTable *plt_offset_to_patch;
+       GHashTable *patch_to_plt_entry;
+       GHashTable *plt_offset_to_entry;
        GHashTable *patch_to_got_offset;
        GHashTable **patch_to_got_offset_by_type;
        GPtrArray *got_patches;
@@ -174,6 +182,12 @@ typedef struct MonoAotCompile {
        MonoDynamicStream blob;
 } MonoAotCompile;
 
+typedef struct {
+       int plt_offset;
+       char *symbol;
+       MonoJumpInfo *ji;
+} MonoPltEntry;
+
 #define mono_acfg_lock(acfg) EnterCriticalSection (&((acfg)->mutex))
 #define mono_acfg_unlock(acfg) LeaveCriticalSection (&((acfg)->mutex))
 
@@ -318,6 +332,14 @@ emit_byte (MonoAotCompile *acfg, guint8 val)
        img_writer_emit_byte (acfg->w, val); 
 }
 
+#ifdef __native_client_codegen__
+static inline void
+emit_nacl_call_alignment (MonoAotCompile *acfg)
+{
+       img_writer_emit_nacl_call_alignment (acfg->w);
+}
+#endif
+
 static G_GNUC_UNUSED void
 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
 {
@@ -344,7 +366,7 @@ emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
 static void
 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
 {
-       img_writer_emit_section_change (acfg->w, ".text", 1);
+       img_writer_emit_section_change (acfg->w, RODATA_SECT, 1);
        emit_global (acfg, name, FALSE);
        img_writer_emit_label (acfg->w, name);
        img_writer_emit_string (acfg->w, value);
@@ -448,6 +470,10 @@ encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
 #else
 #define AOT_FUNC_ALIGNMENT 16
 #endif
+#if defined(TARGET_X86) && defined(__native_client_codegen__)
+#undef AOT_FUNC_ALIGNMENT
+#define AOT_FUNC_ALIGNMENT 32
+#endif
  
 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
 #define PPC_LD_OP "ld"
@@ -642,12 +668,26 @@ arch_emit_plt_entry (MonoAotCompile *acfg, int index)
 #if defined(TARGET_X86)
                guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
 
+#ifdef __native_client_codegen__
+               const guint8 kSizeOfNaClJmp = 11;
+               guint8 bytes[kSizeOfNaClJmp];
+               guint8 *pbytes = &bytes[0];
+               
+               x86_jump_membase32 (pbytes, X86_EBX, offset);
+               emit_bytes (acfg, bytes, kSizeOfNaClJmp);
+               /* four bytes of data, used by mono_arch_patch_plt_entry              */
+               /* For Native Client, make this work with data embedded in push.      */
+               emit_byte (acfg, 0x68);  /* hide data in a push */
+               emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+               emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
+#else
                /* jmp *<offset>(%ebx) */
                emit_byte (acfg, 0xff);
                emit_byte (acfg, 0xa3);
                emit_int32 (acfg, offset);
                /* Used by mono_aot_get_plt_info_offset */
                emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+#endif  /* __native_client_codegen__ */
 #elif defined(TARGET_AMD64)
                /*
                 * We can't emit jumps because they are 32 bits only so they can't be patched.
@@ -834,9 +874,16 @@ arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size
        /* Branch to generic trampoline */
        x86_jump_reg (code, X86_ECX);
 
+#ifdef __native_client_codegen__
+       {
+               /* emit nops to next 32 byte alignment */
+               int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
+               while (code < (buf + a)) x86_nop(code);
+       }
+#endif
        emit_bytes (acfg, buf, code - buf);
 
-       *tramp_size = 17;
+       *tramp_size = NACL_SIZE(17, kNaClAlignment);
        g_assert (code - buf == *tramp_size);
 #else
        g_assert_not_reached ();
@@ -850,14 +897,14 @@ arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size
  * CALL_TARGET is the symbol pointing to the native code of METHOD.
  */
 static void
-arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGenericSharingContext *gsctx, const char *call_target)
+arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, const char *call_target)
 {
 #if defined(TARGET_AMD64)
        guint8 buf [32];
        guint8 *code;
        int this_reg;
 
-       this_reg = mono_arch_get_this_arg_reg (mono_method_signature (method), gsctx, NULL);
+       this_reg = mono_arch_get_this_arg_reg (NULL);
        code = buf;
        amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
 
@@ -870,9 +917,6 @@ arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGeneri
        guint8 *code;
        int this_pos = 4;
 
-       if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
-               this_pos = 8;
-           
        code = buf;
 
        x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
@@ -885,14 +929,10 @@ arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGeneri
 #elif defined(TARGET_ARM)
        guint8 buf [128];
        guint8 *code;
-       int this_pos = 0;
 
        code = buf;
 
-       if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
-               this_pos = 1;
-
-       ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
+       ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
 
        emit_bytes (acfg, buf, code - buf);
        /* jump to method */
@@ -911,9 +951,6 @@ arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGeneri
 #elif defined(TARGET_POWERPC)
        int this_pos = 3;
 
-       if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
-               this_pos = 4;
-
        g_assert (!acfg->use_bin_writer);
 
        fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
@@ -1026,9 +1063,17 @@ arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_
        /* Branch to the target address */
        x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
 
+#ifdef __native_client_codegen__
+       {
+               /* emit nops to next 32 byte alignment */
+               int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
+               while (code < (buf + a)) x86_nop(code);
+       }
+#endif
+
        emit_bytes (acfg, buf, code - buf);
 
-       *tramp_size = 15;
+       *tramp_size = NACL_SIZE (15, kNaClAlignment);
        g_assert (code - buf == *tramp_size);
 #else
        g_assert_not_reached ();
@@ -1058,36 +1103,38 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
        /* FIXME: Optimize this, i.e. use binary search etc. */
        /* Maybe move the body into a separate function (slower, but much smaller) */
 
-       /* R10 is a free register */
+       /* R11 is a free register */
 
        labels [0] = code;
-       amd64_alu_membase_imm (code, X86_CMP, AMD64_R10, 0, 0);
+       amd64_alu_membase_imm (code, X86_CMP, AMD64_R11, 0, 0);
        labels [1] = code;
        amd64_branch8 (code, X86_CC_Z, FALSE, 0);
 
        /* Check key */
-       amd64_alu_membase_reg (code, X86_CMP, AMD64_R10, 0, MONO_ARCH_IMT_REG);
+       amd64_alu_membase_reg (code, X86_CMP, AMD64_R11, 0, MONO_ARCH_IMT_REG);
        labels [2] = code;
        amd64_branch8 (code, X86_CC_Z, FALSE, 0);
 
        /* Loop footer */
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_R10, 2 * sizeof (gpointer));
+       amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, 2 * sizeof (gpointer));
        amd64_jump_code (code, labels [0]);
 
        /* Match */
        mono_amd64_patch (labels [2], code);
-       amd64_mov_reg_membase (code, AMD64_R10, AMD64_R10, sizeof (gpointer), 8);
-       amd64_jump_membase (code, AMD64_R10, 0);
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, sizeof (gpointer), 8);
+       amd64_jump_membase (code, AMD64_R11, 0);
 
        /* No match */
        /* FIXME: */
        mono_amd64_patch (labels [1], code);
        x86_breakpoint (code);
 
-       /* mov <OFFSET>(%rip), %r10 */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 12345678, 8);
+
+       /* mov <OFFSET>(%rip), %r11 */
        emit_byte (acfg, '\x4d');
        emit_byte (acfg, '\x8b');
-       emit_byte (acfg, '\x15');
+       emit_byte (acfg, '\x1d');
        emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
 
        emit_bytes (acfg, buf, code - buf);
@@ -1095,9 +1142,17 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
        *tramp_size = code - buf + 7;
 #elif defined(TARGET_X86)
        guint8 *buf, *code;
+#ifdef __native_client_codegen__
+       guint8 *buf_alloc;
+#endif
        guint8 *labels [3];
 
+#ifdef __native_client_codegen__
+       buf_alloc = g_malloc (256 + kNaClAlignment);
+       code = buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
+#else
        code = buf = g_malloc (256);
+#endif
 
        /* Allocate a temporary stack slot */
        x86_push_reg (code, X86_EAX);
@@ -1139,6 +1194,13 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
        mono_x86_patch (labels [1], code);
        x86_breakpoint (code);
 
+#ifdef __native_client_codegen__
+       {
+               /* emit nops to next 32 byte alignment */
+               int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
+               while (code < (buf + a)) x86_nop(code);
+       }
+#endif
        emit_bytes (acfg, buf, code - buf);
        
        *tramp_size = code - buf;
@@ -1413,7 +1475,7 @@ add_stream_data (MonoDynamicStream *stream, const char *data, guint32 len)
  * blob where the data was stored.
  */
 static guint32
-add_to_blob (MonoAotCompile *acfg, guint8 *data, guint32 data_len)
+add_to_blob (MonoAotCompile *acfg, const guint8 *data, guint32 data_len)
 {
        if (acfg->blob.alloc_size == 0)
                stream_init (&acfg->blob);
@@ -1720,14 +1782,20 @@ encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8
                case MONO_WRAPPER_ALLOC: {
                        AllocatorWrapperInfo *info = mono_marshal_get_wrapper_info (method);
 
+                       /* The GC name is saved once in MonoAotFileInfo */
                        g_assert (info->alloc_type != -1);
                        encode_value (info->alloc_type, p, &p);
                        break;
                }
                case MONO_WRAPPER_WRITE_BARRIER:
                        break;
-               case MONO_WRAPPER_STELEMREF:
+               case MONO_WRAPPER_STELEMREF: {
+                       MonoClass *klass = mono_marshal_get_wrapper_info (method);
+
+                       /* Make sure this is the 'normal' stelemref wrapper, not the virtual one */
+                       g_assert (!klass);
                        break;
+               }
                case MONO_WRAPPER_UNKNOWN:
                        if (strcmp (method->name, "FastMonitorEnter") == 0)
                                encode_value (MONO_AOT_WRAPPER_MONO_ENTER, p, &p);
@@ -1859,6 +1927,26 @@ compare_patches (gconstpointer a, gconstpointer b)
                return 0;
 }
 
+static G_GNUC_UNUSED char*
+patch_to_string (MonoJumpInfo *patch_info)
+{
+       GString *str;
+
+       str = g_string_new ("");
+
+       g_string_append_printf (str, "%s(", get_patch_name (patch_info->type));
+
+       switch (patch_info->type) {
+       case MONO_PATCH_INFO_VTABLE:
+               mono_type_get_desc (str, &patch_info->data.klass->byval_arg, TRUE);
+               break;
+       default:
+               break;
+       }
+       g_string_append_printf (str, ")");
+       return g_string_free (str, FALSE);
+}
+
 /*
  * is_plt_patch:
  *
@@ -1885,36 +1973,67 @@ is_plt_patch (MonoJumpInfo *patch_info)
        }
 }
 
-static int
-get_plt_offset (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
+/*
+ * get_plt_symbol:
+ *
+ *   Return the symbol identifying the plt entry PLT_OFFSET.
+ */
+static char*
+get_plt_symbol (MonoAotCompile *acfg, int plt_offset, MonoJumpInfo *patch_info)
+{
+#ifdef __MACH__
+       /* 
+        * The Apple linker reorganizes object files, so it doesn't like branches to local
+        * labels, since those have no relocations.
+        */
+       return g_strdup_printf ("%sp_%d", acfg->llvm_label_prefix, plt_offset);
+#else
+       return g_strdup_printf ("%s%sp_%d", acfg->llvm_label_prefix, acfg->temp_prefix, plt_offset);
+#endif
+}
+
+/*
+ * get_plt_entry:
+ *
+ *   Return a PLT entry which belongs to the method identified by PATCH_INFO.
+ */
+static MonoPltEntry*
+get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
 {
-       int res = -1;
+       MonoPltEntry *res;
 
-       if (is_plt_patch (patch_info)) {
-               int idx = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_plt_offset, patch_info));
+       if (!is_plt_patch (patch_info))
+               return NULL;
 
-               // FIXME: This breaks the calculation of final_got_size         
-               if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
-                       /* 
-                        * Allocate a separate PLT slot for each such patch, since some plt
-                        * entries will refer to the method itself, and some will refer to the
-                        * wrapper.
-                        */
-                       idx = 0;
-               }
+       res = g_hash_table_lookup (acfg->patch_to_plt_entry, patch_info);
 
-               if (idx) {
-                       res = idx;
-               } else {
-                       MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
+       // FIXME: This breaks the calculation of final_got_size         
+       if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
+               /* 
+                * Allocate a separate PLT slot for each such patch, since some plt
+                * entries will refer to the method itself, and some will refer to the
+                * wrapper.
+                */
+               res = NULL;
+       }
 
-                       g_assert (!acfg->final_got_size);
+       if (!res) {
+               MonoJumpInfo *new_ji;
 
-                       res = acfg->plt_offset;
-                       g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (res), new_ji);
-                       g_hash_table_insert (acfg->patch_to_plt_offset, new_ji, GUINT_TO_POINTER (res));
-                       acfg->plt_offset ++;
-               }
+               g_assert (!acfg->final_got_size);
+
+               new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
+
+               res = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoPltEntry));
+               res->plt_offset = acfg->plt_offset;
+               res->ji = new_ji;
+               res->symbol = get_plt_symbol (acfg, res->plt_offset, patch_info);
+
+               g_hash_table_insert (acfg->patch_to_plt_entry, new_ji, res);
+
+               g_hash_table_insert (acfg->plt_offset_to_entry, GUINT_TO_POINTER (res->plt_offset), res);
+
+               acfg->plt_offset ++;
        }
 
        return res;
@@ -2268,6 +2387,17 @@ add_wrappers (MonoAotCompile *acfg)
                if (method)
                        add_method (acfg, method);
 #endif
+
+               /* Stelemref wrappers */
+               /* There is only a constant number of these, iterating over all types should handle them all */
+               for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
+                       MonoClass *klass;
+               
+                       token = MONO_TOKEN_TYPE_DEF | (i + 1);
+                       klass = mono_class_get (acfg->image, token);
+                       if (klass)
+                               add_method (acfg, mono_marshal_get_virtual_stelemref (mono_array_class_get (klass, 1)));
+               }
        }
 
        /* 
@@ -2390,7 +2520,7 @@ add_wrappers (MonoAotCompile *acfg)
                                klass = mono_class_from_mono_type (t);
                                g_assert (klass->parent == mono_defaults.multicastdelegate_class);
 
-                               add_method (acfg, mono_marshal_get_managed_wrapper (method, klass, NULL));
+                               add_method (acfg, mono_marshal_get_managed_wrapper (method, klass, 0));
                        }
                }
 
@@ -2453,13 +2583,21 @@ method_has_type_vars (MonoMethod *method)
        return FALSE;
 }
 
+static void add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth);
+
+static void
+add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
+{
+       add_generic_class_with_depth (acfg, klass, 0);
+}
+
 /*
  * add_generic_class:
  *
  *   Add all methods of a generic class.
  */
 static void
-add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
+add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth)
 {
        MonoMethod *method;
        gpointer iter;
@@ -2489,7 +2627,7 @@ add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
                 * FIXME: Instances which are referenced by these methods are not added,
                 * for example Array.Resize<int> for List<int>.Add ().
                 */
-               add_extra_method (acfg, method);
+               add_extra_method_with_depth (acfg, method, depth);
        }
 
        if (klass->delegate) {
@@ -2534,7 +2672,7 @@ add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
                while ((method = mono_class_get_methods (array_class, &iter))) {
                        if (strstr (method->name, name_prefix)) {
                                MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
-                               add_extra_method (acfg, m);
+                               add_extra_method_with_depth (acfg, m, depth);
                        }
                }
 
@@ -2561,6 +2699,27 @@ add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
                        add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx));
                }
        }
+
+       /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
+       if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
+               MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
+               MonoClass *iface, *gcomparer;
+               MonoGenericContext ctx;
+               MonoType *args [16];
+
+               memset (&ctx, 0, sizeof (ctx));
+
+               iface = mono_class_from_name (mono_defaults.corlib, "System", "IEquatable`1");
+               g_assert (iface);
+               args [0] = &tclass->byval_arg;
+               ctx.class_inst = mono_metadata_get_generic_inst (1, args);
+
+               if (mono_class_is_assignable_from (mono_class_inflate_generic_class (iface, &ctx), tclass)) {
+                       gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
+                       g_assert (gcomparer);
+                       add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx));
+               }
+       }
 }
 
 static void
@@ -2885,7 +3044,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                                                MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
                                                //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
                                                direct_call = TRUE;
-                                               sprintf (direct_call_target, callee_cfg->asm_symbol);
+                                               sprintf (direct_call_target, "%s", callee_cfg->asm_symbol);
                                                patch_info->type = MONO_PATCH_INFO_NONE;
                                                acfg->stats.direct_calls ++;
                                        }
@@ -2894,11 +3053,11 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                                }
 
                                if (!got_only && !direct_call) {
-                                       int plt_offset = get_plt_offset (acfg, patch_info);
-                                       if (plt_offset != -1) {
+                                       MonoPltEntry *plt_entry = get_plt_entry (acfg, patch_info);
+                                       if (plt_entry) {
                                                /* This patch has a PLT entry, so we must emit a call to the PLT entry */
                                                direct_call = TRUE;
-                                               sprintf (direct_call_target, "%s%sp_%d", acfg->llvm_label_prefix, acfg->temp_prefix, plt_offset);
+                                               sprintf (direct_call_target, "%s", plt_entry->symbol);
                
                                                /* Nullify the patch */
                                                patch_info->type = MONO_PATCH_INFO_NONE;
@@ -3011,7 +3170,7 @@ emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
 
        method = cfg->orig_method;
        code = cfg->native_code;
-       header = mono_method_get_header (method);
+       header = cfg->header;
 
        method_index = get_method_index (acfg, method);
 
@@ -3073,6 +3232,7 @@ encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint
                encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
                break;
        case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
+       case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
                break;
        case MONO_PATCH_INFO_METHOD_REL:
                encode_value ((gint)patch_info->data.offset, p, &p);
@@ -3274,6 +3434,12 @@ emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
                        continue;
                }
 
+               if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR) {
+                       /* Stored in a GOT slot initialized at module load time */
+                       patch_info->type = MONO_PATCH_INFO_NONE;
+                       continue;
+               }
+
                if (is_plt_patch (patch_info)) {
                        /* Calls are made through the PLT */
                        patch_info->type = MONO_PATCH_INFO_NONE;
@@ -3345,7 +3511,7 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
 
        method = cfg->orig_method;
        code = cfg->native_code;
-       header = mono_method_get_header (method);
+       header = cfg->header;
 
        method_index = get_method_index (acfg, method);
 
@@ -3392,6 +3558,11 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
 
        /* Exception table */
        if (cfg->compile_llvm) {
+               /*
+                * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
+                * since the information is only available to llc. Instead, we let llc save the data
+                * into the LSDA, and read it from there at runtime.
+                */
                /* The assembly might be CIL stripped so emit the data ourselves */
                if (header->num_clauses)
                        encode_value (header->num_clauses, p, &p);
@@ -3408,6 +3579,18 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                        } else {
                                encode_value (0, p, &p);
                        }
+
+                       /* Emit a list of nesting clauses */
+                       for (i = 0; i < header->num_clauses; ++i) {
+                               gint32 cindex1 = k;
+                               MonoExceptionClause *clause1 = &header->clauses [cindex1];
+                               gint32 cindex2 = i;
+                               MonoExceptionClause *clause2 = &header->clauses [cindex2];
+
+                               if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
+                                       encode_value (i, p, &p);
+                       }
+                       encode_value (-1, p, &p);
                }
        } else {
                if (jinfo->num_clauses)
@@ -3439,9 +3622,11 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
        if (jinfo->has_generic_jit_info) {
                MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
 
-               encode_value (gi->has_this ? 1 : 0, p, &p);
-               encode_value (gi->this_reg, p, &p);
-               encode_value (gi->this_offset, p, &p);
+               if (!cfg->compile_llvm) {
+                       encode_value (gi->has_this ? 1 : 0, p, &p);
+                       encode_value (gi->this_reg, p, &p);
+                       encode_value (gi->this_offset, p, &p);
+               }
 
                /* 
                 * Need to encode jinfo->method too, since it is not equal to 'method'
@@ -3508,6 +3693,20 @@ emit_klass_info (MonoAotCompile *acfg, guint32 token)
        gboolean no_special_static, cant_encode;
        gpointer iter = NULL;
 
+       if (!klass) {
+               buf_size = 16;
+
+               p = buf = g_malloc (buf_size);
+
+               /* Mark as unusable */
+               encode_value (-1, p, &p);
+
+               res = add_to_blob (acfg, buf, p - buf);
+               g_free (buf);
+
+               return res;
+       }
+               
        buf_size = 10240 + (klass->vtable_size * 16);
        p = buf = g_malloc (buf_size);
 
@@ -3594,21 +3793,27 @@ emit_plt (MonoAotCompile *acfg)
 
        emit_section_change (acfg, ".text", 0);
        emit_global (acfg, symbol, TRUE);
-#ifdef TARGET_X86
-       /* This section will be made read-write by the AOT loader */
-       emit_alignment (acfg, mono_pagesize ());
-#else
        emit_alignment (acfg, 16);
-#endif
        emit_label (acfg, symbol);
        emit_label (acfg, acfg->plt_symbol);
 
        for (i = 0; i < acfg->plt_offset; ++i) {
                char label [128];
                char *debug_sym = NULL;
+               MonoPltEntry *plt_entry = NULL;
                MonoJumpInfo *ji;
 
-               sprintf (label, "%s%sp_%d", acfg->llvm_label_prefix, acfg->temp_prefix, i);
+               if (i == 0) {
+                       /* 
+                        * The first plt entry is used to transfer code to the AOT loader. 
+                        */
+                       arch_emit_plt_entry (acfg, i);
+                       continue;
+               }
+
+               plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
+               ji = plt_entry->ji;
+               sprintf (label, "%s", plt_entry->symbol);
 
                if (acfg->llvm) {
                        /*
@@ -3618,7 +3823,6 @@ emit_plt (MonoAotCompile *acfg)
                         * FIXME: Avoid the got slot.
                         * FIXME: Add support to the binary writer.
                         */
-                       ji = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
                        if (ji && is_direct_callable (acfg, NULL, ji) && !acfg->use_bin_writer) {
                                MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, ji->data.method);
                                fprintf (acfg->fp, "\n.set %s, %s\n", label, callee_cfg->asm_symbol);
@@ -3629,50 +3833,43 @@ emit_plt (MonoAotCompile *acfg)
                emit_label (acfg, label);
 
                if (acfg->aot_opts.write_symbols) {
-                       MonoJumpInfo *ji = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
-
-                       if (ji) {
-                               switch (ji->type) {
-                               case MONO_PATCH_INFO_METHOD:
-                                       debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
-                                       break;
-                               case MONO_PATCH_INFO_INTERNAL_METHOD:
-                                       debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
-                                       break;
-                               case MONO_PATCH_INFO_CLASS_INIT:
-                                       debug_sym = g_strdup_printf ("plt__class_init_%s", mono_type_get_name (&ji->data.klass->byval_arg));
-                                       sanitize_symbol (debug_sym);
-                                       break;
-                               case MONO_PATCH_INFO_RGCTX_FETCH:
-                                       debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
-                                       break;
-                               case MONO_PATCH_INFO_ICALL_ADDR: {
-                                       char *s = get_debug_sym (ji->data.method, "", cache);
+                       switch (ji->type) {
+                       case MONO_PATCH_INFO_METHOD:
+                               debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
+                               break;
+                       case MONO_PATCH_INFO_INTERNAL_METHOD:
+                               debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
+                               break;
+                       case MONO_PATCH_INFO_CLASS_INIT:
+                               debug_sym = g_strdup_printf ("plt__class_init_%s", mono_type_get_name (&ji->data.klass->byval_arg));
+                               sanitize_symbol (debug_sym);
+                               break;
+                       case MONO_PATCH_INFO_RGCTX_FETCH:
+                               debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
+                               break;
+                       case MONO_PATCH_INFO_ICALL_ADDR: {
+                               char *s = get_debug_sym (ji->data.method, "", cache);
                                        
-                                       debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
-                                       g_free (s);
-                                       break;
-                               }
-                               case MONO_PATCH_INFO_JIT_ICALL_ADDR:
-                                       debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
-                                       break;
-                               case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
-                                       debug_sym = g_strdup_printf ("plt__generic_class_init");
-                                       break;
-                               default:
-                                       break;
-                               }
+                               debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
+                               g_free (s);
+                               break;
+                       }
+                       case MONO_PATCH_INFO_JIT_ICALL_ADDR:
+                               debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
+                               break;
+                       case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
+                               debug_sym = g_strdup_printf ("plt__generic_class_init");
+                               break;
+                       default:
+                               break;
+                       }
 
-                               if (debug_sym) {
-                                       emit_local_symbol (acfg, debug_sym, NULL, TRUE);
-                                       emit_label (acfg, debug_sym);
-                               }
+                       if (debug_sym) {
+                               emit_local_symbol (acfg, debug_sym, NULL, TRUE);
+                               emit_label (acfg, debug_sym);
                        }
                }
 
-               /* 
-                * The first plt entry is used to transfer code to the AOT loader. 
-                */
                arch_emit_plt_entry (acfg, i);
 
                if (debug_sym) {
@@ -3711,13 +3908,17 @@ emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
        ji = info->ji;
        unwind_ops = info->unwind_ops;
 
+#ifdef __native_client_codegen__
+       mono_nacl_fix_patches (code, ji);
+#endif
+
        /* Emit code */
 
        sprintf (start_symbol, "%s", name);
 
        emit_section_change (acfg, ".text", 0);
        emit_global (acfg, start_symbol, TRUE);
-       emit_alignment (acfg, 16);
+       emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
        emit_label (acfg, start_symbol);
 
        sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
@@ -3751,7 +3952,7 @@ emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
 
        info_offset = add_to_blob (acfg, buf, p - buf);
 
-       emit_section_change (acfg, ".text", 0);
+       emit_section_change (acfg, RODATA_SECT, 0);
        emit_global (acfg, symbol, FALSE);
        emit_label (acfg, symbol);
 
@@ -3798,48 +3999,56 @@ emit_trampolines (MonoAotCompile *acfg)
                 * method.
                 */
                for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
-                       mono_arch_create_generic_trampoline_full (tramp_type, &info, TRUE);
+                       mono_arch_create_generic_trampoline (tramp_type, &info, TRUE);
                        emit_trampoline (acfg, acfg->got_offset, info);
                }
 
                mono_arch_get_nullified_class_init_trampoline (&info);
                emit_trampoline (acfg, acfg->got_offset, info);
 #if defined(MONO_ARCH_MONITOR_OBJECT_REG)
-               mono_arch_create_monitor_enter_trampoline_full (&info, TRUE);
+               mono_arch_create_monitor_enter_trampoline (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
-               mono_arch_create_monitor_exit_trampoline_full (&info, TRUE);
+               mono_arch_create_monitor_exit_trampoline (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
 #endif
 
-               mono_arch_create_generic_class_init_trampoline_full (&info, TRUE);
+               mono_arch_create_generic_class_init_trampoline (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
 
                /* Emit the exception related code pieces */
-               code = mono_arch_get_restore_context_full (&info, TRUE);
+               code = mono_arch_get_restore_context (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
-               code = mono_arch_get_call_filter_full (&info, TRUE);
+               code = mono_arch_get_call_filter (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
-               code = mono_arch_get_throw_exception_full (&info, TRUE);
+               code = mono_arch_get_throw_exception (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
-               code = mono_arch_get_rethrow_exception_full (&info, TRUE);
+               code = mono_arch_get_rethrow_exception (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
-               code = mono_arch_get_throw_corlib_exception_full (&info, TRUE);
+               code = mono_arch_get_throw_corlib_exception (&info, TRUE);
                emit_trampoline (acfg, acfg->got_offset, info);
 
-#if defined(TARGET_AMD64)
-               code = mono_arch_get_throw_pending_exception_full (&info, TRUE);
-               emit_trampoline (acfg, acfg->got_offset, info);
+#if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
+               {
+                       GSList *l = mono_arch_get_trampolines (TRUE);
+
+                       while (l) {
+                               MonoTrampInfo *info = l->data;
+
+                               emit_trampoline (acfg, acfg->got_offset, info);
+                               l = l->next;
+                       }
+               }
 #endif
 
                for (i = 0; i < 128; ++i) {
                        int offset;
 
                        offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
-                       code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &info, TRUE);
+                       code = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
                        emit_trampoline (acfg, acfg->got_offset, info);
 
                        offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
-                       code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &info, TRUE);
+                       code = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
                        emit_trampoline (acfg, acfg->got_offset, info);
                }
 
@@ -3908,7 +4117,7 @@ emit_trampolines (MonoAotCompile *acfg)
                        }
 
                        emit_global (acfg, symbol, TRUE);
-                       emit_alignment (acfg, 16);
+                       emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
                        emit_label (acfg, symbol);
 
                        acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
@@ -3932,6 +4141,10 @@ emit_trampolines (MonoAotCompile *acfg)
                                default:
                                        g_assert_not_reached ();
                                }
+#ifdef __native_client_codegen__
+                               /* align to avoid 32-byte boundary crossings */
+                               emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
+#endif
 
                                if (!acfg->trampoline_size [ntype]) {
                                        g_assert (tramp_size);
@@ -4270,7 +4483,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                                                        add_extra_method_with_depth (acfg, m, depth + 1);
                                                }
                                        }
-                                       add_generic_class (acfg, m->klass);
+                                       add_generic_class_with_depth (acfg, m->klass, depth + 5);
                                }
                                if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && !strcmp (m->name, "ElementAddr"))
                                        add_extra_method_with_depth (acfg, m, depth + 1);
@@ -4280,7 +4493,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                                MonoClass *klass = patch_info->data.klass;
 
                                if (klass->generic_class && !mono_generic_context_is_sharable (&klass->generic_class->context, FALSE))
-                                       add_generic_class (acfg, klass);
+                                       add_generic_class_with_depth (acfg, klass, depth + 5);
                                break;
                        }
                        default:
@@ -4294,6 +4507,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                switch (patch_info->type) {
                case MONO_PATCH_INFO_GOT_OFFSET:
                case MONO_PATCH_INFO_NONE:
+               case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
                        break;
                case MONO_PATCH_INFO_IMAGE:
                        /* The assembly is stored in GOT slot 0 */
@@ -4520,7 +4734,7 @@ char*
 mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
 {
        MonoJumpInfo *ji = mono_mempool_alloc (llvm_acfg->mempool, sizeof (MonoJumpInfo));
-       int offset;
+       MonoPltEntry *plt_entry;
 
        ji->type = type;
        ji->data.target = data;
@@ -4528,9 +4742,9 @@ mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
        if (!can_encode_patch (llvm_acfg, ji))
                return NULL;
 
-       offset = get_plt_offset (llvm_acfg, ji);
+       plt_entry = get_plt_entry (llvm_acfg, ji);
 
-       return g_strdup_printf ("%sp_%d", llvm_acfg->temp_prefix, offset);
+       return g_strdup_printf (plt_entry->symbol);
 }
 
 MonoJumpInfo*
@@ -4559,7 +4773,7 @@ emit_llvm_file (MonoAotCompile *acfg)
        char *command, *opts;
        int i;
        MonoJumpInfo *patch_info;
-       char *llc_target_args;
+       const char *llc_extra_args;
 
        /*
         * When using LLVM, we let llvm emit the got since the LLVM IL needs to refer
@@ -4574,7 +4788,7 @@ emit_llvm_file (MonoAotCompile *acfg)
                                        if (!is_plt_patch (patch_info))
                                                get_got_offset (acfg, patch_info);
                                        else
-                                               get_plt_offset (acfg, patch_info);
+                                               get_plt_entry (acfg, patch_info);
                                }
                        }
                }
@@ -4606,12 +4820,13 @@ emit_llvm_file (MonoAotCompile *acfg)
         * a lot of time, and doesn't seem to save much space.
         * The following optimizations cannot be enabled:
         * - 'tailcallelim'
+        * - 'jump-threading' changes our blockaddress references to int constants.
         * The opt list below was produced by taking the output of:
         * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
         * then removing tailcallelim + the global opts, and adding a second gvn.
         */
        opts = g_strdup ("-instcombine -simplifycfg");
-       opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -prune-eh -inline -functionattrs -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -jump-threading -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -jump-threading -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify");
+       opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -prune-eh -inline -functionattrs -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify");
 #if 1
        command = g_strdup_printf ("opt -f %s -o temp.opt.bc temp.bc", opts);
        printf ("Executing opt: %s\n", command);
@@ -4621,10 +4836,13 @@ emit_llvm_file (MonoAotCompile *acfg)
 #endif
        g_free (opts);
 
-       llc_target_args = g_strdup (LLC_TARGET_ARGS);
-
-       command = g_strdup_printf ("llc %s -f -relocation-model=pic -unwind-tables -o %s temp.opt.bc", llc_target_args, acfg->tmpfname);
-       g_free (llc_target_args);
+#if !LLVM_CHECK_VERSION(2, 8)
+       /* LLVM 2.8 removed the -f flag ??? */
+       llc_extra_args = "-f";
+#else
+       llc_extra_args = "";
+#endif
+       command = g_strdup_printf ("llc %s %s -relocation-model=pic -unwind-tables -o %s temp.opt.bc", LLC_TARGET_ARGS, llc_extra_args, acfg->tmpfname);
 
        printf ("Executing llc: %s\n", command);
 
@@ -4704,12 +4922,15 @@ emit_code (MonoAotCompile *acfg)
                        }
 
                        emit_section_change (acfg, ".text", 0);
+#ifdef __native_client_codegen__
+                       emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
+#endif
                        emit_global (acfg, symbol, TRUE);
                        emit_label (acfg, symbol);
 
                        sprintf (call_target, "%s", cfg->asm_symbol);
 
-                       arch_emit_unbox_trampoline (acfg, cfg->orig_method, cfg->generic_sharing_context, call_target);
+                       arch_emit_unbox_trampoline (acfg, cfg->orig_method, call_target);
                }
 
                if (cfg->compile_llvm)
@@ -4725,7 +4946,7 @@ emit_code (MonoAotCompile *acfg)
        emit_label (acfg, symbol);
 
        sprintf (symbol, "code_offsets");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -4765,7 +4986,7 @@ emit_info (MonoAotCompile *acfg)
        }
 
        sprintf (symbol, "method_info_offsets");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -4828,17 +5049,21 @@ mono_aot_method_hash (MonoMethod *method)
 {
        MonoMethodSignature *sig;
        MonoClass *klass;
-       int i;
+       int i, hindex;
        int hashes_count;
        guint32 *hashes_start, *hashes;
        guint32 a, b, c;
+       MonoGenericInst *ginst = NULL;
 
        /* Similar to the hash in mono_method_get_imt_slot () */
 
        sig = mono_method_signature (method);
 
-       hashes_count = sig->param_count + 5;
-       hashes_start = malloc (hashes_count * sizeof (guint32));
+       if (method->is_inflated)
+               ginst = ((MonoMethodInflated*)method)->context.method_inst;
+
+       hashes_count = sig->param_count + 5 + (ginst ? ginst->type_argc : 0);
+       hashes_start = g_malloc0 (hashes_count * sizeof (guint32));
        hashes = hashes_start;
 
        /* Some wrappers are assigned to random classes */
@@ -4864,10 +5089,16 @@ mono_aot_method_hash (MonoMethod *method)
                hashes [2] = mono_metadata_str_hash (method->name);
        hashes [3] = method->wrapper_type;
        hashes [4] = mono_aot_type_hash (sig->ret);
+       hindex = 5;
        for (i = 0; i < sig->param_count; i++) {
-               hashes [5 + i] = mono_aot_type_hash (sig->params [i]);
+               hashes [hindex ++] = mono_aot_type_hash (sig->params [i]);
        }
-       
+       if (ginst) {
+               for (i = 0; i < ginst->type_argc; ++i)
+                       hashes [hindex ++] = mono_aot_type_hash (ginst->type_argv [i]);
+       }               
+       g_assert (hindex == hashes_count);
+
        /* Setup internal state */
        a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
 
@@ -5010,7 +5241,7 @@ emit_extra_methods (MonoAotCompile *acfg)
                if (!cfg)
                        continue;
 
-               buf_size = 512;
+               buf_size = 10240;
                p = buf = g_malloc (buf_size);
 
                nmethods ++;
@@ -5105,7 +5336,7 @@ emit_extra_methods (MonoAotCompile *acfg)
 
        /* Emit the table */
        sprintf (symbol, "extra_method_table");
-       emit_section_change (acfg, ".text", 0);
+       emit_section_change (acfg, RODATA_SECT, 0);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5134,7 +5365,7 @@ emit_extra_methods (MonoAotCompile *acfg)
         * This is used by mono_aot_find_jit_info ().
         */
        sprintf (symbol, "extra_method_info_offsets");
-       emit_section_change (acfg, ".text", 0);
+       emit_section_change (acfg, RODATA_SECT, 0);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5166,7 +5397,7 @@ emit_exception_info (MonoAotCompile *acfg)
        }
 
        sprintf (symbol, "ex_info_offsets");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5188,7 +5419,7 @@ emit_unwind_info (MonoAotCompile *acfg)
         */
 
        sprintf (symbol, "unwind_info");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
        emit_global (acfg, symbol, FALSE);
@@ -5223,7 +5454,7 @@ emit_class_info (MonoAotCompile *acfg)
                offsets [i] = emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
 
        sprintf (symbol, "class_info_offsets");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5258,6 +5489,8 @@ emit_class_name_table (MonoAotCompile *acfg)
        for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
                token = MONO_TOKEN_TYPE_DEF | (i + 1);
                klass = mono_class_get (acfg->image, token);
+               if (!klass)
+                       continue;
                full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
                hash = mono_metadata_str_hash (full_name) % table_size;
                g_free (full_name);
@@ -5282,7 +5515,7 @@ emit_class_name_table (MonoAotCompile *acfg)
 
        /* Emit the table */
        sprintf (symbol, "class_name_table");
-       emit_section_change (acfg, ".text", 0);
+       emit_section_change (acfg, RODATA_SECT, 0);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5319,7 +5552,7 @@ emit_image_table (MonoAotCompile *acfg)
         */
 
        sprintf (symbol, "mono_image_table");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5358,9 +5591,9 @@ emit_got_info (MonoAotCompile *acfg)
        acfg->plt_got_offset_base = acfg->got_offset;
        first_plt_got_patch = acfg->got_patches->len;
        for (i = 1; i < acfg->plt_offset; ++i) {
-               MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
+               MonoPltEntry *plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
 
-               g_ptr_array_add (acfg->got_patches, patch_info);
+               g_ptr_array_add (acfg->got_patches, plt_entry->ji);
        }
 
        acfg->got_offset += acfg->plt_offset;
@@ -5402,7 +5635,7 @@ emit_got_info (MonoAotCompile *acfg)
 
        /* Emit got_info_offsets table */
        sprintf (symbol, "got_info_offsets");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5485,7 +5718,7 @@ emit_globals_table (MonoAotCompile *acfg)
 
        /* Emit the table */
        sprintf (symbol, ".Lglobals_hash");
-       emit_section_change (acfg, ".text", 0);
+       emit_section_change (acfg, RODATA_SECT, 0);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
 
@@ -5512,7 +5745,7 @@ emit_globals_table (MonoAotCompile *acfg)
                char *name = g_ptr_array_index (acfg->globals, i);
 
                sprintf (symbol, "name_%d", i);
-               emit_section_change (acfg, ".text", 1);
+               emit_section_change (acfg, RODATA_SECT, 1);
                emit_label (acfg, symbol);
                emit_string (acfg, name);
        }
@@ -5574,7 +5807,7 @@ emit_globals (MonoAotCompile *acfg)
                 * Emit a global symbol which can be passed by an embedding app to
                 * mono_aot_register_module ().
                 */
-#if defined(__MACH__)
+#if defined(__MACH__) && !defined(__native_client_codegen__)
                sprintf (symbol, "_mono_aot_module_%s_info", acfg->image->assembly->aname.name);
 #else
                sprintf (symbol, "mono_aot_module_%s_info", acfg->image->assembly->aname.name);
@@ -5634,6 +5867,15 @@ emit_file_info (MonoAotCompile *acfg)
 {
        char symbol [128];
        int i;
+       int gc_name_offset;
+       const char *gc_name;
+
+       /*
+        * The managed allocators are GC specific, so can't use an AOT image created by one GC
+        * in another.
+        */
+       gc_name = mono_gc_get_gc_name ();
+       gc_name_offset = add_to_blob (acfg, (guint8*)gc_name, strlen (gc_name) + 1);
 
        sprintf (symbol, "mono_aot_file_info");
        emit_section_change (acfg, ".data", 0);
@@ -5641,13 +5883,14 @@ emit_file_info (MonoAotCompile *acfg)
        emit_label (acfg, symbol);
        emit_global (acfg, symbol, FALSE);
 
-       /* The data emitted here must match MonoAotFileInfo in aot-runtime.c. */
+       /* The data emitted here must match MonoAotFileInfo. */
        emit_int32 (acfg, acfg->plt_got_offset_base);
        emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
        emit_int32 (acfg, acfg->plt_offset);
        emit_int32 (acfg, acfg->nmethods);
        emit_int32 (acfg, acfg->flags);
        emit_int32 (acfg, acfg->opts);
+       emit_int32 (acfg, gc_name_offset);
 
        for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
                emit_int32 (acfg, acfg->num_trampolines [i]);
@@ -5663,7 +5906,7 @@ emit_blob (MonoAotCompile *acfg)
        char symbol [128];
 
        sprintf (symbol, "blob");
-       emit_section_change (acfg, ".text", 1);
+       emit_section_change (acfg, RODATA_SECT, 1);
        emit_global (acfg, symbol, FALSE);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
@@ -5826,10 +6069,18 @@ compile_asm (MonoAotCompile *acfg)
 #define LD_OPTIONS "-m elf64ppc"
 #elif defined(sparc) && SIZEOF_VOID_P == 8
 #define AS_OPTIONS "-xarch=v9"
+#elif defined(TARGET_X86) && defined(__APPLE__) && !defined(__native_client_codegen__)
+#define AS_OPTIONS "-arch i386 -W"
 #else
 #define AS_OPTIONS ""
 #endif
 
+#ifdef __native_client_codegen__
+#define AS_NAME "nacl-as"
+#else
+#define AS_NAME "as"
+#endif
+
 #ifndef LD_OPTIONS
 #define LD_OPTIONS ""
 #endif
@@ -5855,7 +6106,7 @@ compile_asm (MonoAotCompile *acfg)
        } else {
                objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
        }
-       command = g_strdup_printf ("%sas %s %s -o %s", tool_prefix, AS_OPTIONS, acfg->tmpfname, objfile);
+       command = g_strdup_printf ("%s%s %s %s -o %s", tool_prefix, AS_NAME, AS_OPTIONS, acfg->tmpfname, objfile);
        printf ("Executing the native assembler: %s\n", command);
        if (system (command) != 0) {
                g_free (command);
@@ -5885,6 +6136,8 @@ compile_asm (MonoAotCompile *acfg)
        command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
 #elif defined(HOST_WIN32)
        command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
+#elif defined(TARGET_X86) && defined(__APPLE__) && !defined(__native_client_codegen__)
+       command = g_strdup_printf ("gcc -m32 -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
 #else
        command = g_strdup_printf ("%sld %s %s -shared -o %s %s.o", tool_prefix, EH_LD_OPTIONS, LD_OPTIONS, tmp_outfile_name, acfg->tmpfname);
 #endif
@@ -5945,8 +6198,8 @@ acfg_create (MonoAssembly *ass, guint32 opts)
        acfg->methods = g_ptr_array_new ();
        acfg->method_indexes = g_hash_table_new (NULL, NULL);
        acfg->method_depth = g_hash_table_new (NULL, NULL);
-       acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
-       acfg->patch_to_plt_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
+       acfg->plt_offset_to_entry = g_hash_table_new (NULL, NULL);
+       acfg->patch_to_plt_entry = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
        acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
        acfg->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
        for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
@@ -5989,8 +6242,8 @@ acfg_free (MonoAotCompile *acfg)
        g_ptr_array_free (acfg->unwind_ops, TRUE);
        g_hash_table_destroy (acfg->method_indexes);
        g_hash_table_destroy (acfg->method_depth);
-       g_hash_table_destroy (acfg->plt_offset_to_patch);
-       g_hash_table_destroy (acfg->patch_to_plt_offset);
+       g_hash_table_destroy (acfg->plt_offset_to_entry);
+       g_hash_table_destroy (acfg->patch_to_plt_entry);
        g_hash_table_destroy (acfg->patch_to_got_offset);
        g_hash_table_destroy (acfg->method_to_cfg);
        g_hash_table_destroy (acfg->token_info_hash);
@@ -6053,11 +6306,11 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                }
        }
 
-#ifdef ENABLE_LLVM
-       acfg->llvm = TRUE;
-       acfg->aot_opts.asm_writer = TRUE;
-       acfg->flags |= MONO_AOT_FILE_FLAG_WITH_LLVM;
-#endif
+       if (mono_use_llvm) {
+               acfg->llvm = TRUE;
+               acfg->aot_opts.asm_writer = TRUE;
+               acfg->flags |= MONO_AOT_FILE_FLAG_WITH_LLVM;
+       }
 
        if (acfg->aot_opts.full_aot)
                acfg->flags |= MONO_AOT_FILE_FLAG_FULL_AOT;
@@ -6085,6 +6338,16 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
 
        acfg->temp_prefix = img_writer_get_temp_label_prefix (NULL);
 
+       /*
+        * The prefix LLVM likes to put in front of symbol names on darwin.
+        * The mach-os specs require this for globals, but LLVM puts them in front of all
+        * symbols. We need to handle this, since we need to refer to LLVM generated
+        * symbols.
+        */
+       acfg->llvm_label_prefix = "";
+       if (acfg->llvm)
+               acfg->llvm_label_prefix = LLVM_LABEL_PREFIX;
+
        acfg->method_index = 1;
 
        collect_methods (acfg);
@@ -6096,8 +6359,10 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
        acfg->plt_offset = 1;
 
 #ifdef ENABLE_LLVM
-       llvm_acfg = acfg;
-       mono_llvm_create_aot_module (acfg->got_symbol_base);
+       if (acfg->llvm) {
+               llvm_acfg = acfg;
+               mono_llvm_create_aot_module (acfg->got_symbol_base);
+       }
 #endif
 
        /* GOT offset 0 is reserved for the address of the current assembly */
@@ -6114,6 +6379,11 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
                ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
                get_got_offset (acfg, ji);
+
+               /* This is very common */
+               ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
+               ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
+               get_got_offset (acfg, ji);
        }
 
        TV_GETTIME (atv);
@@ -6136,9 +6406,9 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                } else {
                        acfg->tmpfname = g_strdup ("temp.s");
                }
-       }
 
-       emit_llvm_file (acfg);
+               emit_llvm_file (acfg);
+       }
 #endif
 
        if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
@@ -6185,16 +6455,6 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                outfile_name = NULL;
        }
 
-       /*
-        * The prefix LLVM likes to put in front of symbol names on darwin.
-        * The mach-os specs require this for globals, but LLVM puts them in front of all
-        * symbols. We need to handle this, since we need to refer to LLVM generated
-        * symbols.
-        */
-       acfg->llvm_label_prefix = "";
-       if (acfg->llvm)
-               acfg->llvm_label_prefix = LLVM_LABEL_PREFIX;
-
        acfg->got_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, acfg->got_symbol_base);
 
        /* Compute symbols for methods */