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