5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/metadata-internals.h>
7 #include <mono/metadata/marshal.h>
8 #include <mono/metadata/tabledefs.h>
10 #ifdef HAVE_VALGRIND_MEMCHECK_H
11 #include <valgrind/memcheck.h>
17 * mono_magic_trampoline:
19 * This trampoline handles calls from JITted code.
22 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
25 gpointer *vtable_slot;
27 addr = mono_compile_method (m);
30 /* the method was jumped to */
34 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
37 if (m->klass->valuetype)
38 addr = mono_arch_get_unbox_trampoline (m, addr);
40 g_assert (*vtable_slot);
42 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
43 *vtable_slot = mono_get_addr_from_ftnptr (addr);
46 guint8 *plt_entry = mono_aot_get_plt_entry (code);
48 /* Patch calling code */
50 mono_arch_patch_plt_entry (plt_entry, addr);
53 mono_jit_info_table_find (mono_domain_get (), code);
54 MonoJitInfo *target_ji =
55 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
57 if (mono_method_same_domain (ji, target_ji))
58 mono_arch_patch_callsite (code, addr);
66 * mono_aot_trampoline:
68 * This trampoline handles calls made from AOT code. We try to bypass the
69 * normal JIT compilation logic to avoid loading the metadata for the method.
71 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
73 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
78 MonoMethod *method = NULL;
80 gpointer *vtable_slot;
81 gboolean is_got_entry;
83 image = *(gpointer*)(gpointer)token_info;
84 token_info += sizeof (gpointer);
85 token = *(guint32*)(gpointer)token_info;
87 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
89 method = mono_get_method (image, token, NULL);
92 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
94 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
95 method = mono_marshal_get_synchronized_wrapper (method);
97 addr = mono_compile_method (method);
101 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
104 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
108 method = mono_get_method (image, token, NULL);
109 if (method->klass->valuetype)
110 addr = mono_arch_get_unbox_trampoline (method, addr);
113 /* This is a normal call through a PLT entry */
114 guint8 *plt_entry = mono_aot_get_plt_entry (code);
116 g_assert (plt_entry);
118 mono_arch_patch_plt_entry (plt_entry, addr);
120 is_got_entry = FALSE;
124 * Since AOT code is only used in the root domain,
125 * mono_domain_get () != mono_get_root_domain () means the calling method
126 * is AppDomain:InvokeInDomain, so this is the same check as in
127 * mono_method_same_domain () but without loading the metadata for the method.
129 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
136 * mono_aot_plt_trampoline:
138 * This trampoline handles calls made from AOT code through the PLT table.
141 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
144 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
145 guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
147 guint32 plt_info_offset = -1;
150 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
155 * mono_class_init_trampoline:
157 * This method calls mono_runtime_class_init () to run the static constructor
158 * for the type, then patches the caller code so it is not called again.
161 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
163 guint8 *plt_entry = mono_aot_get_plt_entry (code);
165 mono_runtime_class_init (vtable);
167 if (!mono_running_on_valgrind ()) {
169 mono_arch_nullify_plt_entry (plt_entry);
171 mono_arch_nullify_class_init_trampoline (code, regs);
176 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
179 * mono_delegate_trampoline:
181 * This trampoline handles calls made from the delegate invoke wrapper. It patches
182 * the function address inside the delegate.
185 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
189 addr = mono_compile_method (m);
192 mono_arch_patch_delegate_trampoline (code, tramp, regs, addr);