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 (), (char*)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 to Delegate:Invoke ().
184 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
186 MonoDomain *domain = mono_domain_get ();
187 MonoDelegate *delegate;
193 /* Find the Invoke method */
195 while ((invoke = mono_class_get_methods (klass, &iter))) {
196 if (!strcmp (invoke->name, "Invoke"))
201 /* Obtain the delegate object according to the calling convention */
203 delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
206 * If the called address is a trampoline, replace it with the compiled method so
207 * further calls don't have to go through the trampoline.
209 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
211 delegate->method_ptr = mono_compile_method (ji->method);
213 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
218 mono_domain_lock (domain);
219 if (delegate->target != NULL) {
220 if (!domain->delegate_invoke_impl_with_target_hash)
221 domain->delegate_invoke_impl_with_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
222 cache = domain->delegate_invoke_impl_with_target_hash;
224 if (!domain->delegate_invoke_impl_no_target_hash)
225 domain->delegate_invoke_impl_no_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
226 cache = domain->delegate_invoke_impl_no_target_hash;
228 code = g_hash_table_lookup (cache, mono_method_signature (invoke));
229 mono_domain_unlock (domain);
231 delegate->invoke_impl = code;
235 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
238 mono_domain_lock (domain);
239 g_hash_table_insert (cache, mono_method_signature (invoke), code);
240 mono_domain_unlock (domain);
242 delegate->invoke_impl = code;
247 /* The general, unoptimized case */
248 delegate->invoke_impl = mono_compile_method (mono_marshal_get_delegate_invoke (invoke));
249 return delegate->invoke_impl;