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>
16 #ifdef MONO_ARCH_HAVE_IMT
19 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
21 MonoObject *this_argument = mono_arch_find_this_argument (regs, method);
22 MonoVTable *vt = this_argument->vtable;
23 int displacement = slot - ((gpointer*)vt);
25 if (displacement > 0) {
26 /* slot is in the vtable, not in the IMT */
28 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
32 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
34 int imt_slot = MONO_IMT_SIZE + displacement;
36 mono_class_setup_vtable (vt->klass);
37 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
39 if (interface_offset < 0) {
40 g_print ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
41 g_assert_not_reached ();
43 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
46 *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
48 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
49 method->klass->name_space, method->klass->name, method->name,
50 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
52 g_assert (imt_slot < MONO_IMT_SIZE);
53 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
54 int vtable_offset = interface_offset + imt_method->slot;
55 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
57 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, and colliding becomes %p[%d] (interface_offset = %d, method->slot = %d)\n", slot, imt_slot, vtable_slot, vtable_offset, interface_offset, imt_method->slot);
62 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
71 * mono_magic_trampoline:
73 * This trampoline handles calls from JITted code.
76 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
79 gpointer *vtable_slot;
81 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
82 if (m == MONO_FAKE_VTABLE_METHOD) {
84 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
86 if (displacement > 0) {
87 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
88 g_assert (displacement >= 0);
89 displacement /= sizeof (gpointer);
90 mono_class_setup_vtable (vt->klass);
91 m = vt->klass->vtable [displacement];
92 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
94 /* We got here from an interface method: redirect to IMT handling */
95 m = MONO_FAKE_IMT_METHOD;
96 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
100 /* this is the IMT trampoline */
101 #ifdef MONO_ARCH_HAVE_IMT
102 if (m == MONO_FAKE_IMT_METHOD) {
103 MonoMethod *impl_method;
104 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
105 * needs the signature to be able to find the this argument
107 m = mono_arch_find_imt_method (regs, code);
108 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
109 g_assert (vtable_slot);
110 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
111 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
112 * to be called, so we compile it and go ahead as usual.
114 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
119 addr = mono_compile_method (m);
122 /* the method was jumped to */
126 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
129 if (m->klass->valuetype)
130 addr = mono_arch_get_unbox_trampoline (m, addr);
132 g_assert (*vtable_slot);
134 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
135 #ifdef MONO_ARCH_HAVE_IMT
136 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
138 *vtable_slot = mono_get_addr_from_ftnptr (addr);
142 guint8 *plt_entry = mono_aot_get_plt_entry (code);
144 /* Patch calling code */
146 mono_arch_patch_plt_entry (plt_entry, addr);
149 mono_jit_info_table_find (mono_domain_get (), (char*)code);
150 MonoJitInfo *target_ji =
151 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
153 if (mono_method_same_domain (ji, target_ji))
154 mono_arch_patch_callsite (code, addr);
162 * mono_aot_trampoline:
164 * This trampoline handles calls made from AOT code. We try to bypass the
165 * normal JIT compilation logic to avoid loading the metadata for the method.
167 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
169 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
174 MonoMethod *method = NULL;
176 gpointer *vtable_slot;
177 gboolean is_got_entry;
179 image = *(gpointer*)(gpointer)token_info;
180 token_info += sizeof (gpointer);
181 token = *(guint32*)(gpointer)token_info;
183 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
185 method = mono_get_method (image, token, NULL);
188 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
190 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
191 method = mono_marshal_get_synchronized_wrapper (method);
193 addr = mono_compile_method (method);
197 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
200 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
204 method = mono_get_method (image, token, NULL);
205 if (method->klass->valuetype)
206 addr = mono_arch_get_unbox_trampoline (method, addr);
209 /* This is a normal call through a PLT entry */
210 guint8 *plt_entry = mono_aot_get_plt_entry (code);
212 g_assert (plt_entry);
214 mono_arch_patch_plt_entry (plt_entry, addr);
216 is_got_entry = FALSE;
220 * Since AOT code is only used in the root domain,
221 * mono_domain_get () != mono_get_root_domain () means the calling method
222 * is AppDomain:InvokeInDomain, so this is the same check as in
223 * mono_method_same_domain () but without loading the metadata for the method.
225 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
226 #ifdef MONO_ARCH_HAVE_IMT
228 method = mono_get_method (image, token, NULL);
229 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
238 * mono_aot_plt_trampoline:
240 * This trampoline handles calls made from AOT code through the PLT table.
243 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
246 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
247 guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
249 guint32 plt_info_offset = -1;
252 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
257 * mono_class_init_trampoline:
259 * This method calls mono_runtime_class_init () to run the static constructor
260 * for the type, then patches the caller code so it is not called again.
263 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
265 guint8 *plt_entry = mono_aot_get_plt_entry (code);
267 mono_runtime_class_init (vtable);
269 if (!mono_running_on_valgrind ()) {
271 mono_arch_nullify_plt_entry (plt_entry);
273 mono_arch_nullify_class_init_trampoline (code, regs);
278 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
281 * mono_delegate_trampoline:
283 * This trampoline handles calls made to Delegate:Invoke ().
286 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
288 MonoDomain *domain = mono_domain_get ();
289 MonoDelegate *delegate;
295 /* Find the Invoke method */
297 while ((invoke = mono_class_get_methods (klass, &iter))) {
298 if (!strcmp (invoke->name, "Invoke"))
303 /* Obtain the delegate object according to the calling convention */
305 delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
308 * If the called address is a trampoline, replace it with the compiled method so
309 * further calls don't have to go through the trampoline.
311 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
313 delegate->method_ptr = mono_compile_method (ji->method);
315 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
317 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
320 delegate->invoke_impl = code;
325 /* The general, unoptimized case */
326 delegate->invoke_impl = mono_compile_method (mono_marshal_get_delegate_invoke (invoke));
327 return delegate->invoke_impl;