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 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * This file is used by the interpreter and the JIT engine to locate
13 * assemblies. Used to load AssemblyRef and later to resolve various
17 * 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/tabledefs.h>
30 #include <mono/metadata/metadata-internals.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/class-internals.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/profiler.h>
36 #include <mono/metadata/profiler-private.h>
37 #include <mono/metadata/exception.h>
38 #include <mono/metadata/marshal.h>
39 #include <mono/utils/mono-logger.h>
40 #include <mono/utils/mono-dl.h>
41 #include <mono/utils/mono-membar.h>
42 #include <mono/utils/mono-counters.h>
44 MonoDefaults mono_defaults;
47 * This lock protects the hash tables inside MonoImage used by the metadata
48 * loading functions in class.c and loader.c.
50 * See domain-internals.h for locking policy in combination with the
53 static CRITICAL_SECTION loader_mutex;
56 static guint32 inflated_signatures_size;
57 static guint32 memberref_sig_cache_size;
60 * This TLS variable contains the last type load error encountered by the loader.
62 guint32 loader_error_thread_id;
67 static gboolean inited;
70 InitializeCriticalSection (&loader_mutex);
72 loader_error_thread_id = TlsAlloc ();
74 mono_counters_register ("Inflated signatures size",
75 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
76 mono_counters_register ("Memberref signature cache size",
77 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &memberref_sig_cache_size);
84 mono_loader_cleanup (void)
86 TlsFree (loader_error_thread_id);
88 /*DeleteCriticalSection (&loader_mutex);*/
92 * Handling of type load errors should be done as follows:
94 * If something could not be loaded, the loader should call one of the
95 * mono_loader_set_error_XXX functions ()
96 * with the appropriate arguments, then return NULL to report the failure. The error
97 * should be propagated until it reaches code which can throw managed exceptions. At that
98 * point, an exception should be thrown based on the information returned by
99 * mono_loader_get_last_error (). Then the error should be cleared by calling
100 * mono_loader_clear_error ().
104 set_loader_error (MonoLoaderError *error)
106 TlsSetValue (loader_error_thread_id, error);
110 * mono_loader_set_error_assembly_load:
112 * Set the loader error for this thread.
115 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
117 MonoLoaderError *error;
119 if (mono_loader_get_last_error ())
122 error = g_new0 (MonoLoaderError, 1);
123 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
124 error->assembly_name = g_strdup (assembly_name);
125 error->ref_only = ref_only;
128 * This is not strictly needed, but some (most) of the loader code still
129 * can't deal with load errors, and this message is more helpful than an
133 g_warning ("Cannot resolve dependency to assembly '%s' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.", assembly_name);
135 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
137 set_loader_error (error);
141 * mono_loader_set_error_type_load:
143 * Set the loader error for this thread.
146 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
148 MonoLoaderError *error;
150 if (mono_loader_get_last_error ())
153 error = g_new0 (MonoLoaderError, 1);
154 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
155 error->class_name = g_strdup (class_name);
156 error->assembly_name = g_strdup (assembly_name);
159 * This is not strictly needed, but some (most) of the loader code still
160 * can't deal with load errors, and this message is more helpful than an
163 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
165 set_loader_error (error);
169 * mono_loader_set_error_method_load:
171 * Set the loader error for this thread. MEMBER_NAME should point to a string
175 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
177 MonoLoaderError *error;
179 /* FIXME: Store the signature as well */
180 if (mono_loader_get_last_error ())
183 error = g_new0 (MonoLoaderError, 1);
184 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
185 error->class_name = g_strdup (class_name);
186 error->member_name = member_name;
188 set_loader_error (error);
192 * mono_loader_set_error_field_load:
194 * Set the loader error for this thread. MEMBER_NAME should point to a string
198 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
200 MonoLoaderError *error;
202 /* FIXME: Store the signature as well */
203 if (mono_loader_get_last_error ())
206 error = g_new0 (MonoLoaderError, 1);
207 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
208 error->klass = klass;
209 error->member_name = member_name;
211 set_loader_error (error);
215 * mono_loader_set_error_bad_image:
217 * Set the loader error for this thread.
220 mono_loader_set_error_bad_image (char *msg)
222 MonoLoaderError *error;
224 if (mono_loader_get_last_error ())
227 error = g_new0 (MonoLoaderError, 1);
228 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
231 set_loader_error (error);
236 * mono_loader_get_last_error:
238 * Returns information about the last type load exception encountered by the loader, or
239 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
242 mono_loader_get_last_error (void)
244 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
248 * mono_loader_clear_error:
250 * Disposes any loader error messages on this thread
253 mono_loader_clear_error (void)
255 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
258 g_free (ex->class_name);
259 g_free (ex->assembly_name);
263 TlsSetValue (loader_error_thread_id, NULL);
268 * mono_loader_error_prepare_exception:
269 * @error: The MonoLoaderError to turn into an exception
271 * This turns a MonoLoaderError into an exception that can be thrown
272 * and resets the Mono Loader Error state during this process.
276 mono_loader_error_prepare_exception (MonoLoaderError *error)
278 MonoException *ex = NULL;
280 switch (error->exception_type) {
281 case MONO_EXCEPTION_TYPE_LOAD: {
282 char *cname = g_strdup (error->class_name);
283 char *aname = g_strdup (error->assembly_name);
284 MonoString *class_name;
286 mono_loader_clear_error ();
288 class_name = mono_string_new (mono_domain_get (), cname);
290 ex = mono_get_exception_type_load (class_name, aname);
295 case MONO_EXCEPTION_MISSING_METHOD: {
296 char *cname = g_strdup (error->class_name);
297 char *aname = g_strdup (error->member_name);
299 mono_loader_clear_error ();
300 ex = mono_get_exception_missing_method (cname, aname);
306 case MONO_EXCEPTION_MISSING_FIELD: {
307 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
308 char *cname = g_strdup (error->klass->name);
309 char *cmembername = g_strdup (error->member_name);
312 mono_loader_clear_error ();
313 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
315 ex = mono_get_exception_missing_field (class_name, cmembername);
318 g_free (cmembername);
323 case MONO_EXCEPTION_FILE_NOT_FOUND: {
328 msg = g_strdup_printf ("Cannot resolve dependency to assembly '%s' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.", error->assembly_name);
330 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
331 filename = g_strdup (error->assembly_name);
332 /* Has to call this before calling anything which might call mono_class_init () */
333 mono_loader_clear_error ();
334 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
340 case MONO_EXCEPTION_BAD_IMAGE: {
341 char *msg = g_strdup (error->msg);
342 mono_loader_clear_error ();
343 ex = mono_get_exception_bad_image_format (msg);
349 g_assert_not_reached ();
356 * find_cached_memberref_sig:
358 * Return a cached copy of the memberref signature identified by SIG_IDX.
359 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
360 * A cache is needed since the type/signature parsing routines allocate everything
361 * from a mempool, so without a cache, multiple requests for the same signature would
362 * lead to unbounded memory growth. For normal methods/fields this is not a problem
363 * since the resulting methods/fields are cached, but inflated methods/fields cannot
365 * LOCKING: Acquires the loader lock.
368 find_cached_memberref_sig (MonoImage *image, guint32 sig_idx)
373 res = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
374 mono_loader_unlock ();
380 cache_memberref_sig (MonoImage *image, guint32 sig_idx, gpointer sig)
385 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
387 /* Somebody got in before us */
391 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (sig_idx), sig);
392 /* An approximation based on glib 2.18 */
393 memberref_sig_cache_size += sizeof (gpointer) * 4;
396 mono_loader_unlock ();
401 static MonoClassField*
402 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
403 MonoGenericContext *context)
406 MonoClassField *field, *sig_field = NULL;
407 MonoTableInfo *tables = image->tables;
410 guint32 nindex, class;
413 guint32 idx = mono_metadata_token_index (token);
415 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
416 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
417 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
419 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
421 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
422 mono_metadata_decode_blob_size (ptr, &ptr);
423 /* we may want to check the signature here... */
426 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token %08x field name %s token %08x", class, fname, token));
429 /* FIXME: This needs a cache, especially for generic instances, since
430 * mono_metadata_parse_type () allocates everything from a mempool.
432 sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
434 sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
435 sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
439 case MONO_MEMBERREF_PARENT_TYPEDEF:
440 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
442 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
443 g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
444 mono_loader_set_error_type_load (name, image->assembly_name);
448 mono_class_init (klass);
451 sig_field = field = mono_class_get_field_from_name (klass, fname);
453 case MONO_MEMBERREF_PARENT_TYPEREF:
454 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
456 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
457 g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
458 mono_loader_set_error_type_load (name, image->assembly_name);
462 mono_class_init (klass);
465 sig_field = field = mono_class_get_field_from_name (klass, fname);
467 case MONO_MEMBERREF_PARENT_TYPESPEC: {
468 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
469 //FIXME can't klass be null?
470 mono_class_init (klass);
473 field = mono_class_get_field_from_name (klass, fname);
475 sig_field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
479 g_warning ("field load from %x", class);
484 mono_loader_set_error_field_load (klass, fname);
485 else if (sig_field && !mono_metadata_type_equal_full (sig_type, sig_field->type, TRUE)) {
486 mono_loader_set_error_field_load (klass, fname);
494 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
495 MonoGenericContext *context)
499 MonoClassField *field;
501 if (image->dynamic) {
502 MonoClassField *result;
503 MonoClass *handle_class;
506 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
507 // This checks the memberref type as well
508 if (result && handle_class != mono_defaults.fieldhandle_class) {
509 mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
512 *retklass = result->parent;
517 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
518 *retklass = field->parent;
519 mono_loader_unlock ();
522 mono_loader_unlock ();
524 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
525 field = field_from_memberref (image, token, retklass, context);
527 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
530 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
536 field = mono_class_get_field (k, token);
540 if (field && !field->parent->generic_class && !field->parent->generic_container)
541 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
542 mono_loader_unlock ();
547 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
551 if (sig1->hasthis != sig2->hasthis ||
552 sig1->sentinelpos != sig2->sentinelpos)
555 for (i = 0; i < sig1->sentinelpos; i++) {
556 MonoType *p1 = sig1->params[i];
557 MonoType *p2 = sig2->params[i];
559 /*if (p1->attrs != p2->attrs)
562 if (!mono_metadata_type_equal (p1, p2))
566 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
572 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
573 MonoMethodSignature *sig, MonoClass *from_class)
577 /* Search directly in the metadata to avoid calling setup_methods () */
579 /* FIXME: !from_class->generic_class condition causes test failures. */
580 if (klass->type_token && !klass->image->dynamic && !klass->methods && !klass->rank && klass == from_class && !from_class->generic_class) {
581 for (i = 0; i < klass->method.count; ++i) {
582 guint32 cols [MONO_METHOD_SIZE];
586 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
588 m_name = mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]);
590 if (!((fqname && !strcmp (m_name, fqname)) ||
591 (qname && !strcmp (m_name, qname)) ||
592 (name && !strcmp (m_name, name))))
595 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
596 if (method && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, mono_method_signature (method)))
601 mono_class_setup_methods (klass);
602 for (i = 0; i < klass->method.count; ++i) {
603 MonoMethod *m = klass->methods [i];
605 if (!((fqname && !strcmp (m->name, fqname)) ||
606 (qname && !strcmp (m->name, qname)) ||
607 (name && !strcmp (m->name, name))))
610 if (sig->call_convention == MONO_CALL_VARARG) {
611 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
614 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
619 if (i < klass->method.count)
620 return mono_class_get_method_by_index (from_class, i);
625 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
628 char *qname, *fqname, *class_name;
629 gboolean is_interface;
630 MonoMethod *result = NULL;
632 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
635 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
637 qname = g_strconcat (class_name, ".", name, NULL);
638 if (ic->name_space && ic->name_space [0])
639 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
643 class_name = qname = fqname = NULL;
646 g_assert (from_class);
647 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
651 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
654 g_assert (from_class->interface_offsets_count == in_class->interface_offsets_count);
655 for (i = 0; i < in_class->interface_offsets_count; i++) {
656 MonoClass *in_ic = in_class->interfaces_packed [i];
657 MonoClass *from_ic = from_class->interfaces_packed [i];
658 char *ic_qname, *ic_fqname, *ic_class_name;
660 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
661 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
662 if (in_ic->name_space && in_ic->name_space [0])
663 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
667 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
668 g_free (ic_class_name);
675 in_class = in_class->parent;
676 from_class = from_class->parent;
678 g_assert (!in_class == !from_class);
681 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
690 static MonoMethodSignature*
691 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
693 MonoMethodSignature *res;
700 res = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
701 res->param_count = sig->param_count;
702 res->sentinelpos = -1;
703 res->ret = mono_class_inflate_generic_type (sig->ret, context);
704 is_open = mono_class_is_open_constructed_type (res->ret);
705 for (i = 0; i < sig->param_count; ++i) {
706 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
708 is_open = mono_class_is_open_constructed_type (res->params [i]);
710 res->hasthis = sig->hasthis;
711 res->explicit_this = sig->explicit_this;
712 res->call_convention = sig->call_convention;
713 res->pinvoke = sig->pinvoke;
714 res->generic_param_count = sig->generic_param_count;
715 res->sentinelpos = sig->sentinelpos;
716 res->has_type_parameters = is_open;
717 res->is_inflated = 1;
721 static MonoMethodHeader*
722 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
724 MonoMethodHeader *res;
726 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
727 res->code = header->code;
728 res->code_size = header->code_size;
729 res->max_stack = header->max_stack;
730 res->num_clauses = header->num_clauses;
731 res->init_locals = header->init_locals;
732 res->num_locals = header->num_locals;
733 res->clauses = header->clauses;
734 for (i = 0; i < header->num_locals; ++i)
735 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
736 if (res->num_clauses) {
737 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
738 for (i = 0; i < header->num_clauses; ++i) {
739 MonoExceptionClause *clause = &res->clauses [i];
740 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
742 clause->data.catch_class = mono_class_inflate_generic_class (clause->data.catch_class, context);
749 * token is the method_ref or method_def token used in a call IL instruction.
752 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
754 int table = mono_metadata_token_table (token);
755 int idx = mono_metadata_token_index (token);
757 guint32 cols [MONO_MEMBERREF_SIZE];
758 MonoMethodSignature *sig;
761 /* !table is for wrappers: we should really assign their own token to them */
762 if (!table || table == MONO_TABLE_METHOD)
763 return mono_method_signature (method);
765 if (table == MONO_TABLE_METHODSPEC) {
766 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
767 mono_method_signature (method));
768 g_assert (method->is_inflated);
770 return mono_method_signature (method);
773 if (method->klass->generic_class)
774 return mono_method_signature (method);
777 /* FIXME: This might be incorrect for vararg methods */
778 return mono_method_signature (method);
780 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
781 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
783 sig = find_cached_memberref_sig (image, sig_idx);
785 ptr = mono_metadata_blob_heap (image, sig_idx);
786 mono_metadata_decode_blob_size (ptr, &ptr);
787 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
789 sig = cache_memberref_sig (image, sig_idx, sig);
793 MonoMethodSignature *cached;
795 /* This signature is not owned by a MonoMethod, so need to cache */
796 sig = inflate_generic_signature (image, sig, context);
797 cached = mono_metadata_get_inflated_signature (sig, context);
799 mono_metadata_free_inflated_signature (sig);
801 inflated_signatures_size += mono_metadata_signature_size (cached);
809 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
811 return mono_method_get_signature_full (method, image, token, NULL);
814 /* this is only for the typespec array methods */
816 mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
820 mono_class_setup_methods (klass);
822 for (i = 0; i < klass->method.count; ++i) {
823 MonoMethod *method = klass->methods [i];
824 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
831 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
832 gboolean *used_context)
834 MonoClass *klass = NULL;
835 MonoMethod *method = NULL;
836 MonoTableInfo *tables = image->tables;
838 guint32 nindex, class, sig_idx;
840 MonoMethodSignature *sig;
843 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
844 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
845 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
846 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
847 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
849 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
852 * Whether we actually used the `typespec_context' or not.
853 * This is used to tell our caller whether or not it's safe to insert the returned
854 * method into a cache.
857 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
860 case MONO_MEMBERREF_PARENT_TYPEREF:
861 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
863 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
864 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
865 mono_loader_set_error_type_load (name, image->assembly_name);
870 case MONO_MEMBERREF_PARENT_TYPESPEC:
872 * Parse the TYPESPEC in the parent's context.
874 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
876 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
877 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
878 mono_loader_set_error_type_load (name, image->assembly_name);
883 case MONO_MEMBERREF_PARENT_TYPEDEF:
884 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
886 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
887 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
888 mono_loader_set_error_type_load (name, image->assembly_name);
893 case MONO_MEMBERREF_PARENT_METHODDEF:
894 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
898 /* This message leaks */
899 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
900 mono_loader_set_error_method_load ("", message);
906 mono_class_init (klass);
908 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
910 ptr = mono_metadata_blob_heap (image, sig_idx);
911 mono_metadata_decode_blob_size (ptr, &ptr);
913 sig = find_cached_memberref_sig (image, sig_idx);
915 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
919 sig = cache_memberref_sig (image, sig_idx, sig);
923 case MONO_MEMBERREF_PARENT_TYPEREF:
924 case MONO_MEMBERREF_PARENT_TYPEDEF:
925 method = find_method (klass, NULL, mname, sig, klass);
928 case MONO_MEMBERREF_PARENT_TYPESPEC: {
932 type = &klass->byval_arg;
934 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
935 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
936 method = find_method (in_class, NULL, mname, sig, klass);
940 /* we're an array and we created these methods already in klass in mono_class_init () */
941 result = mono_method_search_in_array_class (klass, mname, sig);
945 g_assert_not_reached ();
949 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
950 g_assert_not_reached ();
954 char *msig = mono_signature_get_desc (sig, FALSE);
955 char * class_name = mono_type_get_name (&klass->byval_arg);
956 GString *s = g_string_new (mname);
957 if (sig->generic_param_count)
958 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
959 g_string_append_printf (s, "(%s)", msig);
961 msig = g_string_free (s, FALSE);
964 "Missing method %s::%s in assembly %s, referenced in assembly %s",
965 class_name, msig, klass->image->name, image->name);
966 mono_loader_set_error_method_load (class_name, mname);
975 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
979 MonoTableInfo *tables = image->tables;
980 MonoGenericContext new_context;
981 MonoGenericInst *inst;
983 guint32 cols [MONO_METHODSPEC_SIZE];
984 guint32 token, nindex, param_count;
986 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
987 token = cols [MONO_METHODSPEC_METHOD];
988 nindex = token >> MONO_METHODDEFORREF_BITS;
990 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
992 mono_metadata_decode_value (ptr, &ptr);
994 param_count = mono_metadata_decode_value (ptr, &ptr);
995 g_assert (param_count);
997 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
998 if (context && inst->is_open)
999 inst = mono_metadata_inflate_generic_inst (inst, context);
1001 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
1002 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
1004 method = method_from_memberref (image, nindex, context, NULL);
1009 klass = method->klass;
1011 if (klass->generic_class) {
1012 g_assert (method->is_inflated);
1013 method = ((MonoMethodInflated *) method)->declaring;
1016 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
1017 new_context.method_inst = inst;
1019 return mono_class_inflate_generic_method_full (method, klass, &new_context);
1022 struct _MonoDllMap {
1030 static MonoDllMap *global_dll_map;
1033 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
1041 mono_loader_lock ();
1044 * we use the first entry we find that matches, since entries from
1045 * the config file are prepended to the list and we document that the
1046 * later entries win.
1048 for (; dll_map; dll_map = dll_map->next) {
1049 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
1050 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
1052 } else if (strcmp (dll_map->dll, dll)) {
1055 if (!found && dll_map->target) {
1056 *rdll = dll_map->target;
1058 /* we don't quit here, because we could find a full
1059 * entry that matches also function and that has priority.
1062 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
1063 *rfunc = dll_map->target_func;
1068 mono_loader_unlock ();
1073 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
1076 if (assembly && assembly->dll_map) {
1077 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
1081 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
1085 * mono_dllmap_insert:
1087 * LOCKING: Acquires the loader lock.
1089 * NOTE: This can be called before the runtime is initialized, for example from
1090 * mono_config_parse ().
1093 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc)
1097 mono_loader_init ();
1099 mono_loader_lock ();
1102 entry = g_malloc0 (sizeof (MonoDllMap));
1103 entry->dll = dll? g_strdup (dll): NULL;
1104 entry->target = tdll? g_strdup (tdll): NULL;
1105 entry->func = func? g_strdup (func): NULL;
1106 entry->target_func = tfunc? g_strdup (tfunc): NULL;
1107 entry->next = global_dll_map;
1108 global_dll_map = entry;
1110 entry = mono_image_alloc0 (assembly, sizeof (MonoDllMap));
1111 entry->dll = dll? mono_image_strdup (assembly, dll): NULL;
1112 entry->target = tdll? mono_image_strdup (assembly, tdll): NULL;
1113 entry->func = func? mono_image_strdup (assembly, func): NULL;
1114 entry->target_func = tfunc? mono_image_strdup (assembly, tfunc): NULL;
1115 entry->next = assembly->dll_map;
1116 assembly->dll_map = entry;
1119 mono_loader_unlock ();
1122 static GHashTable *global_module_map;
1125 cached_module_load (const char *name, int flags, char **err)
1128 mono_loader_lock ();
1129 if (!global_module_map)
1130 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1131 res = g_hash_table_lookup (global_module_map, name);
1134 mono_loader_unlock ();
1137 res = mono_dl_open (name, flags, err);
1139 g_hash_table_insert (global_module_map, g_strdup (name), res);
1140 mono_loader_unlock ();
1145 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1147 MonoImage *image = method->klass->image;
1148 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1149 MonoTableInfo *tables = image->tables;
1150 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1151 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1152 guint32 im_cols [MONO_IMPLMAP_SIZE];
1153 guint32 scope_token;
1154 const char *import = NULL;
1155 const char *orig_scope;
1156 const char *new_scope;
1158 char *full_name, *file_name;
1160 MonoDl *module = NULL;
1162 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1165 return piinfo->addr;
1167 if (method->klass->image->dynamic) {
1168 MonoReflectionMethodAux *method_aux =
1169 g_hash_table_lookup (
1170 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1174 import = method_aux->dllentry;
1175 orig_scope = method_aux->dll;
1178 if (!piinfo->implmap_idx)
1181 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1183 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1184 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1185 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1186 orig_scope = mono_metadata_string_heap (image, scope_token);
1189 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1191 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1192 "DllImport attempting to load: '%s'.", new_scope);
1199 /* we allow a special name to dlopen from the running process namespace */
1200 if (strcmp (new_scope, "__Internal") == 0)
1201 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1204 * Try loading the module using a variety of names
1206 for (i = 0; i < 4; ++i) {
1209 /* Try the original name */
1210 file_name = g_strdup (new_scope);
1213 /* Try trimming the .dll extension */
1214 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1215 file_name = g_strdup (new_scope);
1216 file_name [strlen (new_scope) - 4] = '\0';
1222 if (strstr (new_scope, "lib") != new_scope) {
1223 file_name = g_strdup_printf ("lib%s", new_scope);
1229 #ifndef PLATFORM_WIN32
1230 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1231 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1232 !g_ascii_strcasecmp ("user32", new_scope) ||
1233 !g_ascii_strcasecmp ("kernel", new_scope)) {
1234 file_name = g_strdup ("libMonoSupportW.so");
1238 #ifndef PLATFORM_WIN32
1245 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1246 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1247 "DllImport loading location: '%s'.", full_name);
1248 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1250 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1251 "DllImport error loading library: '%s'.",
1263 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1264 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1265 "DllImport loading library: '%s'.", full_name);
1266 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1268 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1269 "DllImport error loading library '%s'.",
1280 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1281 "DllImport loading: '%s'.", file_name);
1282 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1284 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1285 "DllImport error loading library '%s'.",
1297 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1298 "DllImport unable to load library '%s'.",
1303 *exc_class = "DllNotFoundException";
1304 *exc_arg = new_scope;
1309 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1310 "Searching for '%s'.", import);
1312 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1313 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1315 char *mangled_name = NULL, *mangled_name2 = NULL;
1318 int mangle_param_count;
1319 #ifdef PLATFORM_WIN32
1324 * Search using a variety of mangled names
1326 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1327 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1328 gboolean need_param_count = FALSE;
1329 #ifdef PLATFORM_WIN32
1330 if (mangle_stdcall > 0)
1331 need_param_count = TRUE;
1333 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1338 mangled_name = (char*)import;
1339 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1340 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1341 /* Try the mangled name first */
1342 if (mangle_charset == 0)
1343 mangled_name = g_strconcat (import, "W", NULL);
1345 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1346 #ifdef PLATFORM_WIN32
1347 if (mangle_charset == 0)
1348 mangled_name = g_strconcat (import, "W", NULL);
1350 /* Try the mangled name last */
1351 if (mangle_charset == 1)
1352 mangled_name = g_strconcat (import, "A", NULL);
1355 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1357 /* Try the mangled name last */
1358 if (mangle_charset == 1)
1359 mangled_name = g_strconcat (import, "A", NULL);
1363 #ifdef PLATFORM_WIN32
1364 if (mangle_param_count == 0)
1365 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1367 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1368 param_count = mangle_param_count;
1370 /* Try the stdcall mangled name */
1372 * gcc under windows creates mangled names without the underscore, but MS.NET
1373 * doesn't support it, so we doesn't support it either.
1375 if (mangle_stdcall == 1)
1376 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1378 mangled_name2 = mangled_name;
1380 mangled_name2 = mangled_name;
1383 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1384 "Probing '%s'.", mangled_name2);
1386 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1389 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1390 "Found as '%s'.", mangled_name2);
1392 if (mangled_name != mangled_name2)
1393 g_free (mangled_name2);
1394 if (mangled_name != import)
1395 g_free (mangled_name);
1401 if (!piinfo->addr) {
1404 *exc_class = "EntryPointNotFoundException";
1409 return piinfo->addr;
1413 * LOCKING: assumes the loader lock to be taken.
1416 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1417 MonoGenericContext *context, gboolean *used_context)
1420 int table = mono_metadata_token_table (token);
1421 int idx = mono_metadata_token_index (token);
1422 MonoTableInfo *tables = image->tables;
1423 MonoGenericContainer *generic_container = NULL, *container = NULL;
1424 const char *sig = NULL;
1426 guint32 cols [MONO_TYPEDEF_SIZE];
1428 if (image->dynamic) {
1429 MonoClass *handle_class;
1431 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1432 // This checks the memberref type as well
1433 if (result && handle_class != mono_defaults.methodhandle_class) {
1434 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1440 if (table != MONO_TABLE_METHOD) {
1441 if (table == MONO_TABLE_METHODSPEC) {
1442 if (used_context) *used_context = TRUE;
1443 return method_from_methodspec (image, context, idx);
1445 if (table != MONO_TABLE_MEMBERREF)
1446 g_print("got wrong token: 0x%08x\n", token);
1447 g_assert (table == MONO_TABLE_MEMBERREF);
1448 return method_from_memberref (image, idx, context, used_context);
1451 if (used_context) *used_context = FALSE;
1453 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1454 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1458 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1460 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1461 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1462 result = (MonoMethod *)mono_image_alloc0_lock (image, sizeof (MonoMethodPInvoke));
1464 result = (MonoMethod *)mono_image_alloc0_lock (image, sizeof (MonoMethodNormal));
1466 mono_stats.method_count ++;
1469 guint32 type = mono_metadata_typedef_from_method (image, token);
1470 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1476 result->klass = klass;
1477 result->flags = cols [2];
1478 result->iflags = cols [1];
1479 result->token = token;
1480 result->name = mono_metadata_string_heap (image, cols [3]);
1482 container = klass->generic_container;
1483 generic_container = mono_metadata_load_generic_params (image, token, container);
1484 if (generic_container) {
1485 result->is_generic = TRUE;
1486 generic_container->owner.method = result;
1488 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1490 container = generic_container;
1494 if (!sig) /* already taken from the methodref */
1495 sig = mono_metadata_blob_heap (image, cols [4]);
1496 size = mono_metadata_decode_blob_size (sig, &sig);
1498 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1499 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1500 result->string_ctor = 1;
1501 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1502 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1504 #ifdef PLATFORM_WIN32
1505 /* IJW is P/Invoke with a predefined function pointer. */
1506 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1507 piinfo->addr = mono_image_rva_map (image, cols [0]);
1508 g_assert (piinfo->addr);
1511 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1512 /* Native methods can have no map. */
1513 if (piinfo->implmap_idx)
1514 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1517 if (generic_container)
1518 mono_method_set_generic_container (result, generic_container);
1524 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1526 return mono_get_method_full (image, token, klass, NULL);
1530 get_method_token (gpointer value)
1532 MonoMethod *m = (MonoMethod*)value;
1534 return GUINT_TO_POINTER (m->token);
1538 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1539 MonoGenericContext *context)
1542 gboolean used_context = FALSE;
1544 /* We do everything inside the lock to prevent creation races */
1546 mono_loader_lock ();
1548 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1549 if (!image->method_cache)
1550 image->method_cache = mono_value_hash_table_new (NULL, NULL, get_method_token);
1551 result = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
1553 if (!image->methodref_cache)
1554 image->methodref_cache = g_hash_table_new (NULL, NULL);
1555 result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1558 mono_loader_unlock ();
1562 mono_loader_unlock ();
1563 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1567 mono_loader_lock ();
1568 if (!used_context && !result->is_inflated) {
1569 MonoMethod *result2;
1570 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1571 result2 = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
1573 result2 = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1576 mono_loader_unlock ();
1580 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1581 mono_value_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1583 g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
1586 mono_loader_unlock ();
1592 * mono_get_method_constrained:
1594 * This is used when JITing the `constrained.' opcode.
1596 * This returns two values: the contrained method, which has been inflated
1597 * as the function return value; And the original CIL-stream method as
1598 * declared in cil_method. The later is used for verification.
1601 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1602 MonoGenericContext *context, MonoMethod **cil_method)
1604 MonoMethod *method, *result;
1605 MonoClass *ic = NULL;
1606 MonoGenericContext *method_context = NULL;
1607 MonoMethodSignature *sig, *original_sig;
1609 mono_loader_lock ();
1611 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1613 mono_loader_unlock ();
1617 mono_class_init (constrained_class);
1618 method = *cil_method;
1619 original_sig = sig = mono_method_signature (method);
1621 if (method->is_inflated && sig->generic_param_count) {
1622 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1623 sig = mono_method_signature (imethod->declaring);
1624 method_context = mono_method_get_context (method);
1628 * We must inflate the signature with the class instantiation to work on
1629 * cases where a class inherit from a generic type and the override replaces
1630 * and type argument which a concrete type. See #325283.
1632 if (method_context->class_inst) {
1633 MonoGenericContext ctx;
1634 ctx.method_inst = NULL;
1635 ctx.class_inst = method_context->class_inst;
1637 sig = inflate_generic_signature (method->klass->image, sig, &ctx);
1641 if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
1644 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1645 if (sig != original_sig)
1646 mono_metadata_free_inflated_signature (sig);
1649 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1650 method->klass->name, method->name, image->name, token);
1651 mono_loader_unlock ();
1656 result = mono_class_inflate_generic_method (result, method_context);
1658 mono_loader_unlock ();
1663 mono_free_method (MonoMethod *method)
1665 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1666 mono_profiler_method_free (method);
1668 /* FIXME: This hack will go away when the profiler will support freeing methods */
1669 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1672 if (method->signature) {
1674 * FIXME: This causes crashes because the types inside signatures and
1675 * locals are shared.
1677 /* mono_metadata_free_method_signature (method->signature); */
1678 /* g_free (method->signature); */
1681 if (method->dynamic) {
1682 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1685 mono_marshal_free_dynamic_wrappers (method);
1687 mono_loader_lock ();
1688 mono_property_hash_remove_object (method->klass->image->property_hash, method);
1689 mono_loader_unlock ();
1691 g_free ((char*)method->name);
1692 if (mw->method.header) {
1693 g_free ((char*)mw->method.header->code);
1694 for (i = 0; i < mw->method.header->num_locals; ++i)
1695 g_free (mw->method.header->locals [i]);
1696 g_free (mw->method.header->clauses);
1697 g_free (mw->method.header);
1699 g_free (mw->method_data);
1700 g_free (method->signature);
1706 mono_method_get_param_names (MonoMethod *method, const char **names)
1710 MonoTableInfo *methodt;
1711 MonoTableInfo *paramt;
1714 if (method->is_inflated)
1715 method = ((MonoMethodInflated *) method)->declaring;
1717 if (!mono_method_signature (method)->param_count)
1719 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1722 klass = method->klass;
1726 mono_class_init (klass);
1728 if (klass->image->dynamic) {
1729 MonoReflectionMethodAux *method_aux =
1730 g_hash_table_lookup (
1731 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1732 if (method_aux && method_aux->param_names) {
1733 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1734 if (method_aux->param_names [i + 1])
1735 names [i] = method_aux->param_names [i + 1];
1740 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1741 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1742 idx = mono_method_get_index (method);
1744 guint32 cols [MONO_PARAM_SIZE];
1747 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1749 if (idx < methodt->rows)
1750 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1752 lastp = paramt->rows + 1;
1753 for (i = param_index; i < lastp; ++i) {
1754 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1755 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1756 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1763 mono_method_get_param_token (MonoMethod *method, int index)
1765 MonoClass *klass = method->klass;
1766 MonoTableInfo *methodt;
1769 mono_class_init (klass);
1771 if (klass->image->dynamic) {
1772 g_assert_not_reached ();
1775 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1776 idx = mono_method_get_index (method);
1778 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1782 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1784 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1791 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1794 MonoClass *klass = method->klass;
1795 MonoTableInfo *methodt;
1796 MonoTableInfo *paramt;
1799 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1802 if (method->klass->image->dynamic) {
1803 MonoReflectionMethodAux *method_aux =
1804 g_hash_table_lookup (
1805 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1806 if (method_aux && method_aux->param_marshall) {
1807 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1808 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1809 if (dyn_specs [i]) {
1810 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1811 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1817 mono_class_init (klass);
1819 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1820 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1821 idx = mono_method_get_index (method);
1823 guint32 cols [MONO_PARAM_SIZE];
1824 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1826 if (idx < methodt->rows)
1827 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1829 lastp = paramt->rows + 1;
1831 for (i = param_index; i < lastp; ++i) {
1832 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1834 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1836 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1838 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1847 mono_method_has_marshal_info (MonoMethod *method)
1850 MonoClass *klass = method->klass;
1851 MonoTableInfo *methodt;
1852 MonoTableInfo *paramt;
1855 if (method->klass->image->dynamic) {
1856 MonoReflectionMethodAux *method_aux =
1857 g_hash_table_lookup (
1858 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1859 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1861 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1868 mono_class_init (klass);
1870 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1871 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1872 idx = mono_method_get_index (method);
1874 guint32 cols [MONO_PARAM_SIZE];
1875 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1877 if (idx + 1 < methodt->rows)
1878 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1880 lastp = paramt->rows + 1;
1882 for (i = param_index; i < lastp; ++i) {
1883 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1885 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1894 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1897 g_assert (method != NULL);
1898 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1900 data = ((MonoMethodWrapper *)method)->method_data;
1901 g_assert (data != NULL);
1902 g_assert (id <= GPOINTER_TO_UINT (*data));
1907 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1908 g_error ("stack walk not installed");
1911 static MonoStackWalkImpl stack_walk = default_stack_walk;
1914 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1916 stack_walk (func, TRUE, user_data);
1920 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1922 stack_walk (func, FALSE, user_data);
1926 mono_install_stack_walk (MonoStackWalkImpl func)
1932 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1934 MonoMethod **dest = data;
1936 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1942 mono_method_get_last_managed (void)
1944 MonoMethod *m = NULL;
1945 stack_walk (last_managed, FALSE, &m);
1950 mono_loader_lock (void)
1952 EnterCriticalSection (&loader_mutex);
1956 mono_loader_unlock (void)
1958 LeaveCriticalSection (&loader_mutex);
1962 * mono_method_signature:
1964 * Return the signature of the method M. On failure, returns NULL.
1966 MonoMethodSignature*
1967 mono_method_signature (MonoMethod *m)
1973 gboolean can_cache_signature;
1974 MonoGenericContainer *container;
1975 MonoMethodSignature *signature = NULL;
1978 /* We need memory barriers below because of the double-checked locking pattern */
1981 return m->signature;
1983 mono_loader_lock ();
1986 mono_loader_unlock ();
1987 return m->signature;
1990 if (m->is_inflated) {
1991 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1992 /* the lock is recursive */
1993 signature = mono_method_signature (imethod->declaring);
1994 signature = inflate_generic_signature (imethod->declaring->klass->image, signature, mono_method_get_context (m));
1996 inflated_signatures_size += mono_metadata_signature_size (signature);
1998 mono_memory_barrier ();
1999 m->signature = signature;
2000 mono_loader_unlock ();
2001 return m->signature;
2004 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
2005 idx = mono_metadata_token_index (m->token);
2006 img = m->klass->image;
2008 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
2010 g_assert (!m->klass->generic_class);
2011 container = mono_method_get_generic_container (m);
2013 container = m->klass->generic_container;
2015 /* Generic signatures depend on the container so they cannot be cached */
2016 /* icall/pinvoke signatures cannot be cached cause we modify them below */
2017 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
2019 /* If the method has parameter attributes, that can modify the signature */
2020 pattrs = mono_metadata_get_param_attrs (img, idx);
2022 can_cache_signature = FALSE;
2026 if (can_cache_signature)
2027 signature = g_hash_table_lookup (img->method_signatures, sig);
2030 const char *sig_body;
2032 size = mono_metadata_decode_blob_size (sig, &sig_body);
2034 signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
2036 mono_loader_unlock ();
2040 if (can_cache_signature)
2041 g_hash_table_insert (img->method_signatures, (gpointer)sig, signature);
2044 /* Verify metadata consistency */
2045 if (signature->generic_param_count) {
2046 if (!container || !container->is_method)
2047 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
2048 if (container->type_argc != signature->generic_param_count)
2049 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
2050 signature->generic_param_count, container->type_argc);
2051 } else if (container && container->is_method && container->type_argc)
2052 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
2054 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
2055 signature->pinvoke = 1;
2056 else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2057 MonoCallConvention conv = 0;
2058 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
2059 signature->pinvoke = 1;
2061 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
2062 case 0: /* no call conv, so using default */
2063 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
2064 conv = MONO_CALL_DEFAULT;
2066 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
2069 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
2070 conv = MONO_CALL_STDCALL;
2072 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
2073 conv = MONO_CALL_THISCALL;
2075 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
2076 conv = MONO_CALL_FASTCALL;
2078 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
2079 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
2081 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
2082 g_assert_not_reached ();
2084 signature->call_convention = conv;
2087 mono_memory_barrier ();
2088 m->signature = signature;
2090 mono_loader_unlock ();
2091 return m->signature;
2095 mono_method_get_name (MonoMethod *method)
2097 return method->name;
2101 mono_method_get_class (MonoMethod *method)
2103 return method->klass;
2107 mono_method_get_token (MonoMethod *method)
2109 return method->token;
2113 mono_method_get_header (MonoMethod *method)
2119 MonoMethodNormal* mn = (MonoMethodNormal*) method;
2120 MonoMethodHeader *header;
2122 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
2126 if (G_LIKELY (mn->header))
2132 if (method->is_inflated) {
2133 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
2134 MonoMethodHeader *header;
2136 header = mono_method_get_header (imethod->declaring);
2138 mono_loader_lock ();
2141 mono_loader_unlock ();
2145 mn->header = inflate_generic_header (header, mono_method_get_context (method));
2146 mono_loader_unlock ();
2151 * Do most of the work outside the loader lock, to avoid assembly loader hook
2154 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
2155 idx = mono_metadata_token_index (method->token);
2156 img = method->klass->image;
2157 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
2158 loc = mono_image_rva_map (img, rva);
2162 header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
2164 mono_loader_lock ();
2167 /* header is allocated from the image mempool, no need to free it */
2168 mono_loader_unlock ();
2172 mono_memory_barrier ();
2174 mn->header = header;
2176 mono_loader_unlock ();
2181 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
2184 *iflags = method->iflags;
2185 return method->flags;
2189 * Find the method index in the metadata methodDef table.
2192 mono_method_get_index (MonoMethod *method) {
2193 MonoClass *klass = method->klass;
2197 return mono_metadata_token_index (method->token);
2199 mono_class_setup_methods (klass);
2200 for (i = 0; i < klass->method.count; ++i) {
2201 if (method == klass->methods [i]) {
2202 if (klass->image->uncompressed_metadata)
2203 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2205 return klass->method.first + i + 1;