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-internal.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;
57 static gboolean loader_lock_inited;
60 static guint32 inflated_signatures_size;
61 static guint32 memberref_sig_cache_size;
64 * This TLS variable contains the last type load error encountered by the loader.
66 guint32 loader_error_thread_id;
69 * This TLS variable holds how many times the current thread has acquired the loader
72 guint32 loader_lock_nest_id;
74 static void dllmap_cleanup (void);
79 static gboolean inited;
82 InitializeCriticalSection (&loader_mutex);
83 loader_lock_inited = TRUE;
85 loader_error_thread_id = TlsAlloc ();
86 loader_lock_nest_id = TlsAlloc ();
88 mono_counters_register ("Inflated signatures size",
89 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
90 mono_counters_register ("Memberref signature cache size",
91 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &memberref_sig_cache_size);
98 mono_loader_cleanup (void)
102 TlsFree (loader_error_thread_id);
103 TlsFree (loader_lock_nest_id);
105 DeleteCriticalSection (&loader_mutex);
106 loader_lock_inited = FALSE;
110 * Handling of type load errors should be done as follows:
112 * If something could not be loaded, the loader should call one of the
113 * mono_loader_set_error_XXX functions ()
114 * with the appropriate arguments, then return NULL to report the failure. The error
115 * should be propagated until it reaches code which can throw managed exceptions. At that
116 * point, an exception should be thrown based on the information returned by
117 * mono_loader_get_last_error (). Then the error should be cleared by calling
118 * mono_loader_clear_error ().
122 set_loader_error (MonoLoaderError *error)
124 TlsSetValue (loader_error_thread_id, error);
128 * mono_loader_set_error_assembly_load:
130 * Set the loader error for this thread.
133 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
135 MonoLoaderError *error;
137 if (mono_loader_get_last_error ())
140 error = g_new0 (MonoLoaderError, 1);
141 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
142 error->assembly_name = g_strdup (assembly_name);
143 error->ref_only = ref_only;
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
151 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);
153 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
155 set_loader_error (error);
159 * mono_loader_set_error_type_load:
161 * Set the loader error for this thread.
164 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
166 MonoLoaderError *error;
168 if (mono_loader_get_last_error ())
171 error = g_new0 (MonoLoaderError, 1);
172 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
173 error->class_name = g_strdup (class_name);
174 error->assembly_name = g_strdup (assembly_name);
177 * This is not strictly needed, but some (most) of the loader code still
178 * can't deal with load errors, and this message is more helpful than an
181 mono_trace_warning (MONO_TRACE_TYPE, "The class %s could not be loaded, used in %s", class_name, assembly_name);
183 set_loader_error (error);
187 * mono_loader_set_error_method_load:
189 * Set the loader error for this thread. MEMBER_NAME should point to a string
193 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
195 MonoLoaderError *error;
197 /* FIXME: Store the signature as well */
198 if (mono_loader_get_last_error ())
201 error = g_new0 (MonoLoaderError, 1);
202 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
203 error->class_name = g_strdup (class_name);
204 error->member_name = member_name;
206 set_loader_error (error);
210 * mono_loader_set_error_field_load:
212 * Set the loader error for this thread. MEMBER_NAME should point to a string
216 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
218 MonoLoaderError *error;
220 /* FIXME: Store the signature as well */
221 if (mono_loader_get_last_error ())
224 error = g_new0 (MonoLoaderError, 1);
225 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
226 error->klass = klass;
227 error->member_name = member_name;
229 set_loader_error (error);
233 * mono_loader_set_error_bad_image:
235 * Set the loader error for this thread.
238 mono_loader_set_error_bad_image (char *msg)
240 MonoLoaderError *error;
242 if (mono_loader_get_last_error ())
245 error = g_new0 (MonoLoaderError, 1);
246 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
249 set_loader_error (error);
254 * mono_loader_get_last_error:
256 * Returns information about the last type load exception encountered by the loader, or
257 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
260 mono_loader_get_last_error (void)
262 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
266 * mono_loader_clear_error:
268 * Disposes any loader error messages on this thread
271 mono_loader_clear_error (void)
273 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
276 g_free (ex->class_name);
277 g_free (ex->assembly_name);
281 TlsSetValue (loader_error_thread_id, NULL);
286 * mono_loader_error_prepare_exception:
287 * @error: The MonoLoaderError to turn into an exception
289 * This turns a MonoLoaderError into an exception that can be thrown
290 * and resets the Mono Loader Error state during this process.
294 mono_loader_error_prepare_exception (MonoLoaderError *error)
296 MonoException *ex = NULL;
298 switch (error->exception_type) {
299 case MONO_EXCEPTION_TYPE_LOAD: {
300 char *cname = g_strdup (error->class_name);
301 char *aname = g_strdup (error->assembly_name);
302 MonoString *class_name;
304 mono_loader_clear_error ();
306 class_name = mono_string_new (mono_domain_get (), cname);
308 ex = mono_get_exception_type_load (class_name, aname);
313 case MONO_EXCEPTION_MISSING_METHOD: {
314 char *cname = g_strdup (error->class_name);
315 char *aname = g_strdup (error->member_name);
317 mono_loader_clear_error ();
318 ex = mono_get_exception_missing_method (cname, aname);
324 case MONO_EXCEPTION_MISSING_FIELD: {
325 char *cnspace = g_strdup ((error->klass && *error->klass->name_space) ? error->klass->name_space : "");
326 char *cname = g_strdup (error->klass ? error->klass->name : "");
327 char *cmembername = g_strdup (error->member_name);
330 mono_loader_clear_error ();
331 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
333 ex = mono_get_exception_missing_field (class_name, cmembername);
336 g_free (cmembername);
341 case MONO_EXCEPTION_FILE_NOT_FOUND: {
346 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);
348 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
349 filename = g_strdup (error->assembly_name);
350 /* Has to call this before calling anything which might call mono_class_init () */
351 mono_loader_clear_error ();
352 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
358 case MONO_EXCEPTION_BAD_IMAGE: {
359 char *msg = g_strdup (error->msg);
360 mono_loader_clear_error ();
361 ex = mono_get_exception_bad_image_format (msg);
367 g_assert_not_reached ();
374 * find_cached_memberref_sig:
376 * Return a cached copy of the memberref signature identified by SIG_IDX.
377 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
378 * A cache is needed since the type/signature parsing routines allocate everything
379 * from a mempool, so without a cache, multiple requests for the same signature would
380 * lead to unbounded memory growth. For normal methods/fields this is not a problem
381 * since the resulting methods/fields are cached, but inflated methods/fields cannot
383 * LOCKING: Acquires the loader lock.
386 find_cached_memberref_sig (MonoImage *image, guint32 sig_idx)
391 res = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
392 mono_loader_unlock ();
398 cache_memberref_sig (MonoImage *image, guint32 sig_idx, gpointer sig)
403 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
405 /* Somebody got in before us */
409 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (sig_idx), sig);
410 /* An approximation based on glib 2.18 */
411 memberref_sig_cache_size += sizeof (gpointer) * 4;
414 mono_loader_unlock ();
419 static MonoClassField*
420 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
421 MonoGenericContext *context)
424 MonoClassField *field;
425 MonoTableInfo *tables = image->tables;
428 guint32 nindex, class, class_table;
431 guint32 idx = mono_metadata_token_index (token);
433 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
434 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
435 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
437 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
439 if (!mono_verifier_verify_memberref_signature (image, cols [MONO_MEMBERREF_SIGNATURE], NULL)) {
440 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));
445 case MONO_MEMBERREF_PARENT_TYPEDEF:
446 class_table = MONO_TOKEN_TYPE_DEF;
447 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
449 case MONO_MEMBERREF_PARENT_TYPEREF:
450 class_table = MONO_TOKEN_TYPE_REF;
451 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
453 case MONO_MEMBERREF_PARENT_TYPESPEC:
454 class_table = MONO_TOKEN_TYPE_SPEC;
455 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
458 /*FIXME this must set a loader error!*/
459 g_warning ("field load from %x", class);
464 char *name = mono_class_name_from_token (image, class_table | nindex);
465 g_warning ("Missing field %s in class %s (type token %d)", fname, name, class_table | nindex);
466 mono_loader_set_error_type_load (name, image->assembly_name);
471 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
472 mono_metadata_decode_blob_size (ptr, &ptr);
473 /* we may want to check the signature here... */
476 g_warning ("Bad field signature class token %08x field name %s token %08x", class, fname, token);
477 mono_loader_set_error_field_load (klass, fname);
480 /* FIXME: This needs a cache, especially for generic instances, since
481 * mono_metadata_parse_type () allocates everything from a mempool.
483 sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
485 sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
486 if (sig_type == NULL) {
487 mono_loader_set_error_field_load (klass, fname);
490 sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
493 mono_class_init (klass); /*FIXME is this really necessary?*/
496 field = mono_class_get_field_from_name_full (klass, fname, sig_type);
499 mono_loader_set_error_field_load (klass, fname);
505 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
506 MonoGenericContext *context)
510 MonoClassField *field;
512 if (image->dynamic) {
513 MonoClassField *result;
514 MonoClass *handle_class;
517 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
518 // This checks the memberref type as well
519 if (!result || handle_class != mono_defaults.fieldhandle_class) {
520 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field token 0x%08x on image %s.", token, image->name));
523 *retklass = result->parent;
528 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
529 *retklass = field->parent;
530 mono_loader_unlock ();
533 mono_loader_unlock ();
535 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
536 field = field_from_memberref (image, token, retklass, context);
538 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
541 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
547 field = mono_class_get_field (k, token);
551 if (field && field->parent && !field->parent->generic_class && !field->parent->generic_container)
552 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
553 mono_loader_unlock ();
558 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
562 if (sig1->hasthis != sig2->hasthis ||
563 sig1->sentinelpos != sig2->sentinelpos)
566 for (i = 0; i < sig1->sentinelpos; i++) {
567 MonoType *p1 = sig1->params[i];
568 MonoType *p2 = sig2->params[i];
570 /*if (p1->attrs != p2->attrs)
573 if (!mono_metadata_type_equal (p1, p2))
577 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
583 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
584 MonoMethodSignature *sig, MonoClass *from_class)
588 /* Search directly in the metadata to avoid calling setup_methods () */
590 /* FIXME: !from_class->generic_class condition causes test failures. */
591 if (klass->type_token && !klass->image->dynamic && !klass->methods && !klass->rank && klass == from_class && !from_class->generic_class) {
592 for (i = 0; i < klass->method.count; ++i) {
593 guint32 cols [MONO_METHOD_SIZE];
596 MonoMethodSignature *other_sig;
598 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
600 m_name = mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]);
602 if (!((fqname && !strcmp (m_name, fqname)) ||
603 (qname && !strcmp (m_name, qname)) ||
604 (name && !strcmp (m_name, name))))
607 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
608 other_sig = mono_method_signature (method);
609 if (method && other_sig && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, other_sig))
614 mono_class_setup_methods (klass);
616 We can't fail lookup of methods otherwise the runtime will fail with MissingMethodException instead of TypeLoadException.
617 See mono/tests/generic-type-load-exception.2.il
618 FIXME we should better report this error to the caller
622 for (i = 0; i < klass->method.count; ++i) {
623 MonoMethod *m = klass->methods [i];
624 MonoMethodSignature *msig;
626 if (!((fqname && !strcmp (m->name, fqname)) ||
627 (qname && !strcmp (m->name, qname)) ||
628 (name && !strcmp (m->name, name))))
630 msig = mono_method_signature (m);
634 if (sig->call_convention == MONO_CALL_VARARG) {
635 if (mono_metadata_signature_vararg_match (sig, msig))
638 if (mono_metadata_signature_equal (sig, msig))
643 if (i < klass->method.count)
644 return mono_class_get_method_by_index (from_class, i);
649 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
652 char *qname, *fqname, *class_name;
653 gboolean is_interface;
654 MonoMethod *result = NULL;
656 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
659 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
661 qname = g_strconcat (class_name, ".", name, NULL);
662 if (ic->name_space && ic->name_space [0])
663 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
667 class_name = qname = fqname = NULL;
670 g_assert (from_class);
671 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
675 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
678 g_assert (from_class->interface_offsets_count == in_class->interface_offsets_count);
679 for (i = 0; i < in_class->interface_offsets_count; i++) {
680 MonoClass *in_ic = in_class->interfaces_packed [i];
681 MonoClass *from_ic = from_class->interfaces_packed [i];
682 char *ic_qname, *ic_fqname, *ic_class_name;
684 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
685 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
686 if (in_ic->name_space && in_ic->name_space [0])
687 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
691 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
692 g_free (ic_class_name);
699 in_class = in_class->parent;
700 from_class = from_class->parent;
702 g_assert (!in_class == !from_class);
705 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
714 static MonoMethodSignature*
715 inflate_generic_signature_checked (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context, MonoError *error)
717 MonoMethodSignature *res;
721 mono_error_init (error);
725 res = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)sig->param_count) * sizeof (MonoType*));
726 res->param_count = sig->param_count;
727 res->sentinelpos = -1;
728 res->ret = mono_class_inflate_generic_type_checked (sig->ret, context, error);
729 if (!mono_error_ok (error))
731 is_open = mono_class_is_open_constructed_type (res->ret);
732 for (i = 0; i < sig->param_count; ++i) {
733 res->params [i] = mono_class_inflate_generic_type_checked (sig->params [i], context, error);
734 if (!mono_error_ok (error))
738 is_open = mono_class_is_open_constructed_type (res->params [i]);
740 res->hasthis = sig->hasthis;
741 res->explicit_this = sig->explicit_this;
742 res->call_convention = sig->call_convention;
743 res->pinvoke = sig->pinvoke;
744 res->generic_param_count = sig->generic_param_count;
745 res->sentinelpos = sig->sentinelpos;
746 res->has_type_parameters = is_open;
747 res->is_inflated = 1;
752 mono_metadata_free_type (res->ret);
753 for (i = 0; i < sig->param_count; ++i) {
755 mono_metadata_free_type (res->params [i]);
761 static MonoMethodHeader*
762 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
764 MonoMethodHeader *res;
766 res = g_malloc0 (MONO_SIZEOF_METHOD_HEADER + sizeof (gpointer) * header->num_locals);
767 res->code = header->code;
768 res->code_size = header->code_size;
769 res->max_stack = header->max_stack;
770 res->num_clauses = header->num_clauses;
771 res->init_locals = header->init_locals;
772 res->num_locals = header->num_locals;
773 res->clauses = header->clauses;
774 for (i = 0; i < header->num_locals; ++i)
775 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
776 if (res->num_clauses) {
777 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
778 for (i = 0; i < header->num_clauses; ++i) {
779 MonoExceptionClause *clause = &res->clauses [i];
780 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
782 clause->data.catch_class = mono_class_inflate_generic_class (clause->data.catch_class, context);
789 * token is the method_ref/def/spec token used in a call IL instruction.
792 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
794 int table = mono_metadata_token_table (token);
795 int idx = mono_metadata_token_index (token);
797 guint32 cols [MONO_MEMBERREF_SIZE];
798 MonoMethodSignature *sig;
801 /* !table is for wrappers: we should really assign their own token to them */
802 if (!table || table == MONO_TABLE_METHOD)
803 return mono_method_signature (method);
805 if (table == MONO_TABLE_METHODSPEC) {
806 /* the verifier (do_invoke_method) will turn the NULL into a verifier error */
807 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || !method->is_inflated)
810 return mono_method_signature (method);
813 if (method->klass->generic_class)
814 return mono_method_signature (method);
816 if (image->dynamic) {
817 sig = mono_reflection_lookup_signature (image, method, token);
819 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
820 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
822 sig = find_cached_memberref_sig (image, sig_idx);
824 if (!mono_verifier_verify_memberref_signature (image, sig_idx, NULL)) {
825 guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
826 const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
828 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));
832 ptr = mono_metadata_blob_heap (image, sig_idx);
833 mono_metadata_decode_blob_size (ptr, &ptr);
834 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
837 sig = cache_memberref_sig (image, sig_idx, sig);
839 /* FIXME: we probably should verify signature compat in the dynamic case too*/
840 if (!mono_verifier_is_sig_compatible (image, method, sig)) {
841 guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
842 const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
844 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));
852 MonoMethodSignature *cached;
854 /* This signature is not owned by a MonoMethod, so need to cache */
855 sig = inflate_generic_signature_checked (image, sig, context, &error);
856 if (!mono_error_ok (&error)) {/*XXX bubble up this and kill one use of loader errors */
857 mono_loader_set_error_bad_image (g_strdup_printf ("Could not inflate signature %s", mono_error_get_message (&error)));
858 mono_error_cleanup (&error);
862 cached = mono_metadata_get_inflated_signature (sig, context);
864 mono_metadata_free_inflated_signature (sig);
866 inflated_signatures_size += mono_metadata_signature_size (cached);
874 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
876 return mono_method_get_signature_full (method, image, token, NULL);
879 /* this is only for the typespec array methods */
881 mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
885 mono_class_setup_methods (klass);
886 g_assert (!klass->exception_type); /*FIXME this should not fail, right?*/
887 for (i = 0; i < klass->method.count; ++i) {
888 MonoMethod *method = klass->methods [i];
889 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
896 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
897 gboolean *used_context)
899 MonoClass *klass = NULL;
900 MonoMethod *method = NULL;
901 MonoTableInfo *tables = image->tables;
903 guint32 nindex, class, sig_idx;
905 MonoMethodSignature *sig;
908 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
909 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
910 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
911 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
912 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
914 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
917 * Whether we actually used the `typespec_context' or not.
918 * This is used to tell our caller whether or not it's safe to insert the returned
919 * method into a cache.
922 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
925 case MONO_MEMBERREF_PARENT_TYPEREF:
926 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
928 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
929 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
930 mono_loader_set_error_type_load (name, image->assembly_name);
935 case MONO_MEMBERREF_PARENT_TYPESPEC:
937 * Parse the TYPESPEC in the parent's context.
939 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
941 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
942 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
943 mono_loader_set_error_type_load (name, image->assembly_name);
948 case MONO_MEMBERREF_PARENT_TYPEDEF:
949 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
951 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
952 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
953 mono_loader_set_error_type_load (name, image->assembly_name);
958 case MONO_MEMBERREF_PARENT_METHODDEF:
959 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
963 /* This message leaks */
964 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
965 mono_loader_set_error_method_load ("", message);
971 mono_class_init (klass);
973 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
975 if (!mono_verifier_verify_memberref_signature (image, sig_idx, NULL)) {
976 mono_loader_set_error_method_load (klass->name, mname);
980 ptr = mono_metadata_blob_heap (image, sig_idx);
981 mono_metadata_decode_blob_size (ptr, &ptr);
983 sig = find_cached_memberref_sig (image, sig_idx);
985 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
989 sig = cache_memberref_sig (image, sig_idx, sig);
993 case MONO_MEMBERREF_PARENT_TYPEREF:
994 case MONO_MEMBERREF_PARENT_TYPEDEF:
995 method = find_method (klass, NULL, mname, sig, klass);
998 case MONO_MEMBERREF_PARENT_TYPESPEC: {
1001 type = &klass->byval_arg;
1003 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
1004 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
1005 method = find_method (in_class, NULL, mname, sig, klass);
1009 /* we're an array and we created these methods already in klass in mono_class_init () */
1010 method = mono_method_search_in_array_class (klass, mname, sig);
1014 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
1015 g_assert_not_reached ();
1019 char *msig = mono_signature_get_desc (sig, FALSE);
1020 char * class_name = mono_type_get_name (&klass->byval_arg);
1021 GString *s = g_string_new (mname);
1022 if (sig->generic_param_count)
1023 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
1024 g_string_append_printf (s, "(%s)", msig);
1026 msig = g_string_free (s, FALSE);
1029 "Missing method %s::%s in assembly %s, referenced in assembly %s",
1030 class_name, msig, klass->image->name, image->name);
1031 mono_loader_set_error_method_load (class_name, mname);
1033 g_free (class_name);
1040 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
1045 MonoTableInfo *tables = image->tables;
1046 MonoGenericContext new_context;
1047 MonoGenericInst *inst;
1049 guint32 cols [MONO_METHODSPEC_SIZE];
1050 guint32 token, nindex, param_count;
1052 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
1053 token = cols [MONO_METHODSPEC_METHOD];
1054 nindex = token >> MONO_METHODDEFORREF_BITS;
1056 if (!mono_verifier_verify_methodspec_signature (image, cols [MONO_METHODSPEC_SIGNATURE], NULL))
1059 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
1061 mono_metadata_decode_value (ptr, &ptr);
1063 param_count = mono_metadata_decode_value (ptr, &ptr);
1064 g_assert (param_count);
1066 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
1067 if (context && inst->is_open) {
1068 inst = mono_metadata_inflate_generic_inst (inst, context, &error);
1069 if (!mono_error_ok (&error)) {
1070 mono_error_cleanup (&error); /*FIXME don't swallow error message.*/
1075 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
1076 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
1078 method = method_from_memberref (image, nindex, context, NULL);
1083 klass = method->klass;
1085 if (klass->generic_class) {
1086 g_assert (method->is_inflated);
1087 method = ((MonoMethodInflated *) method)->declaring;
1090 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
1091 new_context.method_inst = inst;
1093 return mono_class_inflate_generic_method_full (method, klass, &new_context);
1096 struct _MonoDllMap {
1104 static MonoDllMap *global_dll_map;
1107 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
1115 mono_loader_lock ();
1118 * we use the first entry we find that matches, since entries from
1119 * the config file are prepended to the list and we document that the
1120 * later entries win.
1122 for (; dll_map; dll_map = dll_map->next) {
1123 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
1124 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
1126 } else if (strcmp (dll_map->dll, dll)) {
1129 if (!found && dll_map->target) {
1130 *rdll = dll_map->target;
1132 /* we don't quit here, because we could find a full
1133 * entry that matches also function and that has priority.
1136 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
1137 *rfunc = dll_map->target_func;
1142 mono_loader_unlock ();
1147 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
1150 if (assembly && assembly->dll_map) {
1151 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
1155 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
1159 * mono_dllmap_insert:
1161 * LOCKING: Acquires the loader lock.
1163 * NOTE: This can be called before the runtime is initialized, for example from
1164 * mono_config_parse ().
1167 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc)
1171 mono_loader_init ();
1173 mono_loader_lock ();
1176 entry = g_malloc0 (sizeof (MonoDllMap));
1177 entry->dll = dll? g_strdup (dll): NULL;
1178 entry->target = tdll? g_strdup (tdll): NULL;
1179 entry->func = func? g_strdup (func): NULL;
1180 entry->target_func = tfunc? g_strdup (tfunc): NULL;
1181 entry->next = global_dll_map;
1182 global_dll_map = entry;
1184 entry = mono_image_alloc0 (assembly, sizeof (MonoDllMap));
1185 entry->dll = dll? mono_image_strdup (assembly, dll): NULL;
1186 entry->target = tdll? mono_image_strdup (assembly, tdll): NULL;
1187 entry->func = func? mono_image_strdup (assembly, func): NULL;
1188 entry->target_func = tfunc? mono_image_strdup (assembly, tfunc): NULL;
1189 entry->next = assembly->dll_map;
1190 assembly->dll_map = entry;
1193 mono_loader_unlock ();
1197 free_dllmap (MonoDllMap *map)
1200 MonoDllMap *next = map->next;
1203 g_free (map->target);
1205 g_free (map->target_func);
1212 dllmap_cleanup (void)
1214 free_dllmap (global_dll_map);
1215 global_dll_map = NULL;
1218 static GHashTable *global_module_map;
1221 cached_module_load (const char *name, int flags, char **err)
1227 mono_loader_lock ();
1228 if (!global_module_map)
1229 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1230 res = g_hash_table_lookup (global_module_map, name);
1232 mono_loader_unlock ();
1235 res = mono_dl_open (name, flags, NULL);
1237 g_hash_table_insert (global_module_map, g_strdup (name), res);
1238 mono_loader_unlock ();
1243 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1245 MonoImage *image = method->klass->image;
1246 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1247 MonoTableInfo *tables = image->tables;
1248 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1249 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1250 guint32 im_cols [MONO_IMPLMAP_SIZE];
1251 guint32 scope_token;
1252 const char *import = NULL;
1253 const char *orig_scope;
1254 const char *new_scope;
1256 char *full_name, *file_name;
1258 MonoDl *module = NULL;
1260 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1263 return piinfo->addr;
1265 if (method->klass->image->dynamic) {
1266 MonoReflectionMethodAux *method_aux =
1267 g_hash_table_lookup (
1268 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1272 import = method_aux->dllentry;
1273 orig_scope = method_aux->dll;
1276 if (!piinfo->implmap_idx || piinfo->implmap_idx > im->rows)
1279 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1281 if (!im_cols [MONO_IMPLMAP_SCOPE] || im_cols [MONO_IMPLMAP_SCOPE] > mr->rows)
1284 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1285 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1286 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1287 orig_scope = mono_metadata_string_heap (image, scope_token);
1290 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1292 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1293 "DllImport attempting to load: '%s'.", new_scope);
1300 /* we allow a special name to dlopen from the running process namespace */
1301 if (strcmp (new_scope, "__Internal") == 0)
1302 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1305 * Try loading the module using a variety of names
1307 for (i = 0; i < 4; ++i) {
1310 /* Try the original name */
1311 file_name = g_strdup (new_scope);
1314 /* Try trimming the .dll extension */
1315 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1316 file_name = g_strdup (new_scope);
1317 file_name [strlen (new_scope) - 4] = '\0';
1323 if (strstr (new_scope, "lib") != new_scope) {
1324 file_name = g_strdup_printf ("lib%s", new_scope);
1330 #ifndef TARGET_WIN32
1331 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1332 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1333 !g_ascii_strcasecmp ("user32", new_scope) ||
1334 !g_ascii_strcasecmp ("kernel", new_scope)) {
1335 file_name = g_strdup ("libMonoSupportW.so");
1339 #ifndef TARGET_WIN32
1346 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1347 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1348 "DllImport loading location: '%s'.", full_name);
1349 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1351 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1352 "DllImport error loading library: '%s'.",
1364 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1365 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1366 "DllImport loading library: '%s'.", full_name);
1367 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1369 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1370 "DllImport error loading library '%s'.",
1381 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1382 "DllImport loading: '%s'.", file_name);
1383 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1385 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1386 "DllImport error loading library '%s'.",
1398 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1399 "DllImport unable to load library '%s'.",
1404 *exc_class = "DllNotFoundException";
1405 *exc_arg = new_scope;
1411 if (import && import [0] == '#' && isdigit (import [1])) {
1415 id = strtol (import + 1, &end, 10);
1416 if (id > 0 && *end == '\0')
1420 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1421 "Searching for '%s'.", import);
1423 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1424 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1426 char *mangled_name = NULL, *mangled_name2 = NULL;
1429 int mangle_param_count;
1435 * Search using a variety of mangled names
1437 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1438 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1439 gboolean need_param_count = FALSE;
1441 if (mangle_stdcall > 0)
1442 need_param_count = TRUE;
1444 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1449 mangled_name = (char*)import;
1450 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1451 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1452 /* Try the mangled name first */
1453 if (mangle_charset == 0)
1454 mangled_name = g_strconcat (import, "W", NULL);
1456 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1458 if (mangle_charset == 0)
1459 mangled_name = g_strconcat (import, "W", NULL);
1461 /* Try the mangled name last */
1462 if (mangle_charset == 1)
1463 mangled_name = g_strconcat (import, "A", NULL);
1466 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1468 /* Try the mangled name last */
1469 if (mangle_charset == 1)
1470 mangled_name = g_strconcat (import, "A", NULL);
1475 if (mangle_param_count == 0)
1476 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1478 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1479 param_count = mangle_param_count;
1481 /* Try the stdcall mangled name */
1483 * gcc under windows creates mangled names without the underscore, but MS.NET
1484 * doesn't support it, so we doesn't support it either.
1486 if (mangle_stdcall == 1)
1487 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1489 mangled_name2 = mangled_name;
1491 mangled_name2 = mangled_name;
1494 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1495 "Probing '%s'.", mangled_name2);
1497 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1500 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1501 "Found as '%s'.", mangled_name2);
1503 if (mangled_name != mangled_name2)
1504 g_free (mangled_name2);
1505 if (mangled_name != import)
1506 g_free (mangled_name);
1512 if (!piinfo->addr) {
1515 *exc_class = "EntryPointNotFoundException";
1520 return piinfo->addr;
1524 * LOCKING: assumes the loader lock to be taken.
1527 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1528 MonoGenericContext *context, gboolean *used_context)
1531 int table = mono_metadata_token_table (token);
1532 int idx = mono_metadata_token_index (token);
1533 MonoTableInfo *tables = image->tables;
1534 MonoGenericContainer *generic_container = NULL, *container = NULL;
1535 const char *sig = NULL;
1537 guint32 cols [MONO_TYPEDEF_SIZE];
1539 if (image->dynamic) {
1540 MonoClass *handle_class;
1542 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1543 // This checks the memberref type as well
1544 if (result && handle_class != mono_defaults.methodhandle_class) {
1545 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1551 if (table != MONO_TABLE_METHOD) {
1552 if (table == MONO_TABLE_METHODSPEC) {
1553 if (used_context) *used_context = TRUE;
1554 return method_from_methodspec (image, context, idx);
1556 if (table != MONO_TABLE_MEMBERREF) {
1557 g_warning ("got wrong token: 0x%08x\n", token);
1558 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1561 return method_from_memberref (image, idx, context, used_context);
1564 if (used_context) *used_context = FALSE;
1566 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1567 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1571 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1573 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1574 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1575 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
1577 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethod));
1579 mono_stats.method_count ++;
1581 if (!klass) { /*FIXME put this before the image alloc*/
1582 guint32 type = mono_metadata_typedef_from_method (image, token);
1585 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1591 result->klass = klass;
1592 result->flags = cols [2];
1593 result->iflags = cols [1];
1594 result->token = token;
1595 result->name = mono_metadata_string_heap (image, cols [3]);
1597 if (!sig) /* already taken from the methodref */
1598 sig = mono_metadata_blob_heap (image, cols [4]);
1599 size = mono_metadata_decode_blob_size (sig, &sig);
1601 container = klass->generic_container;
1604 * load_generic_params does a binary search so only call it if the method
1608 generic_container = mono_metadata_load_generic_params (image, token, container);
1609 if (generic_container) {
1610 result->is_generic = TRUE;
1611 generic_container->owner.method = result;
1612 /*FIXME put this before the image alloc*/
1613 if (!mono_metadata_load_generic_param_constraints_full (image, token, generic_container))
1616 container = generic_container;
1619 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1620 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1621 result->string_ctor = 1;
1622 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1623 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1626 /* IJW is P/Invoke with a predefined function pointer. */
1627 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1628 piinfo->addr = mono_image_rva_map (image, cols [0]);
1629 g_assert (piinfo->addr);
1632 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1633 /* Native methods can have no map. */
1634 if (piinfo->implmap_idx)
1635 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1638 if (generic_container)
1639 mono_method_set_generic_container (result, generic_container);
1645 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1647 return mono_get_method_full (image, token, klass, NULL);
1651 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1652 MonoGenericContext *context)
1655 gboolean used_context = FALSE;
1657 /* We do everything inside the lock to prevent creation races */
1659 mono_image_lock (image);
1661 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1662 if (!image->method_cache)
1663 image->method_cache = g_hash_table_new (NULL, NULL);
1664 result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
1666 if (!image->methodref_cache)
1667 image->methodref_cache = g_hash_table_new (NULL, NULL);
1668 result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1670 mono_image_unlock (image);
1675 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1679 mono_image_lock (image);
1680 if (!used_context && !result->is_inflated) {
1681 MonoMethod *result2;
1682 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1683 result2 = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
1685 result2 = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1688 mono_image_unlock (image);
1692 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1693 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)), result);
1695 g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
1698 mono_image_unlock (image);
1704 * mono_get_method_constrained:
1706 * This is used when JITing the `constrained.' opcode.
1708 * This returns two values: the contrained method, which has been inflated
1709 * as the function return value; And the original CIL-stream method as
1710 * declared in cil_method. The later is used for verification.
1713 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1714 MonoGenericContext *context, MonoMethod **cil_method)
1716 MonoMethod *method, *result;
1717 MonoClass *ic = NULL;
1718 MonoGenericContext *method_context = NULL;
1719 MonoMethodSignature *sig, *original_sig;
1721 mono_loader_lock ();
1723 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1725 mono_loader_unlock ();
1729 mono_class_init (constrained_class);
1730 method = *cil_method;
1731 original_sig = sig = mono_method_signature (method);
1733 mono_loader_unlock ();
1737 if (method->is_inflated && sig->generic_param_count) {
1738 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1739 sig = mono_method_signature (imethod->declaring); /*We assume that if the inflated method signature is valid, the declaring method is too*/
1740 method_context = mono_method_get_context (method);
1744 * We must inflate the signature with the class instantiation to work on
1745 * cases where a class inherit from a generic type and the override replaces
1746 * any type argument which a concrete type. See #325283.
1748 if (method_context->class_inst) {
1750 MonoGenericContext ctx;
1751 ctx.method_inst = NULL;
1752 ctx.class_inst = method_context->class_inst;
1753 /*Fixme, property propagate this error*/
1754 sig = inflate_generic_signature_checked (method->klass->image, sig, &ctx, &error);
1755 if (!mono_error_ok (&error)) {
1756 mono_loader_unlock ();
1757 mono_error_cleanup (&error);
1763 if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
1766 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1767 if (sig != original_sig)
1768 mono_metadata_free_inflated_signature (sig);
1771 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1772 method->klass->name, method->name, image->name, token);
1773 mono_loader_unlock ();
1778 result = mono_class_inflate_generic_method (result, method_context);
1780 mono_loader_unlock ();
1785 mono_free_method (MonoMethod *method)
1787 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1788 mono_profiler_method_free (method);
1790 /* FIXME: This hack will go away when the profiler will support freeing methods */
1791 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1794 if (method->signature) {
1796 * FIXME: This causes crashes because the types inside signatures and
1797 * locals are shared.
1799 /* mono_metadata_free_method_signature (method->signature); */
1800 /* g_free (method->signature); */
1803 if (method->dynamic) {
1804 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1807 mono_marshal_free_dynamic_wrappers (method);
1809 mono_image_property_remove (method->klass->image, method);
1811 g_free ((char*)method->name);
1813 g_free ((char*)mw->header->code);
1814 for (i = 0; i < mw->header->num_locals; ++i)
1815 g_free (mw->header->locals [i]);
1816 g_free (mw->header->clauses);
1817 g_free (mw->header);
1819 g_free (mw->method_data);
1820 g_free (method->signature);
1826 mono_method_get_param_names (MonoMethod *method, const char **names)
1830 MonoTableInfo *methodt;
1831 MonoTableInfo *paramt;
1832 MonoMethodSignature *signature;
1835 if (method->is_inflated)
1836 method = ((MonoMethodInflated *) method)->declaring;
1838 signature = mono_method_signature (method);
1839 /*FIXME this check is somewhat redundant since the caller usally will have to get the signature to figure out the
1840 number of arguments and allocate a properly sized array. */
1841 if (signature == NULL)
1844 if (!signature->param_count)
1847 for (i = 0; i < signature->param_count; ++i)
1850 klass = method->klass;
1854 mono_class_init (klass);
1856 if (klass->image->dynamic) {
1857 MonoReflectionMethodAux *method_aux =
1858 g_hash_table_lookup (
1859 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1860 if (method_aux && method_aux->param_names) {
1861 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1862 if (method_aux->param_names [i + 1])
1863 names [i] = method_aux->param_names [i + 1];
1868 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1869 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1870 idx = mono_method_get_index (method);
1872 guint32 cols [MONO_PARAM_SIZE];
1875 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1877 if (idx < methodt->rows)
1878 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1880 lastp = paramt->rows + 1;
1881 for (i = param_index; i < lastp; ++i) {
1882 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1883 if (cols [MONO_PARAM_SEQUENCE] && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) /* skip return param spec and bounds check*/
1884 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1890 mono_method_get_param_token (MonoMethod *method, int index)
1892 MonoClass *klass = method->klass;
1893 MonoTableInfo *methodt;
1896 mono_class_init (klass);
1898 if (klass->image->dynamic) {
1899 g_assert_not_reached ();
1902 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1903 idx = mono_method_get_index (method);
1905 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1909 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1911 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1918 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1921 MonoClass *klass = method->klass;
1922 MonoTableInfo *methodt;
1923 MonoTableInfo *paramt;
1924 MonoMethodSignature *signature;
1927 signature = mono_method_signature (method);
1928 g_assert (signature); /*FIXME there is no way to signal error from this function*/
1930 for (i = 0; i < signature->param_count + 1; ++i)
1933 if (method->klass->image->dynamic) {
1934 MonoReflectionMethodAux *method_aux =
1935 g_hash_table_lookup (
1936 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1937 if (method_aux && method_aux->param_marshall) {
1938 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1939 for (i = 0; i < signature->param_count + 1; ++i)
1940 if (dyn_specs [i]) {
1941 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1942 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1943 mspecs [i]->data.custom_data.custom_name = g_strdup (dyn_specs [i]->data.custom_data.custom_name);
1944 mspecs [i]->data.custom_data.cookie = g_strdup (dyn_specs [i]->data.custom_data.cookie);
1950 mono_class_init (klass);
1952 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1953 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1954 idx = mono_method_get_index (method);
1956 guint32 cols [MONO_PARAM_SIZE];
1957 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1959 if (idx < methodt->rows)
1960 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1962 lastp = paramt->rows + 1;
1964 for (i = param_index; i < lastp; ++i) {
1965 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1967 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) {
1969 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1971 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1980 mono_method_has_marshal_info (MonoMethod *method)
1983 MonoClass *klass = method->klass;
1984 MonoTableInfo *methodt;
1985 MonoTableInfo *paramt;
1988 if (method->klass->image->dynamic) {
1989 MonoReflectionMethodAux *method_aux =
1990 g_hash_table_lookup (
1991 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1992 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1994 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
2001 mono_class_init (klass);
2003 methodt = &klass->image->tables [MONO_TABLE_METHOD];
2004 paramt = &klass->image->tables [MONO_TABLE_PARAM];
2005 idx = mono_method_get_index (method);
2007 guint32 cols [MONO_PARAM_SIZE];
2008 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
2010 if (idx + 1 < methodt->rows)
2011 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
2013 lastp = paramt->rows + 1;
2015 for (i = param_index; i < lastp; ++i) {
2016 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
2018 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
2027 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
2030 g_assert (method != NULL);
2031 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
2033 if (method->is_inflated)
2034 method = ((MonoMethodInflated *) method)->declaring;
2035 data = ((MonoMethodWrapper *)method)->method_data;
2036 g_assert (data != NULL);
2037 g_assert (id <= GPOINTER_TO_UINT (*data));
2042 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
2043 g_error ("stack walk not installed");
2046 static MonoStackWalkImpl stack_walk = default_stack_walk;
2049 mono_stack_walk (MonoStackWalk func, gpointer user_data)
2051 stack_walk (func, TRUE, user_data);
2055 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
2057 stack_walk (func, FALSE, user_data);
2061 mono_install_stack_walk (MonoStackWalkImpl func)
2067 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
2069 MonoMethod **dest = data;
2071 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
2077 mono_method_get_last_managed (void)
2079 MonoMethod *m = NULL;
2080 stack_walk (last_managed, FALSE, &m);
2084 static gboolean loader_lock_track_ownership = FALSE;
2089 * See docs/thread-safety.txt for the locking strategy.
2092 mono_loader_lock (void)
2094 mono_locks_acquire (&loader_mutex, LoaderLock);
2095 if (G_UNLIKELY (loader_lock_track_ownership)) {
2096 TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) + 1));
2101 mono_loader_unlock (void)
2103 mono_locks_release (&loader_mutex, LoaderLock);
2104 if (G_UNLIKELY (loader_lock_track_ownership)) {
2105 TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) - 1));
2110 * mono_loader_lock_track_ownership:
2112 * Set whenever the runtime should track ownership of the loader lock. If set to TRUE,
2113 * the mono_loader_lock_is_owned_by_self () can be called to query whenever the current
2114 * thread owns the loader lock.
2117 mono_loader_lock_track_ownership (gboolean track)
2119 loader_lock_track_ownership = track;
2123 * mono_loader_lock_is_owned_by_self:
2125 * Return whenever the current thread owns the loader lock.
2126 * This is useful to avoid blocking operations while holding the loader lock.
2129 mono_loader_lock_is_owned_by_self (void)
2131 g_assert (loader_lock_track_ownership);
2133 return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) > 0;
2137 * mono_loader_lock_if_inited:
2139 * Acquire the loader lock if it has been initialized, no-op otherwise. This can
2140 * be used in runtime initialization code which can be executed before mono_loader_init ().
2143 mono_loader_lock_if_inited (void)
2145 if (loader_lock_inited)
2146 mono_loader_lock ();
2150 mono_loader_unlock_if_inited (void)
2152 if (loader_lock_inited)
2153 mono_loader_unlock ();
2157 * mono_method_signature:
2159 * Return the signature of the method M. On failure, returns NULL, and ERR is set.
2161 MonoMethodSignature*
2162 mono_method_signature_checked (MonoMethod *m, MonoError *error)
2168 gboolean can_cache_signature;
2169 MonoGenericContainer *container;
2170 MonoMethodSignature *signature = NULL;
2173 /* We need memory barriers below because of the double-checked locking pattern */
2175 mono_error_init (error);
2178 return m->signature;
2180 mono_loader_lock ();
2183 mono_loader_unlock ();
2184 return m->signature;
2187 if (m->is_inflated) {
2188 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
2189 /* the lock is recursive */
2190 signature = mono_method_signature (imethod->declaring);
2191 signature = inflate_generic_signature_checked (imethod->declaring->klass->image, signature, mono_method_get_context (m), error);
2192 if (!mono_error_ok (error)) {
2193 mono_loader_unlock ();
2197 inflated_signatures_size += mono_metadata_signature_size (signature);
2199 mono_memory_barrier ();
2200 m->signature = signature;
2201 mono_loader_unlock ();
2202 return m->signature;
2205 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
2206 idx = mono_metadata_token_index (m->token);
2207 img = m->klass->image;
2209 sig = mono_metadata_blob_heap (img, sig_offset = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
2211 g_assert (!m->klass->generic_class);
2212 container = mono_method_get_generic_container (m);
2214 container = m->klass->generic_container;
2216 /* Generic signatures depend on the container so they cannot be cached */
2217 /* icall/pinvoke signatures cannot be cached cause we modify them below */
2218 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
2220 /* If the method has parameter attributes, that can modify the signature */
2221 if (mono_metadata_method_has_param_attrs (img, idx))
2222 can_cache_signature = FALSE;
2224 if (can_cache_signature)
2225 signature = g_hash_table_lookup (img->method_signatures, sig);
2228 const char *sig_body;
2229 /*TODO we should cache the failure result somewhere*/
2230 if (!mono_verifier_verify_method_signature (img, sig_offset, error)) {
2231 mono_loader_unlock ();
2235 size = mono_metadata_decode_blob_size (sig, &sig_body);
2237 signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
2239 mono_loader_unlock ();
2240 mono_error_set_method_load (error, m->klass, m->name, "");
2244 if (can_cache_signature)
2245 g_hash_table_insert (img->method_signatures, (gpointer)sig, signature);
2248 /* Verify metadata consistency */
2249 if (signature->generic_param_count) {
2250 if (!container || !container->is_method) {
2251 mono_loader_unlock ();
2252 mono_error_set_method_load (error, 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);
2255 if (container->type_argc != signature->generic_param_count) {
2256 mono_loader_unlock ();
2257 mono_error_set_method_load (error, 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);
2260 } else if (container && container->is_method && container->type_argc) {
2261 mono_loader_unlock ();
2262 mono_error_set_method_load (error, 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);
2265 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
2266 signature->pinvoke = 1;
2267 else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2268 MonoCallConvention conv = 0;
2269 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
2270 signature->pinvoke = 1;
2272 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
2273 case 0: /* no call conv, so using default */
2274 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
2275 conv = MONO_CALL_DEFAULT;
2277 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
2280 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
2281 conv = MONO_CALL_STDCALL;
2283 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
2284 conv = MONO_CALL_THISCALL;
2286 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
2287 conv = MONO_CALL_FASTCALL;
2289 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
2290 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
2292 mono_loader_unlock ();
2293 mono_error_set_method_load (error, m->klass, m->name, "unsupported calling convention : 0x%04x for method 0x%08x from image %s", piinfo->piflags, idx, img->name);
2296 signature->call_convention = conv;
2299 mono_memory_barrier ();
2300 m->signature = signature;
2302 mono_loader_unlock ();
2303 return m->signature;
2307 * mono_method_signature:
2309 * Return the signature of the method M. On failure, returns NULL.
2311 MonoMethodSignature*
2312 mono_method_signature (MonoMethod *m)
2315 MonoMethodSignature *sig;
2317 sig = mono_method_signature_checked (m, &error);
2319 char *type_name = mono_type_get_full_name (m->klass);
2320 g_warning ("Could not load signature of %s:%s due to: %s", type_name, m->name, mono_error_get_message (&error));
2322 mono_error_cleanup (&error);
2329 mono_method_get_name (MonoMethod *method)
2331 return method->name;
2335 mono_method_get_class (MonoMethod *method)
2337 return method->klass;
2341 mono_method_get_token (MonoMethod *method)
2343 return method->token;
2347 mono_method_get_header (MonoMethod *method)
2353 MonoMethodHeader *header;
2355 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))
2358 if (method->is_inflated) {
2359 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
2360 MonoMethodHeader *header;
2362 mono_loader_lock ();
2364 if (imethod->header) {
2365 mono_loader_unlock ();
2366 return imethod->header;
2369 header = mono_method_get_header (imethod->declaring);
2371 mono_loader_unlock ();
2375 imethod->header = inflate_generic_header (header, mono_method_get_context (method));
2376 mono_loader_unlock ();
2377 mono_metadata_free_mh (header);
2378 return imethod->header;
2381 if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
2382 MonoMethodWrapper *mw = (MonoMethodWrapper *)method;
2383 g_assert (mw->header);
2388 * We don't need locks here: the new header is allocated from malloc memory
2389 * and is not stored anywhere in the runtime, the user needs to free it.
2391 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
2392 idx = mono_metadata_token_index (method->token);
2393 img = method->klass->image;
2394 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
2396 if (!mono_verifier_verify_method_header (img, rva, NULL))
2399 loc = mono_image_rva_map (img, rva);
2403 header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
2409 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
2412 *iflags = method->iflags;
2413 return method->flags;
2417 * Find the method index in the metadata methodDef table.
2420 mono_method_get_index (MonoMethod *method) {
2421 MonoClass *klass = method->klass;
2425 /* constructed array methods are not in the MethodDef table */
2429 return mono_metadata_token_index (method->token);
2431 mono_class_setup_methods (klass);
2432 if (klass->exception_type)
2434 for (i = 0; i < klass->method.count; ++i) {
2435 if (method == klass->methods [i]) {
2436 if (klass->image->uncompressed_metadata)
2437 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2439 return klass->method.first + i + 1;