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