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