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