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/metadata/lock-tracer.h>
40 #include <mono/utils/mono-logger.h>
41 #include <mono/utils/mono-dl.h>
42 #include <mono/utils/mono-membar.h>
43 #include <mono/utils/mono-counters.h>
45 MonoDefaults mono_defaults;
48 * This lock protects the hash tables inside MonoImage used by the metadata
49 * loading functions in class.c and loader.c.
51 * See domain-internals.h for locking policy in combination with the
54 static CRITICAL_SECTION loader_mutex;
57 static guint32 inflated_signatures_size;
58 static guint32 memberref_sig_cache_size;
61 * This TLS variable contains the last type load error encountered by the loader.
63 guint32 loader_error_thread_id;
68 static gboolean inited;
71 InitializeCriticalSection (&loader_mutex);
73 loader_error_thread_id = TlsAlloc ();
75 mono_counters_register ("Inflated signatures size",
76 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
77 mono_counters_register ("Memberref signature cache size",
78 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &memberref_sig_cache_size);
85 mono_loader_cleanup (void)
87 TlsFree (loader_error_thread_id);
89 /*DeleteCriticalSection (&loader_mutex);*/
93 * Handling of type load errors should be done as follows:
95 * If something could not be loaded, the loader should call one of the
96 * mono_loader_set_error_XXX functions ()
97 * with the appropriate arguments, then return NULL to report the failure. The error
98 * should be propagated until it reaches code which can throw managed exceptions. At that
99 * point, an exception should be thrown based on the information returned by
100 * mono_loader_get_last_error (). Then the error should be cleared by calling
101 * mono_loader_clear_error ().
105 set_loader_error (MonoLoaderError *error)
107 TlsSetValue (loader_error_thread_id, error);
111 * mono_loader_set_error_assembly_load:
113 * Set the loader error for this thread.
116 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
118 MonoLoaderError *error;
120 if (mono_loader_get_last_error ())
123 error = g_new0 (MonoLoaderError, 1);
124 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
125 error->assembly_name = g_strdup (assembly_name);
126 error->ref_only = ref_only;
129 * This is not strictly needed, but some (most) of the loader code still
130 * can't deal with load errors, and this message is more helpful than an
134 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);
136 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
138 set_loader_error (error);
142 * mono_loader_set_error_type_load:
144 * Set the loader error for this thread.
147 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
149 MonoLoaderError *error;
151 if (mono_loader_get_last_error ())
154 error = g_new0 (MonoLoaderError, 1);
155 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
156 error->class_name = g_strdup (class_name);
157 error->assembly_name = g_strdup (assembly_name);
160 * This is not strictly needed, but some (most) of the loader code still
161 * can't deal with load errors, and this message is more helpful than an
164 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
166 set_loader_error (error);
170 * mono_loader_set_error_method_load:
172 * Set the loader error for this thread. MEMBER_NAME should point to a string
176 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
178 MonoLoaderError *error;
180 /* FIXME: Store the signature as well */
181 if (mono_loader_get_last_error ())
184 error = g_new0 (MonoLoaderError, 1);
185 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
186 error->class_name = g_strdup (class_name);
187 error->member_name = member_name;
189 set_loader_error (error);
193 * mono_loader_set_error_field_load:
195 * Set the loader error for this thread. MEMBER_NAME should point to a string
199 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
201 MonoLoaderError *error;
203 /* FIXME: Store the signature as well */
204 if (mono_loader_get_last_error ())
207 error = g_new0 (MonoLoaderError, 1);
208 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
209 error->klass = klass;
210 error->member_name = member_name;
212 set_loader_error (error);
216 * mono_loader_set_error_bad_image:
218 * Set the loader error for this thread.
221 mono_loader_set_error_bad_image (char *msg)
223 MonoLoaderError *error;
225 if (mono_loader_get_last_error ())
228 error = g_new0 (MonoLoaderError, 1);
229 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
232 set_loader_error (error);
237 * mono_loader_get_last_error:
239 * Returns information about the last type load exception encountered by the loader, or
240 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
243 mono_loader_get_last_error (void)
245 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
249 * mono_loader_clear_error:
251 * Disposes any loader error messages on this thread
254 mono_loader_clear_error (void)
256 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
259 g_free (ex->class_name);
260 g_free (ex->assembly_name);
264 TlsSetValue (loader_error_thread_id, NULL);
269 * mono_loader_error_prepare_exception:
270 * @error: The MonoLoaderError to turn into an exception
272 * This turns a MonoLoaderError into an exception that can be thrown
273 * and resets the Mono Loader Error state during this process.
277 mono_loader_error_prepare_exception (MonoLoaderError *error)
279 MonoException *ex = NULL;
281 switch (error->exception_type) {
282 case MONO_EXCEPTION_TYPE_LOAD: {
283 char *cname = g_strdup (error->class_name);
284 char *aname = g_strdup (error->assembly_name);
285 MonoString *class_name;
287 mono_loader_clear_error ();
289 class_name = mono_string_new (mono_domain_get (), cname);
291 ex = mono_get_exception_type_load (class_name, aname);
296 case MONO_EXCEPTION_MISSING_METHOD: {
297 char *cname = g_strdup (error->class_name);
298 char *aname = g_strdup (error->member_name);
300 mono_loader_clear_error ();
301 ex = mono_get_exception_missing_method (cname, aname);
307 case MONO_EXCEPTION_MISSING_FIELD: {
308 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
309 char *cname = g_strdup (error->klass->name);
310 char *cmembername = g_strdup (error->member_name);
313 mono_loader_clear_error ();
314 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
316 ex = mono_get_exception_missing_field (class_name, cmembername);
319 g_free (cmembername);
324 case MONO_EXCEPTION_FILE_NOT_FOUND: {
329 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);
331 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
332 filename = g_strdup (error->assembly_name);
333 /* Has to call this before calling anything which might call mono_class_init () */
334 mono_loader_clear_error ();
335 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
341 case MONO_EXCEPTION_BAD_IMAGE: {
342 char *msg = g_strdup (error->msg);
343 mono_loader_clear_error ();
344 ex = mono_get_exception_bad_image_format (msg);
350 g_assert_not_reached ();
357 * find_cached_memberref_sig:
359 * Return a cached copy of the memberref signature identified by SIG_IDX.
360 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
361 * A cache is needed since the type/signature parsing routines allocate everything
362 * from a mempool, so without a cache, multiple requests for the same signature would
363 * lead to unbounded memory growth. For normal methods/fields this is not a problem
364 * since the resulting methods/fields are cached, but inflated methods/fields cannot
366 * LOCKING: Acquires the loader lock.
369 find_cached_memberref_sig (MonoImage *image, guint32 sig_idx)
374 res = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
375 mono_loader_unlock ();
381 cache_memberref_sig (MonoImage *image, guint32 sig_idx, gpointer sig)
386 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
388 /* Somebody got in before us */
392 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (sig_idx), sig);
393 /* An approximation based on glib 2.18 */
394 memberref_sig_cache_size += sizeof (gpointer) * 4;
397 mono_loader_unlock ();
402 static MonoClassField*
403 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
404 MonoGenericContext *context)
407 MonoClassField *field, *sig_field = NULL;
408 MonoTableInfo *tables = image->tables;
411 guint32 nindex, class;
414 guint32 idx = mono_metadata_token_index (token);
416 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
417 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
418 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
420 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
422 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
423 mono_metadata_decode_blob_size (ptr, &ptr);
424 /* we may want to check the signature here... */
427 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token %08x field name %s token %08x", class, fname, token));
430 /* FIXME: This needs a cache, especially for generic instances, since
431 * mono_metadata_parse_type () allocates everything from a mempool.
433 sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
435 sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
436 sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
440 case MONO_MEMBERREF_PARENT_TYPEDEF:
441 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
443 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
444 g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
445 mono_loader_set_error_type_load (name, image->assembly_name);
449 mono_class_init (klass);
452 sig_field = field = mono_class_get_field_from_name (klass, fname);
454 case MONO_MEMBERREF_PARENT_TYPEREF:
455 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
457 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
458 g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
459 mono_loader_set_error_type_load (name, image->assembly_name);
463 mono_class_init (klass);
466 sig_field = field = mono_class_get_field_from_name (klass, fname);
468 case MONO_MEMBERREF_PARENT_TYPESPEC: {
469 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
470 //FIXME can't klass be null?
471 mono_class_init (klass);
474 field = mono_class_get_field_from_name (klass, fname);
476 sig_field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
480 g_warning ("field load from %x", class);
485 mono_loader_set_error_field_load (klass, fname);
486 else if (sig_field && !mono_metadata_type_equal_full (sig_type, sig_field->type, TRUE)) {
487 mono_loader_set_error_field_load (klass, fname);
495 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
496 MonoGenericContext *context)
500 MonoClassField *field;
502 if (image->dynamic) {
503 MonoClassField *result;
504 MonoClass *handle_class;
507 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
508 // This checks the memberref type as well
509 if (result && handle_class != mono_defaults.fieldhandle_class) {
510 mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
513 *retklass = result->parent;
518 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
519 *retklass = field->parent;
520 mono_loader_unlock ();
523 mono_loader_unlock ();
525 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
526 field = field_from_memberref (image, token, retklass, context);
528 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
531 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
537 field = mono_class_get_field (k, token);
541 if (field && !field->parent->generic_class && !field->parent->generic_container)
542 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
543 mono_loader_unlock ();
548 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
552 if (sig1->hasthis != sig2->hasthis ||
553 sig1->sentinelpos != sig2->sentinelpos)
556 for (i = 0; i < sig1->sentinelpos; i++) {
557 MonoType *p1 = sig1->params[i];
558 MonoType *p2 = sig2->params[i];
560 /*if (p1->attrs != p2->attrs)
563 if (!mono_metadata_type_equal (p1, p2))
567 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
573 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
574 MonoMethodSignature *sig, MonoClass *from_class)
578 /* Search directly in the metadata to avoid calling setup_methods () */
580 /* FIXME: !from_class->generic_class condition causes test failures. */
581 if (klass->type_token && !klass->image->dynamic && !klass->methods && !klass->rank && klass == from_class && !from_class->generic_class) {
582 for (i = 0; i < klass->method.count; ++i) {
583 guint32 cols [MONO_METHOD_SIZE];
587 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
589 m_name = mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]);
591 if (!((fqname && !strcmp (m_name, fqname)) ||
592 (qname && !strcmp (m_name, qname)) ||
593 (name && !strcmp (m_name, name))))
596 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
597 if (method && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, mono_method_signature (method)))
602 mono_class_setup_methods (klass);
603 for (i = 0; i < klass->method.count; ++i) {
604 MonoMethod *m = klass->methods [i];
606 if (!((fqname && !strcmp (m->name, fqname)) ||
607 (qname && !strcmp (m->name, qname)) ||
608 (name && !strcmp (m->name, name))))
611 if (sig->call_convention == MONO_CALL_VARARG) {
612 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
615 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
620 if (i < klass->method.count)
621 return mono_class_get_method_by_index (from_class, i);
626 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
629 char *qname, *fqname, *class_name;
630 gboolean is_interface;
631 MonoMethod *result = NULL;
633 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
636 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
638 qname = g_strconcat (class_name, ".", name, NULL);
639 if (ic->name_space && ic->name_space [0])
640 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
644 class_name = qname = fqname = NULL;
647 g_assert (from_class);
648 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
652 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
655 g_assert (from_class->interface_offsets_count == in_class->interface_offsets_count);
656 for (i = 0; i < in_class->interface_offsets_count; i++) {
657 MonoClass *in_ic = in_class->interfaces_packed [i];
658 MonoClass *from_ic = from_class->interfaces_packed [i];
659 char *ic_qname, *ic_fqname, *ic_class_name;
661 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
662 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
663 if (in_ic->name_space && in_ic->name_space [0])
664 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
668 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
669 g_free (ic_class_name);
676 in_class = in_class->parent;
677 from_class = from_class->parent;
679 g_assert (!in_class == !from_class);
682 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
691 static MonoMethodSignature*
692 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
694 MonoMethodSignature *res;
701 res = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
702 res->param_count = sig->param_count;
703 res->sentinelpos = -1;
704 res->ret = mono_class_inflate_generic_type (sig->ret, context);
705 is_open = mono_class_is_open_constructed_type (res->ret);
706 for (i = 0; i < sig->param_count; ++i) {
707 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
709 is_open = mono_class_is_open_constructed_type (res->params [i]);
711 res->hasthis = sig->hasthis;
712 res->explicit_this = sig->explicit_this;
713 res->call_convention = sig->call_convention;
714 res->pinvoke = sig->pinvoke;
715 res->generic_param_count = sig->generic_param_count;
716 res->sentinelpos = sig->sentinelpos;
717 res->has_type_parameters = is_open;
718 res->is_inflated = 1;
722 static MonoMethodHeader*
723 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
725 MonoMethodHeader *res;
727 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
728 res->code = header->code;
729 res->code_size = header->code_size;
730 res->max_stack = header->max_stack;
731 res->num_clauses = header->num_clauses;
732 res->init_locals = header->init_locals;
733 res->num_locals = header->num_locals;
734 res->clauses = header->clauses;
735 for (i = 0; i < header->num_locals; ++i)
736 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
737 if (res->num_clauses) {
738 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
739 for (i = 0; i < header->num_clauses; ++i) {
740 MonoExceptionClause *clause = &res->clauses [i];
741 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
743 clause->data.catch_class = mono_class_inflate_generic_class (clause->data.catch_class, context);
750 * token is the method_ref or method_def token used in a call IL instruction.
753 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
755 int table = mono_metadata_token_table (token);
756 int idx = mono_metadata_token_index (token);
758 guint32 cols [MONO_MEMBERREF_SIZE];
759 MonoMethodSignature *sig;
762 /* !table is for wrappers: we should really assign their own token to them */
763 if (!table || table == MONO_TABLE_METHOD)
764 return mono_method_signature (method);
766 if (table == MONO_TABLE_METHODSPEC) {
767 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
768 mono_method_signature (method));
769 g_assert (method->is_inflated);
771 return mono_method_signature (method);
774 if (method->klass->generic_class)
775 return mono_method_signature (method);
778 /* FIXME: This might be incorrect for vararg methods */
779 return mono_method_signature (method);
781 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
782 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
784 sig = find_cached_memberref_sig (image, sig_idx);
786 ptr = mono_metadata_blob_heap (image, sig_idx);
787 mono_metadata_decode_blob_size (ptr, &ptr);
788 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
790 sig = cache_memberref_sig (image, sig_idx, sig);
794 MonoMethodSignature *cached;
796 /* This signature is not owned by a MonoMethod, so need to cache */
797 sig = inflate_generic_signature (image, sig, context);
798 cached = mono_metadata_get_inflated_signature (sig, context);
800 mono_metadata_free_inflated_signature (sig);
802 inflated_signatures_size += mono_metadata_signature_size (cached);
810 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
812 return mono_method_get_signature_full (method, image, token, NULL);
815 /* this is only for the typespec array methods */
817 mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
821 mono_class_setup_methods (klass);
823 for (i = 0; i < klass->method.count; ++i) {
824 MonoMethod *method = klass->methods [i];
825 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
832 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
833 gboolean *used_context)
835 MonoClass *klass = NULL;
836 MonoMethod *method = NULL;
837 MonoTableInfo *tables = image->tables;
839 guint32 nindex, class, sig_idx;
841 MonoMethodSignature *sig;
844 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
845 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
846 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
847 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
848 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
850 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
853 * Whether we actually used the `typespec_context' or not.
854 * This is used to tell our caller whether or not it's safe to insert the returned
855 * method into a cache.
858 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
861 case MONO_MEMBERREF_PARENT_TYPEREF:
862 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
864 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
865 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
866 mono_loader_set_error_type_load (name, image->assembly_name);
871 case MONO_MEMBERREF_PARENT_TYPESPEC:
873 * Parse the TYPESPEC in the parent's context.
875 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
877 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
878 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
879 mono_loader_set_error_type_load (name, image->assembly_name);
884 case MONO_MEMBERREF_PARENT_TYPEDEF:
885 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
887 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
888 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
889 mono_loader_set_error_type_load (name, image->assembly_name);
894 case MONO_MEMBERREF_PARENT_METHODDEF:
895 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
899 /* This message leaks */
900 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
901 mono_loader_set_error_method_load ("", message);
907 mono_class_init (klass);
909 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
911 ptr = mono_metadata_blob_heap (image, sig_idx);
912 mono_metadata_decode_blob_size (ptr, &ptr);
914 sig = find_cached_memberref_sig (image, sig_idx);
916 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
920 sig = cache_memberref_sig (image, sig_idx, sig);
924 case MONO_MEMBERREF_PARENT_TYPEREF:
925 case MONO_MEMBERREF_PARENT_TYPEDEF:
926 method = find_method (klass, NULL, mname, sig, klass);
929 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 method = mono_method_search_in_array_class (klass, mname, sig);
945 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
946 g_assert_not_reached ();
950 char *msig = mono_signature_get_desc (sig, FALSE);
951 char * class_name = mono_type_get_name (&klass->byval_arg);
952 GString *s = g_string_new (mname);
953 if (sig->generic_param_count)
954 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
955 g_string_append_printf (s, "(%s)", msig);
957 msig = g_string_free (s, FALSE);
960 "Missing method %s::%s in assembly %s, referenced in assembly %s",
961 class_name, msig, klass->image->name, image->name);
962 mono_loader_set_error_method_load (class_name, mname);
971 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
975 MonoTableInfo *tables = image->tables;
976 MonoGenericContext new_context;
977 MonoGenericInst *inst;
979 guint32 cols [MONO_METHODSPEC_SIZE];
980 guint32 token, nindex, param_count;
982 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
983 token = cols [MONO_METHODSPEC_METHOD];
984 nindex = token >> MONO_METHODDEFORREF_BITS;
986 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
988 mono_metadata_decode_value (ptr, &ptr);
990 param_count = mono_metadata_decode_value (ptr, &ptr);
991 g_assert (param_count);
993 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
994 if (context && inst->is_open)
995 inst = mono_metadata_inflate_generic_inst (inst, context);
997 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
998 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
1000 method = method_from_memberref (image, nindex, context, NULL);
1005 klass = method->klass;
1007 if (klass->generic_class) {
1008 g_assert (method->is_inflated);
1009 method = ((MonoMethodInflated *) method)->declaring;
1012 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
1013 new_context.method_inst = inst;
1015 return mono_class_inflate_generic_method_full (method, klass, &new_context);
1018 struct _MonoDllMap {
1026 static MonoDllMap *global_dll_map;
1029 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
1037 mono_loader_lock ();
1040 * we use the first entry we find that matches, since entries from
1041 * the config file are prepended to the list and we document that the
1042 * later entries win.
1044 for (; dll_map; dll_map = dll_map->next) {
1045 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
1046 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
1048 } else if (strcmp (dll_map->dll, dll)) {
1051 if (!found && dll_map->target) {
1052 *rdll = dll_map->target;
1054 /* we don't quit here, because we could find a full
1055 * entry that matches also function and that has priority.
1058 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
1059 *rfunc = dll_map->target_func;
1064 mono_loader_unlock ();
1069 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
1072 if (assembly && assembly->dll_map) {
1073 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
1077 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
1081 * mono_dllmap_insert:
1083 * LOCKING: Acquires the loader lock.
1085 * NOTE: This can be called before the runtime is initialized, for example from
1086 * mono_config_parse ().
1089 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc)
1093 mono_loader_init ();
1095 mono_loader_lock ();
1098 entry = g_malloc0 (sizeof (MonoDllMap));
1099 entry->dll = dll? g_strdup (dll): NULL;
1100 entry->target = tdll? g_strdup (tdll): NULL;
1101 entry->func = func? g_strdup (func): NULL;
1102 entry->target_func = tfunc? g_strdup (tfunc): NULL;
1103 entry->next = global_dll_map;
1104 global_dll_map = entry;
1106 entry = mono_image_alloc0 (assembly, sizeof (MonoDllMap));
1107 entry->dll = dll? mono_image_strdup (assembly, dll): NULL;
1108 entry->target = tdll? mono_image_strdup (assembly, tdll): NULL;
1109 entry->func = func? mono_image_strdup (assembly, func): NULL;
1110 entry->target_func = tfunc? mono_image_strdup (assembly, tfunc): NULL;
1111 entry->next = assembly->dll_map;
1112 assembly->dll_map = entry;
1115 mono_loader_unlock ();
1118 static GHashTable *global_module_map;
1121 cached_module_load (const char *name, int flags, char **err)
1124 mono_loader_lock ();
1125 if (!global_module_map)
1126 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1127 res = g_hash_table_lookup (global_module_map, name);
1130 mono_loader_unlock ();
1133 res = mono_dl_open (name, flags, err);
1135 g_hash_table_insert (global_module_map, g_strdup (name), res);
1136 mono_loader_unlock ();
1141 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1143 MonoImage *image = method->klass->image;
1144 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1145 MonoTableInfo *tables = image->tables;
1146 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1147 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1148 guint32 im_cols [MONO_IMPLMAP_SIZE];
1149 guint32 scope_token;
1150 const char *import = NULL;
1151 const char *orig_scope;
1152 const char *new_scope;
1154 char *full_name, *file_name;
1156 MonoDl *module = NULL;
1158 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1161 return piinfo->addr;
1163 if (method->klass->image->dynamic) {
1164 MonoReflectionMethodAux *method_aux =
1165 g_hash_table_lookup (
1166 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1170 import = method_aux->dllentry;
1171 orig_scope = method_aux->dll;
1174 if (!piinfo->implmap_idx)
1177 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1179 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1180 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1181 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1182 orig_scope = mono_metadata_string_heap (image, scope_token);
1185 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1187 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1188 "DllImport attempting to load: '%s'.", new_scope);
1195 /* we allow a special name to dlopen from the running process namespace */
1196 if (strcmp (new_scope, "__Internal") == 0)
1197 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1200 * Try loading the module using a variety of names
1202 for (i = 0; i < 4; ++i) {
1205 /* Try the original name */
1206 file_name = g_strdup (new_scope);
1209 /* Try trimming the .dll extension */
1210 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1211 file_name = g_strdup (new_scope);
1212 file_name [strlen (new_scope) - 4] = '\0';
1218 if (strstr (new_scope, "lib") != new_scope) {
1219 file_name = g_strdup_printf ("lib%s", new_scope);
1225 #ifndef PLATFORM_WIN32
1226 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1227 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1228 !g_ascii_strcasecmp ("user32", new_scope) ||
1229 !g_ascii_strcasecmp ("kernel", new_scope)) {
1230 file_name = g_strdup ("libMonoSupportW.so");
1234 #ifndef PLATFORM_WIN32
1241 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1242 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1243 "DllImport loading location: '%s'.", full_name);
1244 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1246 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1247 "DllImport error loading library: '%s'.",
1259 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1260 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1261 "DllImport loading library: '%s'.", full_name);
1262 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1264 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1265 "DllImport error loading library '%s'.",
1276 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1277 "DllImport loading: '%s'.", file_name);
1278 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1280 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1281 "DllImport error loading library '%s'.",
1293 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1294 "DllImport unable to load library '%s'.",
1299 *exc_class = "DllNotFoundException";
1300 *exc_arg = new_scope;
1305 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1306 "Searching for '%s'.", import);
1308 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1309 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1311 char *mangled_name = NULL, *mangled_name2 = NULL;
1314 int mangle_param_count;
1315 #ifdef PLATFORM_WIN32
1320 * Search using a variety of mangled names
1322 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1323 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1324 gboolean need_param_count = FALSE;
1325 #ifdef PLATFORM_WIN32
1326 if (mangle_stdcall > 0)
1327 need_param_count = TRUE;
1329 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1334 mangled_name = (char*)import;
1335 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1336 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1337 /* Try the mangled name first */
1338 if (mangle_charset == 0)
1339 mangled_name = g_strconcat (import, "W", NULL);
1341 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1342 #ifdef PLATFORM_WIN32
1343 if (mangle_charset == 0)
1344 mangled_name = g_strconcat (import, "W", NULL);
1346 /* Try the mangled name last */
1347 if (mangle_charset == 1)
1348 mangled_name = g_strconcat (import, "A", NULL);
1351 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1353 /* Try the mangled name last */
1354 if (mangle_charset == 1)
1355 mangled_name = g_strconcat (import, "A", NULL);
1359 #ifdef PLATFORM_WIN32
1360 if (mangle_param_count == 0)
1361 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1363 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1364 param_count = mangle_param_count;
1366 /* Try the stdcall mangled name */
1368 * gcc under windows creates mangled names without the underscore, but MS.NET
1369 * doesn't support it, so we doesn't support it either.
1371 if (mangle_stdcall == 1)
1372 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1374 mangled_name2 = mangled_name;
1376 mangled_name2 = mangled_name;
1379 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1380 "Probing '%s'.", mangled_name2);
1382 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1385 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1386 "Found as '%s'.", mangled_name2);
1388 if (mangled_name != mangled_name2)
1389 g_free (mangled_name2);
1390 if (mangled_name != import)
1391 g_free (mangled_name);
1397 if (!piinfo->addr) {
1400 *exc_class = "EntryPointNotFoundException";
1405 return piinfo->addr;
1409 * LOCKING: assumes the loader lock to be taken.
1412 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1413 MonoGenericContext *context, gboolean *used_context)
1416 int table = mono_metadata_token_table (token);
1417 int idx = mono_metadata_token_index (token);
1418 MonoTableInfo *tables = image->tables;
1419 MonoGenericContainer *generic_container = NULL, *container = NULL;
1420 const char *sig = NULL;
1422 guint32 cols [MONO_TYPEDEF_SIZE];
1424 if (image->dynamic) {
1425 MonoClass *handle_class;
1427 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1428 // This checks the memberref type as well
1429 if (result && handle_class != mono_defaults.methodhandle_class) {
1430 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1436 if (table != MONO_TABLE_METHOD) {
1437 if (table == MONO_TABLE_METHODSPEC) {
1438 if (used_context) *used_context = TRUE;
1439 return method_from_methodspec (image, context, idx);
1441 if (table != MONO_TABLE_MEMBERREF)
1442 g_print("got wrong token: 0x%08x\n", token);
1443 g_assert (table == MONO_TABLE_MEMBERREF);
1444 return method_from_memberref (image, idx, context, used_context);
1447 if (used_context) *used_context = FALSE;
1449 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1450 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1454 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1456 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1457 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1458 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
1460 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodNormal));
1462 mono_stats.method_count ++;
1465 guint32 type = mono_metadata_typedef_from_method (image, token);
1466 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1472 result->klass = klass;
1473 result->flags = cols [2];
1474 result->iflags = cols [1];
1475 result->token = token;
1476 result->name = mono_metadata_string_heap (image, cols [3]);
1478 container = klass->generic_container;
1479 generic_container = mono_metadata_load_generic_params (image, token, container);
1480 if (generic_container) {
1481 result->is_generic = TRUE;
1482 generic_container->owner.method = result;
1484 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1486 container = generic_container;
1490 if (!sig) /* already taken from the methodref */
1491 sig = mono_metadata_blob_heap (image, cols [4]);
1492 size = mono_metadata_decode_blob_size (sig, &sig);
1494 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1495 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1496 result->string_ctor = 1;
1497 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1498 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1500 #ifdef PLATFORM_WIN32
1501 /* IJW is P/Invoke with a predefined function pointer. */
1502 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1503 piinfo->addr = mono_image_rva_map (image, cols [0]);
1504 g_assert (piinfo->addr);
1507 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1508 /* Native methods can have no map. */
1509 if (piinfo->implmap_idx)
1510 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1513 if (generic_container)
1514 mono_method_set_generic_container (result, generic_container);
1520 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1522 return mono_get_method_full (image, token, klass, NULL);
1526 get_method_token (gpointer value)
1528 MonoMethod *m = (MonoMethod*)value;
1530 return GUINT_TO_POINTER (m->token);
1534 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1535 MonoGenericContext *context)
1538 gboolean used_context = FALSE;
1540 /* We do everything inside the lock to prevent creation races */
1542 mono_image_lock (image);
1544 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1545 if (!image->method_cache)
1546 image->method_cache = mono_value_hash_table_new (NULL, NULL, get_method_token);
1547 result = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
1549 if (!image->methodref_cache)
1550 image->methodref_cache = g_hash_table_new (NULL, NULL);
1551 result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1553 mono_image_unlock (image);
1558 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1562 mono_image_lock (image);
1563 if (!used_context && !result->is_inflated) {
1564 MonoMethod *result2;
1565 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1566 result2 = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
1568 result2 = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1571 mono_image_unlock (image);
1575 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1576 mono_value_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1578 g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
1581 mono_image_unlock (image);
1587 * mono_get_method_constrained:
1589 * This is used when JITing the `constrained.' opcode.
1591 * This returns two values: the contrained method, which has been inflated
1592 * as the function return value; And the original CIL-stream method as
1593 * declared in cil_method. The later is used for verification.
1596 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1597 MonoGenericContext *context, MonoMethod **cil_method)
1599 MonoMethod *method, *result;
1600 MonoClass *ic = NULL;
1601 MonoGenericContext *method_context = NULL;
1602 MonoMethodSignature *sig, *original_sig;
1604 mono_loader_lock ();
1606 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1608 mono_loader_unlock ();
1612 mono_class_init (constrained_class);
1613 method = *cil_method;
1614 original_sig = sig = mono_method_signature (method);
1616 if (method->is_inflated && sig->generic_param_count) {
1617 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1618 sig = mono_method_signature (imethod->declaring);
1619 method_context = mono_method_get_context (method);
1623 * We must inflate the signature with the class instantiation to work on
1624 * cases where a class inherit from a generic type and the override replaces
1625 * and type argument which a concrete type. See #325283.
1627 if (method_context->class_inst) {
1628 MonoGenericContext ctx;
1629 ctx.method_inst = NULL;
1630 ctx.class_inst = method_context->class_inst;
1632 sig = inflate_generic_signature (method->klass->image, sig, &ctx);
1636 if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
1639 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1640 if (sig != original_sig)
1641 mono_metadata_free_inflated_signature (sig);
1644 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1645 method->klass->name, method->name, image->name, token);
1646 mono_loader_unlock ();
1651 result = mono_class_inflate_generic_method (result, method_context);
1653 mono_loader_unlock ();
1658 mono_free_method (MonoMethod *method)
1660 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1661 mono_profiler_method_free (method);
1663 /* FIXME: This hack will go away when the profiler will support freeing methods */
1664 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1667 if (method->signature) {
1669 * FIXME: This causes crashes because the types inside signatures and
1670 * locals are shared.
1672 /* mono_metadata_free_method_signature (method->signature); */
1673 /* g_free (method->signature); */
1676 if (method->dynamic) {
1677 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1680 mono_marshal_free_dynamic_wrappers (method);
1682 mono_image_property_remove (method->klass->image, method);
1684 g_free ((char*)method->name);
1685 if (mw->method.header) {
1686 g_free ((char*)mw->method.header->code);
1687 for (i = 0; i < mw->method.header->num_locals; ++i)
1688 g_free (mw->method.header->locals [i]);
1689 g_free (mw->method.header->clauses);
1690 g_free (mw->method.header);
1692 g_free (mw->method_data);
1693 g_free (method->signature);
1699 mono_method_get_param_names (MonoMethod *method, const char **names)
1703 MonoTableInfo *methodt;
1704 MonoTableInfo *paramt;
1707 if (method->is_inflated)
1708 method = ((MonoMethodInflated *) method)->declaring;
1710 if (!mono_method_signature (method)->param_count)
1712 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1715 klass = method->klass;
1719 mono_class_init (klass);
1721 if (klass->image->dynamic) {
1722 MonoReflectionMethodAux *method_aux =
1723 g_hash_table_lookup (
1724 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1725 if (method_aux && method_aux->param_names) {
1726 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1727 if (method_aux->param_names [i + 1])
1728 names [i] = method_aux->param_names [i + 1];
1733 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1734 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1735 idx = mono_method_get_index (method);
1737 guint32 cols [MONO_PARAM_SIZE];
1740 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1742 if (idx < methodt->rows)
1743 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1745 lastp = paramt->rows + 1;
1746 for (i = param_index; i < lastp; ++i) {
1747 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1748 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1749 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1756 mono_method_get_param_token (MonoMethod *method, int index)
1758 MonoClass *klass = method->klass;
1759 MonoTableInfo *methodt;
1762 mono_class_init (klass);
1764 if (klass->image->dynamic) {
1765 g_assert_not_reached ();
1768 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1769 idx = mono_method_get_index (method);
1771 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1775 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1777 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1784 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1787 MonoClass *klass = method->klass;
1788 MonoTableInfo *methodt;
1789 MonoTableInfo *paramt;
1792 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1795 if (method->klass->image->dynamic) {
1796 MonoReflectionMethodAux *method_aux =
1797 g_hash_table_lookup (
1798 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1799 if (method_aux && method_aux->param_marshall) {
1800 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1801 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1802 if (dyn_specs [i]) {
1803 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1804 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1810 mono_class_init (klass);
1812 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1813 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1814 idx = mono_method_get_index (method);
1816 guint32 cols [MONO_PARAM_SIZE];
1817 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1819 if (idx < methodt->rows)
1820 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1822 lastp = paramt->rows + 1;
1824 for (i = param_index; i < lastp; ++i) {
1825 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1827 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1829 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1831 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1840 mono_method_has_marshal_info (MonoMethod *method)
1843 MonoClass *klass = method->klass;
1844 MonoTableInfo *methodt;
1845 MonoTableInfo *paramt;
1848 if (method->klass->image->dynamic) {
1849 MonoReflectionMethodAux *method_aux =
1850 g_hash_table_lookup (
1851 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1852 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1854 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1861 mono_class_init (klass);
1863 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1864 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1865 idx = mono_method_get_index (method);
1867 guint32 cols [MONO_PARAM_SIZE];
1868 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1870 if (idx + 1 < methodt->rows)
1871 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1873 lastp = paramt->rows + 1;
1875 for (i = param_index; i < lastp; ++i) {
1876 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1878 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1887 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1890 g_assert (method != NULL);
1891 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1893 if (method->is_inflated)
1894 method = ((MonoMethodInflated *) method)->declaring;
1895 data = ((MonoMethodWrapper *)method)->method_data;
1896 g_assert (data != NULL);
1897 g_assert (id <= GPOINTER_TO_UINT (*data));
1902 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1903 g_error ("stack walk not installed");
1906 static MonoStackWalkImpl stack_walk = default_stack_walk;
1909 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1911 stack_walk (func, TRUE, user_data);
1915 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1917 stack_walk (func, FALSE, user_data);
1921 mono_install_stack_walk (MonoStackWalkImpl func)
1927 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1929 MonoMethod **dest = data;
1931 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1937 mono_method_get_last_managed (void)
1939 MonoMethod *m = NULL;
1940 stack_walk (last_managed, FALSE, &m);
1947 * See docs/thread-safety.txt for the locking strategy.
1950 mono_loader_lock (void)
1952 mono_locks_acquire (&loader_mutex, LoaderLock);
1956 mono_loader_unlock (void)
1958 mono_locks_release (&loader_mutex, LoaderLock);
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;