[jit] Disable the optimization added by 63eb94e451a59491034516f2ec9f27d586b34d86...
[mono.git] / mono / mini / aot-compiler.c
old mode 100644 (file)
new mode 100755 (executable)
index 4de2bfc..6aef6f1
@@ -71,6 +71,8 @@
 
 #if defined(__linux__) || defined(__native_client_codegen__)
 #define RODATA_SECT ".rodata"
+#elif defined(TARGET_MACH)
+#define RODATA_SECT ".section __TEXT, __const"
 #else
 #define RODATA_SECT ".text"
 #endif
@@ -186,10 +188,12 @@ typedef struct MonoAotCompile {
        guint32 final_got_size;
        /* Number of GOT entries reserved for trampolines */
        guint32 num_trampoline_got_entries;
+       guint32 tramp_page_size;
 
        guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
        guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
        guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
+       guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
 
        MonoAotOptions aot_opts;
        guint32 nmethods;
@@ -231,6 +235,9 @@ typedef struct MonoAotCompile {
        GHashTable *dwarf_ln_filenames;
        gboolean global_symbols;
        gboolean direct_method_addresses;
+       int objc_selector_index, objc_selector_index_2;
+       GPtrArray *objc_selectors;
+       GHashTable *objc_selector_to_index;
 } MonoAotCompile;
 
 typedef struct {
@@ -529,6 +536,26 @@ encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
        *endbuf = p;
 }
 
+static void
+emit_unset_mode (MonoAotCompile *acfg)
+{
+       img_writer_emit_unset_mode (acfg->w);
+}
+
+static G_GNUC_UNUSED void
+emit_set_thumb_mode (MonoAotCompile *acfg)
+{
+       emit_unset_mode (acfg);
+       fprintf (acfg->fp, ".code 16\n");
+}
+
+static G_GNUC_UNUSED void
+emit_set_arm_mode (MonoAotCompile *acfg)
+{
+       emit_unset_mode (acfg);
+       fprintf (acfg->fp, ".code 32\n");
+}
+
 /* ARCHITECTURE SPECIFIC CODE */
 
 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC)
@@ -610,6 +637,15 @@ arch_init (MonoAotCompile *acfg)
        acfg->llvm_label_prefix = "";
        acfg->user_symbol_prefix = "";
 
+#if defined(TARGET_AMD64) && defined(TARGET_MACH)
+       /* osx contains an old as which doesn't support avx opcodes */
+       g_string_append (acfg->llc_args, "-mattr=-avx");
+#endif
+
+#if defined(TARGET_AMD64)
+       g_string_append (acfg->llc_args, " -march=x86-64");
+#endif
+
 #ifdef TARGET_ARM
        if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "darwin")) {
                g_string_append (acfg->llc_args, "-mattr=+v6");
@@ -652,12 +688,12 @@ arch_init (MonoAotCompile *acfg)
  * calling code.
  */
 static void
-arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, int *call_size)
+arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, MonoJumpInfo *ji, int *call_size)
 {
 #if defined(TARGET_X86) || defined(TARGET_AMD64)
        /* Need to make sure this is exactly 5 bytes long */
        if (external && !acfg->use_bin_writer) {
-               img_writer_emit_unset_mode (acfg->w);
+               emit_unset_mode (acfg);
                fprintf (acfg->fp, "call %s\n", target);
        } else {
                emit_byte (acfg, '\xe8');
@@ -675,7 +711,7 @@ arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean extern
                img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
                emit_bytes (acfg, buf, 4);
        } else {
-               img_writer_emit_unset_mode (acfg->w);
+               emit_unset_mode (acfg);
                fprintf (acfg->fp, "bl %s\n", target);
        }
        *call_size = 4;
@@ -683,7 +719,7 @@ arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean extern
        if (acfg->use_bin_writer) {
                g_assert_not_reached ();
        } else {
-               img_writer_emit_unset_mode (acfg->w);
+               emit_unset_mode (acfg);
                fprintf (acfg->fp, "bl %s\n", target);
                *call_size = 4;
        }
@@ -736,7 +772,7 @@ arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
 {
 #if defined(TARGET_POWERPC64)
        g_assert (!acfg->use_bin_writer);
-       img_writer_emit_unset_mode (acfg->w);
+       emit_unset_mode (acfg);
        /* 
         * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
         * unsupported relocations. So we store the got address into the .Lgot_addr
@@ -751,7 +787,7 @@ arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
        *code_size = 16;
 #elif defined(TARGET_POWERPC)
        g_assert (!acfg->use_bin_writer);
-       img_writer_emit_unset_mode (acfg->w);
+       emit_unset_mode (acfg);
        fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
        fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
        fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
@@ -807,6 +843,35 @@ arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *cod
 
 #endif
 
+/*
+ * arch_emit_objc_selector_ref:
+ *
+ *   Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
+ */
+static void
+arch_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
+{
+#if defined(TARGET_ARM)
+       char symbol1 [256];
+       char symbol2 [256];
+       int lindex = acfg->objc_selector_index_2 ++;
+
+       /* Emit ldr.imm/b */
+       emit_bytes (acfg, code, 8);
+
+       sprintf (symbol1, "L_OBJC_SELECTOR_%d", lindex);
+       sprintf (symbol2, "L_OBJC_SELECTOR_REFERENCES_%d", index);
+
+       emit_label (acfg, symbol1);
+       img_writer_emit_unset_mode (acfg->w);
+       fprintf (acfg->fp, ".long %s-(%s+12)", symbol2, symbol1);
+
+       *code_size = 12;
+#else
+       g_assert_not_reached ();
+#endif
+}
+
 /*
  * arch_emit_plt_entry:
  *
@@ -887,7 +952,7 @@ arch_emit_plt_entry (MonoAotCompile *acfg, int index)
 
                /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
                g_assert (!acfg->use_bin_writer);
-               img_writer_emit_unset_mode (acfg->w);
+               emit_unset_mode (acfg);
                fprintf (acfg->fp, "lis 11, %d@h\n", offset);
                fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
                fprintf (acfg->fp, "add 11, 11, 30\n");
@@ -919,13 +984,23 @@ arch_emit_llvm_plt_entry (MonoAotCompile *acfg, int index)
        /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
        /* The caller already transitioned to thumb */
        /* The code below should be 12 bytes long */
+       /* clang has trouble encoding these instructions, so emit the binary */
+#if 0
        fprintf (acfg->fp, "ldr ip, [pc, #8]\n");
        /* thumb can't encode ld pc, [pc, ip] */
        fprintf (acfg->fp, "add ip, pc, ip\n");
        fprintf (acfg->fp, "ldr ip, [ip, #0]\n");
        fprintf (acfg->fp, "bx ip\n");
+#endif
+       emit_set_thumb_mode (acfg);
+       fprintf (acfg->fp, ".4byte 0xc008f8df\n");
+       fprintf (acfg->fp, ".2byte 0x44fc\n");
+       fprintf (acfg->fp, ".4byte 0xc000f8dc\n");
+       fprintf (acfg->fp, ".2byte 0x4760\n");
        emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) + 4);
        emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+       emit_unset_mode (acfg);
+       emit_set_arm_mode (acfg);
 #else
        g_assert_not_reached ();
 #endif
@@ -961,6 +1036,8 @@ arch_emit_specific_trampoline_pages (MonoAotCompile *acfg)
        if (!acfg->aot_opts.use_trampolines_page)
                return;
 
+       acfg->tramp_page_size = mono_pagesize ();
+
        sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
        emit_alignment (acfg, mono_pagesize ());
        emit_global (acfg, symbol, TRUE);
@@ -1233,7 +1310,7 @@ arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size
         * in the second got slot of every aot image. The caller already computed
         * the address of its got and placed it into r30.
         */
-       img_writer_emit_unset_mode (acfg->w);
+       emit_unset_mode (acfg);
        /* Load mscorlib got address */
        fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
        /* Load generic trampoline address */
@@ -1465,7 +1542,7 @@ arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_
         * in the second got slot of every aot image. The caller already computed
         * the address of its got and placed it into r30.
         */
-       img_writer_emit_unset_mode (acfg->w);
+       emit_unset_mode (acfg);
        /* Load mscorlib got address */
        fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
        /* Load rgctx */
@@ -1879,7 +1956,7 @@ arch_emit_autoreg (MonoAotCompile *acfg, char *symbol)
 {
 #if defined(TARGET_POWERPC) && defined(__mono_ilp32__)
        /* Based on code generated by gcc */
-       img_writer_emit_unset_mode (acfg->w);
+       emit_unset_mode (acfg);
 
        fprintf (acfg->fp,
 #if defined(_MSC_VER) || defined(MONO_CROSS_COMPILE) 
@@ -2559,6 +2636,8 @@ encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8
                                encode_klass_ref (acfg, method->klass, p, &p);
                        else if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER)
                                encode_method_ref (acfg, info->d.synchronized_inner.method, p, &p);
+                       else if (info->subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR)
+                               encode_method_ref (acfg, info->d.array_accessor.method, p, &p);
                        break;
                }
                case MONO_WRAPPER_MANAGED_TO_NATIVE: {
@@ -2629,8 +2708,11 @@ encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8
                                encode_klass_ref (acfg, method->klass, p, &p);
                        } else {
                                MonoMethodSignature *sig = mono_method_signature (method);
+                               WrapperInfo *info = mono_marshal_get_wrapper_info (method);
 
                                encode_value (0, p, &p);
+                               if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
+                                       encode_value (info ? info->subtype : 0, p, &p);
                                encode_signature (acfg, sig, p, &p);
                        }
                        break;
@@ -3262,10 +3344,6 @@ add_wrappers (MonoAotCompile *acfg)
                add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
 #endif
 
-               /* JIT icall wrappers */
-               /* FIXME: locking */
-               g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
-
                /* stelemref */
                add_method (acfg, mono_marshal_get_stelemref ());
 
@@ -3329,6 +3407,10 @@ add_wrappers (MonoAotCompile *acfg)
                        }
                }
 #endif
+
+               /* JIT icall wrappers */
+               /* FIXME: locking */
+               g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
        }
 
        /* 
@@ -3442,13 +3524,62 @@ add_wrappers (MonoAotCompile *acfg)
                }
        }
 
+       /* array access wrappers */
+       for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
+               MonoClass *klass;
+               
+               token = MONO_TOKEN_TYPE_SPEC | (i + 1);
+               klass = mono_class_get (acfg->image, token);
+
+               if (!klass) {
+                       mono_loader_clear_error ();
+                       continue;
+               }
+
+               if (klass->rank && MONO_TYPE_IS_PRIMITIVE (&klass->element_class->byval_arg)) {
+                       MonoMethod *m, *wrapper;
+
+                       /* Add runtime-invoke wrappers too */
+
+                       m = mono_class_get_method_from_name (klass, "Get", -1);
+                       g_assert (m);
+                       wrapper = mono_marshal_get_array_accessor_wrapper (m);
+                       add_extra_method (acfg, wrapper);
+                       add_extra_method (acfg, mono_marshal_get_runtime_invoke (wrapper, FALSE));
+
+                       m = mono_class_get_method_from_name (klass, "Set", -1);
+                       g_assert (m);
+                       wrapper = mono_marshal_get_array_accessor_wrapper (m);
+                       add_extra_method (acfg, wrapper);
+                       add_extra_method (acfg, mono_marshal_get_runtime_invoke (wrapper, FALSE));
+               }
+       }
+
        /* Synchronized wrappers */
        for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
                token = MONO_TOKEN_METHOD_DEF | (i + 1);
                method = mono_get_method (acfg->image, token, NULL);
 
-               if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED && !method->is_generic)
-                       add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
+               if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
+                       if (method->is_generic) {
+                               // FIXME:
+                       } else if (method->klass->generic_container) {
+                               MonoGenericContext ctx;
+                               MonoMethod *inst, *gshared, *m;
+
+                               /*
+                                * Create a generic wrapper for a generic instance, and AOT that.
+                                */
+                               create_gsharedvt_inst (acfg, method, &ctx);
+                               inst = mono_class_inflate_generic_method (method, &ctx);        
+                               m = mono_marshal_get_synchronized_wrapper (inst);
+                               g_assert (m->is_inflated);
+                               gshared = mini_get_shared_method_full (m, FALSE, TRUE);
+                               add_method (acfg, gshared);
+                       } else {
+                               add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
+                       }
+               }
        }
 
        /* pinvoke wrappers */
@@ -3613,6 +3744,8 @@ has_type_vars (MonoClass *klass)
                                        return TRUE;
                }
        }
+       if (klass->generic_container)
+               return TRUE;
        return FALSE;
 }
 
@@ -3741,6 +3874,13 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
 
        iter = NULL;
        while ((method = mono_class_get_methods (klass, &iter))) {
+               if ((acfg->opts & MONO_OPT_GSHAREDVT) && method->is_inflated && mono_method_get_context (method)->method_inst) {
+                       /*
+                        * This is partial sharing, and we can't handle it yet
+                        */
+                       continue;
+               }
+               
                if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, use_gsharedvt))
                        /* Already added */
                        continue;
@@ -3904,6 +4044,8 @@ add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
                for (j = 0; j < header->num_locals; ++j)
                        if (header->locals [j]->type == MONO_TYPE_GENERICINST)
                                add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
+       } else {
+               mono_loader_clear_error ();
        }
 }
 
@@ -4249,6 +4391,9 @@ compute_line_numbers (MonoMethod *method, int code_size, MonoDebugMethodJitInfo
        minfo = mono_debug_lookup_method (method);
        if (!minfo)
                return NULL;
+       // FIXME: This seems to happen when two methods have the same cfg->method_to_register
+       if (debug_info->code_size != code_size)
+               return NULL;
 
        g_assert (code_size);
 
@@ -4324,8 +4469,8 @@ get_file_index (MonoAotCompile *acfg, const char *source_file)
        if (!findex) {
                findex = g_hash_table_size (acfg->dwarf_ln_filenames) + 1;
                g_hash_table_insert (acfg->dwarf_ln_filenames, g_strdup (source_file), GINT_TO_POINTER (findex));
-               img_writer_emit_unset_mode (acfg->w);
-               fprintf (acfg->fp, ".file %d \"%s\"\n", findex, source_file);
+               emit_unset_mode (acfg);
+               fprintf (acfg->fp, ".file %d \"%s\"\n", findex, mono_dwarf_escape_path (source_file));
        }
        return findex;
 }
@@ -4348,7 +4493,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
        MonoDebugSourceLocation **locs = NULL;
        gboolean skip, direct_call, external_call;
        guint32 got_slot;
-       const char *direct_call_target;
+       const char *direct_call_target = 0;
        const char *direct_pinvoke;
 
        if (method) {
@@ -4361,7 +4506,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                locs = compute_line_numbers (method, code_len, debug_info);
                if (!locs) {
                        int findex = get_file_index (acfg, "<unknown>");
-                       img_writer_emit_unset_mode (acfg->w);
+                       emit_unset_mode (acfg);
                        fprintf (acfg->fp, ".loc %d %d 0\n", findex, 1);
                }
        }
@@ -4386,13 +4531,13 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                        int findex;
 
                        findex = get_file_index (acfg, loc->source_file);
-                       img_writer_emit_unset_mode (acfg->w);
+                       emit_unset_mode (acfg);
                        fprintf (acfg->fp, ".loc %d %d 0\n", findex, loc->row);
                        mono_debug_symfile_free_location (loc);
                }
 
-#ifdef MONO_ARCH_AOT_SUPPORTED
                skip = FALSE;
+#ifdef MONO_ARCH_AOT_SUPPORTED
                if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
                        start_index = pindex;
 
@@ -4408,6 +4553,30 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                                patch_info->type = MONO_PATCH_INFO_NONE;
                                break;
                        }
+                       case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
+                               int code_size, index;
+                               char *selector = (void*)patch_info->data.target;
+
+                               if (!acfg->objc_selector_to_index)
+                                       acfg->objc_selector_to_index = g_hash_table_new (g_str_hash, g_str_equal);
+                               if (!acfg->objc_selectors)
+                                       acfg->objc_selectors = g_ptr_array_new ();
+                               index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->objc_selector_to_index, selector));
+                               if (index)
+                                       index --;
+                               else {
+                                       index = acfg->objc_selector_index;
+                                       g_ptr_array_add (acfg->objc_selectors, (void*)patch_info->data.target);
+                                       g_hash_table_insert (acfg->objc_selector_to_index, selector, GUINT_TO_POINTER (index + 1));
+                                       acfg->objc_selector_index ++;
+                               }
+
+                               arch_emit_objc_selector_ref (acfg, code + i, index, &code_size);
+                               i += code_size - 1;
+                               skip = TRUE;
+                               patch_info->type = MONO_PATCH_INFO_NONE;
+                               break;
+                       }
                        default: {
                                /*
                                 * If this patch is a call, try emitting a direct call instead of
@@ -4481,7 +4650,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                                if (direct_call) {
                                        int call_size;
 
-                                       arch_emit_direct_call (acfg, direct_call_target, external_call, &call_size);
+                                       arch_emit_direct_call (acfg, direct_call_target, external_call, patch_info, &call_size);
                                        i += call_size - 1;
                                } else {
                                        int code_size;
@@ -4644,7 +4813,7 @@ emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
 
        acfg->cfgs [method_index]->got_offset = acfg->got_offset;
 
-       emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
+       emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE, mono_debug_find_method (cfg->jit_info->d.method, mono_domain_get ()));
 
        emit_line (acfg);
 
@@ -5104,8 +5273,18 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                                encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
                        else {
                                if (ei->data.catch_class) {
-                                       encode_value (1, p, &p);
-                                       encode_klass_ref (acfg, ei->data.catch_class, p, &p);
+                                       guint8 *buf2, *p2;
+                                       int len;
+
+                                       buf2 = g_malloc (4096);
+                                       p2 = buf2;
+                                       encode_klass_ref (acfg, ei->data.catch_class, p2, &p2);
+                                       len = p2 - buf2;
+                                       g_assert (len < 4096);
+                                       encode_value (len, p, &p);
+                                       memcpy (p, buf2, len);
+                                       p += p2 - buf2;
+                                       g_free (buf2);
                                } else {
                                        encode_value (0, p, &p);
                                }
@@ -5117,10 +5296,29 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                }
        }
 
+       if (jinfo->has_try_block_holes) {
+               MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
+               for (i = 0; i < table->num_holes; ++i) {
+                       MonoTryBlockHoleJitInfo *hole = &table->holes [i];
+                       encode_value (hole->clause, p, &p);
+                       encode_value (hole->length, p, &p);
+                       encode_value (hole->offset, p, &p);
+               }
+       }
+
+       if (jinfo->has_arch_eh_info) {
+               MonoArchEHJitInfo *eh_info;
+
+               eh_info = mono_jit_info_get_arch_eh_info (jinfo);
+               encode_value (eh_info->stack_size, p, &p);
+       }
+
        if (jinfo->has_generic_jit_info) {
                MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
                MonoGenericSharingContext* gsctx = gi->generic_sharing_context;
                guint8 *p1;
+               guint8 *buf2, *p2;
+               int len;
 
                p1 = p;
                encode_value (gi->nlocs, p, &p);
@@ -5150,15 +5348,23 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                 * Need to encode jinfo->method too, since it is not equal to 'method'
                 * when using generic sharing.
                 */
-               encode_method_ref (acfg, jinfo->method, p, &p);
+               buf2 = g_malloc (4096);
+               p2 = buf2;
+               encode_method_ref (acfg, jinfo->d.method, p2, &p2);
+               len = p2 - buf2;
+               g_assert (len < 4096);
+               encode_value (len, p, &p);
+               memcpy (p, buf2, len);
+               p += p2 - buf2;
+               g_free (buf2);
 
                if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
                        MonoMethodInflated *inflated;
                        MonoGenericContext *context;
                        MonoGenericInst *inst;
 
-                       g_assert (jinfo->method->is_inflated);
-                       inflated = (MonoMethodInflated*)jinfo->method;
+                       g_assert (jinfo->d.method->is_inflated);
+                       inflated = (MonoMethodInflated*)jinfo->d.method;
                        context = &inflated->context;
 
                        encode_value (1, p, &p);
@@ -5185,23 +5391,6 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                }
        }
 
-       if (jinfo->has_try_block_holes) {
-               MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
-               for (i = 0; i < table->num_holes; ++i) {
-                       MonoTryBlockHoleJitInfo *hole = &table->holes [i];
-                       encode_value (hole->clause, p, &p);
-                       encode_value (hole->length, p, &p);
-                       encode_value (hole->offset, p, &p);
-               }
-       }
-
-       if (jinfo->has_arch_eh_info) {
-               MonoArchEHJitInfo *eh_info;
-
-               eh_info = mono_jit_info_get_arch_eh_info (jinfo);
-               encode_value (eh_info->stack_size, p, &p);
-       }
-
        if (seq_points) {
                int il_offset, native_offset, last_il_offset, last_native_offset, j;
 
@@ -5216,6 +5405,7 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
                        last_il_offset = il_offset;
                        last_native_offset = native_offset;
 
+                       encode_value (sp->flags, p, &p);
                        encode_value (sp->next_len, p, &p);
                        for (j = 0; j < sp->next_len; ++j)
                                encode_value (sp->next [j], p, &p);
@@ -5427,11 +5617,12 @@ emit_plt (MonoAotCompile *acfg)
                                if (callee_cfg) {
                                        if (acfg->thumb_mixed && !callee_cfg->compile_llvm) {
                                                /* LLVM calls the PLT entries using bl, so emit a stub */
+                                               emit_set_thumb_mode (acfg);
                                                fprintf (acfg->fp, "\n.thumb_func\n");
                                                emit_label (acfg, plt_entry->llvm_symbol);
                                                fprintf (acfg->fp, "bx pc\n");
                                                fprintf (acfg->fp, "nop\n");
-                                               fprintf (acfg->fp, ".arm\n");
+                                               emit_set_arm_mode (acfg);
                                                fprintf (acfg->fp, "b %s\n", callee_cfg->asm_symbol);
                                        } else {
                                                fprintf (acfg->fp, "\n.set %s, %s\n", plt_entry->llvm_symbol, callee_cfg->asm_symbol);
@@ -5452,7 +5643,7 @@ emit_plt (MonoAotCompile *acfg)
 
                if (debug_sym) {
                        if (acfg->need_no_dead_strip) {
-                               img_writer_emit_unset_mode (acfg->w);
+                               emit_unset_mode (acfg);
                                fprintf (acfg->fp, "    .no_dead_strip %s\n", debug_sym);
                        }
                        emit_local_symbol (acfg, debug_sym, NULL, TRUE);
@@ -5547,6 +5738,8 @@ emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info,
        MonoJumpInfo *ji;
        GSList *unwind_ops;
 
+       g_assert (info);
+
        name = info->name;
        code = info->code;
        code_size = info->code_size;
@@ -5672,6 +5865,14 @@ emit_trampolines (MonoAotCompile *acfg)
                 */
                for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
                        /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
+#ifdef DISABLE_REMOTING
+                       if (tramp_type == MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING)
+                               continue;
+#endif
+#ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
+                       if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
+                               continue;
+#endif
                        mono_arch_create_generic_trampoline (tramp_type, &info, acfg->aot_opts.use_trampolines_page? 2: TRUE);
                        emit_trampoline (acfg, acfg->got_offset, info);
                }
@@ -5853,6 +6054,7 @@ emit_trampolines (MonoAotCompile *acfg)
                        }
 
                        emit_label (acfg, end_symbol);
+                       emit_int32 (acfg, 0);
                }
 
                arch_emit_specific_trampoline_pages (acfg);
@@ -6124,6 +6326,7 @@ can_encode_method (MonoAotCompile *acfg, MonoMethod *method)
                        case MONO_WRAPPER_DELEGATE_INVOKE:
                        case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
                        case MONO_WRAPPER_DELEGATE_END_INVOKE:
+                       case MONO_WRAPPER_SYNCHRONIZED:
                                break;
                        case MONO_WRAPPER_MANAGED_TO_MANAGED:
                        case MONO_WRAPPER_CASTCLASS: {
@@ -6248,6 +6451,8 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
         * does not need to support them by creating a fake GOT etc.
         */
        cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
+       mono_loader_clear_error ();
+
        if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
                if (acfg->aot_opts.print_skipped_methods)
                        printf ("Skip (gshared failure): %s (%s)\n", mono_method_full_name (method, TRUE), cfg->exception_message);
@@ -6268,6 +6473,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                mono_destroy_compile (cfg);
                return;
        }
+       cfg->method_index = index;
 
        /* Nullify patches which need no aot processing */
        for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
@@ -6638,6 +6844,13 @@ mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
 #endif
 }
 
+int
+mono_aot_get_method_index (MonoMethod *method)
+{
+       g_assert (llvm_acfg);
+       return get_method_index (llvm_acfg, method);
+}
+
 MonoJumpInfo*
 mono_aot_patch_info_dup (MonoJumpInfo* ji)
 {
@@ -6724,7 +6937,7 @@ emit_llvm_file (MonoAotCompile *acfg)
        opts = g_strdup ("-instcombine -simplifycfg");
        opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -domfrontier -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -iv-users -indvars -loop-deletion -loop-simplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -simplifycfg -preverify -domtree -verify");
 #if 1
-       command = g_strdup_printf ("%sopt -f %s -o %s.opt.bc %s.bc", acfg->aot_opts.llvm_path, opts, acfg->tmpfname, acfg->tmpfname);
+       command = g_strdup_printf ("%sopt -f %s -o \"%s.opt.bc\" \"%s.bc\"", acfg->aot_opts.llvm_path, opts, acfg->tmpfname, acfg->tmpfname);
        printf ("Executing opt: %s\n", command);
        if (system (command) != 0) {
                exit (1);
@@ -6741,13 +6954,18 @@ emit_llvm_file (MonoAotCompile *acfg)
        if (acfg->aot_opts.mtriple)
                g_string_append_printf (acfg->llc_args, " -mtriple=%s", acfg->aot_opts.mtriple);
 
+#if defined(TARGET_MACH) && defined(TARGET_ARM)
+       /* ios requires PIC code now */
+       g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
+#else
        if (llvm_acfg->aot_opts.static_link)
                g_string_append_printf (acfg->llc_args, " -relocation-model=static");
        else
                g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
+#endif
        unlink (acfg->tmpfname);
 
-       command = g_strdup_printf ("%sllc %s -disable-gnu-eh-frame -enable-mono-eh-frame -o %s %s.opt.bc", acfg->aot_opts.llvm_path, acfg->llc_args->str, acfg->tmpfname, acfg->tmpfname);
+       command = g_strdup_printf ("%sllc %s -disable-gnu-eh-frame -enable-mono-eh-frame -o \"%s\" \"%s.opt.bc\"", acfg->aot_opts.llvm_path, acfg->llc_args->str, acfg->tmpfname, acfg->tmpfname);
 
        printf ("Executing llc: %s\n", command);
 
@@ -6831,12 +7049,18 @@ emit_code (MonoAotCompile *acfg)
                        emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
 #endif
 
-                       if (acfg->thumb_mixed && cfg->compile_llvm)
+                       if (acfg->thumb_mixed && cfg->compile_llvm) {
+                               emit_set_thumb_mode (acfg);
                                fprintf (acfg->fp, "\n.thumb_func\n");
+                       }
 
                        emit_label (acfg, symbol);
 
                        arch_emit_unbox_trampoline (acfg, cfg, cfg->orig_method, cfg->asm_symbol);
+
+                       if (acfg->thumb_mixed && cfg->compile_llvm) {
+                               emit_set_arm_mode (acfg);
+                       }
                }
 
                if (cfg->compile_llvm)
@@ -6876,11 +7100,11 @@ emit_code (MonoAotCompile *acfg)
                 * This is PIE code, and the linker can update it if needed.
                 */
                sprintf (symbol, "method_addresses");
-               emit_section_change (acfg, RODATA_SECT, 1);
+               emit_section_change (acfg, ".text", 1);
                emit_alignment (acfg, 8);
                emit_label (acfg, symbol);
                emit_local_symbol (acfg, symbol, "method_addresses_end", TRUE);
-               img_writer_emit_unset_mode (acfg->w);
+               emit_unset_mode (acfg);
                if (acfg->need_no_dead_strip)
                        fprintf (acfg->fp, "    .no_dead_strip %s\n", symbol);
 
@@ -6942,7 +7166,10 @@ emit_code (MonoAotCompile *acfg)
 
        /* Emit a sorted table mapping methods to their unbox trampolines */
        sprintf (symbol, "unbox_trampolines");
-       emit_section_change (acfg, RODATA_SECT, 1);
+       if (acfg->direct_method_addresses)
+               emit_section_change (acfg, ".text", 0);
+       else
+               emit_section_change (acfg, RODATA_SECT, 0);
        emit_alignment (acfg, 8);
        emit_label (acfg, symbol);
 
@@ -6965,7 +7192,7 @@ emit_code (MonoAotCompile *acfg)
 
                        emit_int32 (acfg, index);
                        if (acfg->direct_method_addresses) {
-                               img_writer_emit_unset_mode (acfg->w);
+                               emit_unset_mode (acfg);
                                if (acfg->thumb_mixed && cfg->compile_llvm)
                                        fprintf (acfg->fp, "\n\tblx %s\n", symbol);
                                else
@@ -6980,6 +7207,7 @@ emit_code (MonoAotCompile *acfg)
        }
        sprintf (symbol, "unbox_trampolines_end");
        emit_label (acfg, symbol);
+       emit_int32 (acfg, 0);
 }
 
 static void
@@ -7263,6 +7491,7 @@ emit_extra_methods (MonoAotCompile *acfg)
                value = get_method_index (acfg, method);
 
                hash = mono_aot_method_hash (method) % table_size;
+               //printf ("X: %s %d\n", mono_method_full_name (method, 1), hash);
 
                chain_lengths [hash] ++;
                max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
@@ -7900,6 +8129,10 @@ emit_file_info (MonoAotCompile *acfg)
        emit_int32 (acfg, __alignof__ (double));
        emit_int32 (acfg, __alignof__ (gint64));
 #endif
+       emit_int32 (acfg, MONO_TRAMPOLINE_NUM);
+       emit_int32 (acfg, acfg->tramp_page_size);
+       for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
+               emit_int32 (acfg, acfg->tramp_page_code_offsets [i]);
 
        if (acfg->aot_opts.static_link) {
                char *p;
@@ -7919,6 +8152,7 @@ emit_file_info (MonoAotCompile *acfg)
                }
                acfg->static_linking_symbol = g_strdup (symbol);
                emit_global_inner (acfg, symbol, FALSE);
+               emit_alignment (acfg, sizeof (gpointer));
                emit_label (acfg, symbol);
                emit_pointer_2 (acfg, acfg->user_symbol_prefix, "mono_aot_file_info");
        }
@@ -7937,6 +8171,45 @@ emit_blob (MonoAotCompile *acfg)
        emit_bytes (acfg, (guint8*)acfg->blob.data, acfg->blob.index);
 }
 
+static void
+emit_objc_selectors (MonoAotCompile *acfg)
+{
+       int i;
+
+       if (!acfg->objc_selectors || acfg->objc_selectors->len == 0)
+               return;
+
+       /*
+        * From
+        * cat > foo.m << EOF
+        * void *ret ()
+        * {
+        * return @selector(print:);
+        * }
+        * EOF
+        */
+
+       img_writer_emit_unset_mode (acfg->w);
+       g_assert (acfg->fp);
+       fprintf (acfg->fp, ".section    __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
+       fprintf (acfg->fp, ".align      2\n");
+       for (i = 0; i < acfg->objc_selectors->len; ++i) {
+               fprintf (acfg->fp, "L_OBJC_SELECTOR_REFERENCES_%d:\n", i);
+               fprintf (acfg->fp, ".long       L_OBJC_METH_VAR_NAME_%d\n", i);
+       }
+       fprintf (acfg->fp, ".section    __TEXT,__cstring,cstring_literals\n");
+       for (i = 0; i < acfg->objc_selectors->len; ++i) {
+               fprintf (acfg->fp, "L_OBJC_METH_VAR_NAME_%d:\n", i);
+               fprintf (acfg->fp, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg->objc_selectors, i));
+       }
+
+       fprintf (acfg->fp, ".section    __DATA,__objc_imageinfo,regular,no_dead_strip\n");
+       fprintf (acfg->fp, ".align      2\n");
+       fprintf (acfg->fp, "L_OBJC_IMAGE_INFO:\n");
+       fprintf (acfg->fp, ".long       0\n");
+       fprintf (acfg->fp, ".long       16\n");
+}
+
 static void
 emit_dwarf_info (MonoAotCompile *acfg)
 {
@@ -7957,7 +8230,7 @@ emit_dwarf_info (MonoAotCompile *acfg)
 
                sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
 
-               mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, cfg->asm_symbol, symbol2, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
+               mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, cfg->asm_symbol, symbol2, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->d.method, mono_domain_get ()));
        }
 #endif
 }
@@ -8186,7 +8459,7 @@ compile_asm (MonoAotCompile *acfg)
 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
        command = g_strdup_printf ("gcc --shared -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);
+       command = g_strdup_printf ("gcc -shared --dll -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
        command = g_strdup_printf ("gcc -m32 -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
 #else
@@ -8339,11 +8612,15 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
        int i, res;
        MonoAotCompile *acfg;
        char *outfile_name, *tmp_outfile_name, *p;
+       char llvm_stats_msg [256];
        TV_DECLARE (atv);
        TV_DECLARE (btv);
 
-#ifndef MONO_ARCH_GSHAREDVT_SUPPORTED
-       opts &= ~MONO_OPT_GSHAREDVT;
+#if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED) || (!defined(MONO_EXTENSIONS) && !defined(MONOTOUCH))
+       if (opts & MONO_OPT_GSHAREDVT) {
+               fprintf (stderr, "-O=gsharedvt not supported on this platform.\n");
+               exit (1);
+       }
 #endif
 
        printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
@@ -8358,7 +8635,7 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
        acfg->aot_opts.nrgctx_fetch_trampolines = 128;
        acfg->aot_opts.ngsharedvt_arg_trampolines = 128;
        acfg->aot_opts.llvm_path = g_strdup ("");
-#if MONOTOUCH
+#ifdef MONOTOUCH
        acfg->aot_opts.use_trampolines_page = TRUE;
 #endif
 
@@ -8554,10 +8831,10 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                                int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
                                acfg->fp = fdopen (i, "w+");
                        }
-                       if (acfg->fp == 0) {
-                               fprintf (stderr, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
-                               return 1;
-                       }
+               }
+               if (acfg->fp == 0) {
+                       fprintf (stderr, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
+                       return 1;
                }
                acfg->w = img_writer_create (acfg->fp, FALSE);
                
@@ -8645,6 +8922,8 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
 
        emit_blob (acfg);
 
+       emit_objc_selectors (acfg);
+
        emit_globals (acfg);
 
        emit_autoreg (acfg);
@@ -8670,9 +8949,14 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
        if (acfg->llvm)
                g_assert (acfg->got_offset <= acfg->final_got_size);
 
+       if (acfg->llvm)
+               sprintf (llvm_stats_msg, ", LLVM: %d (%d%%)", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
+       else
+               strcpy (llvm_stats_msg, "");
        printf ("Code: %d Info: %d Ex Info: %d Unwind Info: %d Class Info: %d PLT: %d GOT Info: %d GOT: %d Offsets: %d\n", acfg->stats.code_size, acfg->stats.info_size, acfg->stats.ex_info_size, acfg->stats.unwind_info_size, acfg->stats.class_info_size, acfg->plt_offset, acfg->stats.got_info_size, (int)(acfg->got_offset * sizeof (gpointer)), acfg->stats.offsets_size);
-       printf ("Compiled: %d/%d (%d%%), No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n", 
+       printf ("Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n", 
                        acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100,
+                       llvm_stats_msg,
                        acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100,
                        acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
        if (acfg->stats.genericcount)
@@ -8683,8 +8967,6 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
        if (acfg->stats.ocount)
                printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
-       if (acfg->llvm)
-               printf ("Methods compiled with LLVM: %d (%d%%)\n", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
 
        TV_GETTIME (atv);
        res = img_writer_emit_writeout (acfg->w);