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 gboolean is_interface;
227 MonoMethod *result = NULL;
229 is_interface = MONO_CLASS_IS_INTERFACE (klass);
232 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
234 qname = g_strconcat (class_name, ".", name, NULL);
235 if (ic->name_space && ic->name_space [0])
236 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
240 class_name = qname = fqname = NULL;
243 result = find_method_in_class (klass, name, qname, fqname, sig);
247 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
250 for (i = 0; i < klass->interface_count; i++) {
251 MonoClass *ic = klass->interfaces [i];
253 result = find_method_in_class (ic, name, qname, fqname, sig);
258 klass = klass->parent;
262 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig);
272 * token is the method_ref or method_def token used in a call IL instruction.
275 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
277 int table = mono_metadata_token_table (token);
278 int idx = mono_metadata_token_index (token);
279 guint32 cols [MONO_MEMBERREF_SIZE];
280 MonoMethodSignature *sig, *prev_sig;
283 /* !table is for wrappers: we should really assign their own token to them */
284 if (!table || table == MONO_TABLE_METHOD)
285 return mono_method_signature (method);
287 if (table == MONO_TABLE_METHODSPEC) {
288 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
289 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
290 mono_method_signature (method));
291 g_assert (method->is_inflated);
293 return mono_method_signature (method);
296 if (method->klass->generic_class)
297 return mono_method_signature (method);
300 /* FIXME: This might be incorrect for vararg methods */
301 return mono_method_signature (method);
304 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
305 mono_loader_unlock ();
307 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
309 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
310 mono_metadata_decode_blob_size (ptr, &ptr);
311 sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
314 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
316 /* Somebody got in before us */
317 /* FIXME: Free sig */
321 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
322 mono_loader_unlock ();
325 sig = mono_class_inflate_generic_signature (image, sig, context);
331 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
333 return mono_method_get_signature_full (method, image, token, NULL);
337 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
339 MonoClass *klass = NULL;
340 MonoMethod *method = NULL;
341 MonoTableInfo *tables = image->tables;
343 guint32 nindex, class;
344 MonoGenericClass *gclass = NULL;
345 MonoGenericContainer *container = NULL;
347 MonoMethodSignature *sig;
350 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
351 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
352 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
353 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
354 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
356 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
359 case MONO_MEMBERREF_PARENT_TYPEREF:
360 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
362 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
363 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
368 case MONO_MEMBERREF_PARENT_TYPESPEC:
369 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
371 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
372 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
377 case MONO_MEMBERREF_PARENT_TYPEDEF:
378 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
380 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex, context);
381 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
386 case MONO_MEMBERREF_PARENT_METHODDEF:
387 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
389 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
390 g_assert_not_reached ();
394 if (klass->generic_class) {
395 gclass = klass->generic_class;
396 klass = gclass->container_class;
398 if (klass->generic_container)
399 container = klass->generic_container;
400 mono_class_init (klass);
402 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
403 mono_metadata_decode_blob_size (ptr, &ptr);
404 sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
407 case MONO_MEMBERREF_PARENT_TYPEREF:
408 method = find_method (klass, NULL, mname, sig);
410 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
411 mono_metadata_free_method_signature (sig);
413 case MONO_MEMBERREF_PARENT_TYPESPEC: {
417 type = &klass->byval_arg;
419 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
420 method = find_method (klass, NULL, mname, sig);
422 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
423 else if (klass->generic_class && (klass != method->klass))
424 method = mono_class_inflate_generic_method (
425 method, klass->generic_class->context);
426 mono_metadata_free_method_signature (sig);
430 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
431 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
432 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
433 result->signature = sig;
434 result->name = mname;
436 if (!strcmp (mname, ".ctor")) {
437 /* we special-case this in the runtime. */
441 if (!strcmp (mname, "Set")) {
442 g_assert (sig->hasthis);
443 g_assert (type->data.array->rank + 1 == sig->param_count);
444 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
448 if (!strcmp (mname, "Get")) {
449 g_assert (sig->hasthis);
450 g_assert (type->data.array->rank == sig->param_count);
451 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
455 if (!strcmp (mname, "Address")) {
456 g_assert (sig->hasthis);
457 g_assert (type->data.array->rank == sig->param_count);
458 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
462 g_assert_not_reached ();
465 case MONO_MEMBERREF_PARENT_TYPEDEF:
466 method = find_method (klass, NULL, mname, sig);
468 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
469 mono_metadata_free_method_signature (sig);
472 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
473 g_assert_not_reached ();
477 method = mono_class_inflate_generic_method (method, gclass->context);
483 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
485 MonoMethod *method, *inflated;
486 MonoTableInfo *tables = image->tables;
487 MonoGenericContext *new_context = NULL;
488 MonoGenericMethod *gmethod;
489 MonoGenericContainer *container = NULL;
491 guint32 cols [MONO_METHODSPEC_SIZE];
492 guint32 token, param_count;
494 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
495 token = cols [MONO_METHODSPEC_METHOD];
496 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
497 token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
499 token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
501 method = mono_get_method (image, token, NULL);
502 method = mono_get_inflated_method (method);
504 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
506 mono_metadata_decode_value (ptr, &ptr);
508 param_count = mono_metadata_decode_value (ptr, &ptr);
510 g_assert (param_count);
511 if (method->is_inflated)
512 container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
514 container = ((MonoMethodNormal *) method)->generic_container;
515 g_assert (container && container->is_method);
518 g_assert (context->container);
519 container->parent = context->container;
520 if (container->parent->is_method)
521 container->parent = container->parent->parent;
524 gmethod = g_new0 (MonoGenericMethod, 1);
525 gmethod->generic_class = method->klass->generic_class;
526 gmethod->container = container;
528 if (context && context->gmethod)
529 new_context = &context->gmethod->container->context;
530 else if (context && context->gclass)
531 new_context = &context->gclass->container_class->generic_container->context;
533 new_context = context ? &context->container->context : NULL;
535 gmethod->inst = mono_metadata_parse_generic_inst (
536 image, new_context, param_count, ptr, &ptr);
539 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
541 if (!container->method_hash)
542 container->method_hash = g_hash_table_new (
543 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
545 inflated = g_hash_table_lookup (container->method_hash, gmethod);
552 new_context = g_new0 (MonoGenericContext, 1);
553 new_context->container = container;
554 new_context->gmethod = gmethod;
556 context = new_context;
558 new_context = g_new0 (MonoGenericContext, 1);
559 new_context->container = container;
560 new_context->gmethod = gmethod;
561 new_context->gclass = context->gclass;
563 context = new_context;
566 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
567 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
569 inflated = mono_class_inflate_generic_method (method, context);
570 g_hash_table_insert (container->method_hash, gmethod, inflated);
573 context->gclass = inflated->klass->generic_class;
577 typedef struct MonoDllMap MonoDllMap;
586 static GHashTable *global_dll_map;
589 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
590 MonoDllMap *map, *tmp;
599 map = g_hash_table_lookup (dll_map, dll);
601 mono_loader_unlock ();
604 *rdll = map->target? map->target: dll;
606 for (tmp = map->next; tmp; tmp = tmp->next) {
607 if (strcmp (func, tmp->name) == 0) {
611 mono_loader_unlock ();
616 mono_loader_unlock ();
621 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
624 if (assembly && assembly->dll_map) {
625 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
629 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
633 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
634 MonoDllMap *map, *entry;
635 GHashTable *dll_map = NULL;
641 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
642 dll_map = global_dll_map;
644 if (!assembly->dll_map)
645 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
646 dll_map = assembly->dll_map;
649 map = g_hash_table_lookup (dll_map, dll);
651 map = g_new0 (MonoDllMap, 1);
652 map->dll = g_strdup (dll);
654 map->target = g_strdup (tdll);
655 g_hash_table_insert (dll_map, map->dll, map);
658 entry = g_new0 (MonoDllMap, 1);
659 entry->name = g_strdup (func);
661 entry->target = g_strdup (tfunc);
662 if (tdll && map->target && strcmp (map->target, tdll))
663 entry->dll = g_strdup (tdll);
664 entry->next = map->next;
668 mono_loader_unlock ();
672 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
674 MonoImage *image = method->klass->image;
675 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
676 MonoTableInfo *tables = image->tables;
677 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
678 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
679 guint32 im_cols [MONO_IMPLMAP_SIZE];
681 const char *import = NULL;
682 const char *orig_scope;
683 const char *new_scope;
684 char *full_name, *file_name;
686 GModule *gmodule = NULL;
688 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
693 if (method->klass->image->dynamic) {
694 MonoReflectionMethodAux *method_aux =
695 g_hash_table_lookup (
696 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
700 import = method_aux->dllentry;
701 orig_scope = method_aux->dll;
704 if (!piinfo->implmap_idx)
707 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
709 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
710 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
711 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
712 orig_scope = mono_metadata_string_heap (image, scope_token);
715 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
717 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
718 "DllImport attempting to load: '%s'.", new_scope);
725 /* we allow a special name to dlopen from the running process namespace */
726 if (strcmp (new_scope, "__Internal") == 0)
727 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
730 * Try loading the module using a variety of names
732 for (i = 0; i < 4; ++i) {
735 /* Try the original name */
736 file_name = g_strdup (new_scope);
739 /* Try trimming the .dll extension */
740 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
741 file_name = g_strdup (new_scope);
742 file_name [strlen (new_scope) - 4] = '\0';
748 if (strstr (new_scope, "lib") != new_scope) {
749 file_name = g_strdup_printf ("lib%s", new_scope);
755 #ifndef PLATFORM_WIN32
756 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
757 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
758 !g_ascii_strcasecmp ("user32", new_scope) ||
759 !g_ascii_strcasecmp ("kernel", new_scope)) {
760 file_name = g_strdup ("libMonoSupportW.so");
764 #ifndef PLATFORM_WIN32
770 full_name = g_module_build_path (NULL, file_name);
771 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
772 "DllImport loading location: '%s'.", full_name);
773 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
775 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
776 "DllImport error loading library: '%s'.",
783 full_name = g_module_build_path (".", file_name);
784 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
785 "DllImport loading library: '%s'.", full_name);
786 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
788 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
789 "DllImport error loading library '%s'.",
796 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
797 "DllImport loading: '%s'.", file_name);
798 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
800 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
801 "DllImport error loading library '%s'.",
813 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
814 "DllImport unable to load library '%s'.",
818 *exc_class = "DllNotFoundException";
819 *exc_arg = new_scope;
824 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
825 "Searching for '%s'.", import);
827 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
828 g_module_symbol (gmodule, import, &piinfo->addr);
830 char *mangled_name = NULL, *mangled_name2 = NULL;
833 int mangle_param_count;
834 #ifdef PLATFORM_WIN32
839 * Search using a variety of mangled names
841 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
842 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
843 gboolean need_param_count = FALSE;
844 #ifdef PLATFORM_WIN32
845 if (mangle_stdcall > 0)
846 need_param_count = TRUE;
848 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
853 mangled_name = (char*)import;
854 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
855 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
856 /* Try the mangled name first */
857 if (mangle_charset == 0)
858 mangled_name = g_strconcat (import, "W", NULL);
860 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
861 #ifdef PLATFORM_WIN32
862 if (mangle_charset == 0)
863 mangled_name = g_strconcat (import, "W", NULL);
866 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
868 /* Try the mangled name last */
869 if (mangle_charset == 1)
870 mangled_name = g_strconcat (import, "A", NULL);
874 #ifdef PLATFORM_WIN32
875 if (mangle_param_count == 0)
876 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
878 /* Try brute force, since it would be very hard to compute the stack usage correctly */
879 param_count = mangle_param_count;
881 /* Try the stdcall mangled name */
883 * gcc under windows creates mangled names without the underscore, but MS.NET
884 * doesn't support it, so we doesn't support it either.
886 if (mangle_stdcall == 1)
887 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
889 mangled_name2 = mangled_name;
891 mangled_name2 = mangled_name;
894 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
895 "Probing '%s'.", mangled_name2);
897 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
900 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
901 "Found as '%s'.", mangled_name2);
903 if (mangled_name != mangled_name2)
904 g_free (mangled_name2);
905 if (mangled_name != import)
906 g_free (mangled_name);
914 *exc_class = "EntryPointNotFoundException";
923 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
924 MonoGenericContext *context)
927 int table = mono_metadata_token_table (token);
928 int idx = mono_metadata_token_index (token);
929 MonoTableInfo *tables = image->tables;
930 MonoGenericContainer *generic_container = NULL, *container = NULL;
931 const char *sig = NULL;
933 guint32 cols [MONO_TYPEDEF_SIZE];
936 return mono_lookup_dynamic_token (image, token);
938 if (table != MONO_TABLE_METHOD) {
939 MonoGenericContainer *generic_container = NULL;
941 g_assert (context->container);
942 generic_container = context->container;
944 if (table == MONO_TABLE_METHODSPEC)
945 return method_from_methodspec (image, context, idx);
946 if (table != MONO_TABLE_MEMBERREF)
947 g_print("got wrong token: 0x%08x\n", token);
948 g_assert (table == MONO_TABLE_MEMBERREF);
949 result = method_from_memberref (image, idx, context);
954 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
956 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
957 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
958 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
960 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
963 result->klass = klass;
964 result->flags = cols [2];
965 result->iflags = cols [1];
966 result->token = token;
967 result->name = mono_metadata_string_heap (image, cols [3]);
970 container = klass->generic_container;
972 if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
973 (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
974 generic_container = mono_metadata_load_generic_params (image, token, container);
975 if (generic_container)
976 container = generic_container;
979 if (!sig) /* already taken from the methodref */
980 sig = mono_metadata_blob_heap (image, cols [4]);
981 size = mono_metadata_decode_blob_size (sig, &sig);
983 /* there are generic params, or a container. FIXME: be lazy here for generics*/
984 if (* sig & 0x10 || container) {
985 result->signature = mono_metadata_parse_method_signature_full (
986 image, (MonoGenericContext *) container, idx, sig, NULL);
988 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
989 result->signature->pinvoke = 1;
992 if (!result->klass) {
993 guint32 type = mono_metadata_typedef_from_method (image, token);
994 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
997 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
998 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
999 result->string_ctor = 1;
1000 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1001 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1002 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1004 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1005 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1007 if (result->signature && result->signature->generic_param_count) {
1008 MonoMethodSignature *sig = result->signature;
1010 for (i = 0; i < sig->generic_param_count; i++) {
1011 generic_container->type_params [i].method = result;
1013 mono_class_from_generic_parameter (
1014 &generic_container->type_params [i], image, TRUE);
1017 if (sig->ret->type == MONO_TYPE_MVAR) {
1018 int num = sig->ret->data.generic_param->num;
1019 sig->ret->data.generic_param = &generic_container->type_params [num];
1022 for (i = 0; i < sig->param_count; i++) {
1023 MonoType *t = sig->params [i];
1024 if (t->type == MONO_TYPE_MVAR) {
1025 int num = t->data.generic_param->num;
1026 sig->params [i]->data.generic_param = &generic_container->type_params [num];
1031 /* FIXME: lazyness for generics too, but how? */
1032 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1033 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1034 gpointer loc = mono_image_rva_map (image, cols [0]);
1036 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
1037 image, (MonoGenericContext *) container, loc);
1040 ((MonoMethodNormal *) result)->generic_container = generic_container;
1047 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1049 return mono_get_method_full (image, token, klass, NULL);
1053 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1054 MonoGenericContext *context)
1058 /* We do everything inside the lock to prevent creation races */
1060 mono_loader_lock ();
1062 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1063 mono_loader_unlock ();
1067 result = mono_get_method_from_token (image, token, klass, context);
1069 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1071 if (!(result && result->is_inflated))
1072 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1074 mono_loader_unlock ();
1080 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1081 MonoGenericContext *context)
1083 MonoMethod *method, *result;
1084 MonoClass *ic = NULL;
1085 MonoGenericClass *gclass = NULL;
1087 mono_loader_lock ();
1089 method = mono_get_method_from_token (image, token, NULL, context);
1091 mono_loader_unlock ();
1095 mono_class_init (constrained_class);
1096 method = mono_get_inflated_method (method);
1098 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1101 if (constrained_class->generic_class)
1102 gclass = constrained_class->generic_class;
1104 result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1106 g_warning ("Missing method %s in assembly %s token %x", method->name,
1107 image->name, token);
1110 result = mono_class_inflate_generic_method (result, gclass->context);
1112 mono_loader_unlock ();
1117 mono_free_method (MonoMethod *method)
1119 if (method->signature) {
1121 * FIXME: This causes crashes because the types inside signatures and
1122 * locals are shared.
1124 /* mono_metadata_free_method_signature (method->signature); */
1125 g_free (method->signature);
1128 if (method->dynamic) {
1129 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1131 g_free ((char*)method->name);
1132 if (mw->method.header)
1133 g_free ((char*)mw->method.header->code);
1134 g_free (mw->method_data);
1137 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1139 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1140 g_free (((MonoMethodNormal*)method)->header);
1147 mono_method_get_param_names (MonoMethod *method, const char **names)
1150 MonoClass *klass = method->klass;
1151 MonoTableInfo *methodt;
1152 MonoTableInfo *paramt;
1155 if (!mono_method_signature (method)->param_count)
1157 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1160 if (klass->generic_class) /* copy the names later */
1163 mono_class_init (klass);
1165 if (klass->image->dynamic) {
1166 MonoReflectionMethodAux *method_aux =
1167 g_hash_table_lookup (
1168 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1169 if (method_aux && method_aux->param_names) {
1170 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1171 if (method_aux->param_names [i + 1])
1172 names [i] = method_aux->param_names [i + 1];
1177 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1178 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1179 idx = mono_method_get_index (method);
1181 guint32 cols [MONO_PARAM_SIZE];
1182 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1184 if (idx < methodt->rows)
1185 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1187 lastp = paramt->rows + 1;
1188 for (i = param_index; i < lastp; ++i) {
1189 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1190 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1191 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1198 mono_method_get_param_token (MonoMethod *method, int index)
1200 MonoClass *klass = method->klass;
1201 MonoTableInfo *methodt;
1204 if (klass->generic_class)
1205 g_assert_not_reached ();
1207 mono_class_init (klass);
1209 if (klass->image->dynamic) {
1210 g_assert_not_reached ();
1213 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1214 idx = mono_method_get_index (method);
1216 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1218 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1225 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1228 MonoClass *klass = method->klass;
1229 MonoTableInfo *methodt;
1230 MonoTableInfo *paramt;
1233 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1236 if (method->klass->image->dynamic) {
1237 MonoReflectionMethodAux *method_aux =
1238 g_hash_table_lookup (
1239 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1240 if (method_aux && method_aux->param_marshall) {
1241 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1242 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1243 if (dyn_specs [i]) {
1244 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1245 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1251 mono_class_init (klass);
1253 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1254 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1255 idx = mono_method_get_index (method);
1257 guint32 cols [MONO_PARAM_SIZE];
1258 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1260 if (idx < methodt->rows)
1261 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1263 lastp = paramt->rows + 1;
1265 for (i = param_index; i < lastp; ++i) {
1266 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1268 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1270 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1272 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1281 mono_method_has_marshal_info (MonoMethod *method)
1284 MonoClass *klass = method->klass;
1285 MonoTableInfo *methodt;
1286 MonoTableInfo *paramt;
1289 if (method->klass->image->dynamic) {
1290 MonoReflectionMethodAux *method_aux =
1291 g_hash_table_lookup (
1292 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1293 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1295 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1302 mono_class_init (klass);
1304 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1305 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1306 idx = mono_method_get_index (method);
1308 guint32 cols [MONO_PARAM_SIZE];
1309 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1311 if (idx + 1 < methodt->rows)
1312 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1314 lastp = paramt->rows + 1;
1316 for (i = param_index; i < lastp; ++i) {
1317 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1319 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1328 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1331 g_assert (method != NULL);
1332 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1334 data = ((MonoMethodWrapper *)method)->method_data;
1335 g_assert (data != NULL);
1336 g_assert (id <= GPOINTER_TO_UINT (*data));
1341 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1342 g_error ("stack walk not installed");
1345 static MonoStackWalkImpl stack_walk = default_stack_walk;
1348 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1350 stack_walk (func, TRUE, user_data);
1354 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1356 stack_walk (func, FALSE, user_data);
1360 mono_install_stack_walk (MonoStackWalkImpl func)
1366 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1368 MonoMethod **dest = data;
1370 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1376 mono_method_get_last_managed (void)
1378 MonoMethod *m = NULL;
1379 stack_walk (last_managed, FALSE, &m);
1384 mono_loader_lock (void)
1386 EnterCriticalSection (&loader_mutex);
1390 mono_loader_unlock (void)
1392 LeaveCriticalSection (&loader_mutex);
1395 MonoMethodSignature*
1396 mono_method_signature (MonoMethod *m)
1398 m = mono_get_inflated_method (m);
1399 return mono_method_signature_full (m, NULL);
1402 MonoMethodSignature*
1403 mono_method_signature_full (MonoMethod *m, MonoGenericContext *context)
1411 return m->signature;
1413 mono_loader_lock ();
1416 mono_loader_unlock ();
1417 return m->signature;
1420 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1421 idx = mono_metadata_token_index (m->token);
1422 img = m->klass->image;
1424 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1425 size = mono_metadata_decode_blob_size (sig, &sig);
1427 m->signature = mono_metadata_parse_method_signature_full (img, context, idx, sig, NULL);
1429 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1430 m->signature->pinvoke = 1;
1431 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1432 MonoCallConvention conv = 0;
1433 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1434 m->signature->pinvoke = 1;
1436 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1437 case 0: /* no call conv, so using default */
1438 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1439 conv = MONO_CALL_DEFAULT;
1441 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1444 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1445 conv = MONO_CALL_STDCALL;
1447 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1448 conv = MONO_CALL_THISCALL;
1450 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1451 conv = MONO_CALL_FASTCALL;
1453 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1454 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1456 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1457 g_assert_not_reached ();
1459 m->signature->call_convention = conv;
1462 mono_loader_unlock ();
1463 return m->signature;
1467 mono_method_get_name (MonoMethod *method)
1469 return method->name;
1473 mono_method_get_class (MonoMethod *method)
1475 return method->klass;
1479 mono_method_get_token (MonoMethod *method)
1481 return method->token;
1485 mono_method_get_header (MonoMethod *method)
1491 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1494 if (G_LIKELY (mn->header))
1500 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))
1503 mono_loader_lock ();
1506 mono_loader_unlock ();
1510 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1511 idx = mono_metadata_token_index (method->token);
1512 img = method->klass->image;
1513 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1514 loc = mono_image_rva_map (img, rva);
1518 mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1520 mono_loader_unlock ();
1525 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1528 *iflags = method->iflags;
1529 return method->flags;
1533 * Find the method index in the metadata methodDef table.
1536 mono_method_get_index (MonoMethod *method) {
1537 MonoClass *klass = method->klass;
1541 return mono_metadata_token_index (method->token);
1543 mono_class_setup_methods (klass);
1544 for (i = 0; i < klass->method.count; ++i) {
1545 if (method == klass->methods [i])
1546 return klass->method.first + 1 + i;