2005-06-08 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/metadata-internals.h>
32 #include <mono/metadata/loader.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/metadata/reflection.h>
36 #include <mono/utils/mono-logger.h>
37
38 MonoDefaults mono_defaults;
39
40 /*
41  * This lock protects the hash tables inside MonoImage used by the metadata 
42  * loading functions in class.c and loader.c.
43  */
44 static CRITICAL_SECTION loader_mutex;
45
46 void
47 mono_loader_init ()
48 {
49         InitializeCriticalSection (&loader_mutex);
50 }
51
52 static MonoClassField*
53 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
54                       MonoGenericContext *context)
55 {
56         MonoClass *klass;
57         MonoTableInfo *tables = image->tables;
58         guint32 cols[6];
59         guint32 nindex, class;
60         const char *fname;
61         const char *ptr;
62         guint32 idx = mono_metadata_token_index (token);
63
64         if (image->dynamic) {
65                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
66                 *retklass = result->parent;
67                 return result;
68         }
69
70         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
71         nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
72         class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
73
74         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
75         
76         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
77         mono_metadata_decode_blob_size (ptr, &ptr);
78         /* we may want to check the signature here... */
79
80         switch (class) {
81         case MONO_MEMBERREF_PARENT_TYPEREF:
82                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
83                 if (!klass) {
84                         char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
85                         g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
86                         g_free (name);
87                         return NULL;
88                 }
89                 mono_class_init (klass);
90                 if (retklass)
91                         *retklass = klass;
92                 return mono_class_get_field_from_name (klass, fname);
93         case MONO_MEMBERREF_PARENT_TYPESPEC: {
94                 /*guint32 bcols [MONO_TYPESPEC_SIZE];
95                 guint32 len;
96                 MonoType *type;
97
98                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
99                                           bcols, MONO_TYPESPEC_SIZE);
100                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
101                 len = mono_metadata_decode_value (ptr, &ptr);   
102                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
103
104                 klass = mono_class_from_mono_type (type);
105                 mono_class_init (klass);
106                 g_print ("type in sig: %s\n", klass->name);*/
107                 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
108                 mono_class_init (klass);
109                 if (retklass)
110                         *retklass = klass;
111                 return mono_class_get_field_from_name (klass, fname);
112         }
113         default:
114                 g_warning ("field load from %x", class);
115                 return NULL;
116         }
117 }
118
119 MonoClassField*
120 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
121                        MonoGenericContext *context)
122 {
123         MonoClass *k;
124         guint32 type;
125         MonoClassField *field;
126
127         if (image->dynamic) {
128                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
129                 *retklass = result->parent;
130                 return result;
131         }
132
133         mono_loader_lock ();
134         if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
135                 *retklass = field->parent;
136                 mono_loader_unlock ();
137                 return field;
138         }
139         mono_loader_unlock ();
140
141         if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
142                 field = field_from_memberref (image, token, retklass, context);
143         else {
144                 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
145                 if (!type)
146                         return NULL;
147                 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
148                 mono_class_init (k);
149                 if (!k)
150                         return NULL;
151                 if (retklass)
152                         *retklass = k;
153                 field = mono_class_get_field (k, token);
154         }
155
156         mono_loader_lock ();
157         if (!field->parent->generic_class)
158                 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
159         mono_loader_unlock ();
160         return field;
161 }
162
163 static gboolean
164 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
165 {
166         int i;
167
168         if (sig1->hasthis != sig2->hasthis ||
169             sig1->sentinelpos != sig2->sentinelpos)
170                 return FALSE;
171
172         for (i = 0; i < sig1->sentinelpos; i++) { 
173                 MonoType *p1 = sig1->params[i];
174                 MonoType *p2 = sig2->params[i];
175                 
176                 /*if (p1->attrs != p2->attrs)
177                         return FALSE;
178                 */
179                 if (!mono_metadata_type_equal (p1, p2))
180                         return FALSE;
181         }
182
183         if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
184                 return FALSE;
185         return TRUE;
186 }
187
188 static MonoMethod *
189 find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignature *sig)
190 {
191         int i;
192         char *qname, *fqname, *class_name;
193         MonoMethod *result = NULL;
194
195         if (ic) {
196                 class_name = mono_class_get_name_full (ic, FALSE, FALSE, FALSE);
197
198                 qname = g_strconcat (class_name, ".", name, NULL); 
199                 if (ic->name_space && ic->name_space [0])
200                         fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
201                 else
202                         fqname = NULL;
203         } else
204                 class_name = qname = fqname = NULL;
205
206         while (klass) {
207                 MonoGenericContext *context = NULL;
208
209                 if (klass->generic_container)
210                         context = &klass->generic_container->context;
211                 else if (klass->generic_class)
212                         context = klass->generic_class->context;
213
214                 mono_class_setup_methods (klass);
215                 for (i = 0; i < klass->method.count; ++i) {
216                         MonoMethod *m = klass->methods [i];
217
218                         if (!((fqname && !strcmp (m->name, fqname)) ||
219                               (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
220                                 continue;
221
222                         if (sig->call_convention == MONO_CALL_VARARG) {
223                                 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m))) {
224                                         result = m;
225                                         goto out;
226                                 }
227                         } else {
228                                 MonoMethodSignature *msig = mono_method_signature_full (m, context);
229                                 if (mono_metadata_signature_equal (sig, msig)) {
230                                         result = m;
231                                         goto out;
232                                 }
233                         }
234                 }
235
236                 if (name [0] == '.' && (strcmp (name, ".ctor") == 0 || strcmp (name, ".cctor") == 0))
237                         break;
238
239                 klass = klass->parent;
240         }
241
242  out:
243         g_free (class_name);
244         g_free (fqname);
245         g_free (qname);
246         return result;
247 }
248
249 /*
250  * token is the method_ref or method_def token used in a call IL instruction.
251  */
252 MonoMethodSignature*
253 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
254 {
255         int table = mono_metadata_token_table (token);
256         int idx = mono_metadata_token_index (token);
257         guint32 cols [MONO_MEMBERREF_SIZE];
258         MonoMethodSignature *sig, *prev_sig;
259         const char *ptr;
260
261         /* !table is for wrappers: we should really assign their own token to them */
262         if (!table || table == MONO_TABLE_METHOD)
263                 return mono_method_signature (method);
264
265         if (table == MONO_TABLE_METHODSPEC) {
266                 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
267                           !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
268                           mono_method_signature (method));
269                 g_assert (method->is_inflated);
270
271                 return mono_method_signature (method);
272         }
273
274         if (method->klass->generic_class)
275                 return mono_method_signature (method);
276
277         if (image->dynamic)
278                 /* FIXME: This might be incorrect for vararg methods */
279                 return mono_method_signature (method);
280
281         mono_loader_lock ();
282         sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
283         mono_loader_unlock ();
284         if (!sig) {
285                 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
286         
287                 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
288                 mono_metadata_decode_blob_size (ptr, &ptr);
289                 sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
290
291                 mono_loader_lock ();
292                 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
293                 if (prev_sig) {
294                         /* Somebody got in before us */
295                         /* FIXME: Free sig */
296                         sig = prev_sig;
297                 }
298                 else
299                         g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
300                 mono_loader_unlock ();
301         }
302
303         sig = mono_class_inflate_generic_signature (image, sig, context);
304
305         return sig;
306 }
307
308 MonoMethodSignature*
309 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
310 {
311         return mono_method_get_signature_full (method, image, token, NULL);
312 }
313
314 static MonoMethod *
315 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
316 {
317         MonoClass *klass = NULL;
318         MonoMethod *method = NULL;
319         MonoTableInfo *tables = image->tables;
320         guint32 cols[6];
321         guint32 nindex, class;
322         MonoGenericClass *gclass = NULL;
323         MonoGenericContainer *container = NULL;
324         const char *mname;
325         MonoMethodSignature *sig;
326         const char *ptr;
327
328         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
329         nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
330         class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
331         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
332                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
333
334         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
335
336         switch (class) {
337         case MONO_MEMBERREF_PARENT_TYPEREF:
338                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
339                 if (!klass) {
340                         char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
341                         g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
342                         g_free (name);
343                         return NULL;
344                 }
345                 break;
346         case MONO_MEMBERREF_PARENT_TYPESPEC:
347                 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
348                 if (!klass) {
349                         char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
350                         g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
351                         g_free (name);
352                         return NULL;
353                 }
354                 break;
355         case MONO_MEMBERREF_PARENT_TYPEDEF:
356                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
357                 if (!klass) {
358                         char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex, context);
359                         g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
360                         g_free (name);
361                         return NULL;
362                 }
363                 break;
364         case MONO_MEMBERREF_PARENT_METHODDEF:
365                 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
366         default:
367                 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
368                 g_assert_not_reached ();
369         }
370         g_assert (klass);
371
372         if (klass->generic_class) {
373                 gclass = klass->generic_class;
374                 klass = gclass->container_class;
375         }
376         if (klass->generic_container)
377                 container = klass->generic_container;
378         mono_class_init (klass);
379
380         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
381         mono_metadata_decode_blob_size (ptr, &ptr);
382         sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
383
384         switch (class) {
385         case MONO_MEMBERREF_PARENT_TYPEREF:
386                 method = find_method (klass, NULL, mname, sig);
387                 if (!method)
388                         g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
389                 mono_metadata_free_method_signature (sig);
390                 break;
391         case MONO_MEMBERREF_PARENT_TYPESPEC: {
392                 MonoType *type;
393                 MonoMethod *result;
394
395                 type = &klass->byval_arg;
396
397                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
398                         method = find_method (klass, NULL, mname, sig);
399                         if (!method)
400                                 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
401                         else if (klass->generic_class && (klass != method->klass))
402                                 method = mono_class_inflate_generic_method (
403                                         method, klass->generic_class->context, klass);
404                         mono_metadata_free_method_signature (sig);
405                         break;
406                 }
407
408                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
409                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
410                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
411                 result->signature = sig;
412                 result->name = mname;
413
414                 if (!strcmp (mname, ".ctor")) {
415                         /* we special-case this in the runtime. */
416                         return result;
417                 }
418                 
419                 if (!strcmp (mname, "Set")) {
420                         g_assert (sig->hasthis);
421                         g_assert (type->data.array->rank + 1 == sig->param_count);
422                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
423                         return result;
424                 }
425
426                 if (!strcmp (mname, "Get")) {
427                         g_assert (sig->hasthis);
428                         g_assert (type->data.array->rank == sig->param_count);
429                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
430                         return result;
431                 }
432
433                 if (!strcmp (mname, "Address")) {
434                         g_assert (sig->hasthis);
435                         g_assert (type->data.array->rank == sig->param_count);
436                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
437                         return result;
438                 }
439
440                 g_assert_not_reached ();
441                 break;
442         }
443         case MONO_MEMBERREF_PARENT_TYPEDEF:
444                 method = find_method (klass, NULL, mname, sig);
445                 if (!method)
446                         g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
447                 mono_metadata_free_method_signature (sig);
448                 break;
449         default:
450                 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
451                 g_assert_not_reached ();
452         }
453
454         if (gclass)
455                 method = mono_class_inflate_generic_method (method, gclass->context, gclass->klass);
456
457         return method;
458 }
459
460 static MonoMethod *
461 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
462 {
463         MonoMethod *method, *inflated;
464         MonoTableInfo *tables = image->tables;
465         MonoGenericContext *new_context = NULL;
466         MonoGenericMethod *gmethod;
467         MonoGenericContainer *container = NULL;
468         const char *ptr;
469         guint32 cols [MONO_METHODSPEC_SIZE];
470         guint32 token, param_count;
471
472         mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
473         token = cols [MONO_METHODSPEC_METHOD];
474         if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
475                 token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
476         else
477                 token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
478
479         method = mono_get_method (image, token, NULL);
480
481         ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
482         
483         mono_metadata_decode_value (ptr, &ptr);
484         ptr++;
485         param_count = mono_metadata_decode_value (ptr, &ptr);
486
487         g_assert (param_count);
488         if (method->is_inflated)
489                 container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
490         else
491                 container = ((MonoMethodNormal *) method)->generic_container;
492         g_assert (container && container->is_method);
493
494         if (context) {
495                 g_assert (context->container);
496                 container->parent = context->container;
497                 if (container->parent->is_method)
498                         container->parent = container->parent->parent;
499         }
500
501         gmethod = g_new0 (MonoGenericMethod, 1);
502         gmethod->container = container;
503
504         gmethod->inst = mono_metadata_parse_generic_inst (
505                 image, (MonoGenericContext *) container, param_count, ptr, &ptr);
506
507         if (context)
508                 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
509
510         if (!container->method_hash)
511                 container->method_hash = g_hash_table_new (
512                         (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
513
514         inflated = g_hash_table_lookup (container->method_hash, gmethod);
515         if (inflated) {
516                 g_free (gmethod);
517                 return inflated;
518         }
519
520         if (!context) {
521                 new_context = g_new0 (MonoGenericContext, 1);
522                 new_context->container = container;
523                 new_context->gmethod = gmethod;
524
525                 context = new_context;
526         } else {
527                 new_context = g_new0 (MonoGenericContext, 1);
528                 new_context->container = container;
529                 new_context->gmethod = gmethod;
530                 new_context->gclass = context->gclass;
531
532                 context = new_context;
533         }
534
535         mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
536                 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
537
538         inflated = mono_class_inflate_generic_method (method, context, NULL);
539         g_hash_table_insert (container->method_hash, gmethod, inflated);
540
541         if (new_context)
542                 context->gclass = inflated->klass->generic_class;
543         return inflated;
544 }
545
546 typedef struct MonoDllMap MonoDllMap;
547
548 struct MonoDllMap {
549         char *name;
550         char *target;
551         char *dll;
552         MonoDllMap *next;
553 };
554
555 static GHashTable *global_dll_map;
556
557 static int 
558 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
559         MonoDllMap *map, *tmp;
560
561         *rdll = dll;
562
563         if (!dll_map)
564                 return 0;
565
566         mono_loader_lock ();
567
568         map = g_hash_table_lookup (dll_map, dll);
569         if (!map) {
570                 mono_loader_unlock ();
571                 return 0;
572         }
573         *rdll = map->target? map->target: dll;
574                 
575         for (tmp = map->next; tmp; tmp = tmp->next) {
576                 if (strcmp (func, tmp->name) == 0) {
577                         *rfunc = tmp->name;
578                         if (tmp->dll)
579                                 *rdll = tmp->dll;
580                         mono_loader_unlock ();
581                         return 1;
582                 }
583         }
584         *rfunc = func;
585         mono_loader_unlock ();
586         return 1;
587 }
588
589 static int 
590 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
591 {
592         int res;
593         if (assembly && assembly->dll_map) {
594                 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
595                 if (res)
596                         return res;
597         }
598         return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
599 }
600
601 void
602 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
603         MonoDllMap *map, *entry;
604         GHashTable *dll_map = NULL;
605
606         mono_loader_lock ();
607
608         if (!assembly) {
609                 if (!global_dll_map)
610                         global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
611                 dll_map = global_dll_map;
612         } else {
613                 if (!assembly->dll_map)
614                         assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
615                 dll_map = assembly->dll_map;
616         }
617
618         map = g_hash_table_lookup (dll_map, dll);
619         if (!map) {
620                 map = g_new0 (MonoDllMap, 1);
621                 map->dll = g_strdup (dll);
622                 if (tdll)
623                         map->target = g_strdup (tdll);
624                 g_hash_table_insert (dll_map, map->dll, map);
625         }
626         if (func) {
627                 entry = g_new0 (MonoDllMap, 1);
628                 entry->name = g_strdup (func);
629                 if (tfunc)
630                         entry->target = g_strdup (tfunc);
631                 if (tdll && map->target && strcmp (map->target, tdll))
632                         entry->dll = g_strdup (tdll);
633                 entry->next = map->next;
634                 map->next = entry;
635         }
636
637         mono_loader_unlock ();
638 }
639
640 gpointer
641 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
642 {
643         MonoImage *image = method->klass->image;
644         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
645         MonoTableInfo *tables = image->tables;
646         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
647         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
648         guint32 im_cols [MONO_IMPLMAP_SIZE];
649         guint32 scope_token;
650         const char *import = NULL;
651         const char *orig_scope;
652         const char *new_scope;
653         char *full_name, *file_name;
654         int i;
655         GModule *gmodule = NULL;
656
657         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
658
659         if (piinfo->addr)
660                 return piinfo->addr;
661
662         if (method->klass->image->dynamic) {
663                 MonoReflectionMethodAux *method_aux = 
664                         g_hash_table_lookup (
665                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
666                 if (!method_aux)
667                         return NULL;
668
669                 import = method_aux->dllentry;
670                 orig_scope = method_aux->dll;
671         }
672         else {
673                 if (!piinfo->implmap_idx)
674                         return NULL;
675
676                 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
677
678                 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
679                 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
680                 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
681                 orig_scope = mono_metadata_string_heap (image, scope_token);
682         }
683
684         mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
685
686         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
687                         "DllImport attempting to load: '%s'.", new_scope);
688
689         if (exc_class) {
690                 *exc_class = NULL;
691                 *exc_arg = NULL;
692         }
693
694         /* we allow a special name to dlopen from the running process namespace */
695         if (strcmp (new_scope, "__Internal") == 0)
696                 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
697                 
698         /*
699          * Try loading the module using a variety of names
700          */
701         for (i = 0; i < 3; ++i) {
702                 switch (i) {
703                 case 0:
704                         /* Try the original name */
705                         file_name = g_strdup (new_scope);
706                         break;
707                 case 1:
708                         /* Try trimming the .dll extension */
709                         if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
710                                 file_name = g_strdup (new_scope);
711                                 file_name [strlen (new_scope) - 4] = '\0';
712                         }
713                         else
714                                 continue;
715                         break;
716                 default:
717                         if (strstr (new_scope, "lib") != new_scope) {
718                                 file_name = g_strdup_printf ("lib%s", new_scope);
719                         }
720                         else
721                                 continue;
722                         break;
723                 }
724
725                 if (!gmodule) {
726                         full_name = g_module_build_path (NULL, file_name);
727                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
728                                         "DllImport loading location: '%s'.", full_name);
729                         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
730                         if (!gmodule) {
731                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
732                                                 "DllImport error loading library: '%s'.",
733                                                 g_module_error ());
734                         }
735                         g_free (full_name);
736                 }
737
738                 if (!gmodule) {
739                         full_name = g_module_build_path (".", file_name);
740                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
741                                         "DllImport loading library: '%s'.", full_name);
742                         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
743                         if (!gmodule) {
744                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
745                                                 "DllImport error loading library '%s'.",
746                                                 g_module_error ());
747                         }
748                         g_free (full_name);
749                 }
750
751                 if (!gmodule) {
752                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
753                                         "DllImport loading: '%s'.", file_name);
754                         gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
755                         if (!gmodule) {
756                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
757                                                 "DllImport error loading library '%s'.",
758                                                 g_module_error ());
759                         }
760                 }
761
762                 g_free (file_name);
763
764                 if (gmodule)
765                         break;
766         }
767
768         if (!gmodule) {
769                 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
770                                 "DllImport unable to load library '%s'.",
771                                 g_module_error ());
772
773                 if (exc_class) {
774                         *exc_class = "DllNotFoundException";
775                         *exc_arg = new_scope;
776                 }
777                 return NULL;
778         }
779
780         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
781                                 "Searching for '%s'.", import);
782
783         if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
784                 g_module_symbol (gmodule, import, &piinfo->addr); 
785         } else {
786                 char *mangled_name = NULL, *mangled_name2 = NULL;
787                 int mangle_charset;
788                 int mangle_stdcall;
789                 int mangle_param_count;
790 #ifdef PLATFORM_WIN32
791                 int param_count;
792 #endif
793
794                 /*
795                  * Search using a variety of mangled names
796                  */
797                 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
798                         for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
799                                 gboolean need_param_count = FALSE;
800 #ifdef PLATFORM_WIN32
801                                 if (mangle_stdcall > 0)
802                                         need_param_count = TRUE;
803 #endif
804                                 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
805
806                                         if (piinfo->addr)
807                                                 continue;
808
809                                         mangled_name = (char*)import;
810                                         switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
811                                         case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
812                                                 /* Try the mangled name first */
813                                                 if (mangle_charset == 0)
814                                                         mangled_name = g_strconcat (import, "W", NULL);
815                                                 break;
816                                         case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
817 #ifdef PLATFORM_WIN32
818                                                 if (mangle_charset == 0)
819                                                         mangled_name = g_strconcat (import, "W", NULL);
820 #endif
821                                                 break;
822                                         case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
823                                         default:
824                                                 /* Try the mangled name last */
825                                                 if (mangle_charset == 1)
826                                                         mangled_name = g_strconcat (import, "A", NULL);
827                                                 break;
828                                         }
829
830 #ifdef PLATFORM_WIN32
831                                         if (mangle_param_count == 0)
832                                                 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
833                                         else
834                                                 /* Try brute force, since it would be very hard to compute the stack usage correctly */
835                                                 param_count = mangle_param_count;
836
837                                         /* Try the stdcall mangled name */
838                                         /* 
839                                          * gcc under windows creates mangled names without the underscore, but MS.NET
840                                          * doesn't support it, so we doesn't support it either.
841                                          */
842                                         if (mangle_stdcall == 1)
843                                                 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
844                                         else
845                                                 mangled_name2 = mangled_name;
846 #else
847                                         mangled_name2 = mangled_name;
848 #endif
849
850                                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
851                                                                 "Probing '%s'.", mangled_name2);
852
853                                         g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
854
855                                         if (piinfo->addr)
856                                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
857                                                                         "Found as '%s'.", mangled_name2);
858
859                                         if (mangled_name != mangled_name2)
860                                                 g_free (mangled_name2);
861                                         if (mangled_name != import)
862                                                 g_free (mangled_name);
863                                 }
864                         }
865                 }
866         }
867
868         if (!piinfo->addr) {
869                 if (exc_class) {
870                         *exc_class = "EntryPointNotFoundException";
871                         *exc_arg = import;
872                 }
873                 return NULL;
874         }
875         return piinfo->addr;
876 }
877
878 static MonoMethod *
879 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
880                             MonoGenericContext *context)
881 {
882         MonoMethod *result;
883         int table = mono_metadata_token_table (token);
884         int idx = mono_metadata_token_index (token);
885         MonoTableInfo *tables = image->tables;
886         MonoGenericContainer *generic_container = NULL, *container = NULL;
887         const char *sig = NULL;
888         int size, i;
889         guint32 cols [MONO_TYPEDEF_SIZE];
890
891         if (image->dynamic)
892                 return mono_lookup_dynamic_token (image, token);
893
894         if (table != MONO_TABLE_METHOD) {
895                 MonoGenericContainer *generic_container = NULL;
896                 if (context) {
897                         g_assert (context->container);
898                         generic_container = context->container;
899                 }
900                 if (table == MONO_TABLE_METHODSPEC)
901                         return method_from_methodspec (image, context, idx);
902                 if (table != MONO_TABLE_MEMBERREF)
903                         g_print("got wrong token: 0x%08x\n", token);
904                 g_assert (table == MONO_TABLE_MEMBERREF);
905                 result = method_from_memberref (image, idx, context);
906
907                 return result;
908         }
909
910         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
911
912         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
913             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
914                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
915         else 
916                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
917         
918         result->slot = -1;
919         result->klass = klass;
920         result->flags = cols [2];
921         result->iflags = cols [1];
922         result->token = token;
923         result->name = mono_metadata_string_heap (image, cols [3]);
924
925         if (klass)
926                 container = klass->generic_container;
927
928         if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
929             (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
930                 generic_container = mono_metadata_load_generic_params (image, token, container);
931                 if (generic_container)
932                         container = generic_container;
933         }
934
935         if (!sig) /* already taken from the methodref */
936                 sig = mono_metadata_blob_heap (image, cols [4]);
937         size = mono_metadata_decode_blob_size (sig, &sig);
938         
939         /* there are generic params, or a container. FIXME: be lazy here for generics*/
940         if (* sig & 0x10 || container)
941                 result->signature = mono_metadata_parse_method_signature_full (
942                     image, (MonoGenericContext *) container, idx, sig, NULL);
943
944
945         if (!result->klass) {
946                 guint32 type = mono_metadata_typedef_from_method (image, token);
947                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
948         }
949
950         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
951                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
952                         result->string_ctor = 1;
953         } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
954                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
955                 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
956                 
957                 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
958                 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
959         } else {
960                 if (result->signature && result->signature->generic_param_count) {
961                         MonoMethodSignature *sig = result->signature;
962
963                         for (i = 0; i < sig->generic_param_count; i++) {
964                                 generic_container->type_params [i].method = result;
965
966                                 mono_class_from_generic_parameter (
967                                         &generic_container->type_params [i], image, TRUE);
968                         }
969
970                         if (sig->ret->type == MONO_TYPE_MVAR) {
971                                 int num = sig->ret->data.generic_param->num;
972                                 sig->ret->data.generic_param = &generic_container->type_params [num];
973                         }
974
975                         for (i = 0; i < sig->param_count; i++) {
976                                 MonoType *t = sig->params [i];
977                                 if (t->type == MONO_TYPE_MVAR) {
978                                         int num = t->data.generic_param->num;
979                                         sig->params [i]->data.generic_param = &generic_container->type_params [num];
980                                 }
981                         }
982                 }
983                 
984                 /* FIXME: lazyness for generics too, but how? */
985                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
986                     !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
987                         gpointer loc = mono_image_rva_map (image, cols [0]);
988                         g_assert (loc);
989                         ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
990                                 image, (MonoGenericContext *) container, loc);
991                 }
992                 
993                 ((MonoMethodNormal *) result)->generic_container = generic_container;
994         }
995
996         return result;
997 }
998
999 MonoMethod *
1000 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1001 {
1002         return mono_get_method_full (image, token, klass, NULL);
1003 }
1004
1005 MonoMethod *
1006 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1007                       MonoGenericContext *context)
1008 {
1009         MonoMethod *result;
1010
1011         /* We do everything inside the lock to prevent creation races */
1012
1013         mono_loader_lock ();
1014
1015         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1016                 mono_loader_unlock ();
1017                 return result;
1018         }
1019
1020         result = mono_get_method_from_token (image, token, klass, context);
1021
1022         //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1023
1024         if (!(result && result->is_inflated))
1025                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1026
1027         mono_loader_unlock ();
1028
1029         return result;
1030 }
1031
1032 MonoMethod *
1033 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1034                              MonoGenericContext *context)
1035 {
1036         MonoMethod *method, *result;
1037         MonoClass *ic = NULL;
1038         MonoGenericClass *gclass = NULL;
1039
1040         mono_loader_lock ();
1041
1042         method = mono_get_method_from_token (image, token, NULL, context);
1043         if (!method) {
1044                 mono_loader_unlock ();
1045                 return NULL;
1046         }
1047
1048         mono_class_init (constrained_class);
1049         method = mono_get_inflated_method (method);
1050
1051         if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1052                 ic = method->klass;
1053
1054         if (constrained_class->generic_class)
1055                 gclass = constrained_class->generic_class;
1056
1057         result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1058         if (!result)
1059                 g_warning ("Missing method %s in assembly %s token %x", method->name,
1060                            image->name, token);
1061
1062         if (gclass)
1063                 result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
1064
1065         mono_loader_unlock ();
1066         return result;
1067 }
1068
1069 void
1070 mono_free_method  (MonoMethod *method)
1071 {
1072         if (method->signature) {
1073                 /* 
1074                  * FIXME: This causes crashes because the types inside signatures and
1075                  * locals are shared.
1076                  */
1077                 /* mono_metadata_free_method_signature (method->signature); */
1078                 g_free (method->signature);
1079         }
1080
1081         if (method->dynamic) {
1082                 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1083
1084                 g_free ((char*)method->name);
1085                 if (mw->method.header)
1086                         g_free ((char*)mw->method.header->code);
1087                 g_free (mw->method_data);
1088         }
1089
1090         if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1091                 /* FIXME: Ditto */
1092                 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1093                 g_free (((MonoMethodNormal*)method)->header);
1094         }
1095
1096         g_free (method);
1097 }
1098
1099 void
1100 mono_method_get_param_names (MonoMethod *method, const char **names)
1101 {
1102         int i, lastp;
1103         MonoClass *klass = method->klass;
1104         MonoTableInfo *methodt;
1105         MonoTableInfo *paramt;
1106         guint32 idx;
1107
1108         if (!mono_method_signature (method)->param_count)
1109                 return;
1110         for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1111                 names [i] = "";
1112
1113         if (klass->generic_class) /* copy the names later */
1114                 return;
1115
1116         mono_class_init (klass);
1117
1118         if (klass->image->dynamic) {
1119                 MonoReflectionMethodAux *method_aux = 
1120                         g_hash_table_lookup (
1121                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1122                 if (method_aux && method_aux->param_names) {
1123                         for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1124                                 if (method_aux->param_names [i + 1])
1125                                         names [i] = method_aux->param_names [i + 1];
1126                 }
1127                 return;
1128         }
1129
1130         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1131         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1132         idx = mono_method_get_index (method);
1133         if (idx > 0) {
1134                 guint32 cols [MONO_PARAM_SIZE];
1135                 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1136
1137                 if (idx < methodt->rows)
1138                         lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1139                 else
1140                         lastp = paramt->rows + 1;
1141                 for (i = param_index; i < lastp; ++i) {
1142                         mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1143                         if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1144                                 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1145                 }
1146                 return;
1147         }
1148 }
1149
1150 guint32
1151 mono_method_get_param_token (MonoMethod *method, int index)
1152 {
1153         MonoClass *klass = method->klass;
1154         MonoTableInfo *methodt;
1155         guint32 idx;
1156
1157         if (klass->generic_class)
1158                 g_assert_not_reached ();
1159
1160         mono_class_init (klass);
1161
1162         if (klass->image->dynamic) {
1163                 g_assert_not_reached ();
1164         }
1165
1166         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1167         idx = mono_method_get_index (method);
1168         if (idx > 0) {
1169                 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1170
1171                 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1172         }
1173
1174         return 0;
1175 }
1176
1177 void
1178 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1179 {
1180         int i, lastp;
1181         MonoClass *klass = method->klass;
1182         MonoTableInfo *methodt;
1183         MonoTableInfo *paramt;
1184         guint32 idx;
1185
1186         for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1187                 mspecs [i] = NULL;
1188
1189         if (method->klass->image->dynamic) {
1190                 MonoReflectionMethodAux *method_aux = 
1191                         g_hash_table_lookup (
1192                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1193                 if (method_aux && method_aux->param_marshall) {
1194                         MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1195                         for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1196                                 if (dyn_specs [i]) {
1197                                         mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1198                                         memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1199                                 }
1200                 }
1201                 return;
1202         }
1203
1204         mono_class_init (klass);
1205
1206         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1207         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1208         idx = mono_method_get_index (method);
1209         if (idx > 0) {
1210                 guint32 cols [MONO_PARAM_SIZE];
1211                 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1212
1213                 if (idx < methodt->rows)
1214                         lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1215                 else
1216                         lastp = paramt->rows + 1;
1217
1218                 for (i = param_index; i < lastp; ++i) {
1219                         mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1220
1221                         if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1222                                 const char *tp;
1223                                 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1224                                 g_assert (tp);
1225                                 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1226                         }
1227                 }
1228
1229                 return;
1230         }
1231 }
1232
1233 gboolean
1234 mono_method_has_marshal_info (MonoMethod *method)
1235 {
1236         int i, lastp;
1237         MonoClass *klass = method->klass;
1238         MonoTableInfo *methodt;
1239         MonoTableInfo *paramt;
1240         guint32 idx;
1241
1242         if (method->klass->image->dynamic) {
1243                 MonoReflectionMethodAux *method_aux = 
1244                         g_hash_table_lookup (
1245                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1246                 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1247                 if (dyn_specs) {
1248                         for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1249                                 if (dyn_specs [i])
1250                                         return TRUE;
1251                 }
1252                 return FALSE;
1253         }
1254
1255         mono_class_init (klass);
1256
1257         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1258         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1259         idx = mono_method_get_index (method);
1260         if (idx > 0) {
1261                 guint32 cols [MONO_PARAM_SIZE];
1262                 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1263                 
1264                 if (idx + 1 < methodt->rows)
1265                         lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1266                 else
1267                         lastp = paramt->rows + 1;
1268
1269                 for (i = param_index; i < lastp; ++i) {
1270                         mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1271
1272                         if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1273                                 return TRUE;
1274                 }
1275                 return FALSE;
1276         }
1277         return FALSE;
1278 }
1279
1280 gpointer
1281 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1282 {
1283         void **data;
1284         g_assert (method != NULL);
1285         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1286
1287         data = ((MonoMethodWrapper *)method)->method_data;
1288         g_assert (data != NULL);
1289         g_assert (id <= GPOINTER_TO_UINT (*data));
1290         return data [id];
1291 }
1292
1293 static void
1294 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1295         g_error ("stack walk not installed");
1296 }
1297
1298 static MonoStackWalkImpl stack_walk = default_stack_walk;
1299
1300 void
1301 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1302 {
1303         stack_walk (func, TRUE, user_data);
1304 }
1305
1306 void
1307 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1308 {
1309         stack_walk (func, FALSE, user_data);
1310 }
1311
1312 void
1313 mono_install_stack_walk (MonoStackWalkImpl func)
1314 {
1315         stack_walk = func;
1316 }
1317
1318 static gboolean
1319 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1320 {
1321         MonoMethod **dest = data;
1322         *dest = m;
1323         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1324
1325         return managed;
1326 }
1327
1328 MonoMethod*
1329 mono_method_get_last_managed (void)
1330 {
1331         MonoMethod *m = NULL;
1332         stack_walk (last_managed, FALSE, &m);
1333         return m;
1334 }
1335
1336 void
1337 mono_loader_lock (void)
1338 {
1339         EnterCriticalSection (&loader_mutex);
1340 }
1341
1342 void
1343 mono_loader_unlock (void)
1344 {
1345         LeaveCriticalSection (&loader_mutex);
1346 }
1347
1348 MonoMethodSignature* 
1349 mono_method_signature (MonoMethod *m)
1350 {
1351         return mono_method_signature_full (m, NULL);
1352 }
1353
1354 MonoMethodSignature* 
1355 mono_method_signature_full (MonoMethod *m, MonoGenericContext *context)
1356 {
1357         int idx;
1358         int size;
1359         MonoImage* img;
1360         const char *sig;
1361         
1362         if (m->signature)
1363                 return m->signature;
1364                 
1365         mono_loader_lock ();
1366         
1367         if (m->signature) {
1368                 mono_loader_unlock ();
1369                 return m->signature;
1370         }
1371         
1372         g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1373         idx = mono_metadata_token_index (m->token);
1374         img = m->klass->image;
1375         
1376         sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1377         size = mono_metadata_decode_blob_size (sig, &sig);
1378         
1379         m->signature = mono_metadata_parse_method_signature_full (img, context, idx, sig, NULL);
1380         
1381         if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1382                 m->signature->pinvoke = 1;
1383         else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1384                 MonoCallConvention conv = 0;
1385                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1386                 m->signature->pinvoke = 1;
1387                 
1388                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1389                 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1390                         conv = MONO_CALL_DEFAULT;
1391                         break;
1392                 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1393                         conv = MONO_CALL_C;
1394                         break;
1395                 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1396                         conv = MONO_CALL_STDCALL;
1397                         break;
1398                 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1399                         conv = MONO_CALL_THISCALL;
1400                         break;
1401                 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1402                         conv = MONO_CALL_FASTCALL;
1403                         break;
1404                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1405                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1406                 default:
1407                         g_warning ("unsupported calling convention");
1408                         g_assert_not_reached ();
1409                 }       
1410                 m->signature->call_convention = conv;
1411         }
1412         
1413         mono_loader_unlock ();
1414         return m->signature;
1415 }
1416
1417 const char*
1418 mono_method_get_name (MonoMethod *method)
1419 {
1420         return method->name;
1421 }
1422
1423 MonoClass*
1424 mono_method_get_class (MonoMethod *method)
1425 {
1426         return method->klass;
1427 }
1428
1429 guint32
1430 mono_method_get_token (MonoMethod *method)
1431 {
1432         return method->token;
1433 }
1434
1435 MonoMethodHeader* 
1436 mono_method_get_header (MonoMethod *method)
1437 {
1438         int idx;
1439         guint32 rva;
1440         MonoImage* img;
1441         gpointer loc;
1442         MonoMethodNormal* mn = (MonoMethodNormal*) method;
1443         
1444 #ifdef G_LIKELY
1445         if (G_LIKELY (mn->header))
1446 #else
1447         if (mn->header)
1448 #endif
1449                 return mn->header;
1450         
1451         if (method->klass->dummy || (method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1452                 return NULL;
1453         
1454         mono_loader_lock ();
1455         
1456         if (mn->header) {
1457                 mono_loader_unlock ();
1458                 return mn->header;
1459         }
1460         
1461         g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1462         idx = mono_metadata_token_index (method->token);
1463         img = method->klass->image;
1464         rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1465         loc = mono_image_rva_map (img, rva);
1466         
1467         g_assert (loc);
1468         
1469         mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1470         
1471         mono_loader_unlock ();
1472         return mn->header;
1473 }
1474
1475 guint32
1476 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1477 {
1478         if (iflags)
1479                 *iflags = method->iflags;
1480         return method->flags;
1481 }
1482
1483 /*
1484  * Find the method index in the metadata methodDef table.
1485  */
1486 guint32
1487 mono_method_get_index (MonoMethod *method) {
1488         MonoClass *klass = method->klass;
1489         int i;
1490
1491         mono_class_setup_methods (klass);
1492         for (i = 0; i < klass->method.count; ++i) {
1493                 if (method == klass->methods [i])
1494                         return klass->method.first + 1 + i;
1495         }
1496         return 0;
1497 }
1498