2003-06-17 Zoltan Varga <vargaz@freemail.hu>
[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 /*
205  * token is the method_ref or method_def token used in a call IL instruction.
206  */
207 MonoMethodSignature*
208 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
209 {
210         int table = mono_metadata_token_table (token);
211         int idx = mono_metadata_token_index (token);
212         guint32 cols [MONO_MEMBERREF_SIZE];
213         MonoMethodSignature *sig;
214         const char *ptr;
215
216         /* !table is for wrappers: we should really assign their own token to them */
217         if (!table || table == MONO_TABLE_METHOD)
218                 return method->signature;
219
220         if (image->assembly->dynamic)
221                 /* FIXME: This might be incorrect for vararg methods */
222                 return method->signature;
223
224         mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
225         
226         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
227         mono_metadata_decode_blob_size (ptr, &ptr);
228         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
229
230         return sig;
231 }
232
233 static MonoMethod *
234 method_from_memberref (MonoImage *image, guint32 idx)
235 {
236         MonoClass *klass;
237         MonoMethod *method;
238         MonoTableInfo *tables = image->tables;
239         guint32 cols[6];
240         guint32 nindex, class;
241         const char *mname;
242         MonoMethodSignature *sig;
243         const char *ptr;
244
245         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
246         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
247         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
248         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
249                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
250
251         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
252         
253         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
254         mono_metadata_decode_blob_size (ptr, &ptr);
255         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
256
257         switch (class) {
258         case MEMBERREF_PARENT_TYPEREF:
259                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
260                 if (!klass) {
261                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
262                         mono_metadata_free_method_signature (sig);
263                         return NULL;
264                 }
265                 mono_class_init (klass);
266                 method = find_method (klass, mname, sig);
267                 if (!method)
268                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
269                 mono_metadata_free_method_signature (sig);
270                 return method;
271         case MEMBERREF_PARENT_TYPESPEC: {
272                 guint32 bcols [MONO_TYPESPEC_SIZE];
273                 guint32 len;
274                 MonoType *type;
275                 MonoMethod *result;
276
277                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
278                                           bcols, MONO_TYPESPEC_SIZE);
279                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
280                 len = mono_metadata_decode_value (ptr, &ptr);   
281                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
282
283                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
284                         klass = mono_class_from_mono_type (type);
285                         mono_class_init (klass);
286                         method = find_method (klass, mname, sig);
287                         if (!method)
288                                 g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
289                         mono_metadata_free_method_signature (sig);
290                         return method;
291                 }
292
293                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
294                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
295                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
296                 result->signature = sig;
297                 result->name = mname;
298
299                 if (!strcmp (mname, ".ctor")) {
300                         /* we special-case this in the runtime. */
301                         result->addr = NULL;
302                         return result;
303                 }
304                 
305                 if (!strcmp (mname, "Set")) {
306                         g_assert (sig->hasthis);
307                         g_assert (type->data.array->rank + 1 == sig->param_count);
308                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
309                         result->addr = NULL;
310                         return result;
311                 }
312
313                 if (!strcmp (mname, "Get")) {
314                         g_assert (sig->hasthis);
315                         g_assert (type->data.array->rank == sig->param_count);
316                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
317                         result->addr = NULL;
318                         return result;
319                 }
320
321                 if (!strcmp (mname, "Address")) {
322                         g_assert (sig->hasthis);
323                         g_assert (type->data.array->rank == sig->param_count);
324                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
325                         result->addr = NULL;
326                         return result;
327                 }
328
329                 g_assert_not_reached ();
330                 break;
331         }
332         case MEMBERREF_PARENT_TYPEDEF:
333                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
334                 if (!klass) {
335                         g_warning ("Missing method %s in assembly %s typedef index %d", mname, image->name, nindex);
336                         mono_metadata_free_method_signature (sig);
337                         return NULL;
338                 }
339                 mono_class_init (klass);
340                 method = find_method (klass, mname, sig);
341                 if (!method)
342                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
343                 mono_metadata_free_method_signature (sig);
344                 return method;
345         case MEMBERREF_PARENT_METHODDEF:
346                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
347                 return method;
348         default:
349                 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
350                 g_assert_not_reached ();
351         }
352
353         return NULL;
354 }
355
356 typedef struct MonoDllMap MonoDllMap;
357
358 struct MonoDllMap {
359         char *name;
360         char *target;
361         char *dll;
362         MonoDllMap *next;
363 };
364
365 static GHashTable *dll_map;
366
367 int 
368 mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const char **rfunc) {
369         MonoDllMap *map, *tmp;
370
371         if (!dll_map)
372                 return 0;
373         map = g_hash_table_lookup (dll_map, dll);
374         if (!map)
375                 return 0;
376         *rdll = map->target? map->target: dll;
377                 
378         for (tmp = map->next; tmp; tmp = tmp->next) {
379                 if (strcmp (func, tmp->name) == 0) {
380                         *rfunc = tmp->name;
381                         if (tmp->dll)
382                                 *rdll = tmp->dll;
383                         return 1;
384                 }
385         }
386         *rfunc = func;
387         return 1;
388 }
389
390 void
391 mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const char *tfunc) {
392         MonoDllMap *map, *entry;
393
394         if (!dll_map)
395                 dll_map = g_hash_table_new (g_str_hash, g_str_equal);
396
397         map = g_hash_table_lookup (dll_map, dll);
398         if (!map) {
399                 map = g_new0 (MonoDllMap, 1);
400                 map->dll = g_strdup (dll);
401                 if (tdll)
402                         map->target = g_strdup (tdll);
403                 g_hash_table_insert (dll_map, map->dll, map);
404         }
405         if (func) {
406                 entry = g_new0 (MonoDllMap, 1);
407                 entry->name = g_strdup (func);
408                 if (tfunc)
409                         entry->target = g_strdup (tfunc);
410                 if (tdll && map->target && strcmp (map->target, tdll))
411                         entry->dll = g_strdup (tdll);
412                 entry->next = map->next;
413                 map->next = entry;
414         }
415 }
416
417 gpointer
418 mono_lookup_pinvoke_call (MonoMethod *method)
419 {
420         MonoImage *image = method->klass->image;
421         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
422         MonoTableInfo *tables = image->tables;
423         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
424         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
425         guint32 im_cols [MONO_IMPLMAP_SIZE];
426         guint32 scope_token;
427         const char *import = NULL;
428         const char *scope = NULL;
429         char *full_name;
430         GModule *gmodule;
431
432         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
433
434         if (method->addr)
435                 return method->addr;
436         if (!piinfo->implmap_idx)
437                 return NULL;
438         
439         mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
440
441         piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
442         import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
443         scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
444         scope = mono_metadata_string_heap (image, scope_token);
445
446         mono_dllmap_lookup (scope, import, &scope, &import);
447
448         full_name = g_module_build_path (NULL, scope);
449         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
450
451         if (!gmodule) {
452                 g_free (full_name);
453                 full_name = g_module_build_path (".", scope);
454                 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
455         }
456
457         if (!gmodule) {
458                 gchar *error = g_strdup (g_module_error ());
459                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
460                         g_warning ("Failed to load library %s (%s): %s", full_name, scope, error);
461                         g_free (error);
462                         g_free (full_name);
463                         return NULL;
464                 }
465                 g_free (error);
466         }
467         g_free (full_name);
468
469         if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
470                 g_module_symbol (gmodule, import, &method->addr); 
471         } else {
472                 char *mangled_name;
473
474                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
475                 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
476                         mangled_name = g_strconcat (import, "W", NULL);
477                         g_module_symbol (gmodule, mangled_name, &method->addr); 
478                         g_free (mangled_name);
479
480                         if (!method->addr)
481                                 g_module_symbol (gmodule, import, &method->addr); 
482                         break;
483                 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
484                         g_module_symbol (gmodule, import, &method->addr); 
485                         break;
486                 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
487                 default:
488                         mangled_name = g_strconcat (import, "A", NULL);
489                         g_module_symbol (gmodule, mangled_name, &method->addr); 
490                         g_free (mangled_name);
491
492                         if (!method->addr)
493                                 g_module_symbol (gmodule, import, &method->addr); 
494                                
495                         break;                                  
496                 }
497         }
498
499         if (!method->addr) {
500                 g_warning ("Failed to load function %s from %s", import, scope);
501                 return NULL;
502         }
503         return method->addr;
504 }
505
506 MonoMethod *
507 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
508 {
509         MonoMethod *result;
510         int table = mono_metadata_token_table (token);
511         int idx = mono_metadata_token_index (token);
512         MonoTableInfo *tables = image->tables;
513         const char *loc, *sig = NULL;
514         int size;
515         guint32 cols [MONO_TYPEDEF_SIZE];
516
517         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
518                         return result;
519
520         if (image->assembly->dynamic)
521                 return mono_lookup_dynamic_token (image, token);
522
523         if (table != MONO_TABLE_METHOD) {
524                 if (table == MONO_TABLE_METHODSPEC) {
525                         /* just a temporary hack */
526                         mono_metadata_decode_row (&tables [table], idx - 1, cols, MONO_METHODSPEC_SIZE);
527                         token = cols [MONO_METHODSPEC_METHOD];
528                         if ((token & METHODDEFORREF_MASK) == METHODDEFORREF_METHODDEF)
529                                 token = MONO_TOKEN_METHOD_DEF | (token >> METHODDEFORREF_BITS);
530                         else
531                                 token = MONO_TOKEN_MEMBER_REF | (token >> METHODDEFORREF_BITS);
532                         return mono_get_method (image, token, klass);
533                 }
534                 if (table != MONO_TABLE_MEMBERREF)
535                         g_print("got wrong token: 0x%08x\n", token);
536                 g_assert (table == MONO_TABLE_MEMBERREF);
537                 result = method_from_memberref (image, idx);
538                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
539                 return result;
540         }
541
542         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
543
544         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
545             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
546                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
547         else 
548                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
549         
550         result->slot = -1;
551         result->klass = klass;
552         result->flags = cols [2];
553         result->iflags = cols [1];
554         result->token = token;
555         result->name = mono_metadata_string_heap (image, cols [3]);
556
557         if (!sig) /* already taken from the methodref */
558                 sig = mono_metadata_blob_heap (image, cols [4]);
559         size = mono_metadata_decode_blob_size (sig, &sig);
560         result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
561
562         if (!result->klass) {
563                 guint32 type = mono_metadata_typedef_from_method (image, token);
564                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
565         }
566
567         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
568                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
569                         result->string_ctor = 1;
570
571                 result->addr = mono_lookup_internal_call (result);
572                 result->signature->pinvoke = 1;
573         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
574                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
575                 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
576                 MonoCallConvention conv;
577
578                 result->signature->pinvoke = 1;
579                 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
580                 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
581
582                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
583                 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
584                         conv = MONO_CALL_DEFAULT;
585                         break;
586                 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
587                         conv = MONO_CALL_C;
588                         break;
589                 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
590                         conv = MONO_CALL_STDCALL;
591                         break;
592                 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
593                         conv = MONO_CALL_THISCALL;
594                         break;
595                 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
596                         conv = MONO_CALL_FASTCALL;
597                         break;
598                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
599                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
600                 default:
601                         g_warning ("unsupported calling convention");
602                         g_assert_not_reached ();
603                 }       
604                 result->signature->call_convention = conv;
605         } else {
606                 /* if this is a methodref from another module/assembly, this fails */
607                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
608
609                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
610                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
611                         g_assert (loc);
612                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
613                 }
614         }
615
616         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
617
618         return result;
619 }
620
621 void
622 mono_free_method  (MonoMethod *method)
623 {
624         mono_metadata_free_method_signature (method->signature);
625         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
626                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
627                 g_free (piinfo->code);
628         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
629                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
630         }
631
632         g_free (method);
633 }
634
635 void
636 mono_method_get_param_names (MonoMethod *method, const char **names)
637 {
638         int i, lastp;
639         MonoClass *klass = method->klass;
640         MonoTableInfo *methodt;
641         MonoTableInfo *paramt;
642
643         if (!method->signature->param_count)
644                 return;
645         for (i = 0; i < method->signature->param_count; ++i)
646                 names [i] = "";
647
648         mono_class_init (klass);
649
650         if (klass->wastypebuilder) /* copy the names later */
651                 return;
652
653         methodt = &klass->image->tables [MONO_TABLE_METHOD];
654         paramt = &klass->image->tables [MONO_TABLE_PARAM];
655         for (i = 0; i < klass->method.count; ++i) {
656                 if (method == klass->methods [i]) {
657                         guint32 idx = klass->method.first + i;
658                         guint32 cols [MONO_PARAM_SIZE];
659                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
660
661                         if (idx + 1 < methodt->rows)
662                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
663                         else
664                                 lastp = paramt->rows + 1;
665                         for (i = param_index; i < lastp; ++i) {
666                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
667                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
668                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
669                         }
670                         return;
671                 }
672         }
673 }
674
675 void
676 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
677 {
678         int i, lastp;
679         MonoClass *klass = method->klass;
680         MonoTableInfo *methodt;
681         MonoTableInfo *paramt;
682
683         for (i = 0; i < method->signature->param_count + 1; ++i)
684                 mspecs [i] = NULL;
685
686         mono_class_init (klass);
687
688         methodt = &klass->image->tables [MONO_TABLE_METHOD];
689         paramt = &klass->image->tables [MONO_TABLE_PARAM];
690
691         for (i = 0; i < klass->method.count; ++i) {
692                 if (method == klass->methods [i]) {
693                         guint32 idx = klass->method.first + i;
694                         guint32 cols [MONO_PARAM_SIZE];
695                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
696
697                         if (idx + 1 < methodt->rows)
698                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
699                         else
700                                 lastp = paramt->rows + 1;
701
702                         for (i = param_index; i < lastp; ++i) {
703                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
704
705                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
706                                         const char *tp;
707                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
708                                         g_assert (tp);
709                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
710                                 }
711                         }
712
713                         return;
714                 }
715         }
716 }
717
718 gpointer
719 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
720 {
721         GList *l;
722         g_assert (method != NULL);
723         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
724
725         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
726                 g_assert_not_reached ();
727
728         return l->data;
729 }
730
731 static void
732 default_stack_walk (MonoStackWalk func, gpointer user_data) {
733         g_error ("stack walk not installed");
734 }
735
736 static MonoStackWalkImpl stack_walk = default_stack_walk;
737
738 void
739 mono_stack_walk (MonoStackWalk func, gpointer user_data)
740 {
741         stack_walk (func, user_data);
742 }
743
744 void
745 mono_install_stack_walk (MonoStackWalkImpl func)
746 {
747         stack_walk = func;
748 }
749
750 static gboolean
751 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
752 {
753         MonoMethod **dest = data;
754         *dest = m;
755         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
756
757         return managed;
758 }
759
760 MonoMethod*
761 mono_method_get_last_managed (void)
762 {
763         MonoMethod *m = NULL;
764         stack_walk (last_managed, &m);
765         return m;
766 }
767
768
769