New tests.
[mono.git] / mono / mini / aot-runtime.c
index 0958bb0bf03d8b721dd6e059803958c34ee67971..ca03a21712a08b471236124f270b168c32af8f5e 100644 (file)
@@ -59,7 +59,7 @@
 
 #ifdef PLATFORM_WIN32
 #define SHARED_EXT ".dll"
-#elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
+#elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
 #define SHARED_EXT ".dylib"
 #else
 #define SHARED_EXT ".so"
@@ -133,6 +133,12 @@ static CRITICAL_SECTION aot_mutex;
  */
 static GHashTable *static_aot_modules;
 
+/*
+ * Maps MonoJitInfo* to the aot module they belong to, this can be different
+ * from ji->method->klass->image's aot module for generic instances.
+ */
+static GHashTable *ji_to_amodule;
+
 /*
  * Whenever to AOT compile loaded assemblies on demand and store them in
  * a cache under $HOME/.mono/aot-cache.
@@ -521,14 +527,6 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                case MONO_WRAPPER_STELEMREF:
                        *method = mono_marshal_get_stelemref ();
                        break;
-               case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
-                       MonoMethod *m = decode_method_ref_2 (module, p, &p);
-
-                       if (!m)
-                               return NULL;
-                       *method = mono_marshal_get_static_rgctx_invoke (m);
-                       break;
-               }
                case MONO_WRAPPER_SYNCHRONIZED: {
                        MonoMethod *m = decode_method_ref_2 (module, p, &p);
 
@@ -1077,6 +1075,17 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
 
        assembly->image->aot_module = amodule;
 
+       if (mono_aot_only) {
+               if (mono_defaults.corlib) {
+                       /* The second got slot contains the mscorlib got addr */
+                       MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
+
+                       amodule->got [1] = mscorlib_amodule->got;
+               } else {
+                       amodule->got [1] = amodule->got;
+               }
+       }
+
        /*
         * Since we store methoddef and classdef tokens when referring to methods/classes in
         * referenced assemblies, we depend on the exact versions of the referenced assemblies.
@@ -1365,7 +1374,7 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
  * LOCKING: Acquires the domain lock.
  */
 static MonoJitInfo*
-decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain, 
+decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, 
                                                         MonoMethod *method, guint8* ex_info, guint8 *code)
 {
        int i, buf_len;
@@ -1402,7 +1411,7 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
        /* Exception table */
        if (header && header->num_clauses) {
                jinfo = 
-                       mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
+                       mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
                jinfo->num_clauses = header->num_clauses;
 
                for (i = 0; i < header->num_clauses; ++i) {
@@ -1423,7 +1432,7 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
                }
        }
        else {
-               jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
+               jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size);
        }
 
        jinfo->code_size = code_len;
@@ -1448,12 +1457,20 @@ decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
                /* This currently contains no data */
                gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
 
-               jinfo->method = decode_method_ref_2 (aot_module, p, &p);
+               jinfo->method = decode_method_ref_2 (amodule, p, &p);
        }
 
        /* Load debug info */
        buf_len = decode_value (p, &p);
        mono_debug_add_aot_method (domain, method, code, p, buf_len);
+
+       if (amodule != jinfo->method->klass->image->aot_module) {
+               mono_aot_lock ();
+               if (!ji_to_amodule)
+                       ji_to_amodule = g_hash_table_new (NULL, NULL);
+               g_hash_table_insert (ji_to_amodule, jinfo, amodule);
+               mono_aot_unlock ();             
+       }
        
        return jinfo;
 }
@@ -1468,10 +1485,20 @@ mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
 {
        MonoAotModule *amodule = ji->method->klass->image->aot_module;
        guint8 *p;
+       guint8 *code = ji->code_start;
 
        g_assert (amodule);
        g_assert (ji->from_aot);
 
+       if (!(code >= amodule->code && code <= amodule->code_end)) {
+               /* ji belongs to a different aot module than amodule */
+               mono_aot_lock ();
+               g_assert (ji_to_amodule);
+               amodule = g_hash_table_lookup (ji_to_amodule, ji);
+               g_assert (amodule);
+               g_assert (code >= amodule->code && code <= amodule->code_end);
+       }
+
        p = amodule->unwind_info + ji->used_regs;
        *unwind_info_len = decode_value (p, &p);
        return p;
@@ -1662,7 +1689,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
                        goto cleanup;
 
                if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
-                       ji->data.target = mono_create_jit_trampoline_from_token (image, token);
+                       ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
                        ji->type = MONO_PATCH_INFO_ABS;
                }
                else {
@@ -1811,7 +1838,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
 
 static MonoJumpInfo*
 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
-                                guint32 got_index, guint32 **got_slots, 
+                                guint32 **got_slots, 
                                 guint8 *buf, guint8 **endbuf)
 {
        MonoJumpInfo *patches;
@@ -1830,27 +1857,19 @@ load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
                gboolean res;
                guint32 got_offset;
 
-               ji->type = decode_value (p, &p);
+               got_offset = decode_value (p, &p);
 
-               if (mono_aot_is_shared_got_patch (ji)) {
-                       got_offset = decode_value (p, &p);
+               if (aot_module->got [got_offset]) {
+                       /* Already loaded */
+                       //printf ("HIT!\n");
+               } else {
+                       shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset];
 
-                       if (aot_module->got [got_offset]) {
-                               /* Already loaded */
-                               //printf ("HIT!\n");
-                       } else {
-                               shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset];
+                       ji->type = decode_value (shared_p, &shared_p);
 
-                               res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
-                               if (!res)
-                                       goto cleanup;
-                       }
-               } else {
-                       res = decode_patch (aot_module, mp, ji, p, &p);
+                       res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
                        if (!res)
                                goto cleanup;
-
-                       got_offset = got_index ++;
                }
 
                (*got_slots) [pindex] = got_offset;
@@ -1895,12 +1914,12 @@ register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer
  * METHOD might not be set if the caller only has the image/token info.
  */
 static gpointer
-load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
+load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
 {
        MonoClass *klass;
        gboolean from_plt = method == NULL;
        MonoMemPool *mp;
-       int i, pindex, got_index = 0, n_patches, used_strings;
+       int i, pindex, n_patches, used_strings;
        gboolean keep_patches = TRUE;
        guint8 *p, *ex_info;
        MonoJitInfo *jinfo = NULL;
@@ -1909,14 +1928,14 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
        if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
                return NULL;
 
-       if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
+       if ((domain != mono_get_root_domain ()) && (!(amodule->opts & MONO_OPT_SHARED)))
                /* Non shared AOT code can't be used in other appdomains */
                return NULL;
 
-       if (aot_module->out_of_date)
+       if (amodule->out_of_date)
                return NULL;
 
-       if (aot_module->code_offsets [method_index] == 0xffffffff) {
+       if (amodule->code_offsets [method_index] == 0xffffffff) {
                if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
                        char *full_name;
 
@@ -1929,35 +1948,38 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
                return NULL;
        }
 
-       code = &aot_module->code [aot_module->code_offsets [method_index]];
-       info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
+       code = &amodule->code [amodule->code_offsets [method_index]];
+       info = &amodule->method_info [amodule->method_info_offsets [method_index]];
 
        mono_aot_lock ();
-       if (!aot_module->methods_loaded)
-               aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
+       if (!amodule->methods_loaded)
+               amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
        mono_aot_unlock ();
 
-       if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
+       if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
                return code;
 
        if (mono_last_aot_method != -1) {
-               if (mono_jit_stats.methods_aot > mono_last_aot_method)
+               if (mono_jit_stats.methods_aot >= mono_last_aot_method)
                                return NULL;
-               else
-                       if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
-                               printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
+               else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
+                       if (method)
+                               printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name);
+                       else
+                               printf ("LAST AOT METHOD: %p %d\n", code, method_index);
+               }
        }
 
        p = info;
 
        if (method) {
                klass = method->klass;
-               decode_klass_ref (aot_module, p, &p);
+               decode_klass_ref (amodule, p, &p);
        } else {
-               klass = decode_klass_ref (aot_module, p, &p);
+               klass = decode_klass_ref (amodule, p, &p);
        }
 
-       if (aot_module->opts & MONO_OPT_SHARED)
+       if (amodule->opts & MONO_OPT_SHARED)
                used_strings = decode_value (p, &p);
        else
                used_strings = 0;
@@ -1967,7 +1989,7 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
                mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
        }
 
-       if (aot_module->opts & MONO_OPT_SHARED) 
+       if (amodule->opts & MONO_OPT_SHARED)    
                keep_patches = FALSE;
 
        n_patches = decode_value (p, &p);
@@ -1983,19 +2005,19 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
                else
                        mp = mono_mempool_new ();
 
-               got_index = decode_value (p, &p);
-
-               patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
+               patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
                if (patches == NULL)
                        goto cleanup;
 
                for (pindex = 0; pindex < n_patches; ++pindex) {
                        MonoJumpInfo *ji = &patches [pindex];
 
-                       if (!aot_module->got [got_slots [pindex]]) {
-                               aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
+                       if (!amodule->got [got_slots [pindex]]) {
+                               amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
+                               if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
+                                       amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
                                if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
-                                       register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
+                                       register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
                        }
                        ji->type = MONO_PATCH_INFO_NONE;
                }
@@ -2015,8 +2037,8 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
                full_name = mono_method_full_name (method, TRUE);
 
                if (!jinfo) {
-                       ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
-                       jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
+                       ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
+                       jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
                }
 
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info);
@@ -2027,12 +2049,12 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
 
        mono_jit_stats.methods_aot++;
 
-       aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
+       amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
 
-       init_plt (aot_module);
+       init_plt (amodule);
 
        if (method && method->wrapper_type)
-               g_hash_table_insert (aot_module->method_to_code, method, code);
+               g_hash_table_insert (amodule->method_to_code, method, code);
 
        mono_aot_unlock ();
 
@@ -2043,7 +2065,7 @@ load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, Mo
 
  cleanup:
        /* FIXME: The space in domain->mp is wasted */  
-       if (aot_module->opts & MONO_OPT_SHARED)
+       if (amodule->opts & MONO_OPT_SHARED)
                /* No need to cache patches */
                mono_mempool_destroy (mp);
 
@@ -2203,6 +2225,13 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
        return index;
 }
 
+/*
+ * mono_aot_get_method:
+ *
+ *   Return a pointer to the AOTed native code for METHOD if it can be found,
+ * NULL otherwise.
+ * On platforms with function pointers, this doesn't return a function pointer.
+ */
 gpointer
 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
 {
@@ -2223,6 +2252,14 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
                (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
                return NULL;
 
+       /*
+        * Use the original method instead of its invoke-with-check wrapper.
+        * This is not a problem when using full-aot, since it doesn't support
+        * remoting.
+        */
+       if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
+               return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
+
        g_assert (klass->inited);
 
        /* Find method index */
@@ -2380,14 +2417,25 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
        // FIXME: Error handling (how ?)
        g_assert (res);
 
-       target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
+       /* 
+        * Avoid calling resolve_patch_target in the full-aot case if possible, since
+        * it would create a trampoline, and we don't need that.
+        * We could do this only if the method does not need the special handling
+        * in mono_magic_trampoline ().
+        */
+       if (mono_aot_only && ji.type == MONO_PATCH_INFO_METHOD && !ji.data.method->is_generic && !mono_method_check_context_used (ji.data.method) && !(ji.data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) &&
+               !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
+               target = mono_jit_compile_method (ji.data.method);
+       } else {
+               target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
+       }
 
        mono_mempool_destroy (mp);
 
        /* Patch the PLT entry with target which might be the actual method not a trampoline */
        plt_entry = mono_aot_get_plt_entry (code);
        g_assert (plt_entry);
-       mono_arch_patch_plt_entry (plt_entry, target);
+       mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
 
        return target;
 #else
@@ -2406,11 +2454,14 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
 static void
 init_plt (MonoAotModule *amodule)
 {
+#ifndef MONO_CROSS_COMPILE
+
 #ifdef MONO_ARCH_AOT_SUPPORTED
 #ifdef __i386__
        guint8 *buf = amodule->plt;
-#elif defined(__x86_64__) || defined(__arm__)
+#elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
        int i;
+       gpointer plt_0;
 #endif
        gpointer tramp;
 
@@ -2423,22 +2474,26 @@ init_plt (MonoAotModule *amodule)
        /* Initialize the first PLT entry */
        make_writable (amodule->plt, amodule->plt_end - amodule->plt);
        x86_jump_code (buf, tramp);
-#elif defined(__x86_64__) || defined(__arm__)
+#elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
        /*
         * Initialize the PLT entries in the GOT to point to the default targets.
         */
 
+       tramp = mono_create_ftnptr (mono_domain_get (), tramp);
+       plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
         /* The first entry points to the AOT trampoline */
         ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
         for (i = 1; i < amodule->info.plt_size; ++i)
                 /* All the default entries point to the first entry */
-                ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = amodule->plt;
+                ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
 #else
        g_assert_not_reached ();
 #endif
 
        amodule->plt_inited = TRUE;
 #endif
+
+#endif /* MONO_CROSS_COMPILE */
 }
 
 /*
@@ -2450,7 +2505,7 @@ guint8*
 mono_aot_get_plt_entry (guint8 *code)
 {
        MonoAotModule *aot_module = find_aot_module (code);
-#if defined(__arm__)
+#if defined(__arm__) || defined(__mono_ppc__)
        guint32 ins;
 #endif
 
@@ -2476,6 +2531,17 @@ mono_aot_get_plt_entry (guint8 *code)
                if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
                        return target;
        }               
+#elif defined(__mono_ppc__)
+       /* Should be a bl */
+       ins = ((guint32*)(gpointer)code) [-1];
+
+       if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
+               gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
+               guint8 *target = code - 4 + (disp * 4);
+
+               if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
+                       return target;
+       }
 #else
        g_assert_not_reached ();
 #endif
@@ -2489,7 +2555,7 @@ mono_aot_get_plt_entry (guint8 *code)
  *   Return the PLT info offset belonging to the plt entry called by CODE.
  */
 guint32
-mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
+mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
 {
        guint8 *plt_entry = mono_aot_get_plt_entry (code);
 
@@ -2501,14 +2567,36 @@ mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
 #elif defined(__x86_64__)
        return *(guint32*)(plt_entry + 6);
 #elif defined(__arm__)
-       /* The offset is stored as the 5th word of the plt entry */
-       return ((guint32*)plt_entry) [4];
+       /* The offset is stored as the 4th word of the plt entry */
+       return ((guint32*)plt_entry) [3];          
+#elif defined(__mono_ppc__)
+#ifdef PPC_USES_FUNCTION_DESCRIPTOR
+       return ((guint32*)plt_entry) [8];
+#else
+       return ((guint32*)plt_entry) [6];
+#endif
 #else
        g_assert_not_reached ();
        return 0;
 #endif
 }
 
+static gpointer
+mono_create_ftnptr_malloc (guint8 *code)
+{
+#ifdef PPC_USES_FUNCTION_DESCRIPTOR
+       MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
+
+       ftnptr->code = code;
+       ftnptr->toc = NULL;
+       ftnptr->env = NULL;
+
+       return ftnptr;
+#else
+       return code;
+#endif
+}
+
 /*
  * load_function:
  *
@@ -2519,7 +2607,7 @@ load_function (MonoAotModule *amodule, const char *name)
 {
        char *symbol;
        guint8 *p;
-       int n_patches, got_index, pindex;
+       int n_patches, pindex;
        MonoMemPool *mp;
        gpointer code;
 
@@ -2552,9 +2640,7 @@ load_function (MonoAotModule *amodule, const char *name)
 
                mp = mono_mempool_new ();
 
-               got_index = decode_value (p, &p);
-
-               patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
+               patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
                g_assert (patches);
 
                for (pindex = 0; pindex < n_patches; ++pindex) {
@@ -2590,6 +2676,10 @@ load_function (MonoAotModule *amodule, const char *name)
                                        target = mono_arm_throw_exception;
                                } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
                                        target = mono_arm_throw_exception_by_token;
+#endif
+#ifdef __mono_ppc__
+                               } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
+                                       target = mono_ppc_throw_exception;
 #endif
                                } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
                                        int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
@@ -2602,12 +2692,16 @@ load_function (MonoAotModule *amodule, const char *name)
                                        res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
                                        g_assert (res == 1);
                                        target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
+                                       target = mono_create_ftnptr_malloc (target);
                                } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
                                        target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
+                                       target = mono_create_ftnptr_malloc (target);
                                } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
                                        target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
+                                       target = mono_create_ftnptr_malloc (target);
                                } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
                                        target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
+                                       target = mono_create_ftnptr_malloc (target);
                                } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
                                        target = mono_thread_get_and_clear_pending_exception;
                                } else {
@@ -2636,6 +2730,7 @@ load_function (MonoAotModule *amodule, const char *name)
 
 /*
  * Return the piece of code identified by NAME from the mscorlib AOT file.
+ * On ppc64, this returns a function descriptor.
  */
 gpointer
 mono_aot_get_named_code (const char *name)
@@ -2649,7 +2744,7 @@ mono_aot_get_named_code (const char *name)
        amodule = image->aot_module;
        g_assert (amodule);
 
-       return load_function (amodule, name);
+       return mono_create_ftnptr_malloc (load_function (amodule, name));
 }
 
 /* Return a given kind of trampoline */
@@ -2748,7 +2843,8 @@ mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
        amodule->got [got_offset] = ctx;
        amodule->got [got_offset + 1] = addr; 
 
-       return code;
+       /* The caller expects an ftnptr */
+       return mono_create_ftnptr (mono_domain_get (), code);
 }
 
 gpointer
@@ -2772,7 +2868,9 @@ mono_aot_get_unbox_trampoline (MonoMethod *method)
        }
        code = load_function (amodule, symbol);
        g_free (symbol);
-       return code;
+
+       /* The caller expects an ftnptr */
+       return mono_create_ftnptr (mono_domain_get (), code);
 }
 
 gpointer
@@ -2881,7 +2979,7 @@ mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int sl
 }
 
 guint32
-mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
+mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
 {
        g_assert_not_reached ();