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 static gboolean inited;
62 InitializeCriticalSection (&loader_mutex);
64 loader_error_thread_id = TlsAlloc ();
71 mono_loader_cleanup (void)
73 TlsFree (loader_error_thread_id);
75 /*DeleteCriticalSection (&loader_mutex);*/
79 * Handling of type load errors should be done as follows:
81 * If something could not be loaded, the loader should call one of the
82 * mono_loader_set_error_XXX functions ()
83 * with the appropriate arguments, then return NULL to report the failure. The error
84 * should be propagated until it reaches code which can throw managed exceptions. At that
85 * point, an exception should be thrown based on the information returned by
86 * mono_loader_get_last_error (). Then the error should be cleared by calling
87 * mono_loader_clear_error ().
91 set_loader_error (MonoLoaderError *error)
93 TlsSetValue (loader_error_thread_id, error);
97 * mono_loader_set_error_assembly_load:
99 * Set the loader error for this thread.
102 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
104 MonoLoaderError *error;
106 if (mono_loader_get_last_error ())
109 error = g_new0 (MonoLoaderError, 1);
110 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
111 error->assembly_name = g_strdup (assembly_name);
112 error->ref_only = ref_only;
115 * This is not strictly needed, but some (most) of the loader code still
116 * can't deal with load errors, and this message is more helpful than an
120 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);
122 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
124 set_loader_error (error);
128 * mono_loader_set_error_type_load:
130 * Set the loader error for this thread.
133 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
135 MonoLoaderError *error;
137 if (mono_loader_get_last_error ())
140 error = g_new0 (MonoLoaderError, 1);
141 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
142 error->class_name = g_strdup (class_name);
143 error->assembly_name = g_strdup (assembly_name);
146 * This is not strictly needed, but some (most) of the loader code still
147 * can't deal with load errors, and this message is more helpful than an
150 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
152 set_loader_error (error);
156 * mono_loader_set_error_method_load:
158 * Set the loader error for this thread. MEMBER_NAME should point to a string
162 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
164 MonoLoaderError *error;
166 /* FIXME: Store the signature as well */
167 if (mono_loader_get_last_error ())
170 error = g_new0 (MonoLoaderError, 1);
171 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
172 error->class_name = g_strdup (class_name);
173 error->member_name = member_name;
175 set_loader_error (error);
179 * mono_loader_set_error_field_load:
181 * Set the loader error for this thread. MEMBER_NAME should point to a string
185 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
187 MonoLoaderError *error;
189 /* FIXME: Store the signature as well */
190 if (mono_loader_get_last_error ())
193 error = g_new0 (MonoLoaderError, 1);
194 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
195 error->klass = klass;
196 error->member_name = member_name;
198 set_loader_error (error);
202 * mono_loader_set_error_bad_image:
204 * Set the loader error for this thread.
207 mono_loader_set_error_bad_image (char *msg)
209 MonoLoaderError *error;
211 if (mono_loader_get_last_error ())
214 error = g_new0 (MonoLoaderError, 1);
215 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
218 set_loader_error (error);
223 * mono_loader_get_last_error:
225 * Returns information about the last type load exception encountered by the loader, or
226 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
229 mono_loader_get_last_error (void)
231 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
235 * mono_loader_clear_error:
237 * Disposes any loader error messages on this thread
240 mono_loader_clear_error (void)
242 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
245 g_free (ex->class_name);
246 g_free (ex->assembly_name);
250 TlsSetValue (loader_error_thread_id, NULL);
255 * mono_loader_error_prepare_exception:
256 * @error: The MonoLoaderError to turn into an exception
258 * This turns a MonoLoaderError into an exception that can be thrown
259 * and resets the Mono Loader Error state during this process.
263 mono_loader_error_prepare_exception (MonoLoaderError *error)
265 MonoException *ex = NULL;
267 switch (error->exception_type) {
268 case MONO_EXCEPTION_TYPE_LOAD: {
269 char *cname = g_strdup (error->class_name);
270 char *aname = g_strdup (error->assembly_name);
271 MonoString *class_name;
273 mono_loader_clear_error ();
275 class_name = mono_string_new (mono_domain_get (), cname);
277 ex = mono_get_exception_type_load (class_name, aname);
282 case MONO_EXCEPTION_MISSING_METHOD: {
283 char *cname = g_strdup (error->class_name);
284 char *aname = g_strdup (error->member_name);
286 mono_loader_clear_error ();
287 ex = mono_get_exception_missing_method (cname, aname);
293 case MONO_EXCEPTION_MISSING_FIELD: {
294 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
295 char *cname = g_strdup (error->klass->name);
296 char *cmembername = g_strdup (error->member_name);
299 mono_loader_clear_error ();
300 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
302 ex = mono_get_exception_missing_field (class_name, cmembername);
305 g_free (cmembername);
310 case MONO_EXCEPTION_FILE_NOT_FOUND: {
315 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);
317 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
318 filename = g_strdup (error->assembly_name);
319 /* Has to call this before calling anything which might call mono_class_init () */
320 mono_loader_clear_error ();
321 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
327 case MONO_EXCEPTION_BAD_IMAGE: {
328 char *msg = g_strdup (error->msg);
329 mono_loader_clear_error ();
330 ex = mono_get_exception_bad_image_format (msg);
336 g_assert_not_reached ();
342 static MonoClassField*
343 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
344 MonoGenericContext *context)
347 MonoClassField *field, *sig_field = NULL;
348 MonoTableInfo *tables = image->tables;
351 guint32 nindex, class;
354 guint32 idx = mono_metadata_token_index (token);
356 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
357 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
358 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
360 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
362 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
363 mono_metadata_decode_blob_size (ptr, &ptr);
364 /* we may want to check the signature here... */
367 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token %08x field name %s token %08x", class, fname, token));
370 sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
373 case MONO_MEMBERREF_PARENT_TYPEDEF:
374 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
376 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
377 g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
378 mono_loader_set_error_type_load (name, image->assembly_name);
382 mono_class_init (klass);
385 sig_field = field = mono_class_get_field_from_name (klass, fname);
387 case MONO_MEMBERREF_PARENT_TYPEREF:
388 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
390 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
391 g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
392 mono_loader_set_error_type_load (name, image->assembly_name);
396 mono_class_init (klass);
399 sig_field = field = mono_class_get_field_from_name (klass, fname);
401 case MONO_MEMBERREF_PARENT_TYPESPEC: {
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);
409 sig_field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
413 g_warning ("field load from %x", class);
418 mono_loader_set_error_field_load (klass, fname);
419 else if (sig_field && !mono_metadata_type_equal_full (sig_type, sig_field->type, TRUE)) {
420 mono_loader_set_error_field_load (klass, fname);
428 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
429 MonoGenericContext *context)
433 MonoClassField *field;
435 if (image->dynamic) {
436 MonoClassField *result;
437 MonoClass *handle_class;
440 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
441 // This checks the memberref type as well
442 if (result && handle_class != mono_defaults.fieldhandle_class) {
443 mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
446 *retklass = result->parent;
451 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
452 *retklass = field->parent;
453 mono_loader_unlock ();
456 mono_loader_unlock ();
458 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
459 field = field_from_memberref (image, token, retklass, context);
461 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
464 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
470 field = mono_class_get_field (k, token);
474 if (field && !field->parent->generic_class && !field->parent->generic_container)
475 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
476 mono_loader_unlock ();
481 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
485 if (sig1->hasthis != sig2->hasthis ||
486 sig1->sentinelpos != sig2->sentinelpos)
489 for (i = 0; i < sig1->sentinelpos; i++) {
490 MonoType *p1 = sig1->params[i];
491 MonoType *p2 = sig2->params[i];
493 /*if (p1->attrs != p2->attrs)
496 if (!mono_metadata_type_equal (p1, p2))
500 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
506 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
507 MonoMethodSignature *sig, MonoClass *from_class)
511 /* Search directly in the metadata to avoid calling setup_methods () */
512 if (klass->type_token && !klass->image->dynamic && !klass->methods && !klass->rank) {
513 for (i = 0; i < klass->method.count; ++i) {
514 guint32 cols [MONO_METHOD_SIZE];
518 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
520 m_name = mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]);
522 if (!((fqname && !strcmp (m_name, fqname)) ||
523 (qname && !strcmp (m_name, qname)) ||
524 (name && !strcmp (m_name, name))))
527 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
528 if (method && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, mono_method_signature (method))) {
529 if (from_class->generic_class && from_class->generic_class->container_class == klass)
530 return mono_class_inflate_generic_method_full (method, from_class, mono_class_get_context (from_class));
537 mono_class_setup_methods (klass);
538 for (i = 0; i < klass->method.count; ++i) {
539 MonoMethod *m = klass->methods [i];
541 if (!((fqname && !strcmp (m->name, fqname)) ||
542 (qname && !strcmp (m->name, qname)) ||
543 (name && !strcmp (m->name, name))))
546 if (sig->call_convention == MONO_CALL_VARARG) {
547 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
550 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
555 if (i < klass->method.count)
556 return mono_class_get_method_by_index (from_class, i);
561 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
564 char *qname, *fqname, *class_name;
565 gboolean is_interface;
566 MonoMethod *result = NULL;
568 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
571 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
573 qname = g_strconcat (class_name, ".", name, NULL);
574 if (ic->name_space && ic->name_space [0])
575 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
579 class_name = qname = fqname = NULL;
582 g_assert (from_class);
583 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
587 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
590 g_assert (from_class->interface_count == in_class->interface_count);
591 for (i = 0; i < in_class->interface_count; i++) {
592 MonoClass *in_ic = in_class->interfaces [i];
593 MonoClass *from_ic = from_class->interfaces [i];
594 char *ic_qname, *ic_fqname, *ic_class_name;
596 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
597 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
598 if (in_ic->name_space && in_ic->name_space [0])
599 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
603 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
604 g_free (ic_class_name);
611 in_class = in_class->parent;
612 from_class = from_class->parent;
614 g_assert (!in_class == !from_class);
617 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
626 static MonoMethodSignature*
627 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
629 MonoMethodSignature *res;
636 res = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
637 res->param_count = sig->param_count;
638 res->sentinelpos = -1;
639 res->ret = mono_class_inflate_generic_type (sig->ret, context);
640 is_open = mono_class_is_open_constructed_type (res->ret);
641 for (i = 0; i < sig->param_count; ++i) {
642 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
644 is_open = mono_class_is_open_constructed_type (res->params [i]);
646 res->hasthis = sig->hasthis;
647 res->explicit_this = sig->explicit_this;
648 res->call_convention = sig->call_convention;
649 res->pinvoke = sig->pinvoke;
650 res->generic_param_count = sig->generic_param_count;
651 res->sentinelpos = sig->sentinelpos;
652 res->has_type_parameters = is_open;
653 res->is_inflated = 1;
657 static MonoMethodHeader*
658 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
660 MonoMethodHeader *res;
662 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
663 res->code = header->code;
664 res->code_size = header->code_size;
665 res->max_stack = header->max_stack;
666 res->num_clauses = header->num_clauses;
667 res->init_locals = header->init_locals;
668 res->num_locals = header->num_locals;
669 res->clauses = header->clauses;
670 for (i = 0; i < header->num_locals; ++i)
671 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
672 if (res->num_clauses) {
673 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
674 for (i = 0; i < header->num_clauses; ++i) {
675 MonoExceptionClause *clause = &res->clauses [i];
677 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
679 t = mono_class_inflate_generic_type (&clause->data.catch_class->byval_arg, context);
680 clause->data.catch_class = mono_class_from_mono_type (t);
681 mono_metadata_free_type (t);
688 * token is the method_ref or method_def token used in a call IL instruction.
691 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
693 int table = mono_metadata_token_table (token);
694 int idx = mono_metadata_token_index (token);
695 guint32 cols [MONO_MEMBERREF_SIZE];
696 MonoMethodSignature *sig, *prev_sig;
699 /* !table is for wrappers: we should really assign their own token to them */
700 if (!table || table == MONO_TABLE_METHOD)
701 return mono_method_signature (method);
703 if (table == MONO_TABLE_METHODSPEC) {
704 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
705 mono_method_signature (method));
706 g_assert (method->is_inflated);
708 return mono_method_signature (method);
711 if (method->klass->generic_class)
712 return mono_method_signature (method);
715 /* FIXME: This might be incorrect for vararg methods */
716 return mono_method_signature (method);
719 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
720 mono_loader_unlock ();
722 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
724 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
725 mono_metadata_decode_blob_size (ptr, &ptr);
726 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
729 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
731 /* Somebody got in before us */
735 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
736 mono_loader_unlock ();
740 MonoMethodSignature *cached;
742 /* This signature is not owned by a MonoMethod, so need to cache */
743 sig = inflate_generic_signature (image, sig, context);
744 cached = mono_metadata_get_inflated_signature (sig, context);
746 mono_metadata_free_inflated_signature (sig);
754 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
756 return mono_method_get_signature_full (method, image, token, NULL);
759 /* this is only for the typespec array methods */
761 search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
764 for (i = 0; i < klass->method.count; ++i) {
765 MonoMethod *method = klass->methods [i];
766 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
773 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
774 gboolean *used_context)
776 MonoClass *klass = NULL;
777 MonoMethod *method = NULL;
778 MonoTableInfo *tables = image->tables;
780 guint32 nindex, class;
782 MonoMethodSignature *sig;
785 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
786 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
787 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
788 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
789 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
791 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
794 * Whether we actually used the `typespec_context' or not.
795 * This is used to tell our caller whether or not it's safe to insert the returned
796 * method into a cache.
799 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
802 case MONO_MEMBERREF_PARENT_TYPEREF:
803 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
805 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
806 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
807 mono_loader_set_error_type_load (name, image->assembly_name);
812 case MONO_MEMBERREF_PARENT_TYPESPEC:
814 * Parse the TYPESPEC in the parent's context.
816 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
818 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
819 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
820 mono_loader_set_error_type_load (name, image->assembly_name);
825 case MONO_MEMBERREF_PARENT_TYPEDEF:
826 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
828 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
829 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
830 mono_loader_set_error_type_load (name, image->assembly_name);
835 case MONO_MEMBERREF_PARENT_METHODDEF:
836 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
840 /* This message leaks */
841 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
842 mono_loader_set_error_method_load ("", message);
848 mono_class_init (klass);
850 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
851 mono_metadata_decode_blob_size (ptr, &ptr);
853 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
858 case MONO_MEMBERREF_PARENT_TYPEREF:
859 case MONO_MEMBERREF_PARENT_TYPEDEF:
860 method = find_method (klass, NULL, mname, sig, klass);
863 case MONO_MEMBERREF_PARENT_TYPESPEC: {
867 type = &klass->byval_arg;
869 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
870 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
871 method = find_method (in_class, NULL, mname, sig, klass);
875 /* we're an array and we created these methods already in klass in mono_class_init () */
876 result = search_in_array_class (klass, mname, sig);
880 g_assert_not_reached ();
884 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
885 g_assert_not_reached ();
889 char *msig = mono_signature_get_desc (sig, FALSE);
890 char * class_name = mono_type_get_name (&klass->byval_arg);
891 GString *s = g_string_new (mname);
892 if (sig->generic_param_count)
893 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
894 g_string_append_printf (s, "(%s)", msig);
896 msig = g_string_free (s, FALSE);
899 "Missing method %s::%s in assembly %s, referenced in assembly %s",
900 class_name, msig, klass->image->name, image->name);
901 mono_loader_set_error_method_load (class_name, mname);
905 mono_metadata_free_method_signature (sig);
911 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
915 MonoTableInfo *tables = image->tables;
916 MonoGenericContext new_context;
917 MonoGenericInst *inst;
919 guint32 cols [MONO_METHODSPEC_SIZE];
920 guint32 token, nindex, param_count;
922 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
923 token = cols [MONO_METHODSPEC_METHOD];
924 nindex = token >> MONO_METHODDEFORREF_BITS;
926 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
928 mono_metadata_decode_value (ptr, &ptr);
930 param_count = mono_metadata_decode_value (ptr, &ptr);
931 g_assert (param_count);
933 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
934 if (context && inst->is_open)
935 inst = mono_metadata_inflate_generic_inst (inst, context);
937 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
938 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
940 method = method_from_memberref (image, nindex, context, NULL);
945 klass = method->klass;
947 if (klass->generic_class) {
948 g_assert (method->is_inflated);
949 method = ((MonoMethodInflated *) method)->declaring;
952 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
953 new_context.method_inst = inst;
955 return mono_class_inflate_generic_method_full (method, klass, &new_context);
966 static MonoDllMap *global_dll_map;
969 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
980 * we use the first entry we find that matches, since entries from
981 * the config file are prepended to the list and we document that the
984 for (; dll_map; dll_map = dll_map->next) {
985 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
986 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
988 } else if (strcmp (dll_map->dll, dll)) {
991 if (!found && dll_map->target) {
992 *rdll = dll_map->target;
994 /* we don't quit here, because we could find a full
995 * entry that matches also function and that has priority.
998 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
999 *rfunc = dll_map->target_func;
1004 mono_loader_unlock ();
1009 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
1012 if (assembly && assembly->dll_map) {
1013 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
1017 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
1021 * mono_dllmap_insert:
1023 * LOCKING: Acquires the loader lock.
1025 * NOTE: This can be called before the runtime is initialized, for example from
1026 * mono_config_parse ().
1029 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc)
1033 mono_loader_init ();
1035 mono_loader_lock ();
1038 entry = g_malloc0 (sizeof (MonoDllMap));
1039 entry->dll = dll? g_strdup (dll): NULL;
1040 entry->target = tdll? g_strdup (tdll): NULL;
1041 entry->func = func? g_strdup (func): NULL;
1042 entry->target_func = tfunc? g_strdup (tfunc): NULL;
1043 entry->next = global_dll_map;
1044 global_dll_map = entry;
1046 entry = mono_image_alloc0 (assembly, sizeof (MonoDllMap));
1047 entry->dll = dll? mono_image_strdup (assembly, dll): NULL;
1048 entry->target = tdll? mono_image_strdup (assembly, tdll): NULL;
1049 entry->func = func? mono_image_strdup (assembly, func): NULL;
1050 entry->target_func = tfunc? mono_image_strdup (assembly, tfunc): NULL;
1051 entry->next = assembly->dll_map;
1052 assembly->dll_map = entry;
1055 mono_loader_unlock ();
1058 static GHashTable *global_module_map;
1061 cached_module_load (const char *name, int flags, char **err)
1064 mono_loader_lock ();
1065 if (!global_module_map)
1066 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1067 res = g_hash_table_lookup (global_module_map, name);
1070 mono_loader_unlock ();
1073 res = mono_dl_open (name, flags, err);
1075 g_hash_table_insert (global_module_map, g_strdup (name), res);
1076 mono_loader_unlock ();
1081 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1083 MonoImage *image = method->klass->image;
1084 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1085 MonoTableInfo *tables = image->tables;
1086 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1087 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1088 guint32 im_cols [MONO_IMPLMAP_SIZE];
1089 guint32 scope_token;
1090 const char *import = NULL;
1091 const char *orig_scope;
1092 const char *new_scope;
1094 char *full_name, *file_name;
1096 MonoDl *module = NULL;
1098 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1101 return piinfo->addr;
1103 if (method->klass->image->dynamic) {
1104 MonoReflectionMethodAux *method_aux =
1105 g_hash_table_lookup (
1106 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1110 import = method_aux->dllentry;
1111 orig_scope = method_aux->dll;
1114 if (!piinfo->implmap_idx)
1117 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1119 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1120 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1121 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1122 orig_scope = mono_metadata_string_heap (image, scope_token);
1125 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1127 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1128 "DllImport attempting to load: '%s'.", new_scope);
1135 /* we allow a special name to dlopen from the running process namespace */
1136 if (strcmp (new_scope, "__Internal") == 0)
1137 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1140 * Try loading the module using a variety of names
1142 for (i = 0; i < 4; ++i) {
1145 /* Try the original name */
1146 file_name = g_strdup (new_scope);
1149 /* Try trimming the .dll extension */
1150 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1151 file_name = g_strdup (new_scope);
1152 file_name [strlen (new_scope) - 4] = '\0';
1158 if (strstr (new_scope, "lib") != new_scope) {
1159 file_name = g_strdup_printf ("lib%s", new_scope);
1165 #ifndef PLATFORM_WIN32
1166 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1167 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1168 !g_ascii_strcasecmp ("user32", new_scope) ||
1169 !g_ascii_strcasecmp ("kernel", new_scope)) {
1170 file_name = g_strdup ("libMonoSupportW.so");
1174 #ifndef PLATFORM_WIN32
1181 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1182 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1183 "DllImport loading location: '%s'.", full_name);
1184 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1186 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1187 "DllImport error loading library: '%s'.",
1199 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1200 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1201 "DllImport loading library: '%s'.", full_name);
1202 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1204 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1205 "DllImport error loading library '%s'.",
1216 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1217 "DllImport loading: '%s'.", file_name);
1218 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1220 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1221 "DllImport error loading library '%s'.",
1233 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1234 "DllImport unable to load library '%s'.",
1239 *exc_class = "DllNotFoundException";
1240 *exc_arg = new_scope;
1245 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1246 "Searching for '%s'.", import);
1248 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1249 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1251 char *mangled_name = NULL, *mangled_name2 = NULL;
1254 int mangle_param_count;
1255 #ifdef PLATFORM_WIN32
1260 * Search using a variety of mangled names
1262 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1263 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1264 gboolean need_param_count = FALSE;
1265 #ifdef PLATFORM_WIN32
1266 if (mangle_stdcall > 0)
1267 need_param_count = TRUE;
1269 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1274 mangled_name = (char*)import;
1275 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1276 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1277 /* Try the mangled name first */
1278 if (mangle_charset == 0)
1279 mangled_name = g_strconcat (import, "W", NULL);
1281 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1282 #ifdef PLATFORM_WIN32
1283 if (mangle_charset == 0)
1284 mangled_name = g_strconcat (import, "W", NULL);
1286 /* Try the mangled name last */
1287 if (mangle_charset == 1)
1288 mangled_name = g_strconcat (import, "A", NULL);
1291 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1293 /* Try the mangled name last */
1294 if (mangle_charset == 1)
1295 mangled_name = g_strconcat (import, "A", NULL);
1299 #ifdef PLATFORM_WIN32
1300 if (mangle_param_count == 0)
1301 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1303 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1304 param_count = mangle_param_count;
1306 /* Try the stdcall mangled name */
1308 * gcc under windows creates mangled names without the underscore, but MS.NET
1309 * doesn't support it, so we doesn't support it either.
1311 if (mangle_stdcall == 1)
1312 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1314 mangled_name2 = mangled_name;
1316 mangled_name2 = mangled_name;
1319 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1320 "Probing '%s'.", mangled_name2);
1322 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1325 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1326 "Found as '%s'.", mangled_name2);
1328 if (mangled_name != mangled_name2)
1329 g_free (mangled_name2);
1330 if (mangled_name != import)
1331 g_free (mangled_name);
1337 if (!piinfo->addr) {
1340 *exc_class = "EntryPointNotFoundException";
1345 return piinfo->addr;
1349 * LOCKING: assumes the loader lock to be taken.
1352 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1353 MonoGenericContext *context, gboolean *used_context)
1356 int table = mono_metadata_token_table (token);
1357 int idx = mono_metadata_token_index (token);
1358 MonoTableInfo *tables = image->tables;
1359 MonoGenericContainer *generic_container = NULL, *container = NULL;
1360 const char *sig = NULL;
1362 guint32 cols [MONO_TYPEDEF_SIZE];
1364 if (image->dynamic) {
1365 MonoClass *handle_class;
1367 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1368 // This checks the memberref type as well
1369 if (result && handle_class != mono_defaults.methodhandle_class) {
1370 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1376 if (table != MONO_TABLE_METHOD) {
1377 if (table == MONO_TABLE_METHODSPEC) {
1378 if (used_context) *used_context = TRUE;
1379 return method_from_methodspec (image, context, idx);
1381 if (table != MONO_TABLE_MEMBERREF)
1382 g_print("got wrong token: 0x%08x\n", token);
1383 g_assert (table == MONO_TABLE_MEMBERREF);
1384 return method_from_memberref (image, idx, context, used_context);
1387 if (used_context) *used_context = FALSE;
1389 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1390 mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
1394 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1396 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1397 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1398 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
1400 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodNormal));
1402 mono_stats.method_count ++;
1405 guint32 type = mono_metadata_typedef_from_method (image, token);
1406 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1412 result->klass = klass;
1413 result->flags = cols [2];
1414 result->iflags = cols [1];
1415 result->token = token;
1416 result->name = mono_metadata_string_heap (image, cols [3]);
1418 container = klass->generic_container;
1419 generic_container = mono_metadata_load_generic_params (image, token, container);
1420 if (generic_container) {
1421 result->is_generic = TRUE;
1422 generic_container->owner.method = result;
1424 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1426 for (i = 0; i < generic_container->type_argc; i++)
1427 mono_class_from_generic_parameter (&generic_container->type_params [i], image, TRUE);
1429 container = generic_container;
1433 if (!sig) /* already taken from the methodref */
1434 sig = mono_metadata_blob_heap (image, cols [4]);
1435 size = mono_metadata_decode_blob_size (sig, &sig);
1437 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1438 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1439 result->string_ctor = 1;
1440 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1441 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1443 #ifdef PLATFORM_WIN32
1444 /* IJW is P/Invoke with a predefined function pointer. */
1445 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1446 piinfo->addr = mono_image_rva_map (image, cols [0]);
1447 g_assert (piinfo->addr);
1450 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1451 /* Native methods can have no map. */
1452 if (piinfo->implmap_idx)
1453 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1456 if (generic_container)
1457 mono_method_set_generic_container (result, generic_container);
1463 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1465 return mono_get_method_full (image, token, klass, NULL);
1469 get_method_token (gpointer value)
1471 MonoMethod *m = (MonoMethod*)value;
1473 return GUINT_TO_POINTER (m->token);
1477 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1478 MonoGenericContext *context)
1481 gboolean used_context = FALSE;
1483 /* We do everything inside the lock to prevent creation races */
1485 mono_loader_lock ();
1487 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1488 if (!image->method_cache)
1489 image->method_cache = mono_value_hash_table_new (NULL, NULL, get_method_token);
1490 result = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
1492 if (!image->methodref_cache)
1493 image->methodref_cache = g_hash_table_new (NULL, NULL);
1494 result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1497 mono_loader_unlock ();
1501 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1503 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1506 g_message (G_STRLOC ": %s - %d - %d", mono_method_full_name (result, TRUE),
1507 result->is_inflated, used_context);
1511 * `used_context' specifies whether or not mono_get_method_from_token() actually
1512 * used the `context' to get the method. See bug #80969.
1515 if (!used_context && !(result && result->is_inflated) && result) {
1516 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1517 mono_value_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1519 g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
1523 mono_loader_unlock ();
1529 * mono_get_method_constrained:
1531 * This is used when JITing the `constrained.' opcode.
1533 * This returns two values: the contrained method, which has been inflated
1534 * as the function return value; And the original CIL-stream method as
1535 * declared in cil_method. The later is used for verification.
1538 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1539 MonoGenericContext *context, MonoMethod **cil_method)
1541 MonoMethod *method, *result;
1542 MonoClass *ic = NULL;
1543 MonoGenericContext *method_context = NULL;
1544 MonoMethodSignature *sig, *original_sig;
1546 mono_loader_lock ();
1548 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1550 mono_loader_unlock ();
1554 mono_class_init (constrained_class);
1555 method = *cil_method;
1556 original_sig = sig = mono_method_signature (method);
1558 if (method->is_inflated && sig->generic_param_count) {
1559 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1560 sig = mono_method_signature (imethod->declaring);
1561 method_context = mono_method_get_context (method);
1565 * We must inflate the signature with the class instantiation to work on
1566 * cases where a class inherit from a generic type and the override replaces
1567 * and type argument which a concrete type. See #325283.
1569 if (method_context->class_inst) {
1570 MonoGenericContext ctx;
1571 ctx.method_inst = NULL;
1572 ctx.class_inst = method_context->class_inst;
1574 sig = inflate_generic_signature (method->klass->image, sig, &ctx);
1578 if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
1581 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1582 if (sig != original_sig)
1583 mono_metadata_free_inflated_signature (sig);
1586 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1587 method->klass->name, method->name, image->name, token);
1588 mono_loader_unlock ();
1593 result = mono_class_inflate_generic_method (result, method_context);
1595 mono_loader_unlock ();
1600 mono_free_method (MonoMethod *method)
1602 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1603 mono_profiler_method_free (method);
1605 /* FIXME: This hack will go away when the profiler will support freeing methods */
1606 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1609 if (method->signature) {
1611 * FIXME: This causes crashes because the types inside signatures and
1612 * locals are shared.
1614 /* mono_metadata_free_method_signature (method->signature); */
1615 /* g_free (method->signature); */
1618 if (method->dynamic) {
1619 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1622 mono_loader_lock ();
1623 mono_property_hash_remove_object (method->klass->image->property_hash, method);
1624 mono_loader_unlock ();
1626 g_free ((char*)method->name);
1627 if (mw->method.header) {
1628 g_free ((char*)mw->method.header->code);
1629 for (i = 0; i < mw->method.header->num_locals; ++i)
1630 g_free (mw->method.header->locals [i]);
1631 g_free (mw->method.header->clauses);
1632 g_free (mw->method.header);
1634 g_free (mw->method_data);
1635 g_free (method->signature);
1641 mono_method_get_param_names (MonoMethod *method, const char **names)
1645 MonoTableInfo *methodt;
1646 MonoTableInfo *paramt;
1649 if (method->is_inflated)
1650 method = ((MonoMethodInflated *) method)->declaring;
1652 if (!mono_method_signature (method)->param_count)
1654 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1657 klass = method->klass;
1661 mono_class_init (klass);
1663 if (klass->image->dynamic) {
1664 MonoReflectionMethodAux *method_aux =
1665 g_hash_table_lookup (
1666 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1667 if (method_aux && method_aux->param_names) {
1668 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1669 if (method_aux->param_names [i + 1])
1670 names [i] = method_aux->param_names [i + 1];
1675 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1676 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1677 idx = mono_method_get_index (method);
1679 guint32 cols [MONO_PARAM_SIZE];
1682 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1684 if (idx < methodt->rows)
1685 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1687 lastp = paramt->rows + 1;
1688 for (i = param_index; i < lastp; ++i) {
1689 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1690 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1691 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1698 mono_method_get_param_token (MonoMethod *method, int index)
1700 MonoClass *klass = method->klass;
1701 MonoTableInfo *methodt;
1704 mono_class_init (klass);
1706 if (klass->image->dynamic) {
1707 g_assert_not_reached ();
1710 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1711 idx = mono_method_get_index (method);
1713 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1717 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1719 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1726 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1729 MonoClass *klass = method->klass;
1730 MonoTableInfo *methodt;
1731 MonoTableInfo *paramt;
1734 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1737 if (method->klass->image->dynamic) {
1738 MonoReflectionMethodAux *method_aux =
1739 g_hash_table_lookup (
1740 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1741 if (method_aux && method_aux->param_marshall) {
1742 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1743 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1744 if (dyn_specs [i]) {
1745 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1746 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1752 mono_class_init (klass);
1754 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1755 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1756 idx = mono_method_get_index (method);
1758 guint32 cols [MONO_PARAM_SIZE];
1759 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1761 if (idx < methodt->rows)
1762 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1764 lastp = paramt->rows + 1;
1766 for (i = param_index; i < lastp; ++i) {
1767 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1769 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1771 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1773 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1782 mono_method_has_marshal_info (MonoMethod *method)
1785 MonoClass *klass = method->klass;
1786 MonoTableInfo *methodt;
1787 MonoTableInfo *paramt;
1790 if (method->klass->image->dynamic) {
1791 MonoReflectionMethodAux *method_aux =
1792 g_hash_table_lookup (
1793 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1794 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1796 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1803 mono_class_init (klass);
1805 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1806 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1807 idx = mono_method_get_index (method);
1809 guint32 cols [MONO_PARAM_SIZE];
1810 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1812 if (idx + 1 < methodt->rows)
1813 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1815 lastp = paramt->rows + 1;
1817 for (i = param_index; i < lastp; ++i) {
1818 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1820 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1829 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1832 g_assert (method != NULL);
1833 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1835 data = ((MonoMethodWrapper *)method)->method_data;
1836 g_assert (data != NULL);
1837 g_assert (id <= GPOINTER_TO_UINT (*data));
1842 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1843 g_error ("stack walk not installed");
1846 static MonoStackWalkImpl stack_walk = default_stack_walk;
1849 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1851 stack_walk (func, TRUE, user_data);
1855 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1857 stack_walk (func, FALSE, user_data);
1861 mono_install_stack_walk (MonoStackWalkImpl func)
1867 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1869 MonoMethod **dest = data;
1871 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1877 mono_method_get_last_managed (void)
1879 MonoMethod *m = NULL;
1880 stack_walk (last_managed, FALSE, &m);
1885 mono_loader_lock (void)
1887 EnterCriticalSection (&loader_mutex);
1891 mono_loader_unlock (void)
1893 LeaveCriticalSection (&loader_mutex);
1897 * mono_method_signature:
1899 * Return the signature of the method M. On failure, returns NULL.
1901 MonoMethodSignature*
1902 mono_method_signature (MonoMethod *m)
1908 gboolean can_cache_signature;
1909 MonoGenericContainer *container;
1910 MonoMethodSignature *signature = NULL;
1913 /* We need memory barriers below because of the double-checked locking pattern */
1916 return m->signature;
1918 mono_loader_lock ();
1921 mono_loader_unlock ();
1922 return m->signature;
1925 if (m->is_inflated) {
1926 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1927 /* the lock is recursive */
1928 signature = mono_method_signature (imethod->declaring);
1929 signature = inflate_generic_signature (imethod->declaring->klass->image, signature, mono_method_get_context (m));
1930 mono_memory_barrier ();
1931 m->signature = signature;
1932 mono_loader_unlock ();
1933 return m->signature;
1936 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1937 idx = mono_metadata_token_index (m->token);
1938 img = m->klass->image;
1940 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1942 g_assert (!m->klass->generic_class);
1943 container = mono_method_get_generic_container (m);
1945 container = m->klass->generic_container;
1947 /* Generic signatures depend on the container so they cannot be cached */
1948 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1949 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1951 /* If the method has parameter attributes, that can modify the signature */
1952 pattrs = mono_metadata_get_param_attrs (img, idx);
1954 can_cache_signature = FALSE;
1958 if (can_cache_signature)
1959 signature = g_hash_table_lookup (img->method_signatures, sig);
1962 const char *sig_body;
1964 size = mono_metadata_decode_blob_size (sig, &sig_body);
1966 signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1968 mono_loader_unlock ();
1972 if (can_cache_signature)
1973 g_hash_table_insert (img->method_signatures, (gpointer)sig, signature);
1976 /* Verify metadata consistency */
1977 if (signature->generic_param_count) {
1978 if (!container || !container->is_method)
1979 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1980 if (container->type_argc != signature->generic_param_count)
1981 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1982 signature->generic_param_count, container->type_argc);
1983 } else if (container && container->is_method && container->type_argc)
1984 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1986 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1987 signature->pinvoke = 1;
1988 else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1989 MonoCallConvention conv = 0;
1990 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1991 signature->pinvoke = 1;
1993 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1994 case 0: /* no call conv, so using default */
1995 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1996 conv = MONO_CALL_DEFAULT;
1998 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
2001 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
2002 conv = MONO_CALL_STDCALL;
2004 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
2005 conv = MONO_CALL_THISCALL;
2007 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
2008 conv = MONO_CALL_FASTCALL;
2010 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
2011 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
2013 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
2014 g_assert_not_reached ();
2016 signature->call_convention = conv;
2019 mono_memory_barrier ();
2020 m->signature = signature;
2022 mono_loader_unlock ();
2023 return m->signature;
2027 mono_method_get_name (MonoMethod *method)
2029 return method->name;
2033 mono_method_get_class (MonoMethod *method)
2035 return method->klass;
2039 mono_method_get_token (MonoMethod *method)
2041 return method->token;
2045 mono_method_get_header (MonoMethod *method)
2051 MonoMethodNormal* mn = (MonoMethodNormal*) method;
2053 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))
2057 if (G_LIKELY (mn->header))
2063 mono_loader_lock ();
2066 mono_loader_unlock ();
2070 if (method->is_inflated) {
2071 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
2072 MonoMethodHeader *header;
2073 /* the lock is recursive */
2074 header = mono_method_get_header (imethod->declaring);
2075 mn->header = inflate_generic_header (header, mono_method_get_context (method));
2076 mono_loader_unlock ();
2080 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
2081 idx = mono_metadata_token_index (method->token);
2082 img = method->klass->image;
2083 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
2084 loc = mono_image_rva_map (img, rva);
2088 mn->header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
2090 mono_loader_unlock ();
2095 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
2098 *iflags = method->iflags;
2099 return method->flags;
2103 * Find the method index in the metadata methodDef table.
2106 mono_method_get_index (MonoMethod *method) {
2107 MonoClass *klass = method->klass;
2111 return mono_metadata_token_index (method->token);
2113 mono_class_setup_methods (klass);
2114 for (i = 0; i < klass->method.count; ++i) {
2115 if (method == klass->methods [i]) {
2116 if (klass->image->uncompressed_metadata)
2117 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2119 return klass->method.first + i + 1;