2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
10 * Copyright (C) 2002-2006 Novell, Inc.
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/utils/mono-logger.h>
39 #include <mono/utils/mono-dl.h>
40 #include <mono/utils/mono-membar.h>
42 MonoDefaults mono_defaults;
45 * This lock protects the hash tables inside MonoImage used by the metadata
46 * loading functions in class.c and loader.c.
48 static CRITICAL_SECTION loader_mutex;
52 * This TLS variable contains the last type load error encountered by the loader.
54 guint32 loader_error_thread_id;
59 InitializeCriticalSection (&loader_mutex);
61 loader_error_thread_id = TlsAlloc ();
65 mono_loader_cleanup (void)
67 TlsFree (loader_error_thread_id);
69 /*DeleteCriticalSection (&loader_mutex);*/
73 * Handling of type load errors should be done as follows:
75 * If something could not be loaded, the loader should call one of the
76 * mono_loader_set_error_XXX functions ()
77 * with the appropriate arguments, then return NULL to report the failure. The error
78 * should be propagated until it reaches code which can throw managed exceptions. At that
79 * point, an exception should be thrown based on the information returned by
80 * mono_loader_get_last_error (). Then the error should be cleared by calling
81 * mono_loader_clear_error ().
85 set_loader_error (MonoLoaderError *error)
87 TlsSetValue (loader_error_thread_id, error);
91 * mono_loader_set_error_assembly_load:
93 * Set the loader error for this thread.
96 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
98 MonoLoaderError *error;
100 if (mono_loader_get_last_error ())
103 error = g_new0 (MonoLoaderError, 1);
104 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
105 error->assembly_name = g_strdup (assembly_name);
106 error->ref_only = ref_only;
109 * This is not strictly needed, but some (most) of the loader code still
110 * can't deal with load errors, and this message is more helpful than an
114 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);
116 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
118 set_loader_error (error);
122 * mono_loader_set_error_type_load:
124 * Set the loader error for this thread.
127 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
129 MonoLoaderError *error;
131 if (mono_loader_get_last_error ())
134 error = g_new0 (MonoLoaderError, 1);
135 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
136 error->class_name = g_strdup (class_name);
137 error->assembly_name = g_strdup (assembly_name);
140 * This is not strictly needed, but some (most) of the loader code still
141 * can't deal with load errors, and this message is more helpful than an
144 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
146 set_loader_error (error);
150 * mono_loader_set_error_method_load:
152 * Set the loader error for this thread. MEMBER_NAME should point to a string
156 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
158 MonoLoaderError *error;
160 /* FIXME: Store the signature as well */
161 if (mono_loader_get_last_error ())
164 error = g_new0 (MonoLoaderError, 1);
165 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
166 error->class_name = g_strdup (class_name);
167 error->member_name = member_name;
169 set_loader_error (error);
173 * mono_loader_set_error_field_load:
175 * Set the loader error for this thread. MEMBER_NAME should point to a string
179 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
181 MonoLoaderError *error;
183 /* FIXME: Store the signature as well */
184 if (mono_loader_get_last_error ())
187 error = g_new0 (MonoLoaderError, 1);
188 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
189 error->klass = klass;
190 error->member_name = member_name;
192 set_loader_error (error);
196 * mono_loader_set_error_bad_image:
198 * Set the loader error for this thread.
201 mono_loader_set_error_bad_image (char *msg)
203 MonoLoaderError *error;
205 if (mono_loader_get_last_error ())
208 error = g_new0 (MonoLoaderError, 1);
209 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
212 set_loader_error (error);
217 * mono_loader_get_last_error:
219 * Returns information about the last type load exception encountered by the loader, or
220 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
223 mono_loader_get_last_error (void)
225 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
229 * mono_loader_clear_error:
231 * Disposes any loader error messages on this thread
234 mono_loader_clear_error (void)
236 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
239 g_free (ex->class_name);
240 g_free (ex->assembly_name);
244 TlsSetValue (loader_error_thread_id, NULL);
249 * mono_loader_error_prepare_exception:
250 * @error: The MonoLoaderError to turn into an exception
252 * This turns a MonoLoaderError into an exception that can be thrown
253 * and resets the Mono Loader Error state during this process.
257 mono_loader_error_prepare_exception (MonoLoaderError *error)
259 MonoException *ex = NULL;
261 switch (error->exception_type) {
262 case MONO_EXCEPTION_TYPE_LOAD: {
263 char *cname = g_strdup (error->class_name);
264 char *aname = g_strdup (error->assembly_name);
265 MonoString *class_name;
267 mono_loader_clear_error ();
269 class_name = mono_string_new (mono_domain_get (), cname);
271 ex = mono_get_exception_type_load (class_name, aname);
276 case MONO_EXCEPTION_MISSING_METHOD: {
277 char *cname = g_strdup (error->class_name);
278 char *aname = g_strdup (error->member_name);
280 mono_loader_clear_error ();
281 ex = mono_get_exception_missing_method (cname, aname);
287 case MONO_EXCEPTION_MISSING_FIELD: {
288 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
289 char *cname = g_strdup (error->klass->name);
290 char *cmembername = g_strdup (error->member_name);
293 mono_loader_clear_error ();
294 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
296 ex = mono_get_exception_missing_field (class_name, cmembername);
299 g_free (cmembername);
304 case MONO_EXCEPTION_FILE_NOT_FOUND: {
309 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);
311 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
312 filename = g_strdup (error->assembly_name);
313 /* Has to call this before calling anything which might call mono_class_init () */
314 mono_loader_clear_error ();
315 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
321 case MONO_EXCEPTION_BAD_IMAGE: {
322 char *msg = g_strdup (error->msg);
323 mono_loader_clear_error ();
324 ex = mono_get_exception_bad_image_format (msg);
330 g_assert_not_reached ();
336 static MonoClassField*
337 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
338 MonoGenericContext *context)
341 MonoClassField *field;
342 MonoTableInfo *tables = image->tables;
344 guint32 nindex, class;
347 guint32 idx = mono_metadata_token_index (token);
349 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
350 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
351 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
353 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
355 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
356 mono_metadata_decode_blob_size (ptr, &ptr);
357 /* we may want to check the signature here... */
360 case MONO_MEMBERREF_PARENT_TYPEDEF:
361 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
363 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
364 g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
365 mono_loader_set_error_type_load (name, image->assembly_name);
369 mono_class_init (klass);
372 field = mono_class_get_field_from_name (klass, fname);
374 case MONO_MEMBERREF_PARENT_TYPEREF:
375 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
377 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
378 g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
379 mono_loader_set_error_type_load (name, image->assembly_name);
383 mono_class_init (klass);
386 field = mono_class_get_field_from_name (klass, fname);
388 case MONO_MEMBERREF_PARENT_TYPESPEC: {
389 /*guint32 bcols [MONO_TYPESPEC_SIZE];
393 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
394 bcols, MONO_TYPESPEC_SIZE);
395 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
396 len = mono_metadata_decode_value (ptr, &ptr);
397 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
399 klass = mono_class_from_mono_type (type);
400 mono_class_init (klass);
401 g_print ("type in sig: %s\n", klass->name);*/
402 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
403 //FIXME can't klass be null?
404 mono_class_init (klass);
407 field = mono_class_get_field_from_name (klass, fname);
411 g_warning ("field load from %x", class);
416 mono_loader_set_error_field_load (klass, fname);
422 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
423 MonoGenericContext *context)
427 MonoClassField *field;
429 if (image->dynamic) {
430 MonoClassField *result;
431 MonoClass *handle_class;
434 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
435 // This checks the memberref type as well
436 if (result && handle_class != mono_defaults.fieldhandle_class) {
437 mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
440 *retklass = result->parent;
445 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
446 *retklass = field->parent;
447 mono_loader_unlock ();
450 mono_loader_unlock ();
452 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
453 field = field_from_memberref (image, token, retklass, context);
455 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
458 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
464 field = mono_class_get_field (k, token);
468 if (field && !field->parent->generic_class && !field->parent->generic_container)
469 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
470 mono_loader_unlock ();
475 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
479 if (sig1->hasthis != sig2->hasthis ||
480 sig1->sentinelpos != sig2->sentinelpos)
483 for (i = 0; i < sig1->sentinelpos; i++) {
484 MonoType *p1 = sig1->params[i];
485 MonoType *p2 = sig2->params[i];
487 /*if (p1->attrs != p2->attrs)
490 if (!mono_metadata_type_equal (p1, p2))
494 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
500 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
501 MonoMethodSignature *sig, MonoClass *from_class)
505 mono_class_setup_methods (in_class);
506 for (i = 0; i < in_class->method.count; ++i) {
507 MonoMethod *m = in_class->methods [i];
509 if (!((fqname && !strcmp (m->name, fqname)) ||
510 (qname && !strcmp (m->name, qname)) ||
511 (name && !strcmp (m->name, name))))
514 if (sig->call_convention == MONO_CALL_VARARG) {
515 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
518 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
523 if (i < in_class->method.count)
524 return mono_class_get_method_by_index (from_class, i);
529 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
532 char *qname, *fqname, *class_name;
533 gboolean is_interface;
534 MonoMethod *result = NULL;
536 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
539 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
541 qname = g_strconcat (class_name, ".", name, NULL);
542 if (ic->name_space && ic->name_space [0])
543 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
547 class_name = qname = fqname = NULL;
550 g_assert (from_class);
551 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
555 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
558 g_assert (from_class->interface_count == in_class->interface_count);
559 for (i = 0; i < in_class->interface_count; i++) {
560 MonoClass *in_ic = in_class->interfaces [i];
561 MonoClass *from_ic = from_class->interfaces [i];
562 char *ic_qname, *ic_fqname, *ic_class_name;
564 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
565 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
566 if (in_ic->name_space && in_ic->name_space [0])
567 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
571 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
572 g_free (ic_class_name);
579 in_class = in_class->parent;
580 from_class = from_class->parent;
582 g_assert (!in_class == !from_class);
585 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
594 static MonoMethodSignature*
595 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
597 MonoMethodSignature *res;
604 res = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
605 res->param_count = sig->param_count;
606 res->sentinelpos = -1;
607 res->ret = mono_class_inflate_generic_type (sig->ret, context);
608 is_open = mono_class_is_open_constructed_type (res->ret);
609 for (i = 0; i < sig->param_count; ++i) {
610 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
612 is_open = mono_class_is_open_constructed_type (res->params [i]);
614 res->hasthis = sig->hasthis;
615 res->explicit_this = sig->explicit_this;
616 res->call_convention = sig->call_convention;
617 res->pinvoke = sig->pinvoke;
618 res->generic_param_count = sig->generic_param_count;
619 res->sentinelpos = sig->sentinelpos;
620 res->has_type_parameters = is_open;
621 res->is_inflated = 1;
625 static MonoMethodHeader*
626 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
628 MonoMethodHeader *res;
630 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
631 res->code = header->code;
632 res->code_size = header->code_size;
633 res->max_stack = header->max_stack;
634 res->num_clauses = header->num_clauses;
635 res->init_locals = header->init_locals;
636 res->num_locals = header->num_locals;
637 res->clauses = header->clauses;
638 for (i = 0; i < header->num_locals; ++i)
639 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
640 if (res->num_clauses) {
641 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
642 for (i = 0; i < header->num_clauses; ++i) {
643 MonoExceptionClause *clause = &res->clauses [i];
645 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
647 t = mono_class_inflate_generic_type (&clause->data.catch_class->byval_arg, context);
648 clause->data.catch_class = mono_class_from_mono_type (t);
649 mono_metadata_free_type (t);
656 * token is the method_ref or method_def token used in a call IL instruction.
659 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
661 int table = mono_metadata_token_table (token);
662 int idx = mono_metadata_token_index (token);
663 guint32 cols [MONO_MEMBERREF_SIZE];
664 MonoMethodSignature *sig, *prev_sig;
667 /* !table is for wrappers: we should really assign their own token to them */
668 if (!table || table == MONO_TABLE_METHOD)
669 return mono_method_signature (method);
671 if (table == MONO_TABLE_METHODSPEC) {
672 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
673 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
674 mono_method_signature (method));
675 g_assert (method->is_inflated);
677 return mono_method_signature (method);
680 if (method->klass->generic_class)
681 return mono_method_signature (method);
684 /* FIXME: This might be incorrect for vararg methods */
685 return mono_method_signature (method);
688 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
689 mono_loader_unlock ();
691 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
693 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
694 mono_metadata_decode_blob_size (ptr, &ptr);
695 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
698 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
700 /* Somebody got in before us */
704 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
705 mono_loader_unlock ();
709 MonoMethodSignature *cached;
711 /* This signature is not owned by a MonoMethod, so need to cache */
712 sig = inflate_generic_signature (image, sig, context);
713 cached = mono_metadata_get_inflated_signature (sig, context);
715 mono_metadata_free_inflated_signature (sig);
723 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
725 return mono_method_get_signature_full (method, image, token, NULL);
728 /* this is only for the typespec array methods */
730 search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
733 for (i = 0; i < klass->method.count; ++i) {
734 MonoMethod *method = klass->methods [i];
735 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
742 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
743 gboolean *used_context)
745 MonoClass *klass = NULL;
746 MonoMethod *method = NULL;
747 MonoTableInfo *tables = image->tables;
749 guint32 nindex, class;
751 MonoMethodSignature *sig;
754 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
755 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
756 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
757 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
758 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
760 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
763 * Whether we actually used the `typespec_context' or not.
764 * This is used to tell our caller whether or not it's safe to insert the returned
765 * method into a cache.
768 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
771 case MONO_MEMBERREF_PARENT_TYPEREF:
772 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
774 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
775 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
776 mono_loader_set_error_type_load (name, image->assembly_name);
781 case MONO_MEMBERREF_PARENT_TYPESPEC:
783 * Parse the TYPESPEC in the parent's context.
785 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
787 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
788 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
789 mono_loader_set_error_type_load (name, image->assembly_name);
794 case MONO_MEMBERREF_PARENT_TYPEDEF:
795 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
797 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
798 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
799 mono_loader_set_error_type_load (name, image->assembly_name);
804 case MONO_MEMBERREF_PARENT_METHODDEF:
805 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
809 /* This message leaks */
810 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
811 mono_loader_set_error_method_load ("", message);
817 mono_class_init (klass);
819 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
820 mono_metadata_decode_blob_size (ptr, &ptr);
822 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
827 case MONO_MEMBERREF_PARENT_TYPEREF:
828 case MONO_MEMBERREF_PARENT_TYPEDEF:
829 method = find_method (klass, NULL, mname, sig, klass);
832 case MONO_MEMBERREF_PARENT_TYPESPEC: {
836 type = &klass->byval_arg;
838 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
839 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
840 method = find_method (in_class, NULL, mname, sig, klass);
844 /* we're an array and we created these methods already in klass in mono_class_init () */
845 result = search_in_array_class (klass, mname, sig);
849 g_assert_not_reached ();
853 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
854 g_assert_not_reached ();
858 char *msig = mono_signature_get_desc (sig, FALSE);
859 char * class_name = mono_type_get_name (&klass->byval_arg);
860 GString *s = g_string_new (mname);
861 if (sig->generic_param_count)
862 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
863 g_string_append_printf (s, "(%s)", msig);
865 msig = g_string_free (s, FALSE);
868 "Missing method %s::%s in assembly %s, referenced in assembly %s",
869 class_name, msig, klass->image->name, image->name);
870 mono_loader_set_error_method_load (class_name, mname);
874 mono_metadata_free_method_signature (sig);
880 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
884 MonoTableInfo *tables = image->tables;
885 MonoGenericContext new_context;
886 MonoGenericInst *inst;
888 guint32 cols [MONO_METHODSPEC_SIZE];
889 guint32 token, nindex, param_count;
891 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
892 token = cols [MONO_METHODSPEC_METHOD];
893 nindex = token >> MONO_METHODDEFORREF_BITS;
895 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
897 mono_metadata_decode_value (ptr, &ptr);
899 param_count = mono_metadata_decode_value (ptr, &ptr);
900 g_assert (param_count);
902 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
903 if (context && inst->is_open)
904 inst = mono_metadata_inflate_generic_inst (inst, context);
906 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
907 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
909 method = method_from_memberref (image, nindex, context, NULL);
911 klass = method->klass;
913 if (klass->generic_class) {
914 g_assert (method->is_inflated);
915 method = ((MonoMethodInflated *) method)->declaring;
918 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
919 new_context.method_inst = inst;
921 return mono_class_inflate_generic_method_full (method, klass, &new_context);
932 static MonoDllMap *global_dll_map;
935 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
946 * we use the first entry we find that matches, since entries from
947 * the config file are prepended to the list and we document that the
950 for (; dll_map; dll_map = dll_map->next) {
951 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
952 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
954 } else if (strcmp (dll_map->dll, dll)) {
957 if (!found && dll_map->target) {
958 *rdll = dll_map->target;
960 /* we don't quit here, because we could find a full
961 * entry that matches also function and that has priority.
964 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
965 *rfunc = dll_map->target_func;
970 mono_loader_unlock ();
975 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
978 if (assembly && assembly->dll_map) {
979 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
983 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
987 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
993 entry = g_malloc0 (sizeof (MonoDllMap));
994 entry->dll = dll? g_strdup (dll): NULL;
995 entry->target = tdll? g_strdup (tdll): NULL;
996 entry->func = func? g_strdup (func): NULL;
997 entry->target_func = tfunc? g_strdup (tfunc): NULL;
998 entry->next = global_dll_map;
999 global_dll_map = entry;
1001 MonoMemPool *mpool = assembly->mempool;
1002 entry = mono_mempool_alloc0 (mpool, sizeof (MonoDllMap));
1003 entry->dll = dll? mono_mempool_strdup (mpool, dll): NULL;
1004 entry->target = tdll? mono_mempool_strdup (mpool, tdll): NULL;
1005 entry->func = func? mono_mempool_strdup (mpool, func): NULL;
1006 entry->target_func = tfunc? mono_mempool_strdup (mpool, tfunc): NULL;
1007 entry->next = assembly->dll_map;
1008 assembly->dll_map = entry;
1011 mono_loader_unlock ();
1014 static GHashTable *global_module_map;
1017 cached_module_load (const char *name, int flags, char **err)
1020 mono_loader_lock ();
1021 if (!global_module_map)
1022 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1023 res = g_hash_table_lookup (global_module_map, name);
1026 mono_loader_unlock ();
1029 res = mono_dl_open (name, flags, err);
1031 g_hash_table_insert (global_module_map, g_strdup (name), res);
1032 mono_loader_unlock ();
1037 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1039 MonoImage *image = method->klass->image;
1040 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1041 MonoTableInfo *tables = image->tables;
1042 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1043 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1044 guint32 im_cols [MONO_IMPLMAP_SIZE];
1045 guint32 scope_token;
1046 const char *import = NULL;
1047 const char *orig_scope;
1048 const char *new_scope;
1050 char *full_name, *file_name;
1052 MonoDl *module = NULL;
1054 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1057 return piinfo->addr;
1059 if (method->klass->image->dynamic) {
1060 MonoReflectionMethodAux *method_aux =
1061 g_hash_table_lookup (
1062 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1066 import = method_aux->dllentry;
1067 orig_scope = method_aux->dll;
1070 if (!piinfo->implmap_idx)
1073 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1075 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1076 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1077 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1078 orig_scope = mono_metadata_string_heap (image, scope_token);
1081 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1083 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1084 "DllImport attempting to load: '%s'.", new_scope);
1091 /* we allow a special name to dlopen from the running process namespace */
1092 if (strcmp (new_scope, "__Internal") == 0)
1093 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1096 * Try loading the module using a variety of names
1098 for (i = 0; i < 4; ++i) {
1101 /* Try the original name */
1102 file_name = g_strdup (new_scope);
1105 /* Try trimming the .dll extension */
1106 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1107 file_name = g_strdup (new_scope);
1108 file_name [strlen (new_scope) - 4] = '\0';
1114 if (strstr (new_scope, "lib") != new_scope) {
1115 file_name = g_strdup_printf ("lib%s", new_scope);
1121 #ifndef PLATFORM_WIN32
1122 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1123 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1124 !g_ascii_strcasecmp ("user32", new_scope) ||
1125 !g_ascii_strcasecmp ("kernel", new_scope)) {
1126 file_name = g_strdup ("libMonoSupportW.so");
1130 #ifndef PLATFORM_WIN32
1137 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1138 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1139 "DllImport loading location: '%s'.", full_name);
1140 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1142 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1143 "DllImport error loading library: '%s'.",
1155 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1156 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1157 "DllImport loading library: '%s'.", full_name);
1158 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1160 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1161 "DllImport error loading library '%s'.",
1172 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1173 "DllImport loading: '%s'.", file_name);
1174 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1176 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1177 "DllImport error loading library '%s'.",
1189 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1190 "DllImport unable to load library '%s'.",
1195 *exc_class = "DllNotFoundException";
1196 *exc_arg = new_scope;
1201 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1202 "Searching for '%s'.", import);
1204 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1205 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1207 char *mangled_name = NULL, *mangled_name2 = NULL;
1210 int mangle_param_count;
1211 #ifdef PLATFORM_WIN32
1216 * Search using a variety of mangled names
1218 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1219 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1220 gboolean need_param_count = FALSE;
1221 #ifdef PLATFORM_WIN32
1222 if (mangle_stdcall > 0)
1223 need_param_count = TRUE;
1225 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1230 mangled_name = (char*)import;
1231 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1232 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1233 /* Try the mangled name first */
1234 if (mangle_charset == 0)
1235 mangled_name = g_strconcat (import, "W", NULL);
1237 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1238 #ifdef PLATFORM_WIN32
1239 if (mangle_charset == 0)
1240 mangled_name = g_strconcat (import, "W", NULL);
1242 /* Try the mangled name last */
1243 if (mangle_charset == 1)
1244 mangled_name = g_strconcat (import, "A", NULL);
1247 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1249 /* Try the mangled name last */
1250 if (mangle_charset == 1)
1251 mangled_name = g_strconcat (import, "A", NULL);
1255 #ifdef PLATFORM_WIN32
1256 if (mangle_param_count == 0)
1257 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1259 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1260 param_count = mangle_param_count;
1262 /* Try the stdcall mangled name */
1264 * gcc under windows creates mangled names without the underscore, but MS.NET
1265 * doesn't support it, so we doesn't support it either.
1267 if (mangle_stdcall == 1)
1268 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1270 mangled_name2 = mangled_name;
1272 mangled_name2 = mangled_name;
1275 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1276 "Probing '%s'.", mangled_name2);
1278 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1281 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1282 "Found as '%s'.", mangled_name2);
1284 if (mangled_name != mangled_name2)
1285 g_free (mangled_name2);
1286 if (mangled_name != import)
1287 g_free (mangled_name);
1293 if (!piinfo->addr) {
1296 *exc_class = "EntryPointNotFoundException";
1301 return piinfo->addr;
1305 * LOCKING: assumes the loader lock to be taken.
1308 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1309 MonoGenericContext *context, gboolean *used_context)
1312 int table = mono_metadata_token_table (token);
1313 int idx = mono_metadata_token_index (token);
1314 MonoTableInfo *tables = image->tables;
1315 MonoGenericContainer *generic_container = NULL, *container = NULL;
1316 const char *sig = NULL;
1318 guint32 cols [MONO_TYPEDEF_SIZE];
1320 if (image->dynamic) {
1321 MonoClass *handle_class;
1323 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1324 // This checks the memberref type as well
1325 if (result && handle_class != mono_defaults.methodhandle_class) {
1326 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1332 if (table != MONO_TABLE_METHOD) {
1333 if (table == MONO_TABLE_METHODSPEC) {
1334 if (used_context) *used_context = TRUE;
1335 return method_from_methodspec (image, context, idx);
1337 if (table != MONO_TABLE_MEMBERREF)
1338 g_print("got wrong token: 0x%08x\n", token);
1339 g_assert (table == MONO_TABLE_MEMBERREF);
1340 return method_from_memberref (image, idx, context, used_context);
1343 if (used_context) *used_context = FALSE;
1345 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1346 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1350 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1352 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1353 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1354 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1356 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1358 mono_stats.method_count ++;
1361 guint32 type = mono_metadata_typedef_from_method (image, token);
1362 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1368 result->klass = klass;
1369 result->flags = cols [2];
1370 result->iflags = cols [1];
1371 result->token = token;
1372 result->name = mono_metadata_string_heap (image, cols [3]);
1374 container = klass->generic_container;
1375 generic_container = mono_metadata_load_generic_params (image, token, container);
1376 if (generic_container) {
1377 result->is_generic = TRUE;
1378 generic_container->owner.method = result;
1380 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1382 for (i = 0; i < generic_container->type_argc; i++)
1383 mono_class_from_generic_parameter (&generic_container->type_params [i], image, TRUE);
1385 container = generic_container;
1389 if (!sig) /* already taken from the methodref */
1390 sig = mono_metadata_blob_heap (image, cols [4]);
1391 size = mono_metadata_decode_blob_size (sig, &sig);
1393 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1394 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1395 result->string_ctor = 1;
1396 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1397 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1399 #ifdef PLATFORM_WIN32
1400 /* IJW is P/Invoke with a predefined function pointer. */
1401 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1402 piinfo->addr = mono_image_rva_map (image, cols [0]);
1403 g_assert (piinfo->addr);
1406 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1407 /* Native methods can have no map. */
1408 if (piinfo->implmap_idx)
1409 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1412 if (generic_container)
1413 mono_method_set_generic_container (result, generic_container);
1419 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1421 return mono_get_method_full (image, token, klass, NULL);
1425 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1426 MonoGenericContext *context)
1429 gboolean used_context = FALSE;
1431 /* We do everything inside the lock to prevent creation races */
1433 mono_loader_lock ();
1435 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1436 mono_loader_unlock ();
1440 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1442 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1445 g_message (G_STRLOC ": %s - %d - %d", mono_method_full_name (result, TRUE),
1446 result->is_inflated, used_context);
1450 * `used_context' specifies whether or not mono_get_method_from_token() actually
1451 * used the `context' to get the method. See bug #80969.
1454 if (!used_context && !(result && result->is_inflated))
1455 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1457 mono_loader_unlock ();
1463 * mono_get_method_constrained:
1465 * This is used when JITing the `constrained.' opcode.
1467 * This returns two values: the contrained method, which has been inflated
1468 * as the function return value; And the original CIL-stream method as
1469 * declared in cil_method. The later is used for verification.
1472 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1473 MonoGenericContext *context, MonoMethod **cil_method)
1475 MonoMethod *method, *result;
1476 MonoClass *ic = NULL;
1477 MonoGenericContext *class_context = NULL, *method_context = NULL;
1478 MonoMethodSignature *sig, *original_sig;
1480 mono_loader_lock ();
1482 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1484 mono_loader_unlock ();
1488 mono_class_init (constrained_class);
1489 method = *cil_method;
1490 original_sig = sig = mono_method_signature (method);
1492 if (method->is_inflated && sig->generic_param_count) {
1493 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1494 sig = mono_method_signature (imethod->declaring);
1495 method_context = mono_method_get_context (method);
1499 * We must inflate the signature with the class instantiation to work on
1500 * cases where a class inherit from a generic type and the override replaces
1501 * and type argument which a concrete type. See #325283.
1503 if (method_context->class_inst) {
1504 MonoGenericContext ctx;
1505 ctx.method_inst = NULL;
1506 ctx.class_inst = method_context->class_inst;
1508 sig = inflate_generic_signature (method->klass->image, sig, &ctx);
1512 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1515 if (constrained_class->generic_class)
1516 class_context = mono_class_get_context (constrained_class);
1518 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1519 if (sig != original_sig)
1520 mono_metadata_free_inflated_signature (sig);
1523 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1524 method->klass->name, method->name, image->name, token);
1525 mono_loader_unlock ();
1530 result = mono_class_inflate_generic_method (result, class_context);
1532 result = mono_class_inflate_generic_method (result, method_context);
1534 mono_loader_unlock ();
1539 mono_free_method (MonoMethod *method)
1541 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1542 mono_profiler_method_free (method);
1544 /* FIXME: This hack will go away when the profiler will support freeing methods */
1545 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1548 if (method->signature) {
1550 * FIXME: This causes crashes because the types inside signatures and
1551 * locals are shared.
1553 /* mono_metadata_free_method_signature (method->signature); */
1554 /* g_free (method->signature); */
1557 if (method->dynamic) {
1558 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1561 mono_loader_lock ();
1562 mono_property_hash_remove_object (method->klass->image->property_hash, method);
1563 mono_loader_unlock ();
1565 g_free ((char*)method->name);
1566 if (mw->method.header) {
1567 g_free ((char*)mw->method.header->code);
1568 for (i = 0; i < mw->method.header->num_locals; ++i)
1569 g_free (mw->method.header->locals [i]);
1570 g_free (mw->method.header->clauses);
1571 g_free (mw->method.header);
1573 g_free (mw->method_data);
1574 g_free (method->signature);
1580 mono_method_get_param_names (MonoMethod *method, const char **names)
1584 MonoTableInfo *methodt;
1585 MonoTableInfo *paramt;
1588 if (method->is_inflated)
1589 method = ((MonoMethodInflated *) method)->declaring;
1591 if (!mono_method_signature (method)->param_count)
1593 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1596 klass = method->klass;
1600 mono_class_init (klass);
1602 if (klass->image->dynamic) {
1603 MonoReflectionMethodAux *method_aux =
1604 g_hash_table_lookup (
1605 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1606 if (method_aux && method_aux->param_names) {
1607 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1608 if (method_aux->param_names [i + 1])
1609 names [i] = method_aux->param_names [i + 1];
1614 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1615 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1616 idx = mono_method_get_index (method);
1618 guint32 cols [MONO_PARAM_SIZE];
1621 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1623 if (idx < methodt->rows)
1624 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1626 lastp = paramt->rows + 1;
1627 for (i = param_index; i < lastp; ++i) {
1628 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1629 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1630 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1637 mono_method_get_param_token (MonoMethod *method, int index)
1639 MonoClass *klass = method->klass;
1640 MonoTableInfo *methodt;
1643 mono_class_init (klass);
1645 if (klass->image->dynamic) {
1646 g_assert_not_reached ();
1649 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1650 idx = mono_method_get_index (method);
1652 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1656 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1658 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1665 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1668 MonoClass *klass = method->klass;
1669 MonoTableInfo *methodt;
1670 MonoTableInfo *paramt;
1673 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1676 if (method->klass->image->dynamic) {
1677 MonoReflectionMethodAux *method_aux =
1678 g_hash_table_lookup (
1679 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1680 if (method_aux && method_aux->param_marshall) {
1681 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1682 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1683 if (dyn_specs [i]) {
1684 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1685 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1691 mono_class_init (klass);
1693 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1694 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1695 idx = mono_method_get_index (method);
1697 guint32 cols [MONO_PARAM_SIZE];
1698 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1700 if (idx < methodt->rows)
1701 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1703 lastp = paramt->rows + 1;
1705 for (i = param_index; i < lastp; ++i) {
1706 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1708 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1710 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1712 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1721 mono_method_has_marshal_info (MonoMethod *method)
1724 MonoClass *klass = method->klass;
1725 MonoTableInfo *methodt;
1726 MonoTableInfo *paramt;
1729 if (method->klass->image->dynamic) {
1730 MonoReflectionMethodAux *method_aux =
1731 g_hash_table_lookup (
1732 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1733 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1735 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1742 mono_class_init (klass);
1744 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1745 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1746 idx = mono_method_get_index (method);
1748 guint32 cols [MONO_PARAM_SIZE];
1749 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1751 if (idx + 1 < methodt->rows)
1752 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1754 lastp = paramt->rows + 1;
1756 for (i = param_index; i < lastp; ++i) {
1757 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1759 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1768 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1771 g_assert (method != NULL);
1772 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1774 data = ((MonoMethodWrapper *)method)->method_data;
1775 g_assert (data != NULL);
1776 g_assert (id <= GPOINTER_TO_UINT (*data));
1781 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1782 g_error ("stack walk not installed");
1785 static MonoStackWalkImpl stack_walk = default_stack_walk;
1788 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1790 stack_walk (func, TRUE, user_data);
1794 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1796 stack_walk (func, FALSE, user_data);
1800 mono_install_stack_walk (MonoStackWalkImpl func)
1806 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1808 MonoMethod **dest = data;
1810 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1816 mono_method_get_last_managed (void)
1818 MonoMethod *m = NULL;
1819 stack_walk (last_managed, FALSE, &m);
1824 mono_loader_lock (void)
1826 EnterCriticalSection (&loader_mutex);
1830 mono_loader_unlock (void)
1832 LeaveCriticalSection (&loader_mutex);
1836 * mono_method_signature:
1838 * Return the signature of the method M. On failure, returns NULL.
1840 MonoMethodSignature*
1841 mono_method_signature (MonoMethod *m)
1847 gboolean can_cache_signature;
1848 MonoGenericContainer *container;
1849 MonoMethodSignature *signature = NULL;
1852 /* We need memory barriers below because of the double-checked locking pattern */
1855 return m->signature;
1857 mono_loader_lock ();
1860 mono_loader_unlock ();
1861 return m->signature;
1864 if (m->is_inflated) {
1865 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1866 /* the lock is recursive */
1867 signature = mono_method_signature (imethod->declaring);
1868 signature = inflate_generic_signature (imethod->declaring->klass->image, signature, mono_method_get_context (m));
1869 mono_memory_barrier ();
1870 m->signature = signature;
1871 mono_loader_unlock ();
1872 return m->signature;
1875 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1876 idx = mono_metadata_token_index (m->token);
1877 img = m->klass->image;
1879 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1881 g_assert (!m->klass->generic_class);
1882 container = mono_method_get_generic_container (m);
1884 container = m->klass->generic_container;
1886 /* Generic signatures depend on the container so they cannot be cached */
1887 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1888 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1890 /* If the method has parameter attributes, that can modify the signature */
1891 pattrs = mono_metadata_get_param_attrs (img, idx);
1893 can_cache_signature = FALSE;
1897 if (can_cache_signature)
1898 signature = g_hash_table_lookup (img->method_signatures, sig);
1901 const char *sig_body;
1903 size = mono_metadata_decode_blob_size (sig, &sig_body);
1905 signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1907 mono_loader_unlock ();
1911 if (can_cache_signature)
1912 g_hash_table_insert (img->method_signatures, (gpointer)sig, signature);
1915 /* Verify metadata consistency */
1916 if (signature->generic_param_count) {
1917 if (!container || !container->is_method)
1918 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1919 if (container->type_argc != signature->generic_param_count)
1920 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1921 signature->generic_param_count, container->type_argc);
1922 } else if (container && container->is_method && container->type_argc)
1923 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1925 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1926 signature->pinvoke = 1;
1927 else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1928 MonoCallConvention conv = 0;
1929 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1930 signature->pinvoke = 1;
1932 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1933 case 0: /* no call conv, so using default */
1934 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1935 conv = MONO_CALL_DEFAULT;
1937 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1940 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1941 conv = MONO_CALL_STDCALL;
1943 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1944 conv = MONO_CALL_THISCALL;
1946 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1947 conv = MONO_CALL_FASTCALL;
1949 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1950 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1952 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1953 g_assert_not_reached ();
1955 signature->call_convention = conv;
1958 mono_memory_barrier ();
1959 m->signature = signature;
1961 mono_loader_unlock ();
1962 return m->signature;
1966 mono_method_get_name (MonoMethod *method)
1968 return method->name;
1972 mono_method_get_class (MonoMethod *method)
1974 return method->klass;
1978 mono_method_get_token (MonoMethod *method)
1980 return method->token;
1984 mono_method_get_header (MonoMethod *method)
1990 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1992 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))
1996 if (G_LIKELY (mn->header))
2002 mono_loader_lock ();
2005 mono_loader_unlock ();
2009 if (method->is_inflated) {
2010 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
2011 MonoMethodHeader *header;
2012 /* the lock is recursive */
2013 header = mono_method_get_header (imethod->declaring);
2014 mn->header = inflate_generic_header (header, mono_method_get_context (method));
2015 mono_loader_unlock ();
2019 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
2020 idx = mono_metadata_token_index (method->token);
2021 img = method->klass->image;
2022 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
2023 loc = mono_image_rva_map (img, rva);
2027 mn->header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
2029 mono_loader_unlock ();
2034 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
2037 *iflags = method->iflags;
2038 return method->flags;
2042 * Find the method index in the metadata methodDef table.
2045 mono_method_get_index (MonoMethod *method) {
2046 MonoClass *klass = method->klass;
2050 return mono_metadata_token_index (method->token);
2052 mono_class_setup_methods (klass);
2053 for (i = 0; i < klass->method.count; ++i) {
2054 if (method == klass->methods [i]) {
2055 if (klass->image->uncompressed_metadata)
2056 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2058 return klass->method.first + i + 1;