2003-04-24 Martin Baulig <martin@ximian.com>
[mono.git] / mono / metadata / loader.c
1 /*
2  * loader.c: Image Loader 
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Miguel de Icaza (miguel@ximian.com)
7  *   Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  *
11  * This file is used by the interpreter and the JIT engine to locate
12  * assemblies.  Used to load AssemblyRef and later to resolve various
13  * kinds of `Refs'.
14  *
15  * TODO:
16  *   This should keep track of the assembly versions that we are loading.
17  *
18  */
19 #include <config.h>
20 #include <glib.h>
21 #include <gmodule.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/image.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/cil-coff.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35
36 static gboolean dummy_icall = TRUE;
37
38 MonoDefaults mono_defaults;
39
40 static GHashTable *icall_hash = NULL;
41
42 void
43 mono_add_internal_call (const char *name, gconstpointer method)
44 {
45         if (!icall_hash) {
46                 dummy_icall = FALSE;
47                 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
48         }
49
50         g_hash_table_insert (icall_hash, g_strdup (name), (gpointer) method);
51 }
52
53 static void
54 ves_icall_dummy (void)
55 {
56         g_warning ("the mono runtime is not initialized");
57         g_assert_not_reached ();
58 }
59
60 gpointer
61 mono_lookup_internal_call (MonoMethod *method)
62 {
63         char *name;
64         char *tmpsig;
65         gpointer res;
66
67         if (dummy_icall)
68                 return ves_icall_dummy;
69
70         if (!method) {
71                 g_warning ("can't resolve internal call, method is null");
72         }
73
74         if (!icall_hash) {
75                 g_warning ("icall_hash not initialized");
76                 g_assert_not_reached ();
77         }
78
79         if (*method->klass->name_space)
80                 name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, NULL);
81         else
82                 name = g_strconcat (method->klass->name, "::", method->name, NULL);
83         if (!(res = g_hash_table_lookup (icall_hash, name))) {
84                 /* trying to resolve with full signature */
85                 g_free (name);
86         
87                 tmpsig = mono_signature_get_desc(method->signature, TRUE);
88                 if (*method->klass->name_space)
89                         name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
90                 else
91                         name = g_strconcat (method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
92                 if (!(res = g_hash_table_lookup (icall_hash, name))) {
93                         g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", name);
94                         g_print ("\nYour mono runtime and corlib are out of sync.\n");
95                         g_print ("Corlib is: %s", method->klass->image->name);
96                         g_print ("When you update one from cvs you need to update, compile and install\nthe other too.\n");
97                         g_print ("Do not report this as a bug unless you're sure you have updated correctly:\nyou probably have a broken mono install.\n");
98                         g_print ("If you see other errors or faults after this message they are probably related\n");
99                         g_print ("and you need to fix your mono install first.\n");
100
101                         g_free (name);
102                         g_free (tmpsig);
103
104                         return NULL;
105                 }
106
107                 g_free(tmpsig);
108         }
109
110         g_free (name);
111
112         return res;
113 }
114
115 MonoClassField*
116 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
117 {
118         MonoClass *klass;
119         MonoTableInfo *tables = image->tables;
120         guint32 cols[6];
121         guint32 nindex, class;
122         const char *fname;
123         const char *ptr;
124         guint32 idx = mono_metadata_token_index (token);
125
126         if (image->assembly->dynamic) {
127                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
128                 *retklass = result->parent;
129                 return result;
130         }
131
132         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
133         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
134         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
135
136         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
137         
138         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
139         mono_metadata_decode_blob_size (ptr, &ptr);
140         /* we may want to check the signature here... */
141
142         switch (class) {
143         case MEMBERREF_PARENT_TYPEREF:
144                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
145                 if (!klass) {
146                         g_warning ("Missing field %s in typeref index %d", fname, nindex);
147                         return NULL;
148                 }
149                 mono_class_init (klass);
150                 if (retklass)
151                         *retklass = klass;
152                 return mono_class_get_field_from_name (klass, fname);
153         default:
154                 return NULL;
155         }
156 }
157
158 MonoClassField*
159 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass)
160 {
161         MonoClass *k;
162         guint32 type;
163
164         if (image->assembly->dynamic) {
165                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
166                 *retklass = result->parent;
167                 return result;
168         }
169
170         if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
171                 return mono_field_from_memberref (image, token, retklass);
172
173         type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
174         if (!type)
175                 return NULL;
176         k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
177         mono_class_init (k);
178         if (!k)
179                 return NULL;
180         if (retklass)
181                 *retklass = k;
182         return mono_class_get_field (k, token);
183 }
184
185 static MonoMethod *
186 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
187 {
188         int i;
189         while (klass) {
190                 /* mostly dumb search for now */
191                 for (i = 0; i < klass->method.count; ++i) {
192                         MonoMethod *m = klass->methods [i];
193                         if (!strcmp (name, m->name)) {
194                                 if (mono_metadata_signature_equal (sig, m->signature))
195                                         return m;
196                         }
197                 }
198                 klass = klass->parent;
199         }
200         return NULL;
201
202 }
203
204 static MonoMethod *
205 method_from_memberref (MonoImage *image, guint32 idx)
206 {
207         MonoClass *klass;
208         MonoMethod *method;
209         MonoTableInfo *tables = image->tables;
210         guint32 cols[6];
211         guint32 nindex, class;
212         const char *mname;
213         MonoMethodSignature *sig;
214         const char *ptr;
215
216         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
217         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
218         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
219         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
220                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
221
222         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
223         
224         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
225         mono_metadata_decode_blob_size (ptr, &ptr);
226         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
227
228         switch (class) {
229         case MEMBERREF_PARENT_TYPEREF:
230                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
231                 if (!klass) {
232                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
233                         mono_metadata_free_method_signature (sig);
234                         return NULL;
235                 }
236                 mono_class_init (klass);
237                 method = find_method (klass, mname, sig);
238                 if (!method)
239                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
240                 mono_metadata_free_method_signature (sig);
241                 return method;
242         case MEMBERREF_PARENT_TYPESPEC: {
243                 guint32 bcols [MONO_TYPESPEC_SIZE];
244                 guint32 len;
245                 MonoType *type;
246                 MonoMethod *result;
247
248                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
249                                           bcols, MONO_TYPESPEC_SIZE);
250                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
251                 len = mono_metadata_decode_value (ptr, &ptr);   
252                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
253
254                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
255                         klass = mono_class_from_mono_type (type);
256                         mono_class_init (klass);
257                         method = find_method (klass, mname, sig);
258                         if (!method)
259                                 g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
260                         mono_metadata_free_method_signature (sig);
261                         return method;
262                 }
263
264                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
265                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
266                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
267                 result->signature = sig;
268                 result->name = mname;
269
270                 if (!strcmp (mname, ".ctor")) {
271                         /* we special-case this in the runtime. */
272                         result->addr = NULL;
273                         return result;
274                 }
275                 
276                 if (!strcmp (mname, "Set")) {
277                         g_assert (sig->hasthis);
278                         g_assert (type->data.array->rank + 1 == sig->param_count);
279                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
280                         result->addr = NULL;
281                         return result;
282                 }
283
284                 if (!strcmp (mname, "Get")) {
285                         g_assert (sig->hasthis);
286                         g_assert (type->data.array->rank == sig->param_count);
287                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
288                         result->addr = NULL;
289                         return result;
290                 }
291
292                 if (!strcmp (mname, "Address")) {
293                         g_assert (sig->hasthis);
294                         g_assert (type->data.array->rank == sig->param_count);
295                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
296                         result->addr = NULL;
297                         return result;
298                 }
299
300                 g_assert_not_reached ();
301                 break;
302         }
303         case MEMBERREF_PARENT_TYPEDEF:
304                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
305                 if (!klass) {
306                         g_warning ("Missing method %s in assembly %s typedef index %d", mname, image->name, nindex);
307                         mono_metadata_free_method_signature (sig);
308                         return NULL;
309                 }
310                 mono_class_init (klass);
311                 method = find_method (klass, mname, sig);
312                 if (!method)
313                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
314                 mono_metadata_free_method_signature (sig);
315                 return method;
316         default:
317                 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
318                 g_assert_not_reached ();
319         }
320
321         return NULL;
322 }
323
324 typedef struct MonoDllMap MonoDllMap;
325
326 struct MonoDllMap {
327         char *name;
328         char *target;
329         char *dll;
330         MonoDllMap *next;
331 };
332
333 static GHashTable *dll_map;
334
335 int 
336 mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const char **rfunc) {
337         MonoDllMap *map, *tmp;
338
339         if (!dll_map)
340                 return 0;
341         map = g_hash_table_lookup (dll_map, dll);
342         if (!map)
343                 return 0;
344         *rdll = map->target? map->target: dll;
345                 
346         for (tmp = map->next; tmp; tmp = tmp->next) {
347                 if (strcmp (func, tmp->name) == 0) {
348                         *rfunc = tmp->name;
349                         if (tmp->dll)
350                                 *rdll = tmp->dll;
351                         return 1;
352                 }
353         }
354         *rfunc = func;
355         return 1;
356 }
357
358 void
359 mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const char *tfunc) {
360         MonoDllMap *map, *entry;
361
362         if (!dll_map)
363                 dll_map = g_hash_table_new (g_str_hash, g_str_equal);
364
365         map = g_hash_table_lookup (dll_map, dll);
366         if (!map) {
367                 map = g_new0 (MonoDllMap, 1);
368                 map->dll = g_strdup (dll);
369                 if (tdll)
370                         map->target = g_strdup (tdll);
371                 g_hash_table_insert (dll_map, map->dll, map);
372         }
373         if (func) {
374                 entry = g_new0 (MonoDllMap, 1);
375                 entry->name = g_strdup (func);
376                 if (tfunc)
377                         entry->target = g_strdup (tfunc);
378                 if (tdll && map->target && strcmp (map->target, tdll))
379                         entry->dll = g_strdup (tdll);
380                 entry->next = map->next;
381                 map->next = entry;
382         }
383 }
384
385 gpointer
386 mono_lookup_pinvoke_call (MonoMethod *method)
387 {
388         MonoImage *image = method->klass->image;
389         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
390         MonoTableInfo *tables = image->tables;
391         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
392         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
393         guint32 im_cols [MONO_IMPLMAP_SIZE];
394         guint32 scope_token;
395         const char *import = NULL;
396         const char *scope = NULL;
397         char *full_name;
398         GModule *gmodule;
399
400         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
401
402         if (method->addr)
403                 return method->addr;
404         if (!piinfo->implmap_idx)
405                 return NULL;
406         
407         mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
408
409         piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
410         import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
411         scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
412         scope = mono_metadata_string_heap (image, scope_token);
413
414         mono_dllmap_lookup (scope, import, &scope, &import);
415
416         full_name = g_module_build_path (NULL, scope);
417         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
418
419         if (!gmodule) {
420                 gchar *error = g_strdup (g_module_error ());
421                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
422                         g_warning ("Failed to load library %s (%s): %s", full_name, scope, error);
423                         g_free (error);
424                         g_free (full_name);
425                         return NULL;
426                 }
427                 g_free (error);
428         }
429         g_free (full_name);
430
431         g_module_symbol (gmodule, import, &method->addr); 
432
433         if (!method->addr) {
434                 g_warning ("Failed to load function %s from %s", import, scope);
435                 return NULL;
436         }
437         return method->addr;
438 }
439
440 MonoMethod *
441 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
442 {
443         MonoMethod *result;
444         int table = mono_metadata_token_table (token);
445         int idx = mono_metadata_token_index (token);
446         MonoTableInfo *tables = image->tables;
447         const char *loc, *sig = NULL;
448         int size;
449         guint32 cols [MONO_TYPEDEF_SIZE];
450
451         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
452                         return result;
453
454         if (image->assembly->dynamic)
455                 return mono_lookup_dynamic_token (image, token);
456
457         if (table != MONO_TABLE_METHOD) {
458                 if (table != MONO_TABLE_MEMBERREF)
459                         g_print("got wrong token: 0x%08x\n", token);
460                 g_assert (table == MONO_TABLE_MEMBERREF);
461                 result = method_from_memberref (image, idx);
462                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
463                 return result;
464         }
465
466         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
467
468         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
469             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
470                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
471         else 
472                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
473         
474         result->slot = -1;
475         result->klass = klass;
476         result->flags = cols [2];
477         result->iflags = cols [1];
478         result->token = token;
479         result->name = mono_metadata_string_heap (image, cols [3]);
480
481         if (!sig) /* already taken from the methodref */
482                 sig = mono_metadata_blob_heap (image, cols [4]);
483         size = mono_metadata_decode_blob_size (sig, &sig);
484         result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
485
486         if (!result->klass) {
487                 guint32 type = mono_metadata_typedef_from_method (image, token);
488                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
489         }
490
491         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
492                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
493                         result->string_ctor = 1;
494
495                 result->addr = mono_lookup_internal_call (result);
496                 result->signature->pinvoke = 1;
497         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
498                 result->signature->pinvoke = 1;
499                 ((MonoMethodPInvoke *)result)->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
500         } else {
501                 /* if this is a methodref from another module/assembly, this fails */
502                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
503
504                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
505                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
506                         g_assert (loc);
507                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
508                 }
509         }
510
511         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
512
513         return result;
514 }
515
516 void
517 mono_free_method  (MonoMethod *method)
518 {
519         mono_metadata_free_method_signature (method->signature);
520         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
521                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
522                 g_free (piinfo->code);
523         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
524                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
525         }
526
527         g_free (method);
528 }
529
530 void
531 mono_method_get_param_names (MonoMethod *method, const char **names)
532 {
533         int i, lastp;
534         MonoClass *klass = method->klass;
535         MonoTableInfo *methodt;
536         MonoTableInfo *paramt;
537
538         if (!method->signature->param_count)
539                 return;
540         for (i = 0; i < method->signature->param_count; ++i)
541                 names [i] = "";
542
543         mono_class_init (klass);
544
545         if (klass->wastypebuilder) /* copy the names later */
546                 return;
547
548         methodt = &klass->image->tables [MONO_TABLE_METHOD];
549         paramt = &klass->image->tables [MONO_TABLE_PARAM];
550         for (i = 0; i < klass->method.count; ++i) {
551                 if (method == klass->methods [i]) {
552                         guint32 idx = klass->method.first + i;
553                         guint32 cols [MONO_PARAM_SIZE];
554                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
555
556                         if (idx + 1 < methodt->rows)
557                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
558                         else
559                                 lastp = paramt->rows + 1;
560                         for (i = param_index; i < lastp; ++i) {
561                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
562                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
563                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
564                         }
565                         return;
566                 }
567         }
568 }
569
570 void
571 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
572 {
573         int i, lastp;
574         MonoClass *klass = method->klass;
575         MonoTableInfo *methodt;
576         MonoTableInfo *paramt;
577
578         for (i = 0; i < method->signature->param_count + 1; ++i)
579                 mspecs [i] = NULL;
580
581         mono_class_init (klass);
582
583         methodt = &klass->image->tables [MONO_TABLE_METHOD];
584         paramt = &klass->image->tables [MONO_TABLE_PARAM];
585
586         for (i = 0; i < klass->method.count; ++i) {
587                 if (method == klass->methods [i]) {
588                         guint32 idx = klass->method.first + i;
589                         guint32 cols [MONO_PARAM_SIZE];
590                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
591
592                         if (idx + 1 < methodt->rows)
593                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
594                         else
595                                 lastp = paramt->rows + 1;
596
597                         for (i = param_index; i < lastp; ++i) {
598                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
599
600                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
601                                         const char *tp;
602                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
603                                         g_assert (tp);
604                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
605                                 }
606                         }
607                         return;
608                 }
609         }
610 }
611
612 gpointer
613 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
614 {
615         GList *l;
616         g_assert (method != NULL);
617         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
618
619         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
620                 g_assert_not_reached ();
621
622         return l->data;
623 }
624
625 static void
626 default_stack_walk (MonoStackWalk func, gpointer user_data) {
627         g_error ("stack walk not installed");
628 }
629
630 static MonoStackWalkImpl stack_walk = default_stack_walk;
631
632 void
633 mono_stack_walk (MonoStackWalk func, gpointer user_data)
634 {
635         stack_walk (func, user_data);
636 }
637
638 void
639 mono_install_stack_walk (MonoStackWalkImpl func)
640 {
641         stack_walk = func;
642 }
643
644 static gboolean
645 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
646 {
647         MonoMethod **dest = data;
648         *dest = m;
649         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
650
651         return managed;
652 }
653
654 MonoMethod*
655 mono_method_get_last_managed (void)
656 {
657         MonoMethod *m = NULL;
658         stack_walk (last_managed, &m);
659         return m;
660 }
661
662
663