2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
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
16 * This should keep track of the assembly versions that we are loading.
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>
38 MonoDefaults mono_defaults;
41 * This lock protects the hash tables inside MonoImage used by the metadata
42 * loading functions in class.c and loader.c.
44 static CRITICAL_SECTION loader_mutex;
49 InitializeCriticalSection (&loader_mutex);
52 static MonoClassField*
53 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
54 MonoGenericContext *context)
57 MonoTableInfo *tables = image->tables;
59 guint32 nindex, class;
62 guint32 idx = mono_metadata_token_index (token);
65 MonoClassField *result = mono_lookup_dynamic_token (image, token);
66 *retklass = result->parent;
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;
74 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
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... */
81 case MONO_MEMBERREF_PARENT_TYPEREF:
82 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
84 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
85 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
89 mono_class_init (klass);
92 return mono_class_get_field_from_name (klass, fname);
93 case MONO_MEMBERREF_PARENT_TYPESPEC: {
94 /*guint32 bcols [MONO_TYPESPEC_SIZE];
98 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
99 bcols, MONO_TYPESPEC_SIZE);
100 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
101 len = mono_metadata_decode_value (ptr, &ptr);
102 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
104 klass = mono_class_from_mono_type (type);
105 mono_class_init (klass);
106 g_print ("type in sig: %s\n", klass->name);*/
107 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
108 mono_class_init (klass);
111 return mono_class_get_field_from_name (klass, fname);
114 g_warning ("field load from %x", class);
120 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
121 MonoGenericContext *context)
125 MonoClassField *field;
127 if (image->dynamic) {
128 MonoClassField *result = mono_lookup_dynamic_token (image, token);
129 *retklass = result->parent;
134 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
135 *retklass = field->parent;
136 mono_loader_unlock ();
139 mono_loader_unlock ();
141 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
142 field = field_from_memberref (image, token, retklass, context);
144 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
147 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
153 field = mono_class_get_field (k, token);
157 if (!field->parent->generic_class)
158 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
159 mono_loader_unlock ();
164 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
168 if (sig1->hasthis != sig2->hasthis ||
169 sig1->sentinelpos != sig2->sentinelpos)
172 for (i = 0; i < sig1->sentinelpos; i++) {
173 MonoType *p1 = sig1->params[i];
174 MonoType *p2 = sig2->params[i];
176 /*if (p1->attrs != p2->attrs)
179 if (!mono_metadata_type_equal (p1, p2))
183 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
189 find_method_in_class (MonoClass *klass, const char *name, const char *qname,
190 const char *fqname, MonoMethodSignature *sig)
192 MonoGenericContext *context = NULL;
195 if (klass->generic_container)
196 context = &klass->generic_container->context;
197 else if (klass->generic_class)
198 context = klass->generic_class->context;
200 mono_class_setup_methods (klass);
201 for (i = 0; i < klass->method.count; ++i) {
202 MonoMethod *m = klass->methods [i];
204 if (!((fqname && !strcmp (m->name, fqname)) ||
205 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
208 if (sig->call_convention == MONO_CALL_VARARG) {
209 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
212 MonoMethodSignature *msig = mono_method_signature_full (m, context);
213 if (mono_metadata_signature_equal (sig, msig))
222 find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignature *sig)
225 char *qname, *fqname, *class_name;
226 MonoMethod *result = NULL;
229 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
231 qname = g_strconcat (class_name, ".", name, NULL);
232 if (ic->name_space && ic->name_space [0])
233 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
237 class_name = qname = fqname = NULL;
240 result = find_method_in_class (klass, name, qname, fqname, sig);
244 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
247 for (i = 0; i < klass->interface_count; i++) {
248 MonoClass *ic = klass->interfaces [i];
250 result = find_method_in_class (ic, name, qname, fqname, sig);
255 klass = klass->parent;
266 * token is the method_ref or method_def token used in a call IL instruction.
269 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
271 int table = mono_metadata_token_table (token);
272 int idx = mono_metadata_token_index (token);
273 guint32 cols [MONO_MEMBERREF_SIZE];
274 MonoMethodSignature *sig, *prev_sig;
277 /* !table is for wrappers: we should really assign their own token to them */
278 if (!table || table == MONO_TABLE_METHOD)
279 return mono_method_signature (method);
281 if (table == MONO_TABLE_METHODSPEC) {
282 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
283 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
284 mono_method_signature (method));
285 g_assert (method->is_inflated);
287 return mono_method_signature (method);
290 if (method->klass->generic_class)
291 return mono_method_signature (method);
294 /* FIXME: This might be incorrect for vararg methods */
295 return mono_method_signature (method);
298 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
299 mono_loader_unlock ();
301 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
303 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
304 mono_metadata_decode_blob_size (ptr, &ptr);
305 sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
308 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
310 /* Somebody got in before us */
311 /* FIXME: Free sig */
315 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
316 mono_loader_unlock ();
319 sig = mono_class_inflate_generic_signature (image, sig, context);
325 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
327 return mono_method_get_signature_full (method, image, token, NULL);
331 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
333 MonoClass *klass = NULL;
334 MonoMethod *method = NULL;
335 MonoTableInfo *tables = image->tables;
337 guint32 nindex, class;
338 MonoGenericClass *gclass = NULL;
339 MonoGenericContainer *container = NULL;
341 MonoMethodSignature *sig;
344 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
345 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
346 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
347 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
348 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
350 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
353 case MONO_MEMBERREF_PARENT_TYPEREF:
354 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
356 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
357 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
362 case MONO_MEMBERREF_PARENT_TYPESPEC:
363 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
365 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
366 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
371 case MONO_MEMBERREF_PARENT_TYPEDEF:
372 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
374 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex, context);
375 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
380 case MONO_MEMBERREF_PARENT_METHODDEF:
381 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
383 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
384 g_assert_not_reached ();
388 if (klass->generic_class) {
389 gclass = klass->generic_class;
390 klass = gclass->container_class;
392 if (klass->generic_container)
393 container = klass->generic_container;
394 mono_class_init (klass);
396 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
397 mono_metadata_decode_blob_size (ptr, &ptr);
398 sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
401 case MONO_MEMBERREF_PARENT_TYPEREF:
402 method = find_method (klass, NULL, mname, sig);
404 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
405 mono_metadata_free_method_signature (sig);
407 case MONO_MEMBERREF_PARENT_TYPESPEC: {
411 type = &klass->byval_arg;
413 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
414 method = find_method (klass, NULL, mname, sig);
416 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
417 else if (klass->generic_class && (klass != method->klass))
418 method = mono_class_inflate_generic_method (
419 method, klass->generic_class->context, klass);
420 mono_metadata_free_method_signature (sig);
424 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
425 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
426 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
427 result->signature = sig;
428 result->name = mname;
430 if (!strcmp (mname, ".ctor")) {
431 /* we special-case this in the runtime. */
435 if (!strcmp (mname, "Set")) {
436 g_assert (sig->hasthis);
437 g_assert (type->data.array->rank + 1 == sig->param_count);
438 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
442 if (!strcmp (mname, "Get")) {
443 g_assert (sig->hasthis);
444 g_assert (type->data.array->rank == sig->param_count);
445 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
449 if (!strcmp (mname, "Address")) {
450 g_assert (sig->hasthis);
451 g_assert (type->data.array->rank == sig->param_count);
452 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
456 g_assert_not_reached ();
459 case MONO_MEMBERREF_PARENT_TYPEDEF:
460 method = find_method (klass, NULL, mname, sig);
462 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
463 mono_metadata_free_method_signature (sig);
466 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
467 g_assert_not_reached ();
471 method = mono_class_inflate_generic_method (method, gclass->context, gclass->klass);
477 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
479 MonoMethod *method, *inflated;
480 MonoTableInfo *tables = image->tables;
481 MonoGenericContext *new_context = NULL;
482 MonoGenericMethod *gmethod;
483 MonoGenericContainer *container = NULL;
485 guint32 cols [MONO_METHODSPEC_SIZE];
486 guint32 token, param_count;
488 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
489 token = cols [MONO_METHODSPEC_METHOD];
490 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
491 token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
493 token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
495 method = mono_get_method (image, token, NULL);
496 method = mono_get_inflated_method (method);
498 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
500 mono_metadata_decode_value (ptr, &ptr);
502 param_count = mono_metadata_decode_value (ptr, &ptr);
504 g_assert (param_count);
505 if (method->is_inflated)
506 container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
508 container = ((MonoMethodNormal *) method)->generic_container;
509 g_assert (container && container->is_method);
512 g_assert (context->container);
513 container->parent = context->container;
514 if (container->parent->is_method)
515 container->parent = container->parent->parent;
518 gmethod = g_new0 (MonoGenericMethod, 1);
519 gmethod->generic_class = method->klass->generic_class;
520 gmethod->container = container;
522 if (context && context->gmethod)
523 new_context = context->gmethod->container;
524 else if (context && context->gclass)
525 new_context = context->gclass->container_class->generic_container;
527 new_context = context ? context->container : NULL;
529 gmethod->inst = mono_metadata_parse_generic_inst (
530 image, new_context, param_count, ptr, &ptr);
533 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
535 if (!container->method_hash)
536 container->method_hash = g_hash_table_new (
537 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
539 inflated = g_hash_table_lookup (container->method_hash, gmethod);
546 new_context = g_new0 (MonoGenericContext, 1);
547 new_context->container = container;
548 new_context->gmethod = gmethod;
550 context = new_context;
552 new_context = g_new0 (MonoGenericContext, 1);
553 new_context->container = container;
554 new_context->gmethod = gmethod;
555 new_context->gclass = context->gclass;
557 context = new_context;
560 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
561 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
563 inflated = mono_class_inflate_generic_method (method, context, NULL);
564 g_hash_table_insert (container->method_hash, gmethod, inflated);
567 context->gclass = inflated->klass->generic_class;
571 typedef struct MonoDllMap MonoDllMap;
580 static GHashTable *global_dll_map;
583 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
584 MonoDllMap *map, *tmp;
593 map = g_hash_table_lookup (dll_map, dll);
595 mono_loader_unlock ();
598 *rdll = map->target? map->target: dll;
600 for (tmp = map->next; tmp; tmp = tmp->next) {
601 if (strcmp (func, tmp->name) == 0) {
605 mono_loader_unlock ();
610 mono_loader_unlock ();
615 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
618 if (assembly && assembly->dll_map) {
619 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
623 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
627 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
628 MonoDllMap *map, *entry;
629 GHashTable *dll_map = NULL;
635 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
636 dll_map = global_dll_map;
638 if (!assembly->dll_map)
639 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
640 dll_map = assembly->dll_map;
643 map = g_hash_table_lookup (dll_map, dll);
645 map = g_new0 (MonoDllMap, 1);
646 map->dll = g_strdup (dll);
648 map->target = g_strdup (tdll);
649 g_hash_table_insert (dll_map, map->dll, map);
652 entry = g_new0 (MonoDllMap, 1);
653 entry->name = g_strdup (func);
655 entry->target = g_strdup (tfunc);
656 if (tdll && map->target && strcmp (map->target, tdll))
657 entry->dll = g_strdup (tdll);
658 entry->next = map->next;
662 mono_loader_unlock ();
666 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
668 MonoImage *image = method->klass->image;
669 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
670 MonoTableInfo *tables = image->tables;
671 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
672 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
673 guint32 im_cols [MONO_IMPLMAP_SIZE];
675 const char *import = NULL;
676 const char *orig_scope;
677 const char *new_scope;
678 char *full_name, *file_name;
680 GModule *gmodule = NULL;
682 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
687 if (method->klass->image->dynamic) {
688 MonoReflectionMethodAux *method_aux =
689 g_hash_table_lookup (
690 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
694 import = method_aux->dllentry;
695 orig_scope = method_aux->dll;
698 if (!piinfo->implmap_idx)
701 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
703 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
704 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
705 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
706 orig_scope = mono_metadata_string_heap (image, scope_token);
709 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
711 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
712 "DllImport attempting to load: '%s'.", new_scope);
719 /* we allow a special name to dlopen from the running process namespace */
720 if (strcmp (new_scope, "__Internal") == 0)
721 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
724 * Try loading the module using a variety of names
726 for (i = 0; i < 4; ++i) {
729 /* Try the original name */
730 file_name = g_strdup (new_scope);
733 /* Try trimming the .dll extension */
734 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
735 file_name = g_strdup (new_scope);
736 file_name [strlen (new_scope) - 4] = '\0';
742 if (strstr (new_scope, "lib") != new_scope) {
743 file_name = g_strdup_printf ("lib%s", new_scope);
749 #ifndef PLATFORM_WIN32
750 if (g_ascii_strcasecmp ("user32.dll", new_scope) ||
751 g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
752 g_ascii_strcasecmp ("user32", new_scope) ||
753 g_ascii_strcasecmp ("kernel", new_scope)) {
754 file_name = g_strdup ("libMonoSupportW.so");
758 #ifndef PLATFORM_WIN32
764 full_name = g_module_build_path (NULL, file_name);
765 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
766 "DllImport loading location: '%s'.", full_name);
767 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
769 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
770 "DllImport error loading library: '%s'.",
777 full_name = g_module_build_path (".", file_name);
778 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
779 "DllImport loading library: '%s'.", full_name);
780 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
782 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
783 "DllImport error loading library '%s'.",
790 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
791 "DllImport loading: '%s'.", file_name);
792 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
794 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
795 "DllImport error loading library '%s'.",
807 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
808 "DllImport unable to load library '%s'.",
812 *exc_class = "DllNotFoundException";
813 *exc_arg = new_scope;
818 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
819 "Searching for '%s'.", import);
821 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
822 g_module_symbol (gmodule, import, &piinfo->addr);
824 char *mangled_name = NULL, *mangled_name2 = NULL;
827 int mangle_param_count;
828 #ifdef PLATFORM_WIN32
833 * Search using a variety of mangled names
835 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
836 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
837 gboolean need_param_count = FALSE;
838 #ifdef PLATFORM_WIN32
839 if (mangle_stdcall > 0)
840 need_param_count = TRUE;
842 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
847 mangled_name = (char*)import;
848 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
849 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
850 /* Try the mangled name first */
851 if (mangle_charset == 0)
852 mangled_name = g_strconcat (import, "W", NULL);
854 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
855 #ifdef PLATFORM_WIN32
856 if (mangle_charset == 0)
857 mangled_name = g_strconcat (import, "W", NULL);
860 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
862 /* Try the mangled name last */
863 if (mangle_charset == 1)
864 mangled_name = g_strconcat (import, "A", NULL);
868 #ifdef PLATFORM_WIN32
869 if (mangle_param_count == 0)
870 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
872 /* Try brute force, since it would be very hard to compute the stack usage correctly */
873 param_count = mangle_param_count;
875 /* Try the stdcall mangled name */
877 * gcc under windows creates mangled names without the underscore, but MS.NET
878 * doesn't support it, so we doesn't support it either.
880 if (mangle_stdcall == 1)
881 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
883 mangled_name2 = mangled_name;
885 mangled_name2 = mangled_name;
888 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
889 "Probing '%s'.", mangled_name2);
891 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
894 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
895 "Found as '%s'.", mangled_name2);
897 if (mangled_name != mangled_name2)
898 g_free (mangled_name2);
899 if (mangled_name != import)
900 g_free (mangled_name);
908 *exc_class = "EntryPointNotFoundException";
917 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
918 MonoGenericContext *context)
921 int table = mono_metadata_token_table (token);
922 int idx = mono_metadata_token_index (token);
923 MonoTableInfo *tables = image->tables;
924 MonoGenericContainer *generic_container = NULL, *container = NULL;
925 const char *sig = NULL;
927 guint32 cols [MONO_TYPEDEF_SIZE];
930 return mono_lookup_dynamic_token (image, token);
932 if (table != MONO_TABLE_METHOD) {
933 MonoGenericContainer *generic_container = NULL;
935 g_assert (context->container);
936 generic_container = context->container;
938 if (table == MONO_TABLE_METHODSPEC)
939 return method_from_methodspec (image, context, idx);
940 if (table != MONO_TABLE_MEMBERREF)
941 g_print("got wrong token: 0x%08x\n", token);
942 g_assert (table == MONO_TABLE_MEMBERREF);
943 result = method_from_memberref (image, idx, context);
948 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
950 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
951 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
952 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
954 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
957 result->klass = klass;
958 result->flags = cols [2];
959 result->iflags = cols [1];
960 result->token = token;
961 result->name = mono_metadata_string_heap (image, cols [3]);
964 container = klass->generic_container;
966 if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
967 (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
968 generic_container = mono_metadata_load_generic_params (image, token, container);
969 if (generic_container)
970 container = generic_container;
973 if (!sig) /* already taken from the methodref */
974 sig = mono_metadata_blob_heap (image, cols [4]);
975 size = mono_metadata_decode_blob_size (sig, &sig);
977 /* there are generic params, or a container. FIXME: be lazy here for generics*/
978 if (* sig & 0x10 || container) {
979 result->signature = mono_metadata_parse_method_signature_full (
980 image, (MonoGenericContext *) container, idx, sig, NULL);
982 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
983 result->signature->pinvoke = 1;
986 if (!result->klass) {
987 guint32 type = mono_metadata_typedef_from_method (image, token);
988 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
991 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
992 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
993 result->string_ctor = 1;
994 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
995 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
996 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
998 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
999 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1001 if (result->signature && result->signature->generic_param_count) {
1002 MonoMethodSignature *sig = result->signature;
1004 for (i = 0; i < sig->generic_param_count; i++) {
1005 generic_container->type_params [i].method = result;
1007 mono_class_from_generic_parameter (
1008 &generic_container->type_params [i], image, TRUE);
1011 if (sig->ret->type == MONO_TYPE_MVAR) {
1012 int num = sig->ret->data.generic_param->num;
1013 sig->ret->data.generic_param = &generic_container->type_params [num];
1016 for (i = 0; i < sig->param_count; i++) {
1017 MonoType *t = sig->params [i];
1018 if (t->type == MONO_TYPE_MVAR) {
1019 int num = t->data.generic_param->num;
1020 sig->params [i]->data.generic_param = &generic_container->type_params [num];
1025 /* FIXME: lazyness for generics too, but how? */
1026 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1027 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1028 gpointer loc = mono_image_rva_map (image, cols [0]);
1030 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
1031 image, (MonoGenericContext *) container, loc);
1034 ((MonoMethodNormal *) result)->generic_container = generic_container;
1041 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1043 return mono_get_method_full (image, token, klass, NULL);
1047 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1048 MonoGenericContext *context)
1052 /* We do everything inside the lock to prevent creation races */
1054 mono_loader_lock ();
1056 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1057 mono_loader_unlock ();
1061 result = mono_get_method_from_token (image, token, klass, context);
1063 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1065 if (!(result && result->is_inflated))
1066 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1068 mono_loader_unlock ();
1074 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1075 MonoGenericContext *context)
1077 MonoMethod *method, *result;
1078 MonoClass *ic = NULL;
1079 MonoGenericClass *gclass = NULL;
1081 mono_loader_lock ();
1083 method = mono_get_method_from_token (image, token, NULL, context);
1085 mono_loader_unlock ();
1089 mono_class_init (constrained_class);
1090 method = mono_get_inflated_method (method);
1092 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1095 if (constrained_class->generic_class)
1096 gclass = constrained_class->generic_class;
1098 result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1100 g_warning ("Missing method %s in assembly %s token %x", method->name,
1101 image->name, token);
1104 result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
1106 mono_loader_unlock ();
1111 mono_free_method (MonoMethod *method)
1113 if (method->signature) {
1115 * FIXME: This causes crashes because the types inside signatures and
1116 * locals are shared.
1118 /* mono_metadata_free_method_signature (method->signature); */
1119 g_free (method->signature);
1122 if (method->dynamic) {
1123 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1125 g_free ((char*)method->name);
1126 if (mw->method.header)
1127 g_free ((char*)mw->method.header->code);
1128 g_free (mw->method_data);
1131 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1133 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1134 g_free (((MonoMethodNormal*)method)->header);
1141 mono_method_get_param_names (MonoMethod *method, const char **names)
1144 MonoClass *klass = method->klass;
1145 MonoTableInfo *methodt;
1146 MonoTableInfo *paramt;
1149 if (!mono_method_signature (method)->param_count)
1151 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1154 if (klass->generic_class) /* copy the names later */
1157 mono_class_init (klass);
1159 if (klass->image->dynamic) {
1160 MonoReflectionMethodAux *method_aux =
1161 g_hash_table_lookup (
1162 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1163 if (method_aux && method_aux->param_names) {
1164 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1165 if (method_aux->param_names [i + 1])
1166 names [i] = method_aux->param_names [i + 1];
1171 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1172 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1173 idx = mono_method_get_index (method);
1175 guint32 cols [MONO_PARAM_SIZE];
1176 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1178 if (idx < methodt->rows)
1179 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1181 lastp = paramt->rows + 1;
1182 for (i = param_index; i < lastp; ++i) {
1183 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1184 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1185 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1192 mono_method_get_param_token (MonoMethod *method, int index)
1194 MonoClass *klass = method->klass;
1195 MonoTableInfo *methodt;
1198 if (klass->generic_class)
1199 g_assert_not_reached ();
1201 mono_class_init (klass);
1203 if (klass->image->dynamic) {
1204 g_assert_not_reached ();
1207 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1208 idx = mono_method_get_index (method);
1210 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1212 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1219 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1222 MonoClass *klass = method->klass;
1223 MonoTableInfo *methodt;
1224 MonoTableInfo *paramt;
1227 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1230 if (method->klass->image->dynamic) {
1231 MonoReflectionMethodAux *method_aux =
1232 g_hash_table_lookup (
1233 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1234 if (method_aux && method_aux->param_marshall) {
1235 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1236 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1237 if (dyn_specs [i]) {
1238 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1239 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1245 mono_class_init (klass);
1247 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1248 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1249 idx = mono_method_get_index (method);
1251 guint32 cols [MONO_PARAM_SIZE];
1252 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1254 if (idx < methodt->rows)
1255 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1257 lastp = paramt->rows + 1;
1259 for (i = param_index; i < lastp; ++i) {
1260 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1262 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1264 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1266 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1275 mono_method_has_marshal_info (MonoMethod *method)
1278 MonoClass *klass = method->klass;
1279 MonoTableInfo *methodt;
1280 MonoTableInfo *paramt;
1283 if (method->klass->image->dynamic) {
1284 MonoReflectionMethodAux *method_aux =
1285 g_hash_table_lookup (
1286 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1287 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1289 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1296 mono_class_init (klass);
1298 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1299 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1300 idx = mono_method_get_index (method);
1302 guint32 cols [MONO_PARAM_SIZE];
1303 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1305 if (idx + 1 < methodt->rows)
1306 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1308 lastp = paramt->rows + 1;
1310 for (i = param_index; i < lastp; ++i) {
1311 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1313 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1322 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1325 g_assert (method != NULL);
1326 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1328 data = ((MonoMethodWrapper *)method)->method_data;
1329 g_assert (data != NULL);
1330 g_assert (id <= GPOINTER_TO_UINT (*data));
1335 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1336 g_error ("stack walk not installed");
1339 static MonoStackWalkImpl stack_walk = default_stack_walk;
1342 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1344 stack_walk (func, TRUE, user_data);
1348 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1350 stack_walk (func, FALSE, user_data);
1354 mono_install_stack_walk (MonoStackWalkImpl func)
1360 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1362 MonoMethod **dest = data;
1364 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1370 mono_method_get_last_managed (void)
1372 MonoMethod *m = NULL;
1373 stack_walk (last_managed, FALSE, &m);
1378 mono_loader_lock (void)
1380 EnterCriticalSection (&loader_mutex);
1384 mono_loader_unlock (void)
1386 LeaveCriticalSection (&loader_mutex);
1389 MonoMethodSignature*
1390 mono_method_signature (MonoMethod *m)
1392 return mono_method_signature_full (m, NULL);
1395 MonoMethodSignature*
1396 mono_method_signature_full (MonoMethod *m, MonoGenericContext *context)
1404 return m->signature;
1406 mono_loader_lock ();
1409 mono_loader_unlock ();
1410 return m->signature;
1413 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1414 idx = mono_metadata_token_index (m->token);
1415 img = m->klass->image;
1417 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1418 size = mono_metadata_decode_blob_size (sig, &sig);
1420 m->signature = mono_metadata_parse_method_signature_full (img, context, idx, sig, NULL);
1422 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1423 m->signature->pinvoke = 1;
1424 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1425 MonoCallConvention conv = 0;
1426 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1427 m->signature->pinvoke = 1;
1429 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1430 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1431 conv = MONO_CALL_DEFAULT;
1433 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1436 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1437 conv = MONO_CALL_STDCALL;
1439 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1440 conv = MONO_CALL_THISCALL;
1442 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1443 conv = MONO_CALL_FASTCALL;
1445 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1446 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1448 g_warning ("unsupported calling convention");
1449 g_assert_not_reached ();
1451 m->signature->call_convention = conv;
1454 mono_loader_unlock ();
1455 return m->signature;
1459 mono_method_get_name (MonoMethod *method)
1461 return method->name;
1465 mono_method_get_class (MonoMethod *method)
1467 return method->klass;
1471 mono_method_get_token (MonoMethod *method)
1473 return method->token;
1477 mono_method_get_header (MonoMethod *method)
1483 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1486 if (G_LIKELY (mn->header))
1492 if (method->klass->dummy || (method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1495 mono_loader_lock ();
1498 mono_loader_unlock ();
1502 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1503 idx = mono_metadata_token_index (method->token);
1504 img = method->klass->image;
1505 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1506 loc = mono_image_rva_map (img, rva);
1510 mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1512 mono_loader_unlock ();
1517 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1520 *iflags = method->iflags;
1521 return method->flags;
1525 * Find the method index in the metadata methodDef table.
1528 mono_method_get_index (MonoMethod *method) {
1529 MonoClass *klass = method->klass;
1533 return mono_metadata_token_index (method->token);
1535 mono_class_setup_methods (klass);
1536 for (i = 0; i < klass->method.count; ++i) {
1537 if (method == klass->methods [i])
1538 return klass->method.first + 1 + i;