2004-11-28 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_inst)
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_inst) {
226                 MonoClass *gclass;
227                 MonoMethod *res;
228
229                 gclass = mono_class_from_mono_type (sclass->generic_inst->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_inst)
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_inst) {
344                 g_assert (klass->generic_inst->container);
345                 container = klass->generic_inst->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_inst && (klass != method->klass))
370                                 method = mono_class_inflate_generic_method (
371                                         method, klass->generic_inst->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->ginst) {
461                         g_assert (context->ginst->container);
462                         container->parent = context->ginst->container;
463                 } else
464                         container->parent = context->container;
465         }
466
467         gmethod = g_new0 (MonoGenericMethod, 1);
468         gmethod->container = container;
469         gmethod->mtype_argc = param_count;
470         gmethod->mtype_argv = g_new0 (MonoType *, param_count);
471
472         for (i = 0; i < param_count; i++) {
473                 MonoType *t = mono_metadata_parse_type_full (
474                         image, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
475
476                 if (context)
477                         t = mono_class_inflate_generic_type (t, context);
478
479                 gmethod->mtype_argv [i] = t;
480
481                 if (!gmethod->is_open)
482                         gmethod->is_open = mono_class_is_open_constructed_type (gmethod->mtype_argv [i]);
483         }
484
485         if (!container->method_hash)
486                 container->method_hash = g_hash_table_new (
487                         mono_metadata_generic_method_hash, mono_metadata_generic_method_equal);
488
489         inflated = g_hash_table_lookup (container->method_hash, gmethod);
490         if (inflated) {
491                 g_free (gmethod->mtype_argv);
492                 g_free (gmethod);
493
494                 return inflated;
495         }
496
497         if (!context) {
498                 new_context = g_new0 (MonoGenericContext, 1);
499                 new_context->gmethod = gmethod;
500
501                 context = new_context;
502         } else {
503                 new_context = g_new0 (MonoGenericContext, 1);
504                 new_context->gmethod = gmethod;
505                 new_context->ginst = context->ginst;
506
507                 context = new_context;
508         }
509
510         mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
511                 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
512
513         inflated = mono_class_inflate_generic_method (method, context, NULL);
514         g_hash_table_insert (container->method_hash, gmethod, inflated);
515
516         if (new_context)
517                 context->ginst = inflated->klass->generic_inst;
518         return inflated;
519 }
520
521 typedef struct MonoDllMap MonoDllMap;
522
523 struct MonoDllMap {
524         char *name;
525         char *target;
526         char *dll;
527         MonoDllMap *next;
528 };
529
530 static GHashTable *global_dll_map;
531
532 static int 
533 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
534         MonoDllMap *map, *tmp;
535
536         *rdll = dll;
537
538         if (!dll_map)
539                 return 0;
540
541         mono_loader_lock ();
542
543         map = g_hash_table_lookup (dll_map, dll);
544         if (!map) {
545                 mono_loader_unlock ();
546                 return 0;
547         }
548         *rdll = map->target? map->target: dll;
549                 
550         for (tmp = map->next; tmp; tmp = tmp->next) {
551                 if (strcmp (func, tmp->name) == 0) {
552                         *rfunc = tmp->name;
553                         if (tmp->dll)
554                                 *rdll = tmp->dll;
555                         mono_loader_unlock ();
556                         return 1;
557                 }
558         }
559         *rfunc = func;
560         mono_loader_unlock ();
561         return 1;
562 }
563
564 static int 
565 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
566 {
567         int res;
568         if (assembly && assembly->dll_map) {
569                 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
570                 if (res)
571                         return res;
572         }
573         return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
574 }
575
576 void
577 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
578         MonoDllMap *map, *entry;
579         GHashTable *dll_map = NULL;
580
581         mono_loader_lock ();
582
583         if (!assembly) {
584                 if (!global_dll_map)
585                         global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
586                 dll_map = global_dll_map;
587         } else {
588                 if (!assembly->dll_map)
589                         assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
590                 dll_map = assembly->dll_map;
591         }
592
593         map = g_hash_table_lookup (dll_map, dll);
594         if (!map) {
595                 map = g_new0 (MonoDllMap, 1);
596                 map->dll = g_strdup (dll);
597                 if (tdll)
598                         map->target = g_strdup (tdll);
599                 g_hash_table_insert (dll_map, map->dll, map);
600         }
601         if (func) {
602                 entry = g_new0 (MonoDllMap, 1);
603                 entry->name = g_strdup (func);
604                 if (tfunc)
605                         entry->target = g_strdup (tfunc);
606                 if (tdll && map->target && strcmp (map->target, tdll))
607                         entry->dll = g_strdup (tdll);
608                 entry->next = map->next;
609                 map->next = entry;
610         }
611
612         mono_loader_unlock ();
613 }
614
615 gpointer
616 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
617 {
618         MonoImage *image = method->klass->image;
619         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
620         MonoTableInfo *tables = image->tables;
621         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
622         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
623         guint32 im_cols [MONO_IMPLMAP_SIZE];
624         guint32 scope_token;
625         const char *import = NULL;
626         const char *orig_scope;
627         const char *new_scope;
628         char *full_name, *file_name;
629         int i;
630         GModule *gmodule = NULL;
631
632         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
633
634         if (piinfo->addr)
635                 return piinfo->addr;
636
637         if (method->klass->image->dynamic) {
638                 MonoReflectionMethodAux *method_aux = 
639                         mono_g_hash_table_lookup (
640                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
641                 if (!method_aux)
642                         return NULL;
643
644                 import = method_aux->dllentry;
645                 orig_scope = method_aux->dll;
646         }
647         else {
648                 if (!piinfo->implmap_idx)
649                         return NULL;
650
651                 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
652
653                 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
654                 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
655                 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
656                 orig_scope = mono_metadata_string_heap (image, scope_token);
657         }
658
659         mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
660
661         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
662                         "DllImport attempting to load: '%s'.", new_scope);
663
664         if (exc_class) {
665                 *exc_class = NULL;
666                 *exc_arg = NULL;
667         }
668         
669         /*
670          * Try loading the module using a variety of names
671          */
672         for (i = 0; i < 3; ++i) {
673                 switch (i) {
674                 case 0:
675                         /* Try the original name */
676                         file_name = g_strdup (new_scope);
677                         break;
678                 case 1:
679                         /* Try trimming the .dll extension */
680                         if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
681                                 file_name = g_strdup (new_scope);
682                                 file_name [strlen (new_scope) - 4] = '\0';
683                         }
684                         else
685                                 continue;
686                         break;
687                 default:
688                         if (strstr (new_scope, "lib") != new_scope) {
689                                 file_name = g_strdup_printf ("lib%s", new_scope);
690                         }
691                         else
692                                 continue;
693                         break;
694                 }
695
696                 if (!gmodule) {
697                         full_name = g_module_build_path (NULL, file_name);
698                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
699                                         "DllImport loading location: '%s'.", full_name);
700                         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
701                         if (!gmodule) {
702                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
703                                                 "DllImport error loading library: '%s'.",
704                                                 g_module_error ());
705                         }
706                         g_free (full_name);
707                 }
708
709                 if (!gmodule) {
710                         full_name = g_module_build_path (".", file_name);
711                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
712                                         "DllImport loading library: '%s'.", full_name);
713                         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
714                         if (!gmodule) {
715                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
716                                                 "DllImport error loading library '%s'.",
717                                                 g_module_error ());
718                         }
719                         g_free (full_name);
720                 }
721
722                 if (!gmodule) {
723                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
724                                         "DllImport loading: '%s'.", file_name);
725                         gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
726                         if (!gmodule) {
727                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
728                                                 "DllImport error loading library '%s'.",
729                                                 g_module_error ());
730                         }
731                 }
732
733                 g_free (file_name);
734
735                 if (gmodule)
736                         break;
737         }
738
739         if (!gmodule) {
740                 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
741                                 "DllImport unable to load library '%s'.",
742                                 g_module_error ());
743
744                 if (exc_class) {
745                         *exc_class = "DllNotFoundException";
746                         *exc_arg = orig_scope;
747                 }
748                 return NULL;
749         }
750
751         if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
752                 g_module_symbol (gmodule, import, &piinfo->addr); 
753         } else {
754                 char *mangled_name;
755
756                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
757                 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
758                         mangled_name = g_strconcat (import, "W", NULL);
759                         g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
760                         g_free (mangled_name);
761
762                         if (!piinfo->addr)
763                                 g_module_symbol (gmodule, import, &piinfo->addr); 
764                         break;
765                 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
766                         g_module_symbol (gmodule, import, &piinfo->addr); 
767                         break;
768                 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
769                 default:
770                         mangled_name = g_strconcat (import, "A", NULL);
771                         g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
772                         g_free (mangled_name);
773
774                         if (!piinfo->addr)
775                                 g_module_symbol (gmodule, import, &piinfo->addr); 
776                                
777                         break;                                  
778                 }
779
780 #ifdef PLATFORM_WIN32
781                 /* Try the stdcall mangled name */
782                 if (!piinfo->addr) {
783                         /* FIX: Compute this correctly */
784                         mangled_name = g_strdup_printf ("%s@%d", import, method->signature->param_count * sizeof (gpointer));
785                         g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
786                         g_free (mangled_name);
787                 }
788                 if (!piinfo->addr) {
789                         mangled_name = g_strdup_printf ("_%s@%d", import, method->signature->param_count * sizeof (gpointer));
790                         g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
791                         g_free (mangled_name);
792                 }
793 #endif
794         }
795
796         if (!piinfo->addr) {
797                 if (exc_class) {
798                         *exc_class = "EntryPointNotFoundException";
799                         *exc_arg = import;
800                 }
801                 return NULL;
802         }
803         return piinfo->addr;
804 }
805
806 static MonoMethod *
807 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
808                             MonoGenericContext *context)
809 {
810         MonoMethod *result;
811         int table = mono_metadata_token_table (token);
812         int idx = mono_metadata_token_index (token);
813         MonoTableInfo *tables = image->tables;
814         MonoGenericContainer *generic_container = NULL, *container = NULL;
815         const char *sig = NULL;
816         int size, i;
817         guint32 cols [MONO_TYPEDEF_SIZE];
818
819         if (image->dynamic)
820                 return mono_lookup_dynamic_token (image, token);
821
822         if (table != MONO_TABLE_METHOD) {
823                 MonoGenericContainer *generic_container = NULL;
824                 if (context) {
825                         if (context->ginst) {
826                                 g_assert (context->ginst->container);
827                                 generic_container = context->ginst->container;
828                         } else
829                                 generic_container = context->container;
830                 }
831                 if (table == MONO_TABLE_METHODSPEC)
832                         return method_from_methodspec (image, context, idx);
833                 if (table != MONO_TABLE_MEMBERREF)
834                         g_print("got wrong token: 0x%08x\n", token);
835                 g_assert (table == MONO_TABLE_MEMBERREF);
836                 result = method_from_memberref (image, idx, context);
837
838                 return result;
839         }
840
841         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
842
843         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
844             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
845                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
846         else 
847                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
848         
849         result->slot = -1;
850         result->klass = klass;
851         result->flags = cols [2];
852         result->iflags = cols [1];
853         result->token = token;
854         result->name = mono_metadata_string_heap (image, cols [3]);
855
856         if (klass)
857                 container = klass->generic_container;
858
859         if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
860             (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
861                 generic_container = mono_metadata_load_generic_params (image, token);
862                 if (generic_container) {
863                         generic_container->parent = container;
864                         generic_container->is_method = 1;
865                         container = generic_container;
866                 }
867         }
868
869         if (!sig) /* already taken from the methodref */
870                 sig = mono_metadata_blob_heap (image, cols [4]);
871         size = mono_metadata_decode_blob_size (sig, &sig);
872         result->signature = mono_metadata_parse_method_signature_full (image, container, idx, sig, NULL);
873
874         if (!result->klass) {
875                 guint32 type = mono_metadata_typedef_from_method (image, token);
876                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
877         }
878
879         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
880                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
881                         result->string_ctor = 1;
882
883                 result->signature->pinvoke = 1;
884         } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
885                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
886                 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
887                 MonoCallConvention conv = 0;
888
889                 result->signature->pinvoke = 1;
890                 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
891                 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
892
893                 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
894                 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
895                         conv = MONO_CALL_DEFAULT;
896                         break;
897                 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
898                         conv = MONO_CALL_C;
899                         break;
900                 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
901                         conv = MONO_CALL_STDCALL;
902                         break;
903                 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
904                         conv = MONO_CALL_THISCALL;
905                         break;
906                 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
907                         conv = MONO_CALL_FASTCALL;
908                         break;
909                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
910                 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
911                 default:
912                         g_warning ("unsupported calling convention");
913                         g_assert_not_reached ();
914                 }       
915                 result->signature->call_convention = conv;
916         } else {
917                 if (result->signature->generic_param_count) {
918                         MonoMethodSignature *sig = result->signature;
919
920                         for (i = 0; i < sig->generic_param_count; i++) {
921                                 generic_container->type_params [i].method = result;
922
923                                 mono_class_from_generic_parameter (
924                                         &generic_container->type_params [i], image, TRUE);
925                         }
926
927                         if (sig->ret->type == MONO_TYPE_MVAR) {
928                                 int num = sig->ret->data.generic_param->num;
929                                 sig->ret->data.generic_param = &generic_container->type_params [num];
930                         }
931
932                         for (i = 0; i < sig->param_count; i++) {
933                                 MonoType *t = sig->params [i];
934                                 if (t->type == MONO_TYPE_MVAR) {
935                                         int num = t->data.generic_param->num;
936                                         sig->params [i]->data.generic_param = &generic_container->type_params [num];
937                                 }
938                         }
939                 }
940                 
941                 /* FIXME: lazyness for generics too, but how? */
942                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
943                     !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
944                         gpointer loc = mono_image_rva_map (image, cols [0]);
945                         g_assert (loc);
946                         ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
947                 }
948                 
949                 ((MonoMethodNormal *) result)->generic_container = generic_container;
950         }
951
952         return result;
953 }
954
955 MonoMethod *
956 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
957 {
958         return mono_get_method_full (image, token, klass, NULL);
959 }
960
961 MonoMethod *
962 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
963                       MonoGenericContext *context)
964 {
965         MonoMethod *result;
966
967         /* We do everything inside the lock to prevent creation races */
968
969         mono_loader_lock ();
970
971         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
972                 mono_loader_unlock ();
973                 return result;
974         }
975
976         result = mono_get_method_from_token (image, token, klass, context);
977
978         if (!(result && result->signature->is_inflated))
979                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
980
981         mono_loader_unlock ();
982
983         return result;
984 }
985
986 MonoMethod *
987 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
988                              MonoGenericContext *context)
989 {
990         MonoMethod *method, *result;
991         MonoClass *ic = NULL;
992
993         mono_loader_lock ();
994
995         method = mono_get_method_from_token (image, token, NULL, context);
996         if (!method) {
997                 mono_loader_unlock ();
998                 return NULL;
999         }
1000
1001         mono_class_init (constrained_class);
1002
1003         if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1004                 ic = method->klass;
1005
1006         result = find_method (constrained_class, ic, method->name, method->signature);
1007         if (!result)
1008                 g_warning ("Missing method %s in assembly %s token %x", method->name,
1009                            image->name, token);
1010
1011         mono_loader_unlock ();
1012         return result;
1013 }
1014
1015 void
1016 mono_free_method  (MonoMethod *method)
1017 {
1018         mono_metadata_free_method_signature (method->signature);
1019         if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1020                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
1021         }
1022
1023         g_free (method);
1024 }
1025
1026 void
1027 mono_method_get_param_names (MonoMethod *method, const char **names)
1028 {
1029         int i, lastp;
1030         MonoClass *klass = method->klass;
1031         MonoTableInfo *methodt;
1032         MonoTableInfo *paramt;
1033
1034         if (!method->signature->param_count)
1035                 return;
1036         for (i = 0; i < method->signature->param_count; ++i)
1037                 names [i] = "";
1038
1039         if (klass->generic_inst) /* copy the names later */
1040                 return;
1041
1042         mono_class_init (klass);
1043
1044         if (klass->image->dynamic) {
1045                 MonoReflectionMethodAux *method_aux = 
1046                         mono_g_hash_table_lookup (
1047                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1048                 if (method_aux && method_aux->param_names) {
1049                         for (i = 0; i < method->signature->param_count; ++i)
1050                                 if (method_aux->param_names [i + 1])
1051                                         names [i] = method_aux->param_names [i + 1];
1052                 }
1053                 return;
1054         }
1055
1056         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1057         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1058         for (i = 0; i < klass->method.count; ++i) {
1059                 if (method == klass->methods [i]) {
1060                         guint32 idx = klass->method.first + i;
1061                         guint32 cols [MONO_PARAM_SIZE];
1062                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1063
1064                         if (idx + 1 < methodt->rows)
1065                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1066                         else
1067                                 lastp = paramt->rows + 1;
1068                         for (i = param_index; i < lastp; ++i) {
1069                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1070                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1071                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1072                         }
1073                         return;
1074                 }
1075         }
1076 }
1077
1078 guint32
1079 mono_method_get_param_token (MonoMethod *method, int index)
1080 {
1081         int i;
1082         MonoClass *klass = method->klass;
1083         MonoTableInfo *methodt;
1084
1085         if (klass->generic_inst)
1086                 g_assert_not_reached ();
1087
1088         mono_class_init (klass);
1089
1090         if (klass->image->dynamic) {
1091                 g_assert_not_reached ();
1092         }
1093
1094         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1095         for (i = 0; i < klass->method.count; ++i) {
1096                 if (method == klass->methods [i]) {
1097                         guint32 idx = klass->method.first + i;
1098                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1099
1100                         return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1101                 }
1102         }
1103
1104         return 0;
1105 }
1106
1107 void
1108 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1109 {
1110         int i, lastp;
1111         MonoClass *klass = method->klass;
1112         MonoTableInfo *methodt;
1113         MonoTableInfo *paramt;
1114
1115         for (i = 0; i < method->signature->param_count + 1; ++i)
1116                 mspecs [i] = NULL;
1117
1118         if (method->klass->image->dynamic) {
1119                 MonoReflectionMethodAux *method_aux = 
1120                         mono_g_hash_table_lookup (
1121                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1122                 if (method_aux && method_aux->param_marshall) {
1123                         MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1124                         for (i = 0; i < method->signature->param_count + 1; ++i)
1125                                 if (dyn_specs [i]) {
1126                                         mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1127                                         memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1128                                 }
1129                 }
1130                 return;
1131         }
1132
1133         mono_class_init (klass);
1134
1135         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1136         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1137
1138         for (i = 0; i < klass->method.count; ++i) {
1139                 if (method == klass->methods [i]) {
1140                         guint32 idx = klass->method.first + i;
1141                         guint32 cols [MONO_PARAM_SIZE];
1142                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1143
1144                         if (idx + 1 < methodt->rows)
1145                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1146                         else
1147                                 lastp = paramt->rows + 1;
1148
1149                         for (i = param_index; i < lastp; ++i) {
1150                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1151
1152                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1153                                         const char *tp;
1154                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1155                                         g_assert (tp);
1156                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1157                                 }
1158                         }
1159
1160                         return;
1161                 }
1162         }
1163 }
1164
1165 gboolean
1166 mono_method_has_marshal_info (MonoMethod *method)
1167 {
1168         int i, lastp;
1169         MonoClass *klass = method->klass;
1170         MonoTableInfo *methodt;
1171         MonoTableInfo *paramt;
1172
1173         if (method->klass->image->dynamic) {
1174                 MonoReflectionMethodAux *method_aux = 
1175                         mono_g_hash_table_lookup (
1176                                 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1177                 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1178                 if (dyn_specs) {
1179                         for (i = 0; i < method->signature->param_count + 1; ++i)
1180                                 if (dyn_specs [i])
1181                                         return TRUE;
1182                 }
1183                 return FALSE;
1184         }
1185
1186         mono_class_init (klass);
1187
1188         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1189         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1190
1191         for (i = 0; i < klass->method.count; ++i) {
1192                 if (method == klass->methods [i]) {
1193                         guint32 idx = klass->method.first + i;
1194                         guint32 cols [MONO_PARAM_SIZE];
1195                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1196
1197                         if (idx + 1 < methodt->rows)
1198                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1199                         else
1200                                 lastp = paramt->rows + 1;
1201
1202                         for (i = param_index; i < lastp; ++i) {
1203                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1204
1205                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1206                                         return TRUE;
1207                         }
1208                         return FALSE;
1209                 }
1210         }
1211         return FALSE;
1212 }
1213
1214 gpointer
1215 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1216 {
1217         GList *l;
1218         g_assert (method != NULL);
1219         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1220
1221         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
1222                 g_assert_not_reached ();
1223
1224         return l->data;
1225 }
1226
1227 static void
1228 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1229         g_error ("stack walk not installed");
1230 }
1231
1232 static MonoStackWalkImpl stack_walk = default_stack_walk;
1233
1234 void
1235 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1236 {
1237         stack_walk (func, FALSE, user_data);
1238 }
1239
1240 void
1241 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1242 {
1243         stack_walk (func, TRUE, user_data);
1244 }
1245
1246 void
1247 mono_install_stack_walk (MonoStackWalkImpl func)
1248 {
1249         stack_walk = func;
1250 }
1251
1252 static gboolean
1253 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1254 {
1255         MonoMethod **dest = data;
1256         *dest = m;
1257         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1258
1259         return managed;
1260 }
1261
1262 MonoMethod*
1263 mono_method_get_last_managed (void)
1264 {
1265         MonoMethod *m = NULL;
1266         stack_walk (last_managed, FALSE, &m);
1267         return m;
1268 }
1269
1270 void
1271 mono_loader_lock (void)
1272 {
1273         EnterCriticalSection (&loader_mutex);
1274 }
1275
1276 void
1277 mono_loader_unlock (void)
1278 {
1279         LeaveCriticalSection (&loader_mutex);
1280 }
1281
1282 MonoMethodSignature* 
1283 mono_method_signature (MonoMethod *method)
1284 {
1285         return method->signature;
1286 }
1287
1288 const char*
1289 mono_method_get_name (MonoMethod *method)
1290 {
1291         return method->name;
1292 }
1293
1294 MonoClass*
1295 mono_method_get_class (MonoMethod *method)
1296 {
1297         return method->klass;
1298 }
1299
1300 guint32
1301 mono_method_get_token (MonoMethod *method)
1302 {
1303         return method->token;
1304 }
1305
1306 MonoMethodHeader* 
1307 mono_method_get_header (MonoMethod *method)
1308 {
1309         int idx;
1310         guint32 rva;
1311         MonoImage* img;
1312         gpointer loc;
1313         MonoMethodNormal* mn = (MonoMethodNormal*) method;
1314         
1315 #ifdef G_LIKELY
1316         if (G_LIKELY (mn->header))
1317 #else
1318         if (mn->header)
1319 #endif
1320                 return mn->header;
1321         
1322         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))
1323                 return NULL;
1324         
1325         mono_loader_lock ();
1326         
1327         if (mn->header) {
1328                 mono_loader_unlock ();
1329                 return mn->header;
1330         }
1331         
1332         g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1333         idx = mono_metadata_token_index (method->token);
1334         img = method->klass->image;
1335         rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1336         loc = mono_image_rva_map (img, rva);
1337         
1338         g_assert (loc);
1339         
1340         mn->header = mono_metadata_parse_mh_full (img, mn->generic_container, loc);
1341         
1342         mono_loader_unlock ();
1343         return mn->header;
1344 }
1345
1346 guint32
1347 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1348 {
1349         if (iflags)
1350                 *iflags = method->iflags;
1351         return method->flags;
1352 }
1353
1354