Merge pull request #487 from mayerwin/patch-1
[mono.git] / mono / mini / tramp-arm.c
index bcfced8d20b359a594dc362a291e6854a0c6e786..1aeeeffa6bd5cb8f8a1c6a0977adbdb5c23c1100 100644 (file)
@@ -4,7 +4,9 @@
  * Authors:
  *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
+ * (C) 2001-2003 Ximian, Inc.
+ * Copyright 2003-2011 Novell Inc
+ * Copyright 2011 Xamarin Inc
  */
 
 #include <config.h>
 #include "mini.h"
 #include "mini-arm.h"
 
-static guint8* nullified_class_init_trampoline;
-
-/*
- * Return the instruction to jump from code to target, 0 if not
- * reachable with a single instruction
- */
-static guint32
-branch_for_target_reachable (guint8 *branch, guint8 *target)
-{
-       gint diff = target - branch - 8;
-       g_assert ((diff & 3) == 0);
-       if (diff >= 0) {
-               if (diff <= 33554431)
-                       return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
-       } else {
-               /* diff between 0 and -33554432 */
-               if (diff >= -33554432)
-                       return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
-       }
-       return 0;
-}
-
-/*
- * mono_arch_get_unbox_trampoline:
- * @gsctx: the generic sharing context
- * @m: method pointer
- * @addr: pointer to native code for @m
- *
- * when value type methods are called through the vtable we need to unbox the
- * this argument. This method returns a pointer to a trampoline which does
- * unboxing before calling the method
- */
-gpointer
-mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
-{
-       guint8 *code, *start;
-       int this_pos = 0;
-       MonoDomain *domain = mono_domain_get ();
-
-       if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
-               this_pos = 1;
-
-       mono_domain_lock (domain);
-       start = code = mono_code_manager_reserve (domain->code_mp, 16);
-       mono_domain_unlock (domain);
-
-       ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
-       ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
-       *(guint32*)code = (guint32)addr;
-       code += 4;
-       mono_arch_flush_icache (start, code - start);
-       g_assert ((code - start) <= 16);
-       /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
-       g_print ("unbox code is at %p for method at %p\n", start, addr);*/
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
-       return start;
-}
+static guint8* nullified_class_init_trampoline;
 
 void
 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
@@ -106,33 +53,85 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
 }
 
 void
-mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
+mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
 {
+       guint8 *jump_entry;
+
        /* Patch the jump table entry used by the plt entry */
-       guint32 offset = ((guint32*)code)[3];
-       guint8 *jump_entry = code + offset + 12;
+       if (*(guint32*)code == 0xe59fc000) {
+               /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
+               guint32 offset = ((guint32*)code)[2];
+               
+               jump_entry = code + offset + 12;
+       } else if (*(guint16*)(code - 4) == 0xf8df) {
+               /* 
+                * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
+                * mono_arm_get_thumb_plt_entry ().
+                */
+               guint32 offset;
+
+               code -= 4;
+               offset = *(guint32*)(code + 12);
+               jump_entry = code + offset + 8;
+       } else {
+               g_assert_not_reached ();
+       }
 
        *(guint8**)jump_entry = addr;
 }
 
 void
-mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
+mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
 {
        mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
 }
 
 void
-mono_arch_nullify_plt_entry (guint8 *code)
+mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
 {
        if (mono_aot_only && !nullified_class_init_trampoline)
-               nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
+               nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
 
-       mono_arch_patch_plt_entry (code, nullified_class_init_trampoline);
+       mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
+}
+
+#ifndef DISABLE_JIT
+
+#define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
+
+/*
+ * Return the instruction to jump from code to target, 0 if not
+ * reachable with a single instruction
+ */
+static guint32
+branch_for_target_reachable (guint8 *branch, guint8 *target)
+{
+       gint diff = target - branch - 8;
+       g_assert ((diff & 3) == 0);
+       if (diff >= 0) {
+               if (diff <= 33554431)
+                       return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
+       } else {
+               /* diff between 0 and -33554432 */
+               if (diff >= -33554432)
+                       return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
+       }
+       return 0;
+}
+
+static inline guint8*
+emit_bx (guint8* code, int reg)
+{
+       if (mono_arm_thumb_supported ())
+               ARM_BX (code, reg);
+       else
+               ARM_MOV_REG_REG (code, ARMREG_PC, reg);
+       return code;
 }
 
 /* Stack size for trampoline function 
  */
-#define STACK (sizeof (MonoLMF))
+#define STACK ALIGN_TO (sizeof (MonoLMF), 8)
 
 /* Method-specific trampoline code fragment size */
 #define METHOD_TRAMPOLINE_SIZE 64
@@ -140,51 +139,53 @@ mono_arch_nullify_plt_entry (guint8 *code)
 /* Jump-specific trampoline code fragment size */
 #define JUMP_TRAMPOLINE_SIZE   64
 
-#define GEN_TRAMP_SIZE 192
-
-/*
- * Stack frame description when the generic trampoline is called.
- * caller frame
- * ------------------- old sp
- *  MonoLMF
- * ------------------- sp
- */
-guchar*
-mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
-{
-       MonoJumpInfo *ji;
-       guint32 code_size;
-
-       return mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, FALSE);
-}
-       
 guchar*
-mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *buf, *code = NULL;
        guint8 *load_get_lmf_addr, *load_trampoline;
        gpointer *constants;
-
-       *ji = NULL;
+       int cfa_offset, lmf_offset, regsave_size, lr_offset;
+       GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
+       int buf_len;
 
        /* Now we'll create in 'buf' the ARM trampoline code. This
         is the trampoline code common to all methods  */
-       
-       code = buf = mono_global_codeman_reserve (GEN_TRAMP_SIZE);
+
+       buf_len = 212;
+       code = buf = mono_global_codeman_reserve (buf_len);
 
        /*
         * At this point lr points to the specific arg and sp points to the saved
         * regs on the stack (all but PC and SP). The original LR value has been
         * saved as sp + LR_OFFSET by the push in the specific trampoline
         */
-#define LR_OFFSET (sizeof (gpointer) * 13)
-       ARM_MOV_REG_REG (code, ARMREG_V1, ARMREG_SP);
+
+       /* The offset of lmf inside the stack frame */
+       lmf_offset = STACK - sizeof (MonoLMF);
+       /* The size of the area already allocated by the push in the specific trampoline */
+       regsave_size = 14 * sizeof (mgreg_t);
+       /* The offset where lr was saved inside the regsave area */
+       lr_offset = 13 * sizeof (mgreg_t);
+
+       // FIXME: Finish the unwind info, the current info allows us to unwind
+       // when the trampoline is not in the epilog
+
+       // CFA = SP + (num registers pushed) * 4
+       cfa_offset = 14 * sizeof (mgreg_t);
+       mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
+       // PC saved at sp+LR_OFFSET
+       mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
+
        if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
                /* 
-                * The trampoline contains a pc-relative offset to the got slot where the
-                * value is stored. The offset can be found at [lr + 4].
+                * The trampoline contains a pc-relative offset to the got slot 
+                * preceeding the got slot where the value is stored. The offset can be
+                * found at [lr + 0].
                 */
-               ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 4);
+               ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
+               ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
                ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
        } else {
                if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
@@ -192,14 +193,14 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
                else
                        ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
        }
-       ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, LR_OFFSET);
+       ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
 
        /* ok, now we can continue with the MonoLMF setup, mostly untouched 
         * from emit_prolog in mini-arm.c
         * This is a synthetized call to mono_get_lmf_addr ()
         */
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
+               ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
                ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
                ARM_B (code, 0);
                *(gpointer*)code = NULL;
@@ -210,39 +211,58 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
                code += 4;
        }
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R0);
+       code = emit_bx (code, ARMREG_R0);
 
        /* we build the MonoLMF structure on the stack - see mini-arm.h
         * The pointer to the struct is put in r1.
         * the iregs array is already allocated on the stack by push.
         */
-       ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
-       ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_SP, STACK - sizeof (MonoLMF));
+       ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
+       cfa_offset += STACK - regsave_size;
+       mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
+       /* V1 == lmf */
+       ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
+
+       /*
+        * The stack now looks like:
+        *       <saved regs>
+        * v1 -> <rest of LMF>
+        * sp -> <alignment>
+        */
+
        /* r0 is the result from mono_get_lmf_addr () */
-       ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
+       ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
        /* new_lmf->previous_lmf = *lmf_addr */
        ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
-       ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
+       ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
        /* *(lmf_addr) = r1 */
-       ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
+       ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
        /* save method info (it's in v2) */
        if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
-               ARM_STR_IMM (code, ARMREG_V2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
-       ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
+               ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
+       else {
+               ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
+               ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
+       }
+       /* save caller SP */
+       ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
+       ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
+       /* save caller FP */
+       ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
+       ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
        /* save the IP (caller ip) */
        if (tramp_type == MONO_TRAMPOLINE_JUMP) {
                ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
        } else {
-               /* assumes STACK == sizeof (MonoLMF) */
-               ARM_LDR_IMM (code, ARMREG_R2, ARMREG_SP, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
+               ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
        }
-       ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
+       ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
 
        /*
         * Now we're ready to call xxx_trampoline ().
         */
-       /* Arg 1: the saved registers. It was put in v1 */
-       ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V1);
+       /* Arg 1: the saved registers */
+       ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
 
        /* Arg 2: code (next address to the instruction that called us) */
        if (tramp_type == MONO_TRAMPOLINE_JUMP) {
@@ -257,7 +277,7 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
 
        if (aot) {
                char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
+               ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
                ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
                ARM_B (code, 0);
                *(gpointer*)code = NULL;
@@ -269,14 +289,14 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
        }
 
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
-       
+       code = emit_bx (code, ARMREG_IP);
+
        /* OK, code address is now on r0. Move it to the place on the stack
         * where IP was saved (it is now no more useful to us and it can be
         * clobbered). This way we can just restore all the regs in one inst
         * and branch to IP.
         */
-       ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, (ARMREG_R12 * 4));
+       ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
 
        /* Check for thread interruption */
        /* This is not perf critical code so no need to check the interrupt flag */
@@ -284,7 +304,7 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
         * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
         */
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
+               ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
                ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
                ARM_B (code, 0);
                *(gpointer*)code = NULL;
@@ -297,19 +317,17 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
                code += 4;
        }
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
+       code = emit_bx (code, ARMREG_IP);
 
        /*
         * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
         * and the rest of the registers, so the method called will see
         * the same state as before we executed.
-        * The pointer to MonoLMF is in r2.
         */
-       ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_SP);
        /* ip = previous_lmf */
-       ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
+       ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
        /* lr = lmf_addr */
-       ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
+       ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
        /* *(lmf_addr) = previous_lmf */
        ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
 
@@ -319,16 +337,15 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
        /* Restore the registers and jump to the code:
         * Note that IP has been conveniently set to the method addr.
         */
-       ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
+       ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
        ARM_POP_NWB (code, 0x5fff);
        if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
                ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
-       /* do we need to set sp? */
-       ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, (14 * 4));
+       ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
        if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
-               ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
+               code = emit_bx (code, ARMREG_LR);
        else
-               ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
+               code = emit_bx (code, ARMREG_IP);
 
        constants = (gpointer*)code;
        constants [0] = mono_get_lmf_addr;
@@ -343,35 +360,34 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
        code += 8;
 
        /* Flush instruction cache, since we've generated code */
-       mono_arch_flush_icache (code, code - buf);
+       mono_arch_flush_icache (buf, code - buf);
 
        /* Sanity check */
-       g_assert ((code - buf) <= GEN_TRAMP_SIZE);
-
-       *code_size = code - buf;
-
-       if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
-               guint32 code_len;
+       g_assert ((code - buf) <= buf_len);
 
+       if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
                /* Initialize the nullified class init trampoline used in the AOT case */
-               nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
-       }
+               nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
+
+       if (info)
+               *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
 
 gpointer
-mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
+mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
 {
        guint8 *buf, *code;
 
        code = buf = mono_global_codeman_reserve (16);
 
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
+       code = emit_bx (code, ARMREG_LR);
 
-       mono_arch_flush_icache (code, code - buf);
+       mono_arch_flush_icache (buf, code - buf);
 
-       *code_len = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
 
        return buf;
 }
@@ -388,10 +404,10 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        tramp = mono_get_trampoline_code (tramp_type);
 
        mono_domain_lock (domain);
-       code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 4);
-       if ((short_branch = branch_for_target_reachable (code + 8, tramp))) {
+       code = buf = mono_domain_code_reserve_align (domain, size, 4);
+       if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
                size = 12;
-               mono_code_manager_commit (domain->code_mp, code, SPEC_TRAMP_SIZE, size);
+               mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
        }
        mono_domain_unlock (domain);
 
@@ -417,7 +433,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        } else {
                ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
                ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
-               ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
+               code = emit_bx (code, ARMREG_R1);
 
                constants = (gpointer*)code;
                constants [0] = arg1;
@@ -426,7 +442,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        }
 
        /* Flush instruction cache, since we've generated code */
-       mono_arch_flush_icache (code, code - buf);
+       mono_arch_flush_icache (buf, code - buf);
 
        g_assert ((code - buf) <= size);
 
@@ -436,19 +452,64 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        return buf;
 }
 
-#define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
+/*
+ * mono_arch_get_unbox_trampoline:
+ * @m: method pointer
+ * @addr: pointer to native code for @m
+ *
+ * when value type methods are called through the vtable we need to unbox the
+ * this argument. This method returns a pointer to a trampoline which does
+ * unboxing before calling the method
+ */
+gpointer
+mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
+{
+       guint8 *code, *start;
+       MonoDomain *domain = mono_domain_get ();
+
+       start = code = mono_domain_code_reserve (domain, 16);
+
+       ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
+       ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
+       code = emit_bx (code, ARMREG_IP);
+       *(guint32*)code = (guint32)addr;
+       code += 4;
+       mono_arch_flush_icache (start, code - start);
+       g_assert ((code - start) <= 16);
+       /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
+       g_print ("unbox code is at %p for method at %p\n", start, addr);*/
+
+       return start;
+}
 
 gpointer
-mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
+mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
 {
-       guint32 code_size;
-       MonoJumpInfo *ji;
+       guint8 *code, *start;
+       int buf_len;
 
-       return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
+       MonoDomain *domain = mono_domain_get ();
+
+       buf_len = 16;
+
+       start = code = mono_domain_code_reserve (domain, buf_len);
+
+       ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
+       ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
+       *(guint32*)code = (guint32)mrgctx;
+       code += 4;
+       *(guint32*)code = (guint32)addr;
+       code += 4;
+
+       g_assert ((code - start) <= buf_len);
+
+       mono_arch_flush_icache (start, code - start);
+
+       return start;
 }
 
 gpointer
-mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *tramp;
        guint8 *code, *buf;
@@ -458,13 +519,13 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        int depth, index;
        int i, njumps;
        gboolean mrgctx;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
        index = MONO_RGCTX_SLOT_INDEX (slot);
        if (mrgctx)
-               index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
+               index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
        for (depth = 0; ; ++depth) {
                int size = mono_class_rgctx_get_array_size (depth, mrgctx);
 
@@ -477,6 +538,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
+       mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
+
        rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
        njumps = 0;
 
@@ -500,8 +563,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        for (i = 0; i < depth; ++i) {
                /* load ptr to next array */
                if (mrgctx && i == 0) {
-                       g_assert (arm_is_imm12 (sizeof (MonoMethodRuntimeGenericContext)));
-                       ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, sizeof (MonoMethodRuntimeGenericContext));
+                       g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
+                       ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
                } else {
                        ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
                }
@@ -522,7 +585,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        ARM_B_COND (code, ARMCOND_EQ, 0);
        /* otherwise return, result is in R1 */
        ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
+       code = emit_bx (code, ARMREG_LR);
 
        g_assert (njumps <= depth + 2);
        for (i = 0; i < njumps; ++i)
@@ -535,7 +598,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        /* The vtable/mrgctx is still in R0 */
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
+               ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
                ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
                ARM_B (code, 0);
                *(gpointer*)code = NULL;
@@ -546,8 +609,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 
                /* Jump to the actual trampoline */
                ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
-               ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
-               *(guint32*)code = tramp;
+               code = emit_bx (code, ARMREG_R1);
+               *(gpointer*)code = tramp;
                code += 4;
        }
 
@@ -555,7 +618,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 
        g_assert (code - buf <= tramp_size);
 
-       *code_size = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -563,7 +627,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
 
 gpointer
-mono_arch_create_generic_class_init_trampoline (void)
+mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
 {
        guint8 *tramp;
        guint8 *code, *buf;
@@ -573,6 +637,8 @@ mono_arch_create_generic_class_init_trampoline (void)
        int tramp_size;
        guint32 code_len, imm8;
        gint rot_amount;
+       GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
 
        tramp_size = 64;
 
@@ -596,17 +662,152 @@ mono_arch_create_generic_class_init_trampoline (void)
        /* Uninitialized case */
        arm_patch (jump, code);
 
-       tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
+       if (aot) {
+               ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
+               ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               *(gpointer*)code = NULL;
+               code += 4;
+               ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
+       } else {
+               tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
 
-       /* Jump to the actual trampoline */
-       ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
-       ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
-       *(guint32*)code = tramp;
-       code += 4;
+               /* Jump to the actual trampoline */
+               ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
+               code = emit_bx (code, ARMREG_R1);
+               *(gpointer*)code = tramp;
+               code += 4;
+       }
 
        mono_arch_flush_icache (buf, code - buf);
 
        g_assert (code - buf <= tramp_size);
 
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
+
        return buf;
 }
+
+#else
+
+guchar*
+mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+
+gpointer
+mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+
+gpointer
+mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+
+gpointer
+mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+
+gpointer
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+
+gpointer
+mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
+       
+#endif /* DISABLE_JIT */
+
+guint8*
+mono_arch_get_call_target (guint8 *code)
+{
+       guint32 ins = ((guint32*)(gpointer)code) [-1];
+
+#if MONOTOUCH
+       /* Should be a 'bl' or a 'b' */
+       if (((ins >> 25) & 0x7) == 0x5) {
+#else
+       /* Should be a 'bl' */
+       if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
+#endif
+               gint32 disp = ((gint32)ins) & 0xffffff;
+               guint8 *target = code - 4 + 8 + (disp * 4);
+
+               return target;
+       } else {
+               return NULL;
+       }
+}
+
+guint32
+mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
+{
+       /* The offset is stored as the 4th word of the plt entry */
+       return ((guint32*)plt_entry) [3];
+}
+
+/*
+ * Return the address of the PLT entry called by the thumb code CODE.
+ */
+guint8*
+mono_arm_get_thumb_plt_entry (guint8 *code)
+{
+       int s, j1, j2, imm10, imm11, i1, i2, imm32;
+       guint8 *bl, *base;
+       guint16 t1, t2;
+       guint8 *target;
+
+       /* code should be right after a BL */
+       code = (guint8*)((mgreg_t)code & ~1);
+       base = (guint8*)((mgreg_t)code & ~3);
+       bl = code - 4;
+       t1 = ((guint16*)bl) [0];
+       t2 = ((guint16*)bl) [1];
+
+       g_assert ((t1 >> 11) == 0x1e);
+
+       s = (t1 >> 10) & 0x1;
+       imm10 = (t1 >> 0) & 0x3ff;
+       j1 = (t2 >> 13) & 0x1;
+       j2 = (t2 >> 11) & 0x1;
+       imm11 = t2 & 0x7ff;
+
+       i1 = (s ^ j1) ? 0 : 1;
+       i2 = (s ^ j2) ? 0 : 1;
+
+       imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
+       // FIXME:
+       g_assert (s == 0);
+
+       target = code + imm32;
+
+       /* target now points to the thumb plt entry */
+       /* ldr.w r12, [pc, #8] */
+       g_assert (((guint16*)target) [0] == 0xf8df);
+       g_assert (((guint16*)target) [1] == 0xc008);
+
+       /* 
+        * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
+        * the 3rd word, so compensate by returning a different value.
+        */
+       target += 4;
+
+       return target;
+}