2003-01-10 Dietmar Maurer <dietmar@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), 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 ("When you update one from cvs you need to update, compile and install\nthe other too.\n");
96                         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");
97                         g_print ("If you see other errors or faults after this message they are probably related\n");
98                         g_print ("and you need to fix your mono install first.\n");
99
100                         g_free (name);
101                         g_free (tmpsig);
102
103                         return NULL;
104                 }
105
106                 g_free(tmpsig);
107         }
108
109         g_free (name);
110
111         return res;
112 }
113
114 MonoClassField*
115 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
116 {
117         MonoClass *klass;
118         MonoTableInfo *tables = image->tables;
119         guint32 cols[6];
120         guint32 nindex, class;
121         const char *fname;
122         const char *ptr;
123         guint32 idx = mono_metadata_token_index (token);
124
125         if (image->assembly->dynamic) {
126                 MonoClassField *result;
127                 MonoDynamicAssembly *assembly = image->assembly->dynamic;
128                 MonoObject *obj;
129
130                 obj = g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token));
131                 g_assert (obj);
132                 if (strcmp (obj->vtable->klass->name, "MonoField") == 0) {
133                         result = ((MonoReflectionField*)obj)->field;
134                         g_assert (result);
135                 }
136                 else
137                         g_assert_not_reached ();
138                 *retklass = result->parent;
139                 return result;
140         }
141
142         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
143         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
144         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
145
146         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
147         
148         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
149         mono_metadata_decode_blob_size (ptr, &ptr);
150         /* we may want to check the signature here... */
151
152         switch (class) {
153         case MEMBERREF_PARENT_TYPEREF:
154                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
155                 if (!klass) {
156                         g_warning ("Missing field %s in typeref index %d", fname, nindex);
157                         return NULL;
158                 }
159                 mono_class_init (klass);
160                 if (retklass)
161                         *retklass = klass;
162                 return mono_class_get_field_from_name (klass, fname);
163         default:
164                 return NULL;
165         }
166 }
167
168 MonoClassField*
169 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass)
170 {
171         MonoClass *k;
172         guint32 type;
173
174         if (image->assembly->dynamic) {
175                 MonoClassField *result;
176                 MonoDynamicAssembly *assembly = image->assembly->dynamic;
177                 MonoObject *obj;
178
179                 obj = g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token));
180                 g_assert (obj);
181                 if (strcmp (obj->vtable->klass->name, "MonoField") == 0) {
182                         result = ((MonoReflectionField*)obj)->field;
183                         g_assert (result);
184                 }
185                 else if (strcmp (obj->vtable->klass->name, "FieldBuilder") == 0) {
186                         MonoReflectionFieldBuilder *fb = (MonoReflectionFieldBuilder *)obj;
187                         result = fb->handle;
188                         g_assert (result);
189                 }
190                 else {
191                         g_print (obj->vtable->klass->name);
192                         g_assert_not_reached ();
193                 }
194                 *retklass = result->parent;
195                 return result;
196         }
197
198         if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
199                 return mono_field_from_memberref (image, token, retklass);
200
201         type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
202         if (!type)
203                 return NULL;
204         k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
205         mono_class_init (k);
206         if (!k)
207                 return NULL;
208         if (retklass)
209                 *retklass = k;
210         return mono_class_get_field (k, token);
211 }
212
213 static MonoMethod *
214 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
215 {
216         int i;
217         while (klass) {
218                 /* mostly dumb search for now */
219                 for (i = 0; i < klass->method.count; ++i) {
220                         MonoMethod *m = klass->methods [i];
221                         if (!strcmp (name, m->name)) {
222                                 if (mono_metadata_signature_equal (sig, m->signature))
223                                         return m;
224                         }
225                 }
226                 klass = klass->parent;
227         }
228         return NULL;
229
230 }
231
232 static MonoMethod *
233 method_from_memberref (MonoImage *image, guint32 idx)
234 {
235         MonoClass *klass;
236         MonoMethod *method;
237         MonoTableInfo *tables = image->tables;
238         guint32 cols[6];
239         guint32 nindex, class;
240         const char *mname;
241         MonoMethodSignature *sig;
242         const char *ptr;
243
244         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
245         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
246         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
247         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
248                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
249
250         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
251         
252         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
253         mono_metadata_decode_blob_size (ptr, &ptr);
254         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
255
256         switch (class) {
257         case MEMBERREF_PARENT_TYPEREF:
258                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
259                 if (!klass) {
260                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
261                         mono_metadata_free_method_signature (sig);
262                         return NULL;
263                 }
264                 mono_class_init (klass);
265                 method = find_method (klass, mname, sig);
266                 if (!method)
267                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
268                 mono_metadata_free_method_signature (sig);
269                 return method;
270         case MEMBERREF_PARENT_TYPESPEC: {
271                 guint32 bcols [MONO_TYPESPEC_SIZE];
272                 guint32 len;
273                 MonoType *type;
274                 MonoMethod *result;
275
276                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
277                                           bcols, MONO_TYPESPEC_SIZE);
278                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
279                 len = mono_metadata_decode_value (ptr, &ptr);   
280                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
281
282                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
283                         klass = mono_class_from_mono_type (type);
284                         mono_class_init (klass);
285                         method = find_method (klass, mname, sig);
286                         if (!method)
287                                 g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
288                         mono_metadata_free_method_signature (sig);
289                         return method;
290                 }
291
292                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
293                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
294                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
295                 result->signature = sig;
296                 result->name = mname;
297
298                 if (!strcmp (mname, ".ctor")) {
299                         /* we special-case this in the runtime. */
300                         result->addr = NULL;
301                         return result;
302                 }
303                 
304                 if (!strcmp (mname, "Set")) {
305                         g_assert (sig->hasthis);
306                         g_assert (type->data.array->rank + 1 == sig->param_count);
307                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
308                         result->addr = NULL;
309                         return result;
310                 }
311
312                 if (!strcmp (mname, "Get")) {
313                         g_assert (sig->hasthis);
314                         g_assert (type->data.array->rank == sig->param_count);
315                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
316                         result->addr = NULL;
317                         return result;
318                 }
319
320                 if (!strcmp (mname, "Address")) {
321                         g_assert (sig->hasthis);
322                         g_assert (type->data.array->rank == sig->param_count);
323                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
324                         result->addr = NULL;
325                         return result;
326                 }
327
328                 g_assert_not_reached ();
329                 break;
330         }
331         default:
332                 g_assert_not_reached ();
333         }
334
335         return NULL;
336 }
337
338 typedef struct MonoDllMap MonoDllMap;
339
340 struct MonoDllMap {
341         char *name;
342         char *target;
343         char *dll;
344         MonoDllMap *next;
345 };
346
347 static GHashTable *dll_map;
348
349 int 
350 mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const char **rfunc) {
351         MonoDllMap *map, *tmp;
352
353         if (!dll_map)
354                 return 0;
355         map = g_hash_table_lookup (dll_map, dll);
356         if (!map)
357                 return 0;
358         *rdll = map->target? map->target: dll;
359                 
360         for (tmp = map->next; tmp; tmp = tmp->next) {
361                 if (strcmp (func, tmp->name) == 0) {
362                         *rfunc = tmp->name;
363                         if (tmp->dll)
364                                 *rdll = tmp->dll;
365                         return 1;
366                 }
367         }
368         *rfunc = func;
369         return 1;
370 }
371
372 void
373 mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const char *tfunc) {
374         MonoDllMap *map, *entry;
375
376         if (!dll_map)
377                 dll_map = g_hash_table_new (g_str_hash, g_str_equal);
378
379         map = g_hash_table_lookup (dll_map, dll);
380         if (!map) {
381                 map = g_new0 (MonoDllMap, 1);
382                 map->dll = g_strdup (dll);
383                 if (tdll)
384                         map->target = g_strdup (tdll);
385                 g_hash_table_insert (dll_map, map->dll, map);
386         }
387         if (func) {
388                 entry = g_new0 (MonoDllMap, 1);
389                 entry->name = g_strdup (func);
390                 if (tfunc)
391                         entry->target = g_strdup (tfunc);
392                 if (tdll && map->target && strcmp (map->target, tdll))
393                         entry->dll = g_strdup (tdll);
394                 entry->next = map->next;
395                 map->next = entry;
396         }
397 }
398
399 gpointer
400 mono_lookup_pinvoke_call (MonoMethod *method)
401 {
402         MonoImage *image = method->klass->image;
403         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
404         MonoTableInfo *tables = image->tables;
405         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
406         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
407         guint32 im_cols [MONO_IMPLMAP_SIZE];
408         guint32 scope_token;
409         const char *import = NULL;
410         const char *scope = NULL;
411         char *full_name;
412         GModule *gmodule;
413
414         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
415
416         if (method->addr)
417                 return method->addr;
418         if (!piinfo->implmap_idx)
419                 return NULL;
420         
421         mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
422
423         piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
424         import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
425         scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
426         scope = mono_metadata_string_heap (image, scope_token);
427
428         mono_dllmap_lookup (scope, import, &scope, &import);
429
430         full_name = g_module_build_path (NULL, scope);
431         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
432
433         if (!gmodule) {
434                 gchar *error = g_strdup (g_module_error ());
435                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
436                         g_warning ("Failed to load library %s (%s): %s", full_name, scope, error);
437                         g_free (error);
438                         g_free (full_name);
439                         return NULL;
440                 }
441                 g_free (error);
442         }
443         g_free (full_name);
444
445         g_module_symbol (gmodule, import, &method->addr); 
446
447         if (!method->addr) {
448                 g_warning ("Failed to load function %s from %s", import, scope);
449                 return NULL;
450         }
451         return method->addr;
452 }
453
454 MonoMethod *
455 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
456 {
457         MonoMethod *result;
458         int table = mono_metadata_token_table (token);
459         int idx = mono_metadata_token_index (token);
460         MonoTableInfo *tables = image->tables;
461         const char *loc, *sig = NULL;
462         int size;
463         guint32 cols [MONO_TYPEDEF_SIZE];
464
465         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
466                         return result;
467
468         if (image->assembly->dynamic) {
469                 MonoDynamicAssembly *assembly = image->assembly->dynamic;
470                 MonoObject *obj;
471
472                 obj = g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token));
473                 g_assert (obj);
474                 if (strcmp (obj->vtable->klass->name, "MonoMethod") == 0) {
475                         result = ((MonoReflectionMethod*)obj)->method;
476                 }
477                 else if (strcmp (obj->vtable->klass->name, "MethodBuilder") == 0) {
478                         result = ((MonoReflectionMethodBuilder*)obj)->mhandle;
479                 }
480                 else {
481                         g_print (obj->vtable->klass->name);
482                         g_assert_not_reached ();
483                 }
484                 g_assert (result);
485                 return result;
486         }
487
488         if (table != MONO_TABLE_METHOD) {
489                 if (table != MONO_TABLE_MEMBERREF)
490                         g_print("got wrong token: 0x%08x\n", token);
491                 g_assert (table == MONO_TABLE_MEMBERREF);
492                 result = method_from_memberref (image, idx);
493                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
494                 return result;
495         }
496
497         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
498
499         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
500             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
501                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
502         else 
503                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
504         
505         result->slot = -1;
506         result->klass = klass;
507         result->flags = cols [2];
508         result->iflags = cols [1];
509         result->token = token;
510         result->name = mono_metadata_string_heap (image, cols [3]);
511
512         if (!sig) /* already taken from the methodref */
513                 sig = mono_metadata_blob_heap (image, cols [4]);
514         size = mono_metadata_decode_blob_size (sig, &sig);
515         result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
516
517         if (!result->klass) {
518                 guint32 type = mono_metadata_typedef_from_method (image, token);
519                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
520         }
521
522         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
523                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
524                         result->string_ctor = 1;
525
526                 result->addr = mono_lookup_internal_call (result);
527                 result->signature->pinvoke = 1;
528         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
529                 result->signature->pinvoke = 1;
530                 ((MonoMethodPInvoke *)result)->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
531         } else {
532                 /* if this is a methodref from another module/assembly, this fails */
533                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
534
535                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
536                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
537                         g_assert (loc);
538                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
539                 }
540         }
541
542         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
543
544         return result;
545 }
546
547 void
548 mono_free_method  (MonoMethod *method)
549 {
550         mono_metadata_free_method_signature (method->signature);
551         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
552                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
553                 g_free (piinfo->code);
554         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
555                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
556         }
557
558         g_free (method);
559 }
560
561 void
562 mono_method_get_param_names (MonoMethod *method, const char **names)
563 {
564         int i, lastp;
565         MonoClass *klass = method->klass;
566         MonoTableInfo *methodt;
567         MonoTableInfo *paramt;
568
569         if (!method->signature->param_count)
570                 return;
571         for (i = 0; i < method->signature->param_count; ++i)
572                 names [i] = "";
573
574         mono_class_init (klass);
575
576         if (klass->wastypebuilder) /* copy the names later */
577                 return;
578
579         methodt = &klass->image->tables [MONO_TABLE_METHOD];
580         paramt = &klass->image->tables [MONO_TABLE_PARAM];
581         for (i = 0; i < klass->method.count; ++i) {
582                 if (method == klass->methods [i]) {
583                         guint32 idx = klass->method.first + i;
584                         guint32 cols [MONO_PARAM_SIZE];
585                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
586
587                         if (idx + 1 < methodt->rows)
588                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
589                         else
590                                 lastp = paramt->rows + 1;
591                         for (i = param_index; i < lastp; ++i) {
592                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
593                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
594                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
595                         }
596                         return;
597                 }
598         }
599 }
600
601 void
602 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
603 {
604         int i, lastp;
605         MonoClass *klass = method->klass;
606         MonoTableInfo *methodt;
607         MonoTableInfo *paramt;
608
609         for (i = 0; i < method->signature->param_count + 1; ++i)
610                 mspecs [i] = NULL;
611
612         mono_class_init (klass);
613
614         methodt = &klass->image->tables [MONO_TABLE_METHOD];
615         paramt = &klass->image->tables [MONO_TABLE_PARAM];
616
617         for (i = 0; i < klass->method.count; ++i) {
618                 if (method == klass->methods [i]) {
619                         guint32 idx = klass->method.first + i;
620                         guint32 cols [MONO_PARAM_SIZE];
621                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
622
623                         if (idx + 1 < methodt->rows)
624                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
625                         else
626                                 lastp = paramt->rows + 1;
627
628                         for (i = param_index; i < lastp; ++i) {
629                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
630
631                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
632                                         const char *tp;
633                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
634                                         g_assert (tp);
635                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
636                                 }
637                         }
638                         return;
639                 }
640         }
641 }
642
643 gpointer
644 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
645 {
646         GList *l;
647         g_assert (method != NULL);
648         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
649
650         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
651                 g_assert_not_reached ();
652
653         return l->data;
654 }
655
656 static void
657 default_stack_walk (MonoStackWalk func, gpointer user_data) {
658         g_error ("stack walk not installed");
659 }
660
661 static MonoStackWalkImpl stack_walk = default_stack_walk;
662
663 void
664 mono_stack_walk (MonoStackWalk func, gpointer user_data)
665 {
666         stack_walk (func, user_data);
667 }
668
669 void
670 mono_install_stack_walk (MonoStackWalkImpl func)
671 {
672         stack_walk = func;
673 }
674
675 static gboolean
676 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
677 {
678         MonoMethod **dest = data;
679         *dest = m;
680         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
681
682         return managed;
683 }
684
685 MonoMethod*
686 mono_method_get_last_managed (void)
687 {
688         MonoMethod *m = NULL;
689         stack_walk (last_managed, &m);
690         return m;
691 }
692
693
694