Thu May 29 11:34:55 CEST 2003 Paolo Molaro <lupus@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\n", method->klass->image->name);
96                         g_print ("\nWhen 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         if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
432                 g_module_symbol (gmodule, import, &method->addr); 
433         } else {
434                 char *mangled_name;
435
436                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
437                 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
438                         mangled_name = g_strconcat (import, "W", NULL);
439                         g_module_symbol (gmodule, mangled_name, &method->addr); 
440                         g_free (mangled_name);
441
442                         if (!method->addr)
443                                 g_module_symbol (gmodule, import, &method->addr); 
444                         break;
445                 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
446                         g_module_symbol (gmodule, import, &method->addr); 
447                         break;
448                 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
449                 default:
450                         mangled_name = g_strconcat (import, "A", NULL);
451                         g_module_symbol (gmodule, mangled_name, &method->addr); 
452                         g_free (mangled_name);
453
454                         if (!method->addr)
455                                 g_module_symbol (gmodule, import, &method->addr); 
456                                
457                         break;                                  
458                 }
459         }
460
461         if (!method->addr) {
462                 g_warning ("Failed to load function %s from %s", import, scope);
463                 return NULL;
464         }
465         return method->addr;
466 }
467
468 MonoMethod *
469 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
470 {
471         MonoMethod *result;
472         int table = mono_metadata_token_table (token);
473         int idx = mono_metadata_token_index (token);
474         MonoTableInfo *tables = image->tables;
475         const char *loc, *sig = NULL;
476         int size;
477         guint32 cols [MONO_TYPEDEF_SIZE];
478
479         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
480                         return result;
481
482         if (image->assembly->dynamic)
483                 return mono_lookup_dynamic_token (image, token);
484
485         if (table != MONO_TABLE_METHOD) {
486                 if (table == MONO_TABLE_METHODSPEC) {
487                         /* just a temporary hack */
488                         mono_metadata_decode_row (&tables [table], idx - 1, cols, MONO_METHODSPEC_SIZE);
489                         token = cols [MONO_METHODSPEC_METHOD];
490                         if ((token & METHODDEFORREF_MASK) == METHODDEFORREF_METHODDEF)
491                                 token = MONO_TOKEN_METHOD_DEF | (token >> METHODDEFORREF_BITS);
492                         else
493                                 token = MONO_TOKEN_MEMBER_REF | (token >> METHODDEFORREF_BITS);
494                         return mono_get_method (image, token, klass);
495                 }
496                 if (table != MONO_TABLE_MEMBERREF)
497                         g_print("got wrong token: 0x%08x\n", token);
498                 g_assert (table == MONO_TABLE_MEMBERREF);
499                 result = method_from_memberref (image, idx);
500                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
501                 return result;
502         }
503
504         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
505
506         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
507             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
508                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
509         else 
510                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
511         
512         result->slot = -1;
513         result->klass = klass;
514         result->flags = cols [2];
515         result->iflags = cols [1];
516         result->token = token;
517         result->name = mono_metadata_string_heap (image, cols [3]);
518
519         if (!sig) /* already taken from the methodref */
520                 sig = mono_metadata_blob_heap (image, cols [4]);
521         size = mono_metadata_decode_blob_size (sig, &sig);
522         result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
523
524         if (!result->klass) {
525                 guint32 type = mono_metadata_typedef_from_method (image, token);
526                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
527         }
528
529         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
530                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
531                         result->string_ctor = 1;
532
533                 result->addr = mono_lookup_internal_call (result);
534                 result->signature->pinvoke = 1;
535         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
536                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
537                 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
538                 MonoCallConvention conv;
539
540                 result->signature->pinvoke = 1;
541                 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
542                 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
543
544                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
545                 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
546                         conv = MONO_CALL_DEFAULT;
547                         break;
548                 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
549                         conv = MONO_CALL_C;
550                         break;
551                 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
552                         conv = MONO_CALL_STDCALL;
553                         break;
554                 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
555                         conv = MONO_CALL_THISCALL;
556                         break;
557                 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
558                         conv = MONO_CALL_FASTCALL;
559                         break;
560                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
561                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
562                 default:
563                         g_warning ("unsupported calling convention");
564                         g_assert_not_reached ();
565                 }       
566                 result->signature->call_convention = conv;
567         } else {
568                 /* if this is a methodref from another module/assembly, this fails */
569                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
570
571                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
572                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
573                         g_assert (loc);
574                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
575                 }
576         }
577
578         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
579
580         return result;
581 }
582
583 void
584 mono_free_method  (MonoMethod *method)
585 {
586         mono_metadata_free_method_signature (method->signature);
587         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
588                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
589                 g_free (piinfo->code);
590         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
591                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
592         }
593
594         g_free (method);
595 }
596
597 void
598 mono_method_get_param_names (MonoMethod *method, const char **names)
599 {
600         int i, lastp;
601         MonoClass *klass = method->klass;
602         MonoTableInfo *methodt;
603         MonoTableInfo *paramt;
604
605         if (!method->signature->param_count)
606                 return;
607         for (i = 0; i < method->signature->param_count; ++i)
608                 names [i] = "";
609
610         mono_class_init (klass);
611
612         if (klass->wastypebuilder) /* copy the names later */
613                 return;
614
615         methodt = &klass->image->tables [MONO_TABLE_METHOD];
616         paramt = &klass->image->tables [MONO_TABLE_PARAM];
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                         for (i = param_index; i < lastp; ++i) {
628                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
629                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
630                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
631                         }
632                         return;
633                 }
634         }
635 }
636
637 void
638 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
639 {
640         int i, lastp;
641         MonoClass *klass = method->klass;
642         MonoTableInfo *methodt;
643         MonoTableInfo *paramt;
644
645         for (i = 0; i < method->signature->param_count + 1; ++i)
646                 mspecs [i] = NULL;
647
648         mono_class_init (klass);
649
650         methodt = &klass->image->tables [MONO_TABLE_METHOD];
651         paramt = &klass->image->tables [MONO_TABLE_PARAM];
652
653         for (i = 0; i < klass->method.count; ++i) {
654                 if (method == klass->methods [i]) {
655                         guint32 idx = klass->method.first + i;
656                         guint32 cols [MONO_PARAM_SIZE];
657                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
658
659                         if (idx + 1 < methodt->rows)
660                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
661                         else
662                                 lastp = paramt->rows + 1;
663
664                         for (i = param_index; i < lastp; ++i) {
665                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
666
667                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
668                                         const char *tp;
669                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
670                                         g_assert (tp);
671                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
672                                 }
673                         }
674
675                         return;
676                 }
677         }
678 }
679
680 gpointer
681 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
682 {
683         GList *l;
684         g_assert (method != NULL);
685         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
686
687         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
688                 g_assert_not_reached ();
689
690         return l->data;
691 }
692
693 static void
694 default_stack_walk (MonoStackWalk func, gpointer user_data) {
695         g_error ("stack walk not installed");
696 }
697
698 static MonoStackWalkImpl stack_walk = default_stack_walk;
699
700 void
701 mono_stack_walk (MonoStackWalk func, gpointer user_data)
702 {
703         stack_walk (func, user_data);
704 }
705
706 void
707 mono_install_stack_walk (MonoStackWalkImpl func)
708 {
709         stack_walk = func;
710 }
711
712 static gboolean
713 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
714 {
715         MonoMethod **dest = data;
716         *dest = m;
717         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
718
719         return managed;
720 }
721
722 MonoMethod*
723 mono_method_get_last_managed (void)
724 {
725         MonoMethod *m = NULL;
726         stack_walk (last_managed, &m);
727         return m;
728 }
729
730
731