2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * This file is used by the interpreter and the JIT engine to locate
13 * assemblies. Used to load AssemblyRef and later to resolve various
17 * This should keep track of the assembly versions that we are loading.
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/image.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/metadata-internals.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/class-internals.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/profiler.h>
36 #include <mono/metadata/profiler-private.h>
37 #include <mono/metadata/exception.h>
38 #include <mono/metadata/marshal.h>
39 #include <mono/metadata/lock-tracer.h>
40 #include <mono/metadata/verify-internals.h>
41 #include <mono/utils/mono-logger.h>
42 #include <mono/utils/mono-dl.h>
43 #include <mono/utils/mono-membar.h>
44 #include <mono/utils/mono-counters.h>
45 #include <mono/utils/mono-error-internals.h>
47 MonoDefaults mono_defaults
;
50 * This lock protects the hash tables inside MonoImage used by the metadata
51 * loading functions in class.c and loader.c.
53 * See domain-internals.h for locking policy in combination with the
56 static CRITICAL_SECTION loader_mutex
;
59 static guint32 inflated_signatures_size
;
60 static guint32 memberref_sig_cache_size
;
63 * This TLS variable contains the last type load error encountered by the loader.
65 guint32 loader_error_thread_id
;
68 * This TLS variable holds how many times the current thread has acquired the loader
71 guint32 loader_lock_nest_id
;
76 static gboolean inited
;
79 InitializeCriticalSection (&loader_mutex
);
81 loader_error_thread_id
= TlsAlloc ();
82 loader_lock_nest_id
= TlsAlloc ();
84 mono_counters_register ("Inflated signatures size",
85 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_signatures_size
);
86 mono_counters_register ("Memberref signature cache size",
87 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &memberref_sig_cache_size
);
94 mono_loader_cleanup (void)
96 TlsFree (loader_error_thread_id
);
97 TlsFree (loader_lock_nest_id
);
99 /*DeleteCriticalSection (&loader_mutex);*/
103 * Handling of type load errors should be done as follows:
105 * If something could not be loaded, the loader should call one of the
106 * mono_loader_set_error_XXX functions ()
107 * with the appropriate arguments, then return NULL to report the failure. The error
108 * should be propagated until it reaches code which can throw managed exceptions. At that
109 * point, an exception should be thrown based on the information returned by
110 * mono_loader_get_last_error (). Then the error should be cleared by calling
111 * mono_loader_clear_error ().
115 set_loader_error (MonoLoaderError
*error
)
117 TlsSetValue (loader_error_thread_id
, error
);
121 * mono_loader_set_error_assembly_load:
123 * Set the loader error for this thread.
126 mono_loader_set_error_assembly_load (const char *assembly_name
, gboolean ref_only
)
128 MonoLoaderError
*error
;
130 if (mono_loader_get_last_error ())
133 error
= g_new0 (MonoLoaderError
, 1);
134 error
->exception_type
= MONO_EXCEPTION_FILE_NOT_FOUND
;
135 error
->assembly_name
= g_strdup (assembly_name
);
136 error
->ref_only
= ref_only
;
139 * This is not strictly needed, but some (most) of the loader code still
140 * can't deal with load errors, and this message is more helpful than an
144 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
);
146 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name
);
148 set_loader_error (error
);
152 * mono_loader_set_error_type_load:
154 * Set the loader error for this thread.
157 mono_loader_set_error_type_load (const char *class_name
, const char *assembly_name
)
159 MonoLoaderError
*error
;
161 if (mono_loader_get_last_error ())
164 error
= g_new0 (MonoLoaderError
, 1);
165 error
->exception_type
= MONO_EXCEPTION_TYPE_LOAD
;
166 error
->class_name
= g_strdup (class_name
);
167 error
->assembly_name
= g_strdup (assembly_name
);
170 * This is not strictly needed, but some (most) of the loader code still
171 * can't deal with load errors, and this message is more helpful than an
174 g_warning ("The class %s could not be loaded, used in %s", class_name
, assembly_name
);
176 set_loader_error (error
);
180 * mono_loader_set_error_method_load:
182 * Set the loader error for this thread. MEMBER_NAME should point to a string
186 mono_loader_set_error_method_load (const char *class_name
, const char *member_name
)
188 MonoLoaderError
*error
;
190 /* FIXME: Store the signature as well */
191 if (mono_loader_get_last_error ())
194 error
= g_new0 (MonoLoaderError
, 1);
195 error
->exception_type
= MONO_EXCEPTION_MISSING_METHOD
;
196 error
->class_name
= g_strdup (class_name
);
197 error
->member_name
= member_name
;
199 set_loader_error (error
);
203 * mono_loader_set_error_field_load:
205 * Set the loader error for this thread. MEMBER_NAME should point to a string
209 mono_loader_set_error_field_load (MonoClass
*klass
, const char *member_name
)
211 MonoLoaderError
*error
;
213 /* FIXME: Store the signature as well */
214 if (mono_loader_get_last_error ())
217 error
= g_new0 (MonoLoaderError
, 1);
218 error
->exception_type
= MONO_EXCEPTION_MISSING_FIELD
;
219 error
->klass
= klass
;
220 error
->member_name
= member_name
;
222 set_loader_error (error
);
226 * mono_loader_set_error_bad_image:
228 * Set the loader error for this thread.
231 mono_loader_set_error_bad_image (char *msg
)
233 MonoLoaderError
*error
;
235 if (mono_loader_get_last_error ())
238 error
= g_new0 (MonoLoaderError
, 1);
239 error
->exception_type
= MONO_EXCEPTION_BAD_IMAGE
;
242 set_loader_error (error
);
247 * mono_loader_get_last_error:
249 * Returns information about the last type load exception encountered by the loader, or
250 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
253 mono_loader_get_last_error (void)
255 return (MonoLoaderError
*)TlsGetValue (loader_error_thread_id
);
259 * mono_loader_clear_error:
261 * Disposes any loader error messages on this thread
264 mono_loader_clear_error (void)
266 MonoLoaderError
*ex
= (MonoLoaderError
*)TlsGetValue (loader_error_thread_id
);
269 g_free (ex
->class_name
);
270 g_free (ex
->assembly_name
);
274 TlsSetValue (loader_error_thread_id
, NULL
);
279 * mono_loader_error_prepare_exception:
280 * @error: The MonoLoaderError to turn into an exception
282 * This turns a MonoLoaderError into an exception that can be thrown
283 * and resets the Mono Loader Error state during this process.
287 mono_loader_error_prepare_exception (MonoLoaderError
*error
)
289 MonoException
*ex
= NULL
;
291 switch (error
->exception_type
) {
292 case MONO_EXCEPTION_TYPE_LOAD
: {
293 char *cname
= g_strdup (error
->class_name
);
294 char *aname
= g_strdup (error
->assembly_name
);
295 MonoString
*class_name
;
297 mono_loader_clear_error ();
299 class_name
= mono_string_new (mono_domain_get (), cname
);
301 ex
= mono_get_exception_type_load (class_name
, aname
);
306 case MONO_EXCEPTION_MISSING_METHOD
: {
307 char *cname
= g_strdup (error
->class_name
);
308 char *aname
= g_strdup (error
->member_name
);
310 mono_loader_clear_error ();
311 ex
= mono_get_exception_missing_method (cname
, aname
);
317 case MONO_EXCEPTION_MISSING_FIELD
: {
318 char *cnspace
= g_strdup ((error
->klass
&& *error
->klass
->name_space
) ? error
->klass
->name_space
: "");
319 char *cname
= g_strdup (error
->klass
? error
->klass
->name
: "");
320 char *cmembername
= g_strdup (error
->member_name
);
323 mono_loader_clear_error ();
324 class_name
= g_strdup_printf ("%s%s%s", cnspace
, cnspace
? "." : "", cname
);
326 ex
= mono_get_exception_missing_field (class_name
, cmembername
);
329 g_free (cmembername
);
334 case MONO_EXCEPTION_FILE_NOT_FOUND
: {
339 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
);
341 msg
= g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error
->assembly_name
);
342 filename
= g_strdup (error
->assembly_name
);
343 /* Has to call this before calling anything which might call mono_class_init () */
344 mono_loader_clear_error ();
345 ex
= mono_get_exception_file_not_found2 (msg
, mono_string_new (mono_domain_get (), filename
));
351 case MONO_EXCEPTION_BAD_IMAGE
: {
352 char *msg
= g_strdup (error
->msg
);
353 mono_loader_clear_error ();
354 ex
= mono_get_exception_bad_image_format (msg
);
360 g_assert_not_reached ();
367 * find_cached_memberref_sig:
369 * Return a cached copy of the memberref signature identified by SIG_IDX.
370 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
371 * A cache is needed since the type/signature parsing routines allocate everything
372 * from a mempool, so without a cache, multiple requests for the same signature would
373 * lead to unbounded memory growth. For normal methods/fields this is not a problem
374 * since the resulting methods/fields are cached, but inflated methods/fields cannot
376 * LOCKING: Acquires the loader lock.
379 find_cached_memberref_sig (MonoImage
*image
, guint32 sig_idx
)
384 res
= g_hash_table_lookup (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
));
385 mono_loader_unlock ();
391 cache_memberref_sig (MonoImage
*image
, guint32 sig_idx
, gpointer sig
)
396 prev_sig
= g_hash_table_lookup (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
));
398 /* Somebody got in before us */
402 g_hash_table_insert (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
), sig
);
403 /* An approximation based on glib 2.18 */
404 memberref_sig_cache_size
+= sizeof (gpointer
) * 4;
407 mono_loader_unlock ();
412 static MonoClassField
*
413 field_from_memberref (MonoImage
*image
, guint32 token
, MonoClass
**retklass
,
414 MonoGenericContext
*context
)
417 MonoClassField
*field
;
418 MonoTableInfo
*tables
= image
->tables
;
421 guint32 nindex
, class, class_table
;
424 guint32 idx
= mono_metadata_token_index (token
);
426 mono_metadata_decode_row (&tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, MONO_MEMBERREF_SIZE
);
427 nindex
= cols
[MONO_MEMBERREF_CLASS
] >> MONO_MEMBERREF_PARENT_BITS
;
428 class = cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
430 fname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
432 if (!mono_verifier_verify_memberref_signature (image
, cols
[MONO_MEMBERREF_SIGNATURE
], NULL
)) {
433 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname
, token
, image
->name
));
438 case MONO_MEMBERREF_PARENT_TYPEDEF
:
439 class_table
= MONO_TOKEN_TYPE_DEF
;
440 klass
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| nindex
);
442 case MONO_MEMBERREF_PARENT_TYPEREF
:
443 class_table
= MONO_TOKEN_TYPE_REF
;
444 klass
= mono_class_from_typeref (image
, MONO_TOKEN_TYPE_REF
| nindex
);
446 case MONO_MEMBERREF_PARENT_TYPESPEC
:
447 class_table
= MONO_TOKEN_TYPE_SPEC
;
448 klass
= mono_class_get_full (image
, MONO_TOKEN_TYPE_SPEC
| nindex
, context
);
451 /*FIXME this must set a loader error!*/
452 g_warning ("field load from %x", class);
457 char *name
= mono_class_name_from_token (image
, class_table
| nindex
);
458 g_warning ("Missing field %s in class %s (type token %d)", fname
, name
, class_table
| nindex
);
459 mono_loader_set_error_type_load (name
, image
->assembly_name
);
464 ptr
= mono_metadata_blob_heap (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
465 mono_metadata_decode_blob_size (ptr
, &ptr
);
466 /* we may want to check the signature here... */
469 g_warning ("Bad field signature class token %08x field name %s token %08x", class, fname
, token
);
470 mono_loader_set_error_field_load (klass
, fname
);
473 /* FIXME: This needs a cache, especially for generic instances, since
474 * mono_metadata_parse_type () allocates everything from a mempool.
476 sig_type
= find_cached_memberref_sig (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
478 sig_type
= mono_metadata_parse_type (image
, MONO_PARSE_TYPE
, 0, ptr
, &ptr
);
479 if (sig_type
== NULL
) {
480 mono_loader_set_error_field_load (klass
, fname
);
483 sig_type
= cache_memberref_sig (image
, cols
[MONO_MEMBERREF_SIGNATURE
], sig_type
);
486 mono_class_init (klass
); /*FIXME is this really necessary?*/
489 field
= mono_class_get_field_from_name_full (klass
, fname
, sig_type
);
492 mono_loader_set_error_field_load (klass
, fname
);
498 mono_field_from_token (MonoImage
*image
, guint32 token
, MonoClass
**retklass
,
499 MonoGenericContext
*context
)
503 MonoClassField
*field
;
505 if (image
->dynamic
) {
506 MonoClassField
*result
;
507 MonoClass
*handle_class
;
510 result
= mono_lookup_dynamic_token_class (image
, token
, TRUE
, &handle_class
, context
);
511 // This checks the memberref type as well
512 if (!result
|| handle_class
!= mono_defaults
.fieldhandle_class
) {
513 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field token 0x%08x on image %s.", token
, image
->name
));
516 *retklass
= result
->parent
;
521 if ((field
= g_hash_table_lookup (image
->field_cache
, GUINT_TO_POINTER (token
)))) {
522 *retklass
= field
->parent
;
523 mono_loader_unlock ();
526 mono_loader_unlock ();
528 if (mono_metadata_token_table (token
) == MONO_TABLE_MEMBERREF
)
529 field
= field_from_memberref (image
, token
, retklass
, context
);
531 type
= mono_metadata_typedef_from_field (image
, mono_metadata_token_index (token
));
534 k
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| type
);
540 field
= mono_class_get_field (k
, token
);
544 if (field
&& field
->parent
&& !field
->parent
->generic_class
&& !field
->parent
->generic_container
)
545 g_hash_table_insert (image
->field_cache
, GUINT_TO_POINTER (token
), field
);
546 mono_loader_unlock ();
551 mono_metadata_signature_vararg_match (MonoMethodSignature
*sig1
, MonoMethodSignature
*sig2
)
555 if (sig1
->hasthis
!= sig2
->hasthis
||
556 sig1
->sentinelpos
!= sig2
->sentinelpos
)
559 for (i
= 0; i
< sig1
->sentinelpos
; i
++) {
560 MonoType
*p1
= sig1
->params
[i
];
561 MonoType
*p2
= sig2
->params
[i
];
563 /*if (p1->attrs != p2->attrs)
566 if (!mono_metadata_type_equal (p1
, p2
))
570 if (!mono_metadata_type_equal (sig1
->ret
, sig2
->ret
))
576 find_method_in_class (MonoClass
*klass
, const char *name
, const char *qname
, const char *fqname
,
577 MonoMethodSignature
*sig
, MonoClass
*from_class
)
581 /* Search directly in the metadata to avoid calling setup_methods () */
583 /* FIXME: !from_class->generic_class condition causes test failures. */
584 if (klass
->type_token
&& !klass
->image
->dynamic
&& !klass
->methods
&& !klass
->rank
&& klass
== from_class
&& !from_class
->generic_class
) {
585 for (i
= 0; i
< klass
->method
.count
; ++i
) {
586 guint32 cols
[MONO_METHOD_SIZE
];
589 MonoMethodSignature
*other_sig
;
591 mono_metadata_decode_table_row (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
, cols
, MONO_METHOD_SIZE
);
593 m_name
= mono_metadata_string_heap (klass
->image
, cols
[MONO_METHOD_NAME
]);
595 if (!((fqname
&& !strcmp (m_name
, fqname
)) ||
596 (qname
&& !strcmp (m_name
, qname
)) ||
597 (name
&& !strcmp (m_name
, name
))))
600 method
= mono_get_method (klass
->image
, MONO_TOKEN_METHOD_DEF
| (klass
->method
.first
+ i
+ 1), klass
);
601 other_sig
= mono_method_signature (method
);
602 if (method
&& other_sig
&& (sig
->call_convention
!= MONO_CALL_VARARG
) && mono_metadata_signature_equal (sig
, other_sig
))
607 mono_class_setup_methods (klass
);
609 We can't fail lookup of methods otherwise the runtime will fail with MissingMethodException instead of TypeLoadException.
610 See mono/tests/generic-type-load-exception.2.il
611 FIXME we should better report this error to the caller
615 for (i
= 0; i
< klass
->method
.count
; ++i
) {
616 MonoMethod
*m
= klass
->methods
[i
];
617 MonoMethodSignature
*msig
;
619 if (!((fqname
&& !strcmp (m
->name
, fqname
)) ||
620 (qname
&& !strcmp (m
->name
, qname
)) ||
621 (name
&& !strcmp (m
->name
, name
))))
623 msig
= mono_method_signature (m
);
627 if (sig
->call_convention
== MONO_CALL_VARARG
) {
628 if (mono_metadata_signature_vararg_match (sig
, msig
))
631 if (mono_metadata_signature_equal (sig
, msig
))
636 if (i
< klass
->method
.count
)
637 return mono_class_get_method_by_index (from_class
, i
);
642 find_method (MonoClass
*in_class
, MonoClass
*ic
, const char* name
, MonoMethodSignature
*sig
, MonoClass
*from_class
)
645 char *qname
, *fqname
, *class_name
;
646 gboolean is_interface
;
647 MonoMethod
*result
= NULL
;
649 is_interface
= MONO_CLASS_IS_INTERFACE (in_class
);
652 class_name
= mono_type_get_name_full (&ic
->byval_arg
, MONO_TYPE_NAME_FORMAT_IL
);
654 qname
= g_strconcat (class_name
, ".", name
, NULL
);
655 if (ic
->name_space
&& ic
->name_space
[0])
656 fqname
= g_strconcat (ic
->name_space
, ".", class_name
, ".", name
, NULL
);
660 class_name
= qname
= fqname
= NULL
;
663 g_assert (from_class
);
664 result
= find_method_in_class (in_class
, name
, qname
, fqname
, sig
, from_class
);
668 if (name
[0] == '.' && (!strcmp (name
, ".ctor") || !strcmp (name
, ".cctor")))
671 g_assert (from_class
->interface_offsets_count
== in_class
->interface_offsets_count
);
672 for (i
= 0; i
< in_class
->interface_offsets_count
; i
++) {
673 MonoClass
*in_ic
= in_class
->interfaces_packed
[i
];
674 MonoClass
*from_ic
= from_class
->interfaces_packed
[i
];
675 char *ic_qname
, *ic_fqname
, *ic_class_name
;
677 ic_class_name
= mono_type_get_name_full (&in_ic
->byval_arg
, MONO_TYPE_NAME_FORMAT_IL
);
678 ic_qname
= g_strconcat (ic_class_name
, ".", name
, NULL
);
679 if (in_ic
->name_space
&& in_ic
->name_space
[0])
680 ic_fqname
= g_strconcat (in_ic
->name_space
, ".", ic_class_name
, ".", name
, NULL
);
684 result
= find_method_in_class (in_ic
, ic
? name
: NULL
, ic_qname
, ic_fqname
, sig
, from_ic
);
685 g_free (ic_class_name
);
692 in_class
= in_class
->parent
;
693 from_class
= from_class
->parent
;
695 g_assert (!in_class
== !from_class
);
698 result
= find_method_in_class (mono_defaults
.object_class
, name
, qname
, fqname
, sig
, mono_defaults
.object_class
);
707 static MonoMethodSignature
*
708 inflate_generic_signature (MonoImage
*image
, MonoMethodSignature
*sig
, MonoGenericContext
*context
)
710 MonoMethodSignature
*res
;
717 res
= g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE
+ ((gint32
)sig
->param_count
) * sizeof (MonoType
*));
718 res
->param_count
= sig
->param_count
;
719 res
->sentinelpos
= -1;
720 res
->ret
= mono_class_inflate_generic_type (sig
->ret
, context
);
721 is_open
= mono_class_is_open_constructed_type (res
->ret
);
722 for (i
= 0; i
< sig
->param_count
; ++i
) {
723 res
->params
[i
] = mono_class_inflate_generic_type (sig
->params
[i
], context
);
725 is_open
= mono_class_is_open_constructed_type (res
->params
[i
]);
727 res
->hasthis
= sig
->hasthis
;
728 res
->explicit_this
= sig
->explicit_this
;
729 res
->call_convention
= sig
->call_convention
;
730 res
->pinvoke
= sig
->pinvoke
;
731 res
->generic_param_count
= sig
->generic_param_count
;
732 res
->sentinelpos
= sig
->sentinelpos
;
733 res
->has_type_parameters
= is_open
;
734 res
->is_inflated
= 1;
738 static MonoMethodHeader
*
739 inflate_generic_header (MonoMethodHeader
*header
, MonoGenericContext
*context
)
741 MonoMethodHeader
*res
;
743 res
= g_malloc0 (MONO_SIZEOF_METHOD_HEADER
+ sizeof (gpointer
) * header
->num_locals
);
744 res
->code
= header
->code
;
745 res
->code_size
= header
->code_size
;
746 res
->max_stack
= header
->max_stack
;
747 res
->num_clauses
= header
->num_clauses
;
748 res
->init_locals
= header
->init_locals
;
749 res
->num_locals
= header
->num_locals
;
750 res
->clauses
= header
->clauses
;
751 for (i
= 0; i
< header
->num_locals
; ++i
)
752 res
->locals
[i
] = mono_class_inflate_generic_type (header
->locals
[i
], context
);
753 if (res
->num_clauses
) {
754 res
->clauses
= g_memdup (header
->clauses
, sizeof (MonoExceptionClause
) * res
->num_clauses
);
755 for (i
= 0; i
< header
->num_clauses
; ++i
) {
756 MonoExceptionClause
*clause
= &res
->clauses
[i
];
757 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
759 clause
->data
.catch_class
= mono_class_inflate_generic_class (clause
->data
.catch_class
, context
);
766 * token is the method_ref/def/spec token used in a call IL instruction.
769 mono_method_get_signature_full (MonoMethod
*method
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
771 int table
= mono_metadata_token_table (token
);
772 int idx
= mono_metadata_token_index (token
);
774 guint32 cols
[MONO_MEMBERREF_SIZE
];
775 MonoMethodSignature
*sig
;
778 /* !table is for wrappers: we should really assign their own token to them */
779 if (!table
|| table
== MONO_TABLE_METHOD
)
780 return mono_method_signature (method
);
782 if (table
== MONO_TABLE_METHODSPEC
) {
783 /* the verifier (do_invoke_method) will turn the NULL into a verifier error */
784 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) || !method
->is_inflated
)
787 return mono_method_signature (method
);
790 if (method
->klass
->generic_class
)
791 return mono_method_signature (method
);
794 /* FIXME: This might be incorrect for vararg methods */
795 return mono_method_signature (method
);
797 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, MONO_MEMBERREF_SIZE
);
798 sig_idx
= cols
[MONO_MEMBERREF_SIGNATURE
];
800 sig
= find_cached_memberref_sig (image
, sig_idx
);
802 if (!mono_verifier_verify_memberref_signature (image
, sig_idx
, NULL
)) {
803 guint32
class = cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
804 const char *fname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
806 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname
, token
, image
->name
));
810 ptr
= mono_metadata_blob_heap (image
, sig_idx
);
811 mono_metadata_decode_blob_size (ptr
, &ptr
);
812 sig
= mono_metadata_parse_method_signature (image
, 0, ptr
, NULL
);
815 sig
= cache_memberref_sig (image
, sig_idx
, sig
);
818 if (!mono_verifier_is_sig_compatible (image
, method
, sig
)) {
819 guint32
class = cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
820 const char *fname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
822 mono_loader_set_error_bad_image (g_strdup_printf ("Incompatible method signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname
, token
, image
->name
));
827 MonoMethodSignature
*cached
;
829 /* This signature is not owned by a MonoMethod, so need to cache */
830 sig
= inflate_generic_signature (image
, sig
, context
);
831 cached
= mono_metadata_get_inflated_signature (sig
, context
);
833 mono_metadata_free_inflated_signature (sig
);
835 inflated_signatures_size
+= mono_metadata_signature_size (cached
);
843 mono_method_get_signature (MonoMethod
*method
, MonoImage
*image
, guint32 token
)
845 return mono_method_get_signature_full (method
, image
, token
, NULL
);
848 /* this is only for the typespec array methods */
850 mono_method_search_in_array_class (MonoClass
*klass
, const char *name
, MonoMethodSignature
*sig
)
854 mono_class_setup_methods (klass
);
855 g_assert (!klass
->exception_type
); /*FIXME this should not fail, right?*/
856 for (i
= 0; i
< klass
->method
.count
; ++i
) {
857 MonoMethod
*method
= klass
->methods
[i
];
858 if (strcmp (method
->name
, name
) == 0 && sig
->param_count
== method
->signature
->param_count
)
865 method_from_memberref (MonoImage
*image
, guint32 idx
, MonoGenericContext
*typespec_context
,
866 gboolean
*used_context
)
868 MonoClass
*klass
= NULL
;
869 MonoMethod
*method
= NULL
;
870 MonoTableInfo
*tables
= image
->tables
;
872 guint32 nindex
, class, sig_idx
;
874 MonoMethodSignature
*sig
;
877 mono_metadata_decode_row (&tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, 3);
878 nindex
= cols
[MONO_MEMBERREF_CLASS
] >> MONO_MEMBERREF_PARENT_BITS
;
879 class = cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
880 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
881 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
883 mname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
886 * Whether we actually used the `typespec_context' or not.
887 * This is used to tell our caller whether or not it's safe to insert the returned
888 * method into a cache.
891 *used_context
= class == MONO_MEMBERREF_PARENT_TYPESPEC
;
894 case MONO_MEMBERREF_PARENT_TYPEREF
:
895 klass
= mono_class_from_typeref (image
, MONO_TOKEN_TYPE_REF
| nindex
);
897 char *name
= mono_class_name_from_token (image
, MONO_TOKEN_TYPE_REF
| nindex
);
898 g_warning ("Missing method %s in assembly %s, type %s", mname
, image
->name
, name
);
899 mono_loader_set_error_type_load (name
, image
->assembly_name
);
904 case MONO_MEMBERREF_PARENT_TYPESPEC
:
906 * Parse the TYPESPEC in the parent's context.
908 klass
= mono_class_get_full (image
, MONO_TOKEN_TYPE_SPEC
| nindex
, typespec_context
);
910 char *name
= mono_class_name_from_token (image
, MONO_TOKEN_TYPE_SPEC
| nindex
);
911 g_warning ("Missing method %s in assembly %s, type %s", mname
, image
->name
, name
);
912 mono_loader_set_error_type_load (name
, image
->assembly_name
);
917 case MONO_MEMBERREF_PARENT_TYPEDEF
:
918 klass
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| nindex
);
920 char *name
= mono_class_name_from_token (image
, MONO_TOKEN_TYPE_DEF
| nindex
);
921 g_warning ("Missing method %s in assembly %s, type %s", mname
, image
->name
, name
);
922 mono_loader_set_error_type_load (name
, image
->assembly_name
);
927 case MONO_MEMBERREF_PARENT_METHODDEF
:
928 return mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| nindex
, NULL
);
932 /* This message leaks */
933 char *message
= g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex
);
934 mono_loader_set_error_method_load ("", message
);
940 mono_class_init (klass
);
942 sig_idx
= cols
[MONO_MEMBERREF_SIGNATURE
];
944 if (!mono_verifier_verify_memberref_signature (image
, sig_idx
, NULL
)) {
945 mono_loader_set_error_method_load (klass
->name
, mname
);
949 ptr
= mono_metadata_blob_heap (image
, sig_idx
);
950 mono_metadata_decode_blob_size (ptr
, &ptr
);
952 sig
= find_cached_memberref_sig (image
, sig_idx
);
954 sig
= mono_metadata_parse_method_signature (image
, 0, ptr
, NULL
);
958 sig
= cache_memberref_sig (image
, sig_idx
, sig
);
962 case MONO_MEMBERREF_PARENT_TYPEREF
:
963 case MONO_MEMBERREF_PARENT_TYPEDEF
:
964 method
= find_method (klass
, NULL
, mname
, sig
, klass
);
967 case MONO_MEMBERREF_PARENT_TYPESPEC
: {
970 type
= &klass
->byval_arg
;
972 if (type
->type
!= MONO_TYPE_ARRAY
&& type
->type
!= MONO_TYPE_SZARRAY
) {
973 MonoClass
*in_class
= klass
->generic_class
? klass
->generic_class
->container_class
: klass
;
974 method
= find_method (in_class
, NULL
, mname
, sig
, klass
);
978 /* we're an array and we created these methods already in klass in mono_class_init () */
979 method
= mono_method_search_in_array_class (klass
, mname
, sig
);
983 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex
);
984 g_assert_not_reached ();
988 char *msig
= mono_signature_get_desc (sig
, FALSE
);
989 char * class_name
= mono_type_get_name (&klass
->byval_arg
);
990 GString
*s
= g_string_new (mname
);
991 if (sig
->generic_param_count
)
992 g_string_append_printf (s
, "<[%d]>", sig
->generic_param_count
);
993 g_string_append_printf (s
, "(%s)", msig
);
995 msig
= g_string_free (s
, FALSE
);
998 "Missing method %s::%s in assembly %s, referenced in assembly %s",
999 class_name
, msig
, klass
->image
->name
, image
->name
);
1000 mono_loader_set_error_method_load (class_name
, mname
);
1002 g_free (class_name
);
1009 method_from_methodspec (MonoImage
*image
, MonoGenericContext
*context
, guint32 idx
)
1014 MonoTableInfo
*tables
= image
->tables
;
1015 MonoGenericContext new_context
;
1016 MonoGenericInst
*inst
;
1018 guint32 cols
[MONO_METHODSPEC_SIZE
];
1019 guint32 token
, nindex
, param_count
;
1021 mono_metadata_decode_row (&tables
[MONO_TABLE_METHODSPEC
], idx
- 1, cols
, MONO_METHODSPEC_SIZE
);
1022 token
= cols
[MONO_METHODSPEC_METHOD
];
1023 nindex
= token
>> MONO_METHODDEFORREF_BITS
;
1025 if (!mono_verifier_verify_methodspec_signature (image
, cols
[MONO_METHODSPEC_SIGNATURE
], NULL
))
1028 ptr
= mono_metadata_blob_heap (image
, cols
[MONO_METHODSPEC_SIGNATURE
]);
1030 mono_metadata_decode_value (ptr
, &ptr
);
1032 param_count
= mono_metadata_decode_value (ptr
, &ptr
);
1033 g_assert (param_count
);
1035 inst
= mono_metadata_parse_generic_inst (image
, NULL
, param_count
, ptr
, &ptr
);
1036 if (context
&& inst
->is_open
) {
1037 inst
= mono_metadata_inflate_generic_inst (inst
, context
, &error
);
1038 if (!mono_error_ok (&error
)) {
1039 mono_error_cleanup (&error
); /*FIXME don't swallow error message.*/
1044 if ((token
& MONO_METHODDEFORREF_MASK
) == MONO_METHODDEFORREF_METHODDEF
)
1045 method
= mono_get_method_full (image
, MONO_TOKEN_METHOD_DEF
| nindex
, NULL
, context
);
1047 method
= method_from_memberref (image
, nindex
, context
, NULL
);
1052 klass
= method
->klass
;
1054 if (klass
->generic_class
) {
1055 g_assert (method
->is_inflated
);
1056 method
= ((MonoMethodInflated
*) method
)->declaring
;
1059 new_context
.class_inst
= klass
->generic_class
? klass
->generic_class
->context
.class_inst
: NULL
;
1060 new_context
.method_inst
= inst
;
1062 return mono_class_inflate_generic_method_full (method
, klass
, &new_context
);
1065 struct _MonoDllMap
{
1073 static MonoDllMap
*global_dll_map
;
1076 mono_dllmap_lookup_list (MonoDllMap
*dll_map
, const char *dll
, const char* func
, const char **rdll
, const char **rfunc
) {
1084 mono_loader_lock ();
1087 * we use the first entry we find that matches, since entries from
1088 * the config file are prepended to the list and we document that the
1089 * later entries win.
1091 for (; dll_map
; dll_map
= dll_map
->next
) {
1092 if (dll_map
->dll
[0] == 'i' && dll_map
->dll
[1] == ':') {
1093 if (g_ascii_strcasecmp (dll_map
->dll
+ 2, dll
))
1095 } else if (strcmp (dll_map
->dll
, dll
)) {
1098 if (!found
&& dll_map
->target
) {
1099 *rdll
= dll_map
->target
;
1101 /* we don't quit here, because we could find a full
1102 * entry that matches also function and that has priority.
1105 if (dll_map
->func
&& strcmp (dll_map
->func
, func
) == 0) {
1106 *rfunc
= dll_map
->target_func
;
1111 mono_loader_unlock ();
1116 mono_dllmap_lookup (MonoImage
*assembly
, const char *dll
, const char* func
, const char **rdll
, const char **rfunc
)
1119 if (assembly
&& assembly
->dll_map
) {
1120 res
= mono_dllmap_lookup_list (assembly
->dll_map
, dll
, func
, rdll
, rfunc
);
1124 return mono_dllmap_lookup_list (global_dll_map
, dll
, func
, rdll
, rfunc
);
1128 * mono_dllmap_insert:
1130 * LOCKING: Acquires the loader lock.
1132 * NOTE: This can be called before the runtime is initialized, for example from
1133 * mono_config_parse ().
1136 mono_dllmap_insert (MonoImage
*assembly
, const char *dll
, const char *func
, const char *tdll
, const char *tfunc
)
1140 mono_loader_init ();
1142 mono_loader_lock ();
1145 entry
= g_malloc0 (sizeof (MonoDllMap
));
1146 entry
->dll
= dll
? g_strdup (dll
): NULL
;
1147 entry
->target
= tdll
? g_strdup (tdll
): NULL
;
1148 entry
->func
= func
? g_strdup (func
): NULL
;
1149 entry
->target_func
= tfunc
? g_strdup (tfunc
): NULL
;
1150 entry
->next
= global_dll_map
;
1151 global_dll_map
= entry
;
1153 entry
= mono_image_alloc0 (assembly
, sizeof (MonoDllMap
));
1154 entry
->dll
= dll
? mono_image_strdup (assembly
, dll
): NULL
;
1155 entry
->target
= tdll
? mono_image_strdup (assembly
, tdll
): NULL
;
1156 entry
->func
= func
? mono_image_strdup (assembly
, func
): NULL
;
1157 entry
->target_func
= tfunc
? mono_image_strdup (assembly
, tfunc
): NULL
;
1158 entry
->next
= assembly
->dll_map
;
1159 assembly
->dll_map
= entry
;
1162 mono_loader_unlock ();
1165 static GHashTable
*global_module_map
;
1168 cached_module_load (const char *name
, int flags
, char **err
)
1171 mono_loader_lock ();
1172 if (!global_module_map
)
1173 global_module_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
1174 res
= g_hash_table_lookup (global_module_map
, name
);
1177 mono_loader_unlock ();
1180 res
= mono_dl_open (name
, flags
, err
);
1182 g_hash_table_insert (global_module_map
, g_strdup (name
), res
);
1183 mono_loader_unlock ();
1188 mono_lookup_pinvoke_call (MonoMethod
*method
, const char **exc_class
, const char **exc_arg
)
1190 MonoImage
*image
= method
->klass
->image
;
1191 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)method
;
1192 MonoTableInfo
*tables
= image
->tables
;
1193 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
1194 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
1195 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
1196 guint32 scope_token
;
1197 const char *import
= NULL
;
1198 const char *orig_scope
;
1199 const char *new_scope
;
1201 char *full_name
, *file_name
;
1203 MonoDl
*module
= NULL
;
1205 g_assert (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
);
1208 return piinfo
->addr
;
1210 if (method
->klass
->image
->dynamic
) {
1211 MonoReflectionMethodAux
*method_aux
=
1212 g_hash_table_lookup (
1213 ((MonoDynamicImage
*)method
->klass
->image
)->method_aux_hash
, method
);
1217 import
= method_aux
->dllentry
;
1218 orig_scope
= method_aux
->dll
;
1221 if (!piinfo
->implmap_idx
)
1224 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
1226 piinfo
->piflags
= im_cols
[MONO_IMPLMAP_FLAGS
];
1227 import
= mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]);
1228 scope_token
= mono_metadata_decode_row_col (mr
, im_cols
[MONO_IMPLMAP_SCOPE
] - 1, MONO_MODULEREF_NAME
);
1229 orig_scope
= mono_metadata_string_heap (image
, scope_token
);
1232 mono_dllmap_lookup (image
, orig_scope
, import
, &new_scope
, &import
);
1234 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1235 "DllImport attempting to load: '%s'.", new_scope
);
1242 /* we allow a special name to dlopen from the running process namespace */
1243 if (strcmp (new_scope
, "__Internal") == 0)
1244 module
= mono_dl_open (NULL
, MONO_DL_LAZY
, &error_msg
);
1247 * Try loading the module using a variety of names
1249 for (i
= 0; i
< 4; ++i
) {
1252 /* Try the original name */
1253 file_name
= g_strdup (new_scope
);
1256 /* Try trimming the .dll extension */
1257 if (strstr (new_scope
, ".dll") == (new_scope
+ strlen (new_scope
) - 4)) {
1258 file_name
= g_strdup (new_scope
);
1259 file_name
[strlen (new_scope
) - 4] = '\0';
1265 if (strstr (new_scope
, "lib") != new_scope
) {
1266 file_name
= g_strdup_printf ("lib%s", new_scope
);
1272 #ifndef TARGET_WIN32
1273 if (!g_ascii_strcasecmp ("user32.dll", new_scope
) ||
1274 !g_ascii_strcasecmp ("kernel32.dll", new_scope
) ||
1275 !g_ascii_strcasecmp ("user32", new_scope
) ||
1276 !g_ascii_strcasecmp ("kernel", new_scope
)) {
1277 file_name
= g_strdup ("libMonoSupportW.so");
1281 #ifndef TARGET_WIN32
1288 while ((full_name
= mono_dl_build_path (NULL
, file_name
, &iter
))) {
1289 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1290 "DllImport loading location: '%s'.", full_name
);
1291 module
= cached_module_load (full_name
, MONO_DL_LAZY
, &error_msg
);
1293 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1294 "DllImport error loading library: '%s'.",
1306 while ((full_name
= mono_dl_build_path (".", file_name
, &iter
))) {
1307 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1308 "DllImport loading library: '%s'.", full_name
);
1309 module
= cached_module_load (full_name
, MONO_DL_LAZY
, &error_msg
);
1311 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1312 "DllImport error loading library '%s'.",
1323 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1324 "DllImport loading: '%s'.", file_name
);
1325 module
= cached_module_load (file_name
, MONO_DL_LAZY
, &error_msg
);
1327 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1328 "DllImport error loading library '%s'.",
1340 mono_trace (G_LOG_LEVEL_WARNING
, MONO_TRACE_DLLIMPORT
,
1341 "DllImport unable to load library '%s'.",
1346 *exc_class
= "DllNotFoundException";
1347 *exc_arg
= new_scope
;
1352 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1353 "Searching for '%s'.", import
);
1355 if (piinfo
->piflags
& PINVOKE_ATTRIBUTE_NO_MANGLE
) {
1356 error_msg
= mono_dl_symbol (module
, import
, &piinfo
->addr
);
1358 char *mangled_name
= NULL
, *mangled_name2
= NULL
;
1361 int mangle_param_count
;
1367 * Search using a variety of mangled names
1369 for (mangle_charset
= 0; mangle_charset
<= 1; mangle_charset
++) {
1370 for (mangle_stdcall
= 0; mangle_stdcall
<= 1; mangle_stdcall
++) {
1371 gboolean need_param_count
= FALSE
;
1373 if (mangle_stdcall
> 0)
1374 need_param_count
= TRUE
;
1376 for (mangle_param_count
= 0; mangle_param_count
<= (need_param_count
? 256 : 0); mangle_param_count
+= 4) {
1381 mangled_name
= (char*)import
;
1382 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
1383 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
1384 /* Try the mangled name first */
1385 if (mangle_charset
== 0)
1386 mangled_name
= g_strconcat (import
, "W", NULL
);
1388 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
1390 if (mangle_charset
== 0)
1391 mangled_name
= g_strconcat (import
, "W", NULL
);
1393 /* Try the mangled name last */
1394 if (mangle_charset
== 1)
1395 mangled_name
= g_strconcat (import
, "A", NULL
);
1398 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
1400 /* Try the mangled name last */
1401 if (mangle_charset
== 1)
1402 mangled_name
= g_strconcat (import
, "A", NULL
);
1407 if (mangle_param_count
== 0)
1408 param_count
= mono_method_signature (method
)->param_count
* sizeof (gpointer
);
1410 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1411 param_count
= mangle_param_count
;
1413 /* Try the stdcall mangled name */
1415 * gcc under windows creates mangled names without the underscore, but MS.NET
1416 * doesn't support it, so we doesn't support it either.
1418 if (mangle_stdcall
== 1)
1419 mangled_name2
= g_strdup_printf ("_%s@%d", mangled_name
, param_count
);
1421 mangled_name2
= mangled_name
;
1423 mangled_name2
= mangled_name
;
1426 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1427 "Probing '%s'.", mangled_name2
);
1429 error_msg
= mono_dl_symbol (module
, mangled_name2
, &piinfo
->addr
);
1432 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1433 "Found as '%s'.", mangled_name2
);
1435 if (mangled_name
!= mangled_name2
)
1436 g_free (mangled_name2
);
1437 if (mangled_name
!= import
)
1438 g_free (mangled_name
);
1444 if (!piinfo
->addr
) {
1447 *exc_class
= "EntryPointNotFoundException";
1452 return piinfo
->addr
;
1456 * LOCKING: assumes the loader lock to be taken.
1459 mono_get_method_from_token (MonoImage
*image
, guint32 token
, MonoClass
*klass
,
1460 MonoGenericContext
*context
, gboolean
*used_context
)
1463 int table
= mono_metadata_token_table (token
);
1464 int idx
= mono_metadata_token_index (token
);
1465 MonoTableInfo
*tables
= image
->tables
;
1466 MonoGenericContainer
*generic_container
= NULL
, *container
= NULL
;
1467 const char *sig
= NULL
;
1469 guint32 cols
[MONO_TYPEDEF_SIZE
];
1471 if (image
->dynamic
) {
1472 MonoClass
*handle_class
;
1474 result
= mono_lookup_dynamic_token_class (image
, token
, TRUE
, &handle_class
, context
);
1475 // This checks the memberref type as well
1476 if (result
&& handle_class
!= mono_defaults
.methodhandle_class
) {
1477 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token
, image
->name
));
1483 if (table
!= MONO_TABLE_METHOD
) {
1484 if (table
== MONO_TABLE_METHODSPEC
) {
1485 if (used_context
) *used_context
= TRUE
;
1486 return method_from_methodspec (image
, context
, idx
);
1488 if (table
!= MONO_TABLE_MEMBERREF
) {
1489 g_warning ("got wrong token: 0x%08x\n", token
);
1490 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token
, image
->name
));
1493 return method_from_memberref (image
, idx
, context
, used_context
);
1496 if (used_context
) *used_context
= FALSE
;
1498 if (idx
> image
->tables
[MONO_TABLE_METHOD
].rows
) {
1499 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token
, image
->name
));
1503 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_METHOD
], idx
- 1, cols
, 6);
1505 if ((cols
[2] & METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
1506 (cols
[1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
))
1507 result
= (MonoMethod
*)mono_image_alloc0 (image
, sizeof (MonoMethodPInvoke
));
1509 result
= (MonoMethod
*)mono_image_alloc0 (image
, sizeof (MonoMethodNormal
));
1511 mono_stats
.method_count
++;
1513 if (!klass
) { /*FIXME put this before the image alloc*/
1514 guint32 type
= mono_metadata_typedef_from_method (image
, token
);
1517 klass
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| type
);
1523 result
->klass
= klass
;
1524 result
->flags
= cols
[2];
1525 result
->iflags
= cols
[1];
1526 result
->token
= token
;
1527 result
->name
= mono_metadata_string_heap (image
, cols
[3]);
1529 if (!sig
) /* already taken from the methodref */
1530 sig
= mono_metadata_blob_heap (image
, cols
[4]);
1531 size
= mono_metadata_decode_blob_size (sig
, &sig
);
1533 container
= klass
->generic_container
;
1536 * load_generic_params does a binary search so only call it if the method
1540 generic_container
= mono_metadata_load_generic_params (image
, token
, container
);
1541 if (generic_container
) {
1542 result
->is_generic
= TRUE
;
1543 generic_container
->owner
.method
= result
;
1544 /*FIXME put this before the image alloc*/
1545 if (!mono_metadata_load_generic_param_constraints_full (image
, token
, generic_container
))
1548 container
= generic_container
;
1551 if (cols
[1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
1552 if (result
->klass
== mono_defaults
.string_class
&& !strcmp (result
->name
, ".ctor"))
1553 result
->string_ctor
= 1;
1554 } else if (cols
[2] & METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
1555 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)result
;
1558 /* IJW is P/Invoke with a predefined function pointer. */
1559 if (image
->is_module_handle
&& (cols
[1] & METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
1560 piinfo
->addr
= mono_image_rva_map (image
, cols
[0]);
1561 g_assert (piinfo
->addr
);
1564 piinfo
->implmap_idx
= mono_metadata_implmap_from_method (image
, idx
- 1);
1565 /* Native methods can have no map. */
1566 if (piinfo
->implmap_idx
)
1567 piinfo
->piflags
= mono_metadata_decode_row_col (&tables
[MONO_TABLE_IMPLMAP
], piinfo
->implmap_idx
- 1, MONO_IMPLMAP_FLAGS
);
1570 if (generic_container
)
1571 mono_method_set_generic_container (result
, generic_container
);
1577 mono_get_method (MonoImage
*image
, guint32 token
, MonoClass
*klass
)
1579 return mono_get_method_full (image
, token
, klass
, NULL
);
1583 mono_get_method_full (MonoImage
*image
, guint32 token
, MonoClass
*klass
,
1584 MonoGenericContext
*context
)
1587 gboolean used_context
= FALSE
;
1589 /* We do everything inside the lock to prevent creation races */
1591 mono_image_lock (image
);
1593 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
) {
1594 if (!image
->method_cache
)
1595 image
->method_cache
= g_hash_table_new (NULL
, NULL
);
1596 result
= g_hash_table_lookup (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)));
1598 if (!image
->methodref_cache
)
1599 image
->methodref_cache
= g_hash_table_new (NULL
, NULL
);
1600 result
= g_hash_table_lookup (image
->methodref_cache
, GINT_TO_POINTER (token
));
1602 mono_image_unlock (image
);
1607 result
= mono_get_method_from_token (image
, token
, klass
, context
, &used_context
);
1611 mono_image_lock (image
);
1612 if (!used_context
&& !result
->is_inflated
) {
1613 MonoMethod
*result2
;
1614 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
)
1615 result2
= g_hash_table_lookup (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)));
1617 result2
= g_hash_table_lookup (image
->methodref_cache
, GINT_TO_POINTER (token
));
1620 mono_image_unlock (image
);
1624 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
)
1625 g_hash_table_insert (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)), result
);
1627 g_hash_table_insert (image
->methodref_cache
, GINT_TO_POINTER (token
), result
);
1630 mono_image_unlock (image
);
1636 * mono_get_method_constrained:
1638 * This is used when JITing the `constrained.' opcode.
1640 * This returns two values: the contrained method, which has been inflated
1641 * as the function return value; And the original CIL-stream method as
1642 * declared in cil_method. The later is used for verification.
1645 mono_get_method_constrained (MonoImage
*image
, guint32 token
, MonoClass
*constrained_class
,
1646 MonoGenericContext
*context
, MonoMethod
**cil_method
)
1648 MonoMethod
*method
, *result
;
1649 MonoClass
*ic
= NULL
;
1650 MonoGenericContext
*method_context
= NULL
;
1651 MonoMethodSignature
*sig
, *original_sig
;
1653 mono_loader_lock ();
1655 *cil_method
= mono_get_method_from_token (image
, token
, NULL
, context
, NULL
);
1657 mono_loader_unlock ();
1661 mono_class_init (constrained_class
);
1662 method
= *cil_method
;
1663 original_sig
= sig
= mono_method_signature (method
);
1667 if (method
->is_inflated
&& sig
->generic_param_count
) {
1668 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
1669 sig
= mono_method_signature (imethod
->declaring
); /*We assume that if the inflated method signature is valid, the declaring method is too*/
1670 method_context
= mono_method_get_context (method
);
1674 * We must inflate the signature with the class instantiation to work on
1675 * cases where a class inherit from a generic type and the override replaces
1676 * any type argument which a concrete type. See #325283.
1678 if (method_context
->class_inst
) {
1679 MonoGenericContext ctx
;
1680 ctx
.method_inst
= NULL
;
1681 ctx
.class_inst
= method_context
->class_inst
;
1683 sig
= inflate_generic_signature (method
->klass
->image
, sig
, &ctx
);
1689 if ((constrained_class
!= method
->klass
) && (MONO_CLASS_IS_INTERFACE (method
->klass
)))
1692 result
= find_method (constrained_class
, ic
, method
->name
, sig
, constrained_class
);
1693 if (sig
!= original_sig
)
1694 mono_metadata_free_inflated_signature (sig
);
1697 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method
->klass
->name_space
,
1698 method
->klass
->name
, method
->name
, image
->name
, token
);
1699 mono_loader_unlock ();
1704 result
= mono_class_inflate_generic_method (result
, method_context
);
1706 mono_loader_unlock ();
1711 mono_free_method (MonoMethod
*method
)
1713 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS
)
1714 mono_profiler_method_free (method
);
1716 /* FIXME: This hack will go away when the profiler will support freeing methods */
1717 if (mono_profiler_get_events () != MONO_PROFILE_NONE
)
1720 if (method
->signature
) {
1722 * FIXME: This causes crashes because the types inside signatures and
1723 * locals are shared.
1725 /* mono_metadata_free_method_signature (method->signature); */
1726 /* g_free (method->signature); */
1729 if (method
->dynamic
) {
1730 MonoMethodWrapper
*mw
= (MonoMethodWrapper
*)method
;
1733 mono_marshal_free_dynamic_wrappers (method
);
1735 mono_image_property_remove (method
->klass
->image
, method
);
1737 g_free ((char*)method
->name
);
1738 if (mw
->method
.header
) {
1739 g_free ((char*)mw
->method
.header
->code
);
1740 for (i
= 0; i
< mw
->method
.header
->num_locals
; ++i
)
1741 g_free (mw
->method
.header
->locals
[i
]);
1742 g_free (mw
->method
.header
->clauses
);
1743 g_free (mw
->method
.header
);
1745 g_free (mw
->method_data
);
1746 g_free (method
->signature
);
1752 mono_method_get_param_names (MonoMethod
*method
, const char **names
)
1756 MonoTableInfo
*methodt
;
1757 MonoTableInfo
*paramt
;
1758 MonoMethodSignature
*signature
;
1761 if (method
->is_inflated
)
1762 method
= ((MonoMethodInflated
*) method
)->declaring
;
1764 signature
= mono_method_signature (method
);
1765 /*FIXME this check is somewhat redundant since the caller usally will have to get the signature to figure out the
1766 number of arguments and allocate a properly sized array. */
1767 if (signature
== NULL
)
1770 if (!signature
->param_count
)
1773 for (i
= 0; i
< signature
->param_count
; ++i
)
1776 klass
= method
->klass
;
1780 mono_class_init (klass
);
1782 if (klass
->image
->dynamic
) {
1783 MonoReflectionMethodAux
*method_aux
=
1784 g_hash_table_lookup (
1785 ((MonoDynamicImage
*)method
->klass
->image
)->method_aux_hash
, method
);
1786 if (method_aux
&& method_aux
->param_names
) {
1787 for (i
= 0; i
< mono_method_signature (method
)->param_count
; ++i
)
1788 if (method_aux
->param_names
[i
+ 1])
1789 names
[i
] = method_aux
->param_names
[i
+ 1];
1794 methodt
= &klass
->image
->tables
[MONO_TABLE_METHOD
];
1795 paramt
= &klass
->image
->tables
[MONO_TABLE_PARAM
];
1796 idx
= mono_method_get_index (method
);
1798 guint32 cols
[MONO_PARAM_SIZE
];
1801 param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
1803 if (idx
< methodt
->rows
)
1804 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
1806 lastp
= paramt
->rows
+ 1;
1807 for (i
= param_index
; i
< lastp
; ++i
) {
1808 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
1809 if (cols
[MONO_PARAM_SEQUENCE
] && cols
[MONO_PARAM_SEQUENCE
] <= signature
->param_count
) /* skip return param spec and bounds check*/
1810 names
[cols
[MONO_PARAM_SEQUENCE
] - 1] = mono_metadata_string_heap (klass
->image
, cols
[MONO_PARAM_NAME
]);
1816 mono_method_get_param_token (MonoMethod
*method
, int index
)
1818 MonoClass
*klass
= method
->klass
;
1819 MonoTableInfo
*methodt
;
1822 mono_class_init (klass
);
1824 if (klass
->image
->dynamic
) {
1825 g_assert_not_reached ();
1828 methodt
= &klass
->image
->tables
[MONO_TABLE_METHOD
];
1829 idx
= mono_method_get_index (method
);
1831 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
1835 return mono_metadata_make_token (MONO_TABLE_PARAM
, 0);
1837 return mono_metadata_make_token (MONO_TABLE_PARAM
, param_index
+ index
);
1844 mono_method_get_marshal_info (MonoMethod
*method
, MonoMarshalSpec
**mspecs
)
1847 MonoClass
*klass
= method
->klass
;
1848 MonoTableInfo
*methodt
;
1849 MonoTableInfo
*paramt
;
1850 MonoMethodSignature
*signature
;
1853 signature
= mono_method_signature (method
);
1854 g_assert (signature
); /*FIXME there is no way to signal error from this function*/
1856 for (i
= 0; i
< signature
->param_count
+ 1; ++i
)
1859 if (method
->klass
->image
->dynamic
) {
1860 MonoReflectionMethodAux
*method_aux
=
1861 g_hash_table_lookup (
1862 ((MonoDynamicImage
*)method
->klass
->image
)->method_aux_hash
, method
);
1863 if (method_aux
&& method_aux
->param_marshall
) {
1864 MonoMarshalSpec
**dyn_specs
= method_aux
->param_marshall
;
1865 for (i
= 0; i
< signature
->param_count
+ 1; ++i
)
1866 if (dyn_specs
[i
]) {
1867 mspecs
[i
] = g_new0 (MonoMarshalSpec
, 1);
1868 memcpy (mspecs
[i
], dyn_specs
[i
], sizeof (MonoMarshalSpec
));
1874 mono_class_init (klass
);
1876 methodt
= &klass
->image
->tables
[MONO_TABLE_METHOD
];
1877 paramt
= &klass
->image
->tables
[MONO_TABLE_PARAM
];
1878 idx
= mono_method_get_index (method
);
1880 guint32 cols
[MONO_PARAM_SIZE
];
1881 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
1883 if (idx
< methodt
->rows
)
1884 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
1886 lastp
= paramt
->rows
+ 1;
1888 for (i
= param_index
; i
< lastp
; ++i
) {
1889 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
1891 if (cols
[MONO_PARAM_FLAGS
] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL
&& cols
[MONO_PARAM_SEQUENCE
] <= signature
->param_count
) {
1893 tp
= mono_metadata_get_marshal_info (klass
->image
, i
- 1, FALSE
);
1895 mspecs
[cols
[MONO_PARAM_SEQUENCE
]]= mono_metadata_parse_marshal_spec (klass
->image
, tp
);
1904 mono_method_has_marshal_info (MonoMethod
*method
)
1907 MonoClass
*klass
= method
->klass
;
1908 MonoTableInfo
*methodt
;
1909 MonoTableInfo
*paramt
;
1912 if (method
->klass
->image
->dynamic
) {
1913 MonoReflectionMethodAux
*method_aux
=
1914 g_hash_table_lookup (
1915 ((MonoDynamicImage
*)method
->klass
->image
)->method_aux_hash
, method
);
1916 MonoMarshalSpec
**dyn_specs
= method_aux
->param_marshall
;
1918 for (i
= 0; i
< mono_method_signature (method
)->param_count
+ 1; ++i
)
1925 mono_class_init (klass
);
1927 methodt
= &klass
->image
->tables
[MONO_TABLE_METHOD
];
1928 paramt
= &klass
->image
->tables
[MONO_TABLE_PARAM
];
1929 idx
= mono_method_get_index (method
);
1931 guint32 cols
[MONO_PARAM_SIZE
];
1932 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
1934 if (idx
+ 1 < methodt
->rows
)
1935 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
1937 lastp
= paramt
->rows
+ 1;
1939 for (i
= param_index
; i
< lastp
; ++i
) {
1940 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
1942 if (cols
[MONO_PARAM_FLAGS
] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL
)
1951 mono_method_get_wrapper_data (MonoMethod
*method
, guint32 id
)
1954 g_assert (method
!= NULL
);
1955 g_assert (method
->wrapper_type
!= MONO_WRAPPER_NONE
);
1957 if (method
->is_inflated
)
1958 method
= ((MonoMethodInflated
*) method
)->declaring
;
1959 data
= ((MonoMethodWrapper
*)method
)->method_data
;
1960 g_assert (data
!= NULL
);
1961 g_assert (id
<= GPOINTER_TO_UINT (*data
));
1966 default_stack_walk (MonoStackWalk func
, gboolean do_il_offset
, gpointer user_data
) {
1967 g_error ("stack walk not installed");
1970 static MonoStackWalkImpl stack_walk
= default_stack_walk
;
1973 mono_stack_walk (MonoStackWalk func
, gpointer user_data
)
1975 stack_walk (func
, TRUE
, user_data
);
1979 mono_stack_walk_no_il (MonoStackWalk func
, gpointer user_data
)
1981 stack_walk (func
, FALSE
, user_data
);
1985 mono_install_stack_walk (MonoStackWalkImpl func
)
1991 last_managed (MonoMethod
*m
, gint no
, gint ilo
, gboolean managed
, gpointer data
)
1993 MonoMethod
**dest
= data
;
1995 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
2001 mono_method_get_last_managed (void)
2003 MonoMethod
*m
= NULL
;
2004 stack_walk (last_managed
, FALSE
, &m
);
2008 static gboolean loader_lock_track_ownership
= FALSE
;
2013 * See docs/thread-safety.txt for the locking strategy.
2016 mono_loader_lock (void)
2018 mono_locks_acquire (&loader_mutex
, LoaderLock
);
2019 if (G_UNLIKELY (loader_lock_track_ownership
)) {
2020 TlsSetValue (loader_lock_nest_id
, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id
)) + 1));
2025 mono_loader_unlock (void)
2027 mono_locks_release (&loader_mutex
, LoaderLock
);
2028 if (G_UNLIKELY (loader_lock_track_ownership
)) {
2029 TlsSetValue (loader_lock_nest_id
, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id
)) - 1));
2034 * mono_loader_lock_track_ownership:
2036 * Set whenever the runtime should track ownership of the loader lock. If set to TRUE,
2037 * the mono_loader_lock_is_owned_by_self () can be called to query whenever the current
2038 * thread owns the loader lock.
2041 mono_loader_lock_track_ownership (gboolean track
)
2043 loader_lock_track_ownership
= track
;
2047 * mono_loader_lock_is_owned_by_self:
2049 * Return whenever the current thread owns the loader lock.
2050 * This is useful to avoid blocking operations while holding the loader lock.
2053 mono_loader_lock_is_owned_by_self (void)
2055 g_assert (loader_lock_track_ownership
);
2057 return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id
)) > 0;
2061 * mono_method_signature:
2063 * Return the signature of the method M. On failure, returns NULL, and ERR is set.
2065 MonoMethodSignature
*
2066 mono_method_signature_checked (MonoMethod
*m
, MonoError
*err
)
2072 gboolean can_cache_signature
;
2073 MonoGenericContainer
*container
;
2074 MonoMethodSignature
*signature
= NULL
;
2077 /* We need memory barriers below because of the double-checked locking pattern */
2080 return m
->signature
;
2082 mono_loader_lock ();
2085 mono_loader_unlock ();
2086 return m
->signature
;
2089 if (m
->is_inflated
) {
2090 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) m
;
2091 /* the lock is recursive */
2092 signature
= mono_method_signature (imethod
->declaring
);
2093 signature
= inflate_generic_signature (imethod
->declaring
->klass
->image
, signature
, mono_method_get_context (m
));
2095 inflated_signatures_size
+= mono_metadata_signature_size (signature
);
2097 mono_memory_barrier ();
2098 m
->signature
= signature
;
2099 mono_loader_unlock ();
2100 return m
->signature
;
2103 g_assert (mono_metadata_token_table (m
->token
) == MONO_TABLE_METHOD
);
2104 idx
= mono_metadata_token_index (m
->token
);
2105 img
= m
->klass
->image
;
2107 sig
= mono_metadata_blob_heap (img
, sig_offset
= mono_metadata_decode_row_col (&img
->tables
[MONO_TABLE_METHOD
], idx
- 1, MONO_METHOD_SIGNATURE
));
2109 g_assert (!m
->klass
->generic_class
);
2110 container
= mono_method_get_generic_container (m
);
2112 container
= m
->klass
->generic_container
;
2114 /* Generic signatures depend on the container so they cannot be cached */
2115 /* icall/pinvoke signatures cannot be cached cause we modify them below */
2116 can_cache_signature
= !(m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) && !(m
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) && !container
;
2118 /* If the method has parameter attributes, that can modify the signature */
2119 if (mono_metadata_method_has_param_attrs (img
, idx
))
2120 can_cache_signature
= FALSE
;
2122 if (can_cache_signature
)
2123 signature
= g_hash_table_lookup (img
->method_signatures
, sig
);
2126 const char *sig_body
;
2127 /*TODO we should cache the failure result somewhere*/
2128 if (!mono_verifier_verify_method_signature (img
, sig_offset
, NULL
)) {
2129 mono_loader_unlock ();
2130 mono_error_set_method_load (err
, m
->klass
, m
->name
, "");
2134 size
= mono_metadata_decode_blob_size (sig
, &sig_body
);
2136 signature
= mono_metadata_parse_method_signature_full (img
, container
, idx
, sig_body
, NULL
);
2138 mono_loader_unlock ();
2139 mono_error_set_method_load (err
, m
->klass
, m
->name
, "");
2143 if (can_cache_signature
)
2144 g_hash_table_insert (img
->method_signatures
, (gpointer
)sig
, signature
);
2147 /* Verify metadata consistency */
2148 if (signature
->generic_param_count
) {
2149 if (!container
|| !container
->is_method
) {
2150 mono_loader_unlock ();
2151 mono_error_set_method_load (err
, m
->klass
, m
->name
, "Signature claims method has generic parameters, but generic_params table says it doesn't for method 0x%08x from image %s", idx
, img
->name
);
2154 if (container
->type_argc
!= signature
->generic_param_count
) {
2155 mono_loader_unlock ();
2156 mono_error_set_method_load (err
, m
->klass
, m
->name
, "Inconsistent generic parameter count. Signature says %d, generic_params table says %d for method 0x%08x from image %s", signature
->generic_param_count
, container
->type_argc
, idx
, img
->name
);
2159 } else if (container
&& container
->is_method
&& container
->type_argc
) {
2160 mono_loader_unlock ();
2161 mono_error_set_method_load (err
, m
->klass
, m
->name
, "generic_params table claims method has generic parameters, but signature says it doesn't for method 0x%08x from image %s", idx
, img
->name
);
2164 if (m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
2165 signature
->pinvoke
= 1;
2166 else if (m
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2167 MonoCallConvention conv
= 0;
2168 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)m
;
2169 signature
->pinvoke
= 1;
2171 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CALL_CONV_MASK
) {
2172 case 0: /* no call conv, so using default */
2173 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI
:
2174 conv
= MONO_CALL_DEFAULT
;
2176 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL
:
2179 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL
:
2180 conv
= MONO_CALL_STDCALL
;
2182 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL
:
2183 conv
= MONO_CALL_THISCALL
;
2185 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL
:
2186 conv
= MONO_CALL_FASTCALL
;
2188 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC
:
2189 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST
:
2191 mono_loader_unlock ();
2192 mono_error_set_method_load (err
, m
->klass
, m
->name
, "unsupported calling convention : 0x%04x for method 0x%08x from image %s", piinfo
->piflags
, idx
, img
->name
);
2195 signature
->call_convention
= conv
;
2198 mono_memory_barrier ();
2199 m
->signature
= signature
;
2201 mono_loader_unlock ();
2202 return m
->signature
;
2206 * mono_method_signature:
2208 * Return the signature of the method M. On failure, returns NULL.
2210 MonoMethodSignature
*
2211 mono_method_signature (MonoMethod
*m
)
2214 MonoMethodSignature
*sig
;
2216 mono_error_init (&err
);
2218 sig
= mono_method_signature_checked (m
, &err
);
2220 g_warning ("Could not load signature due to: %s", mono_error_get_message (&err
));
2221 mono_error_cleanup (&err
);
2228 mono_method_get_name (MonoMethod
*method
)
2230 return method
->name
;
2234 mono_method_get_class (MonoMethod
*method
)
2236 return method
->klass
;
2240 mono_method_get_token (MonoMethod
*method
)
2242 return method
->token
;
2246 mono_method_get_header (MonoMethod
*method
)
2252 MonoMethodNormal
* mn
= (MonoMethodNormal
*) method
;
2253 MonoMethodHeader
*header
;
2255 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
))
2259 if (G_LIKELY (mn
->header
))
2265 if (method
->is_inflated
) {
2266 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
2267 MonoMethodHeader
*header
;
2269 header
= mono_method_get_header (imethod
->declaring
);
2271 mono_loader_lock ();
2274 mono_loader_unlock ();
2278 mn
->header
= inflate_generic_header (header
, mono_method_get_context (method
));
2279 mono_loader_unlock ();
2284 * Do most of the work outside the loader lock, to avoid assembly loader hook
2287 g_assert (mono_metadata_token_table (method
->token
) == MONO_TABLE_METHOD
);
2288 idx
= mono_metadata_token_index (method
->token
);
2289 img
= method
->klass
->image
;
2290 rva
= mono_metadata_decode_row_col (&img
->tables
[MONO_TABLE_METHOD
], idx
- 1, MONO_METHOD_RVA
);
2292 if (!mono_verifier_verify_method_header (img
, rva
, NULL
))
2295 loc
= mono_image_rva_map (img
, rva
);
2298 header
= mono_metadata_parse_mh_full (img
, mono_method_get_generic_container (method
), loc
);
2300 mono_loader_lock ();
2303 /* header is allocated from the image mempool, no need to free it */
2304 mono_loader_unlock ();
2308 mono_memory_barrier ();
2310 mn
->header
= header
;
2312 mono_loader_unlock ();
2317 mono_method_get_flags (MonoMethod
*method
, guint32
*iflags
)
2320 *iflags
= method
->iflags
;
2321 return method
->flags
;
2325 * Find the method index in the metadata methodDef table.
2328 mono_method_get_index (MonoMethod
*method
) {
2329 MonoClass
*klass
= method
->klass
;
2333 return mono_metadata_token_index (method
->token
);
2335 mono_class_setup_methods (klass
);
2336 if (klass
->exception_type
)
2338 for (i
= 0; i
< klass
->method
.count
; ++i
) {
2339 if (method
== klass
->methods
[i
]) {
2340 if (klass
->image
->uncompressed_metadata
)
2341 return mono_metadata_translate_token_index (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
+ 1);
2343 return klass
->method
.first
+ i
+ 1;