2009-04-19 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-trampolines.c
1
2 #include <config.h>
3 #include <glib.h>
4
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>
9 #include <mono/utils/mono-counters.h>
10
11 #ifdef HAVE_VALGRIND_MEMCHECK_H
12 #include <valgrind/memcheck.h>
13 #endif
14
15 #include "mini.h"
16 #include "debug-mini.h"
17
18 /*
19  * Address of the trampoline code.  This is used by the debugger to check
20  * whether a method is a trampoline.
21  */
22 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
23
24 static GHashTable *class_init_hash_addr = NULL;
25 static GHashTable *delegate_trampoline_hash_addr = NULL;
26 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
27 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
28
29 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
30 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
31 static CRITICAL_SECTION trampolines_mutex;
32
33 static gpointer
34 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
35 {
36         if (mono_aot_only)
37                 return mono_aot_get_unbox_trampoline (m);
38         else
39                 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
40 }
41
42 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
43 /*
44  * mono_create_static_rgctx_trampoline:
45  *
46  *   Return a static rgctx trampoline for M which branches to ADDR which should
47  * point to the compiled code of M.
48  *
49  *   Static rgctx trampolines are used when a shared generic method which doesn't
50  * have a this argument is called indirectly, ie. from code which can't pass in
51  * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
52  * methods code. These trampolines are similar to the unbox trampolines, they
53  * perform the same task as the static rgctx wrappers, but they are smaller/faster,
54  * and can be made to work with full AOT.
55  */
56 gpointer
57 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
58 {
59         gpointer ctx;
60         gpointer res;
61         MonoDomain *domain;
62
63         if (mini_method_get_context (m)->method_inst)
64                 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
65         else
66                 ctx = mono_class_vtable (mono_domain_get (), m->klass);
67
68         if (mono_aot_only)
69                 return mono_aot_get_static_rgctx_trampoline (ctx, addr);
70
71         domain = mono_domain_get ();
72
73         mono_domain_lock (domain);
74         res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
75                                                            m);
76         mono_domain_unlock (domain);
77         if (res)
78                 return res;
79
80         res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
81
82         mono_domain_lock (domain);
83         /* Duplicates inserted while we didn't hold the lock are OK */
84         g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, m, res);
85         mono_domain_unlock (domain);
86
87         return res;
88 }
89 #endif
90
91 gpointer*
92 mono_get_vcall_slot_addr (guint8* code, gpointer *regs)
93 {
94         gpointer vt;
95         int displacement;
96         vt = mono_arch_get_vcall_slot (code, regs, &displacement);
97         if (!vt)
98                 return NULL;
99         return (gpointer*)((char*)vt + displacement);
100 }
101
102 #ifdef MONO_ARCH_HAVE_IMT
103
104 static gpointer*
105 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
106 {
107         MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
108         MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
109         MonoVTable *vt = this_argument->vtable;
110         int displacement = slot - ((gpointer*)vt);
111
112         if (displacement > 0) {
113                 /* slot is in the vtable, not in the IMT */
114 #if DEBUG_IMT
115                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
116 #endif
117                 return slot;
118         } else {
119                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
120                 int interface_offset;
121                 int imt_slot = MONO_IMT_SIZE + displacement;
122
123                 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
124
125                 if (interface_offset < 0) {
126                         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));
127                         g_assert_not_reached ();
128                 }
129                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
130
131                 if (impl_method) {
132                         MonoMethod *impl;
133
134                         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
135                                 MonoGenericContext context = { NULL, NULL };
136
137                                 /* 
138                                  * Generic virtual method, imt_method contains the inflated interface 
139                                  * method, need to get the inflated impl method.
140                                  */
141                                 /* imt_method->slot might not be set */
142                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
143
144                                 if (impl->klass->generic_class)
145                                         context.class_inst = impl->klass->generic_class->context.class_inst;
146                                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
147                                 impl = mono_class_inflate_generic_method (impl, &context);
148                         } else {
149                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
150                         }
151
152                         *impl_method = impl;
153 #if DEBUG_IMT
154                 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
155                                 method->klass->name_space, method->klass->name, method->name, 
156                                 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
157 #endif
158                 }
159                 g_assert (imt_slot < MONO_IMT_SIZE);
160                 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
161                         int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
162                         gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
163 #if DEBUG_IMT
164                         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);
165 #endif
166                         return vtable_slot;
167                 } else {
168 #if DEBUG_IMT
169                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
170 #endif
171                         return slot;
172                 }
173         }
174 }
175 #endif
176
177 /**
178  * mono_magic_trampoline:
179  *
180  *   This trampoline handles calls from JITted code.
181  */
182 gpointer
183 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
184 {
185         gpointer addr;
186         gpointer *vtable_slot;
187         gboolean generic_shared = FALSE;
188         MonoMethod *declaring = NULL;
189         MonoMethod *generic_virtual = NULL;
190         int context_used;
191         gboolean proxy = FALSE;
192 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
193         gboolean need_rgctx_tramp = FALSE;
194 #endif
195
196         if (m == MONO_FAKE_VTABLE_METHOD) {
197                 int displacement;
198                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
199                 if (!vt) {
200                         int i;
201                         MonoJitInfo *ji;
202
203                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
204                         if (ji)
205                                 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
206                         /* Print some debug info */
207                         for (i = 0; i < 32; ++i)
208                                 printf ("0x%x ", code [-32 + i]);
209                         printf ("\n");
210                         g_assert (vt);
211                 }
212                 if (displacement > 0) {
213                         displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
214                         g_assert (displacement >= 0);
215                         displacement /= sizeof (gpointer);
216
217                         /* Avoid loading metadata or creating a generic vtable if possible */
218                         addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
219                         if (addr && !vt->klass->valuetype) {
220                                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
221                                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
222                                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
223                                 }
224
225                                 return addr;
226                         }
227
228                         m = mono_class_get_vtable_entry (vt->klass, displacement);
229                         /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
230                 } else {
231                         /* We got here from an interface method: redirect to IMT handling */
232                         m = MONO_FAKE_IMT_METHOD;
233                         /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
234                 }
235         }
236
237         /* this is the IMT trampoline */
238 #ifdef MONO_ARCH_HAVE_IMT
239         if (m == MONO_FAKE_IMT_METHOD) {
240                 MonoMethod *impl_method;
241                 MonoGenericSharingContext *gsctx;
242                 MonoObject *this_arg;
243
244                 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
245                  * needs the signature to be able to find the this argument
246                  */
247                 m = mono_arch_find_imt_method ((gpointer*)regs, code);
248                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
249                 g_assert (vtable_slot);
250
251                 gsctx = mono_get_generic_context_from_code (code);
252                 this_arg = mono_arch_find_this_argument ((gpointer*)regs, m, gsctx);
253
254                 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
255                         /* Use the slow path for now */
256                         proxy = TRUE;
257                     m = mono_object_get_virtual_method (this_arg, m);
258                 } else {
259                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
260                         /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
261                          * to be called, so we compile it and go ahead as usual.
262                          */
263                         /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
264                         if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
265                                 /* Generic virtual method */
266                                 generic_virtual = m;
267                                 m = impl_method;
268 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
269                                 need_rgctx_tramp = TRUE;
270 #else
271                                 m = mono_marshal_get_static_rgctx_invoke (m);
272 #endif
273                         } else {
274                                 m = impl_method;
275                         }
276                 }
277         }
278 #endif
279
280         if (m->is_generic) {
281                 MonoGenericContext context = { NULL, NULL };
282                 MonoMethod *declaring;
283
284                 if (m->is_inflated)
285                         declaring = mono_method_get_declaring_generic_method (m);
286                 else
287                         declaring = m;
288
289                 if (m->klass->generic_class)
290                         context.class_inst = m->klass->generic_class->context.class_inst;
291                 else
292                         g_assert (!m->klass->generic_container);
293
294 #ifdef MONO_ARCH_HAVE_IMT
295                 generic_virtual = mono_arch_find_imt_method ((gpointer*)regs, code);
296 #endif
297                 if (generic_virtual) {
298                         g_assert (generic_virtual->is_inflated);
299                         context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
300                 }
301
302                 m = mono_class_inflate_generic_method (declaring, &context);
303 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
304                 need_rgctx_tramp = TRUE;
305 #else
306                 /* FIXME: only do this if the method is sharable */
307                 m = mono_marshal_get_static_rgctx_invoke (m);
308 #endif
309         } else if ((context_used = mono_method_check_context_used (m))) {
310                 MonoClass *klass = NULL;
311                 MonoMethod *actual_method = NULL;
312                 MonoVTable *vt = NULL;
313                 MonoGenericInst *method_inst = NULL;
314
315                 vtable_slot = NULL;
316                 generic_shared = TRUE;
317
318                 g_assert (code);
319
320                 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
321 #ifdef MONO_ARCH_RGCTX_REG
322                         MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
323
324                         klass = mrgctx->class_vtable->klass;
325                         method_inst = mrgctx->method_inst;
326 #else
327                         g_assert_not_reached ();
328 #endif
329                 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
330 #ifdef MONO_ARCH_RGCTX_REG
331                         MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
332
333                         klass = vtable->klass;
334 #else
335                         g_assert_not_reached ();
336 #endif
337                 } else {
338 #ifdef MONO_ARCH_HAVE_IMT
339                         MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
340                                 mono_get_generic_context_from_code (code));
341
342                         vt = this_argument->vtable;
343                         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
344
345                         g_assert (this_argument->vtable->klass->inited);
346                         //mono_class_init (this_argument->vtable->klass);
347
348                         if (!vtable_slot)
349                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
350 #else
351                         NOT_IMPLEMENTED;
352 #endif
353                 }
354
355                 g_assert (vtable_slot || klass);
356
357                 if (vtable_slot) {
358                         int displacement = vtable_slot - ((gpointer*)vt);
359
360                         g_assert_not_reached ();
361
362                         g_assert (displacement > 0);
363
364                         actual_method = vt->klass->vtable [displacement];
365                 }
366
367                 if (method_inst) {
368                         MonoGenericContext context = { NULL, NULL };
369
370                         if (m->is_inflated)
371                                 declaring = mono_method_get_declaring_generic_method (m);
372                         else
373                                 declaring = m;
374
375                         if (klass->generic_class)
376                                 context.class_inst = klass->generic_class->context.class_inst;
377                         else if (klass->generic_container)
378                                 context.class_inst = klass->generic_container->context.class_inst;
379                         context.method_inst = method_inst;
380
381                         actual_method = mono_class_inflate_generic_method (declaring, &context);
382                 } else {
383                         actual_method = mono_class_get_method_generic (klass, m);
384                 }
385
386                 g_assert (klass);
387                 g_assert (actual_method->klass == klass);
388
389                 if (actual_method->is_inflated)
390                         declaring = mono_method_get_declaring_generic_method (actual_method);
391                 else
392                         declaring = NULL;
393
394                 m = actual_method;
395         }
396
397         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
398                 MonoJitInfo *ji;
399
400                 if (code)
401                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
402                 else
403                         ji = NULL;
404
405                 /* Avoid recursion */
406                 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
407                         m = mono_marshal_get_synchronized_wrapper (m);
408         }
409
410         /* Calls made through delegates on platforms without delegate trampolines */
411         if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
412                 m = mono_marshal_get_static_rgctx_invoke (m);
413
414         addr = mono_compile_method (m);
415         g_assert (addr);
416
417         mono_debugger_trampoline_compiled (m, addr);
418
419 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
420         if (need_rgctx_tramp)
421                 addr = mono_create_static_rgctx_trampoline (m, addr);
422 #endif
423
424         if (generic_virtual) {
425                 int displacement;
426                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
427
428                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
429                 g_assert (vtable_slot);
430
431                 if (vt->klass->valuetype)
432                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);                       
433
434                 mono_method_add_generic_virtual_invocation (mono_domain_get (), 
435                                                                                                         vt, vtable_slot,
436                                                                                                         generic_virtual, addr);
437
438                 return addr;
439         }
440
441         /* the method was jumped to */
442         if (!code) {
443                 MonoDomain *domain = mono_domain_get ();
444
445                 /* Patch the got entries pointing to this method */
446                 /* 
447                  * We do this here instead of in mono_codegen () to cover the case when m
448                  * was loaded from an aot image.
449                  */
450                 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
451                         GSList *list, *tmp;
452
453                         mono_domain_lock (domain);
454                         list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
455                         if (list) {
456                                 for (tmp = list; tmp; tmp = tmp->next) {
457                                         gpointer *got_slot = tmp->data;
458                                         *got_slot = addr;
459                                 }
460                                 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
461                                 g_slist_free (list);
462                         }
463                         mono_domain_unlock (domain);
464                 }
465
466                 return addr;
467         }
468
469         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
470
471         if (vtable_slot) {
472                 if (m->klass->valuetype)
473                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
474                 g_assert (*vtable_slot);
475
476                 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
477 #ifdef MONO_ARCH_HAVE_IMT
478                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
479 #endif
480                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
481                 }
482         }
483         else {
484                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
485
486                 if (plt_entry) {
487                         mono_arch_patch_plt_entry (plt_entry, addr);
488                 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
489                         mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
490                         if (generic_shared) {
491                                 if (m->wrapper_type != MONO_WRAPPER_NONE)
492                                         m = mono_marshal_method_from_wrapper (m);
493                                 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
494                         }
495
496                         /* Patch calling code */
497                         if (plt_entry) {
498
499                         } else {
500                                 MonoJitInfo *ji = 
501                                         mono_jit_info_table_find (mono_domain_get (), (char*)code);
502                                 MonoJitInfo *target_ji = 
503                                         mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
504
505                                 if (mono_method_same_domain (ji, target_ji))
506                                         mono_arch_patch_callsite (ji->code_start, code, addr);
507                         }
508                 }
509         }
510
511         return addr;
512 }
513
514 gpointer
515 mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
516 {
517         MonoGenericContext context = { NULL, NULL };
518         MonoMethod *imt_method, *declaring;
519         gpointer addr;
520
521         g_assert (m->is_generic);
522
523         if (m->is_inflated)
524                 declaring = mono_method_get_declaring_generic_method (m);
525         else
526                 declaring = m;
527
528         if (m->klass->generic_class)
529                 context.class_inst = m->klass->generic_class->context.class_inst;
530         else
531                 g_assert (!m->klass->generic_container);
532
533 #ifdef MONO_ARCH_HAVE_IMT
534         imt_method = mono_arch_find_imt_method ((gpointer*)regs, code);
535         if (imt_method->is_inflated)
536                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
537 #endif
538         m = mono_class_inflate_generic_method (declaring, &context);
539         m = mono_marshal_get_remoting_invoke_with_check (m);
540
541         addr = mono_compile_method (m);
542         g_assert (addr);
543
544         mono_debugger_trampoline_compiled (m, addr);
545
546         return addr;
547 }
548
549 /*
550  * mono_aot_trampoline:
551  *
552  *   This trampoline handles calls made from AOT code. We try to bypass the 
553  * normal JIT compilation logic to avoid loading the metadata for the method.
554  */
555 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
556 gpointer
557 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
558                                          guint8* tramp)
559 {
560         MonoImage *image;
561         guint32 token;
562         MonoMethod *method = NULL;
563         gpointer addr;
564         gpointer *vtable_slot;
565         gboolean is_got_entry;
566         guint8 *plt_entry;
567
568         image = *(gpointer*)(gpointer)token_info;
569         token_info += sizeof (gpointer);
570         token = *(guint32*)(gpointer)token_info;
571
572         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
573         if (!addr) {
574                 method = mono_get_method (image, token, NULL);
575                 g_assert (method);
576
577                 /* Use the generic code */
578                 return mono_magic_trampoline (regs, code, method, tramp);
579         }
580
581         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
582         g_assert (!vtable_slot);
583
584         /* This is a normal call through a PLT entry */
585         plt_entry = mono_aot_get_plt_entry (code);
586         g_assert (plt_entry);
587
588         mono_arch_patch_plt_entry (plt_entry, addr);
589
590         is_got_entry = FALSE;
591
592         /*
593          * Since AOT code is only used in the root domain, 
594          * mono_domain_get () != mono_get_root_domain () means the calling method
595          * is AppDomain:InvokeInDomain, so this is the same check as in 
596          * mono_method_same_domain () but without loading the metadata for the method.
597          */
598         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
599 #ifdef MONO_ARCH_HAVE_IMT
600                 if (!method)
601                         method = mono_get_method (image, token, NULL);
602                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
603 #endif
604                 *vtable_slot = addr;
605         }
606
607         return addr;
608 }
609
610 /*
611  * mono_aot_plt_trampoline:
612  *
613  *   This trampoline handles calls made from AOT code through the PLT table.
614  */
615 gpointer
616 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
617                                                  guint8* tramp)
618 {
619         guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
620
621         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
622 }
623 #endif
624
625 /**
626  * mono_class_init_trampoline:
627  *
628  * This method calls mono_runtime_class_init () to run the static constructor
629  * for the type, then patches the caller code so it is not called again.
630  */
631 void
632 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
633 {
634         guint8 *plt_entry = mono_aot_get_plt_entry (code);
635
636         mono_runtime_class_init (vtable);
637
638         if (plt_entry) {
639                 mono_arch_nullify_plt_entry (plt_entry);
640         } else {
641                 mono_arch_nullify_class_init_trampoline (code, regs);
642         }
643 }
644
645 /**
646  * mono_generic_class_init_trampoline:
647  *
648  * This method calls mono_runtime_class_init () to run the static constructor
649  * for the type.
650  */
651 void
652 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
653 {
654         g_assert (!vtable->initialized);
655
656         mono_runtime_class_init (vtable);
657 }
658
659 static gpointer
660 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
661 {
662 #ifdef MONO_ARCH_VTABLE_REG
663         static gboolean inited = FALSE;
664         static int num_lookups = 0;
665         guint32 slot = GPOINTER_TO_UINT (data);
666         gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
667         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
668         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
669
670         if (!inited) {
671                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
672                 inited = TRUE;
673         }
674
675         num_lookups++;
676
677         if (mrgctx)
678                 return mono_method_fill_runtime_generic_context (arg, index);
679         else
680                 return mono_class_fill_runtime_generic_context (arg, index);
681 #else
682         g_assert_not_reached ();
683 #endif
684 }
685
686 void
687 mono_monitor_enter_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
688 {
689         mono_monitor_enter (obj);
690 }
691
692 void
693 mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
694 {
695         mono_monitor_exit (obj);
696 }
697
698 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
699
700 /**
701  * mono_delegate_trampoline:
702  *
703  *   This trampoline handles calls made to Delegate:Invoke ().
704  * This is called once the first time a delegate is invoked, so it must be fast.
705  */
706 gpointer
707 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
708 {
709         MonoDomain *domain = mono_domain_get ();
710         MonoDelegate *delegate;
711         MonoJitInfo *ji;
712         MonoMethod *m;
713         MonoMethod *method = NULL;
714         gboolean multicast, callvirt;
715 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
716         gboolean need_rgctx_tramp = FALSE;
717 #endif
718         MonoMethod *invoke = tramp_data [0];
719         guint8 *impl_this = tramp_data [1];
720         guint8 *impl_nothis = tramp_data [2];
721
722         /* Obtain the delegate object according to the calling convention */
723
724         /* 
725          * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
726          * get_this_arg_from_call will call it if needed.
727          */
728         delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
729
730         if (delegate->method) {
731                 method = delegate->method;
732
733                 /*
734                  * delegate->method_ptr == NULL means the delegate was initialized by 
735                  * mini_delegate_ctor, while != NULL means it is initialized by 
736                  * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
737                  * (ctor_with_method () does this, but it doesn't store the wrapper back into
738                  * delegate->method).
739                  */
740                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
741                         method = mono_marshal_get_remoting_invoke (method);
742                 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
743                         method = mono_marshal_get_unbox_wrapper (method);
744         } else {
745                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
746                 if (ji)
747                         method = ji->method;
748         }
749         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
750
751         if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
752                 method = mono_marshal_get_synchronized_wrapper (method);
753
754         if (method && mono_method_needs_static_rgctx_invoke (method, FALSE)) {
755 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
756                 need_rgctx_tramp = TRUE;
757 #else
758                 method = mono_marshal_get_static_rgctx_invoke (method);
759 #endif
760         }
761
762         /* 
763          * If the called address is a trampoline, replace it with the compiled method so
764          * further calls don't have to go through the trampoline.
765          */
766         if (method && !callvirt) {
767                 /* Avoid the overhead of looking up an already compiled method if possible */
768                 if (delegate->method_code && *delegate->method_code) {
769                         delegate->method_ptr = *delegate->method_code;
770                 } else {
771                         delegate->method_ptr = mono_compile_method (method);
772                         if (delegate->method_code)
773                                 *delegate->method_code = delegate->method_ptr;
774                         mono_debugger_trampoline_compiled (method, delegate->method_ptr);
775                 }
776         }
777
778 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
779         if (need_rgctx_tramp)
780                 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
781 #endif
782
783         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
784         if (!multicast && !callvirt) {
785                 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
786                         /* Closed static delegate */
787                         code = impl_this;
788                 else
789                         code = delegate->target ? impl_this : impl_nothis;
790
791                 if (code) {
792                         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
793                         return code;
794                 }
795         }
796
797         /* The general, unoptimized case */
798         m = mono_marshal_get_delegate_invoke (invoke, delegate);
799         code = mono_compile_method (m);
800         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
801         mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
802
803         return code;
804 }
805
806 #endif
807
808 /*
809  * mono_get_trampoline_func:
810  *
811  *   Return the C function which needs to be called by the generic trampoline of type
812  * TRAMP_TYPE.
813  */
814 gconstpointer
815 mono_get_trampoline_func (MonoTrampolineType tramp_type)
816 {
817         switch (tramp_type) {
818         case MONO_TRAMPOLINE_JIT:
819         case MONO_TRAMPOLINE_JUMP:
820                 return mono_magic_trampoline;
821         case MONO_TRAMPOLINE_CLASS_INIT:
822                 return mono_class_init_trampoline;
823         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
824                 return mono_generic_class_init_trampoline;
825         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
826                 return mono_rgctx_lazy_fetch_trampoline;
827 #ifdef MONO_ARCH_AOT_SUPPORTED
828         case MONO_TRAMPOLINE_AOT:
829                 return mono_aot_trampoline;
830         case MONO_TRAMPOLINE_AOT_PLT:
831                 return mono_aot_plt_trampoline;
832 #endif
833 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
834         case MONO_TRAMPOLINE_DELEGATE:
835                 return mono_delegate_trampoline;
836 #endif
837         case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
838                 return mono_altstack_restore_prot;
839         case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
840                 return mono_generic_virtual_remoting_trampoline;
841         case MONO_TRAMPOLINE_MONITOR_ENTER:
842                 return mono_monitor_enter_trampoline;
843         case MONO_TRAMPOLINE_MONITOR_EXIT:
844                 return mono_monitor_exit_trampoline;
845         default:
846                 g_assert_not_reached ();
847                 return NULL;
848         }
849 }
850
851 void
852 mono_trampolines_init (void)
853 {
854         InitializeCriticalSection (&trampolines_mutex);
855
856         if (mono_aot_only)
857                 return;
858
859         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
860         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
861         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
862         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
863         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
864 #ifdef MONO_ARCH_AOT_SUPPORTED
865         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
866         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
867 #endif
868 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
869         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
870 #endif
871         mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
872         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
873         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
874         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
875 }
876
877 void
878 mono_trampolines_cleanup (void)
879 {
880         if (class_init_hash_addr)
881                 g_hash_table_destroy (class_init_hash_addr);
882         if (delegate_trampoline_hash_addr)
883                 g_hash_table_destroy (delegate_trampoline_hash_addr);
884
885         DeleteCriticalSection (&trampolines_mutex);
886 }
887
888 guint8 *
889 mono_get_trampoline_code (MonoTrampolineType tramp_type)
890 {
891         g_assert (mono_trampoline_code [tramp_type]);
892
893         return mono_trampoline_code [tramp_type];
894 }
895
896 gpointer
897 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
898 {
899         if (mono_aot_only)
900                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
901         else
902                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
903 }
904
905 gpointer
906 mono_create_class_init_trampoline (MonoVTable *vtable)
907 {
908         gpointer code, ptr;
909         MonoDomain *domain = vtable->domain;
910
911         g_assert (!vtable->klass->generic_container);
912
913         /* previously created trampoline code */
914         mono_domain_lock (domain);
915         ptr = 
916                 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
917                                                                   vtable);
918         mono_domain_unlock (domain);
919         if (ptr)
920                 return ptr;
921
922         code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
923
924         ptr = mono_create_ftnptr (domain, code);
925
926         /* store trampoline address */
927         mono_domain_lock (domain);
928         g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
929                                                           vtable, ptr);
930         mono_domain_unlock (domain);
931
932         mono_trampolines_lock ();
933         if (!class_init_hash_addr)
934                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
935         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
936         mono_trampolines_unlock ();
937
938         return ptr;
939 }
940
941 gpointer
942 mono_create_generic_class_init_trampoline (void)
943 {
944 #ifdef MONO_ARCH_VTABLE_REG
945         static gpointer code;
946
947         mono_trampolines_lock ();
948
949         if (!code) {
950                 if (mono_aot_only)
951                         code = mono_aot_get_named_code ("generic_class_init_trampoline");
952                 else
953                         code = mono_arch_create_generic_class_init_trampoline ();
954         }
955
956         mono_trampolines_unlock ();
957
958         return code;
959 #else
960         g_assert_not_reached ();
961 #endif
962 }
963
964 gpointer
965 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
966 {
967         MonoJitInfo *ji;
968         gpointer code;
969         guint32 code_size = 0;
970
971         code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
972         /*
973          * We cannot recover the correct type of a shared generic
974          * method from its native code address, so we use the
975          * trampoline instead.
976          */
977         if (code && !ji->has_generic_jit_info)
978                 return code;
979
980         mono_domain_lock (domain);
981         code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
982         mono_domain_unlock (domain);
983         if (code)
984                 return code;
985
986         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
987         g_assert (code_size);
988
989         ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
990         ji->code_start = code;
991         ji->code_size = code_size;
992         ji->method = method;
993
994         /*
995          * mono_delegate_ctor needs to find the method metadata from the 
996          * trampoline address, so we save it here.
997          */
998
999         mono_jit_info_table_add (domain, ji);
1000
1001         mono_domain_lock (domain);
1002         g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1003         mono_domain_unlock (domain);
1004
1005         return ji->code_start;
1006 }
1007
1008 gpointer
1009 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1010 {
1011         gpointer tramp;
1012
1013         if (mono_aot_only) {
1014                 /* Avoid creating trampolines if possible */
1015                 gpointer code = mono_jit_find_compiled_method (domain, method);
1016                 
1017                 if (code)
1018                         return code;
1019         }
1020
1021         mono_domain_lock (domain);
1022         tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1023         mono_domain_unlock (domain);
1024         if (tramp)
1025                 return tramp;
1026
1027         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1028         
1029         mono_domain_lock (domain);
1030         g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1031         mono_domain_unlock (domain);
1032
1033         mono_jit_stats.method_trampolines++;
1034
1035         return tramp;
1036 }       
1037
1038 gpointer
1039 mono_create_jit_trampoline (MonoMethod *method)
1040 {
1041         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1042 }
1043
1044 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1045 gpointer
1046 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1047 {
1048         gpointer tramp;
1049
1050         MonoDomain *domain = mono_domain_get ();
1051         guint8 *buf, *start;
1052
1053         buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1054
1055         *(gpointer*)(gpointer)buf = image;
1056         buf += sizeof (gpointer);
1057         *(guint32*)(gpointer)buf = token;
1058
1059         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1060
1061         mono_jit_stats.method_trampolines++;
1062
1063         return tramp;
1064 }       
1065 #endif
1066
1067 gpointer
1068 mono_create_delegate_trampoline (MonoClass *klass)
1069 {
1070 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1071         MonoDomain *domain = mono_domain_get ();
1072         gpointer ptr;
1073         guint32 code_size = 0;
1074         gpointer *tramp_data;
1075         MonoMethod *invoke;
1076
1077         mono_domain_lock (domain);
1078         ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1079         mono_domain_unlock (domain);
1080         if (ptr)
1081                 return ptr;
1082
1083         // Precompute the delegate invoke impl and pass it to the delegate trampoline
1084         invoke = mono_get_delegate_invoke (klass);
1085         g_assert (invoke);
1086
1087         tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1088         tramp_data [0] = invoke;
1089         tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1090         tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1091
1092         ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1093         g_assert (code_size);
1094
1095         /* store trampoline address */
1096         mono_domain_lock (domain);
1097         g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1098                                                           klass, ptr);
1099         mono_domain_unlock (domain);
1100
1101         mono_trampolines_lock ();
1102         if (!delegate_trampoline_hash_addr)
1103                 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1104         g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
1105         mono_trampolines_unlock ();
1106
1107         return ptr;
1108 #else
1109         return NULL;
1110 #endif
1111 }
1112
1113 gpointer
1114 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1115 {
1116         static gboolean inited = FALSE;
1117         static int num_trampolines = 0;
1118
1119         gpointer tramp, ptr;
1120
1121         if (mono_aot_only)
1122                 return mono_aot_get_lazy_fetch_trampoline (offset);
1123
1124         mono_trampolines_lock ();
1125         if (rgctx_lazy_fetch_trampoline_hash)
1126                 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1127         else
1128                 tramp = NULL;
1129         mono_trampolines_unlock ();
1130         if (tramp)
1131                 return tramp;
1132
1133         tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1134         ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1135
1136         mono_trampolines_lock ();
1137         if (!rgctx_lazy_fetch_trampoline_hash) {
1138                 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1139                 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1140         }
1141         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1142         g_assert (offset != -1);
1143         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1144         mono_trampolines_unlock ();
1145
1146         if (!inited) {
1147                 mono_counters_register ("RGCTX num lazy fetch trampolines",
1148                                 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1149                 inited = TRUE;
1150         }
1151         num_trampolines++;
1152
1153         return ptr;
1154 }
1155
1156 gpointer
1157 mono_create_monitor_enter_trampoline (void)
1158 {
1159         static gpointer code;
1160
1161         if (mono_aot_only) {
1162                 if (!code)
1163                         code = mono_aot_get_named_code ("monitor_enter_trampoline");
1164                 return code;
1165         }
1166
1167 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1168         mono_trampolines_lock ();
1169
1170         if (!code)
1171                 code = mono_arch_create_monitor_enter_trampoline ();
1172
1173         mono_trampolines_unlock ();
1174 #else
1175         code = NULL;
1176         g_assert_not_reached ();
1177 #endif
1178
1179         return code;
1180 }
1181
1182 gpointer
1183 mono_create_monitor_exit_trampoline (void)
1184 {
1185         static gpointer code;
1186
1187         if (mono_aot_only) {
1188                 if (!code)
1189                         code = mono_aot_get_named_code ("monitor_exit_trampoline");
1190                 return code;
1191         }
1192
1193 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1194         mono_trampolines_lock ();
1195
1196         if (!code)
1197                 code = mono_arch_create_monitor_exit_trampoline ();
1198
1199         mono_trampolines_unlock ();
1200 #else
1201         code = NULL;
1202         g_assert_not_reached ();
1203 #endif
1204         return code;
1205 }
1206
1207 MonoVTable*
1208 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1209 {
1210         MonoVTable *res;
1211
1212         mono_trampolines_lock ();
1213         if (class_init_hash_addr)
1214                 res = g_hash_table_lookup (class_init_hash_addr, addr);
1215         else
1216                 res = NULL;
1217         mono_trampolines_unlock ();
1218         return res;
1219 }
1220
1221 MonoClass*
1222 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
1223 {
1224         MonoClass *res;
1225
1226         mono_trampolines_lock ();
1227         if (delegate_trampoline_hash_addr)
1228                 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
1229         else
1230                 res = NULL;
1231         mono_trampolines_unlock ();
1232         return res;
1233 }
1234
1235 guint32
1236 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1237 {
1238         int offset;
1239
1240         mono_trampolines_lock ();
1241         if (rgctx_lazy_fetch_trampoline_hash_addr) {
1242                 /* We store the real offset + 1 so we can detect when the lookup fails */
1243                 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1244                 if (offset)
1245                         offset -= 1;
1246                 else
1247                         offset = -1;
1248         } else {
1249                 offset = -1;
1250         }
1251         mono_trampolines_unlock ();
1252         return offset;
1253 }