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;
62 static guint32 methods_size;
63 static guint32 signatures_size;
66 * This TLS variable contains the last type load error encountered by the loader.
68 guint32 loader_error_thread_id;
71 * This TLS variable holds how many times the current thread has acquired the loader
74 guint32 loader_lock_nest_id;
76 static void dllmap_cleanup (void);
81 static gboolean inited;
84 InitializeCriticalSection (&loader_mutex);
85 loader_lock_inited = TRUE;
87 loader_error_thread_id = TlsAlloc ();
88 loader_lock_nest_id = TlsAlloc ();
90 mono_counters_register ("Inflated signatures size",
91 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
92 mono_counters_register ("Memberref signature cache size",
93 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &memberref_sig_cache_size);
94 mono_counters_register ("MonoMethod size",
95 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &methods_size);
96 mono_counters_register ("MonoMethodSignature size",
97 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &signatures_size);
104 mono_loader_cleanup (void)
108 TlsFree (loader_error_thread_id);
109 TlsFree (loader_lock_nest_id);
111 DeleteCriticalSection (&loader_mutex);
112 loader_lock_inited = FALSE;
116 * Handling of type load errors should be done as follows:
118 * If something could not be loaded, the loader should call one of the
119 * mono_loader_set_error_XXX functions ()
120 * with the appropriate arguments, then return NULL to report the failure. The error
121 * should be propagated until it reaches code which can throw managed exceptions. At that
122 * point, an exception should be thrown based on the information returned by
123 * mono_loader_get_last_error (). Then the error should be cleared by calling
124 * mono_loader_clear_error ().
128 set_loader_error (MonoLoaderError *error)
130 TlsSetValue (loader_error_thread_id, error);
134 * mono_loader_set_error_assembly_load:
136 * Set the loader error for this thread.
139 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
141 MonoLoaderError *error;
143 if (mono_loader_get_last_error ())
146 error = g_new0 (MonoLoaderError, 1);
147 error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
148 error->assembly_name = g_strdup (assembly_name);
149 error->ref_only = ref_only;
152 * This is not strictly needed, but some (most) of the loader code still
153 * can't deal with load errors, and this message is more helpful than an
157 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "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);
159 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
161 set_loader_error (error);
165 * mono_loader_set_error_type_load:
167 * Set the loader error for this thread.
170 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
172 MonoLoaderError *error;
174 if (mono_loader_get_last_error ())
177 error = g_new0 (MonoLoaderError, 1);
178 error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
179 error->class_name = g_strdup (class_name);
180 error->assembly_name = g_strdup (assembly_name);
183 * This is not strictly needed, but some (most) of the loader code still
184 * can't deal with load errors, and this message is more helpful than an
187 mono_trace_warning (MONO_TRACE_TYPE, "The class %s could not be loaded, used in %s", class_name, assembly_name);
189 set_loader_error (error);
193 * mono_loader_set_error_method_load:
195 * Set the loader error for this thread. MEMBER_NAME should point to a string
199 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
201 MonoLoaderError *error;
203 /* FIXME: Store the signature as well */
204 if (mono_loader_get_last_error ())
207 error = g_new0 (MonoLoaderError, 1);
208 error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
209 error->class_name = g_strdup (class_name);
210 error->member_name = member_name;
212 set_loader_error (error);
216 * mono_loader_set_error_field_load:
218 * Set the loader error for this thread. MEMBER_NAME should point to a string
222 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
224 MonoLoaderError *error;
226 /* FIXME: Store the signature as well */
227 if (mono_loader_get_last_error ())
230 error = g_new0 (MonoLoaderError, 1);
231 error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
232 error->klass = klass;
233 error->member_name = member_name;
235 set_loader_error (error);
239 * mono_loader_set_error_bad_image:
241 * Set the loader error for this thread.
244 mono_loader_set_error_bad_image (char *msg)
246 MonoLoaderError *error;
248 if (mono_loader_get_last_error ())
251 error = g_new0 (MonoLoaderError, 1);
252 error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
255 set_loader_error (error);
260 * mono_loader_get_last_error:
262 * Returns information about the last type load exception encountered by the loader, or
263 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
266 mono_loader_get_last_error (void)
268 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
272 * mono_loader_clear_error:
274 * Disposes any loader error messages on this thread
277 mono_loader_clear_error (void)
279 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
282 g_free (ex->class_name);
283 g_free (ex->assembly_name);
287 TlsSetValue (loader_error_thread_id, NULL);
292 * mono_loader_error_prepare_exception:
293 * @error: The MonoLoaderError to turn into an exception
295 * This turns a MonoLoaderError into an exception that can be thrown
296 * and resets the Mono Loader Error state during this process.
300 mono_loader_error_prepare_exception (MonoLoaderError *error)
302 MonoException *ex = NULL;
304 switch (error->exception_type) {
305 case MONO_EXCEPTION_TYPE_LOAD: {
306 char *cname = g_strdup (error->class_name);
307 char *aname = g_strdup (error->assembly_name);
308 MonoString *class_name;
310 mono_loader_clear_error ();
312 class_name = mono_string_new (mono_domain_get (), cname);
314 ex = mono_get_exception_type_load (class_name, aname);
319 case MONO_EXCEPTION_MISSING_METHOD: {
320 char *cname = g_strdup (error->class_name);
321 char *aname = g_strdup (error->member_name);
323 mono_loader_clear_error ();
324 ex = mono_get_exception_missing_method (cname, aname);
330 case MONO_EXCEPTION_MISSING_FIELD: {
331 char *cnspace = g_strdup ((error->klass && *error->klass->name_space) ? error->klass->name_space : "");
332 char *cname = g_strdup (error->klass ? error->klass->name : "");
333 char *cmembername = g_strdup (error->member_name);
336 mono_loader_clear_error ();
337 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
339 ex = mono_get_exception_missing_field (class_name, cmembername);
342 g_free (cmembername);
347 case MONO_EXCEPTION_FILE_NOT_FOUND: {
352 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);
354 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
355 filename = g_strdup (error->assembly_name);
356 /* Has to call this before calling anything which might call mono_class_init () */
357 mono_loader_clear_error ();
358 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
364 case MONO_EXCEPTION_BAD_IMAGE: {
365 char *msg = g_strdup (error->msg);
366 mono_loader_clear_error ();
367 ex = mono_get_exception_bad_image_format (msg);
373 g_assert_not_reached ();
380 * find_cached_memberref_sig:
382 * Return a cached copy of the memberref signature identified by SIG_IDX.
383 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
384 * A cache is needed since the type/signature parsing routines allocate everything
385 * from a mempool, so without a cache, multiple requests for the same signature would
386 * lead to unbounded memory growth. For normal methods/fields this is not a problem
387 * since the resulting methods/fields are cached, but inflated methods/fields cannot
389 * LOCKING: Acquires the loader lock.
392 find_cached_memberref_sig (MonoImage *image, guint32 sig_idx)
397 res = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
398 mono_loader_unlock ();
404 cache_memberref_sig (MonoImage *image, guint32 sig_idx, gpointer sig)
409 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (sig_idx));
411 /* Somebody got in before us */
415 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (sig_idx), sig);
416 /* An approximation based on glib 2.18 */
417 memberref_sig_cache_size += sizeof (gpointer) * 4;
420 mono_loader_unlock ();
425 static MonoClassField*
426 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
427 MonoGenericContext *context)
430 MonoClassField *field;
431 MonoTableInfo *tables = image->tables;
434 guint32 nindex, class, class_table;
437 guint32 idx = mono_metadata_token_index (token);
439 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
440 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
441 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
443 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
445 if (!mono_verifier_verify_memberref_field_signature (image, cols [MONO_MEMBERREF_SIGNATURE], NULL)) {
446 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));
451 case MONO_MEMBERREF_PARENT_TYPEDEF:
452 class_table = MONO_TOKEN_TYPE_DEF;
453 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
455 case MONO_MEMBERREF_PARENT_TYPEREF:
456 class_table = MONO_TOKEN_TYPE_REF;
457 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
459 case MONO_MEMBERREF_PARENT_TYPESPEC:
460 class_table = MONO_TOKEN_TYPE_SPEC;
461 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
464 /*FIXME this must set a loader error!*/
465 g_warning ("field load from %x", class);
470 char *name = mono_class_name_from_token (image, class_table | nindex);
471 g_warning ("Missing field %s in class %s (type token %d)", fname, name, class_table | nindex);
472 mono_loader_set_error_type_load (name, image->assembly_name);
477 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
478 mono_metadata_decode_blob_size (ptr, &ptr);
479 /* we may want to check the signature here... */
482 g_warning ("Bad field signature class token %08x field name %s token %08x", class, fname, token);
483 mono_loader_set_error_field_load (klass, fname);
486 /* FIXME: This needs a cache, especially for generic instances, since
487 * mono_metadata_parse_type () allocates everything from a mempool.
489 sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
491 sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
492 if (sig_type == NULL) {
493 mono_loader_set_error_field_load (klass, fname);
496 sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
499 mono_class_init (klass); /*FIXME is this really necessary?*/
502 field = mono_class_get_field_from_name_full (klass, fname, sig_type);
505 mono_loader_set_error_field_load (klass, fname);
511 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
512 MonoGenericContext *context)
516 MonoClassField *field;
518 if (image->dynamic) {
519 MonoClassField *result;
520 MonoClass *handle_class;
523 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
524 // This checks the memberref type as well
525 if (!result || handle_class != mono_defaults.fieldhandle_class) {
526 mono_loader_set_error_bad_image (g_strdup_printf ("Bad field token 0x%08x on image %s.", token, image->name));
529 *retklass = result->parent;
534 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
535 *retklass = field->parent;
536 mono_loader_unlock ();
539 mono_loader_unlock ();
541 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
542 field = field_from_memberref (image, token, retklass, context);
544 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
547 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
553 field = mono_class_get_field (k, token);
557 if (field && field->parent && !field->parent->generic_class && !field->parent->generic_container)
558 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
559 mono_loader_unlock ();
564 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
568 if (sig1->hasthis != sig2->hasthis ||
569 sig1->sentinelpos != sig2->sentinelpos)
572 for (i = 0; i < sig1->sentinelpos; i++) {
573 MonoType *p1 = sig1->params[i];
574 MonoType *p2 = sig2->params[i];
576 /*if (p1->attrs != p2->attrs)
579 if (!mono_metadata_type_equal (p1, p2))
583 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
589 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
590 MonoMethodSignature *sig, MonoClass *from_class)
594 /* Search directly in the metadata to avoid calling setup_methods () */
596 /* FIXME: !from_class->generic_class condition causes test failures. */
597 if (klass->type_token && !klass->image->dynamic && !klass->methods && !klass->rank && klass == from_class && !from_class->generic_class) {
598 for (i = 0; i < klass->method.count; ++i) {
599 guint32 cols [MONO_METHOD_SIZE];
602 MonoMethodSignature *other_sig;
604 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
606 m_name = mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]);
608 if (!((fqname && !strcmp (m_name, fqname)) ||
609 (qname && !strcmp (m_name, qname)) ||
610 (name && !strcmp (m_name, name))))
613 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
615 other_sig = mono_method_signature (method);
616 if (other_sig && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, other_sig))
622 mono_class_setup_methods (klass);
624 We can't fail lookup of methods otherwise the runtime will fail with MissingMethodException instead of TypeLoadException.
625 See mono/tests/generic-type-load-exception.2.il
626 FIXME we should better report this error to the caller
630 for (i = 0; i < klass->method.count; ++i) {
631 MonoMethod *m = klass->methods [i];
632 MonoMethodSignature *msig;
634 /* We must cope with failing to load some of the types. */
638 if (!((fqname && !strcmp (m->name, fqname)) ||
639 (qname && !strcmp (m->name, qname)) ||
640 (name && !strcmp (m->name, name))))
642 msig = mono_method_signature (m);
646 if (sig->call_convention == MONO_CALL_VARARG) {
647 if (mono_metadata_signature_vararg_match (sig, msig))
650 if (mono_metadata_signature_equal (sig, msig))
655 if (i < klass->method.count)
656 return mono_class_get_method_by_index (from_class, i);
661 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
664 char *qname, *fqname, *class_name;
665 gboolean is_interface;
666 MonoMethod *result = NULL;
668 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
671 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
673 qname = g_strconcat (class_name, ".", name, NULL);
674 if (ic->name_space && ic->name_space [0])
675 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
679 class_name = qname = fqname = NULL;
682 g_assert (from_class);
683 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
687 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
691 * This happens when we fail to lazily load the interfaces of one of the types.
692 * On such case we can't just bail out since user code depends on us trying harder.
694 if (from_class->interface_offsets_count != in_class->interface_offsets_count) {
695 in_class = in_class->parent;
696 from_class = from_class->parent;
700 for (i = 0; i < in_class->interface_offsets_count; i++) {
701 MonoClass *in_ic = in_class->interfaces_packed [i];
702 MonoClass *from_ic = from_class->interfaces_packed [i];
703 char *ic_qname, *ic_fqname, *ic_class_name;
705 ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
706 ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
707 if (in_ic->name_space && in_ic->name_space [0])
708 ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
712 result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
713 g_free (ic_class_name);
720 in_class = in_class->parent;
721 from_class = from_class->parent;
723 g_assert (!in_class == !from_class);
726 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
735 static MonoMethodSignature*
736 inflate_generic_signature_checked (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context, MonoError *error)
738 MonoMethodSignature *res;
742 mono_error_init (error);
746 res = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)sig->param_count) * sizeof (MonoType*));
747 res->param_count = sig->param_count;
748 res->sentinelpos = -1;
749 res->ret = mono_class_inflate_generic_type_checked (sig->ret, context, error);
750 if (!mono_error_ok (error))
752 is_open = mono_class_is_open_constructed_type (res->ret);
753 for (i = 0; i < sig->param_count; ++i) {
754 res->params [i] = mono_class_inflate_generic_type_checked (sig->params [i], context, error);
755 if (!mono_error_ok (error))
759 is_open = mono_class_is_open_constructed_type (res->params [i]);
761 res->hasthis = sig->hasthis;
762 res->explicit_this = sig->explicit_this;
763 res->call_convention = sig->call_convention;
764 res->pinvoke = sig->pinvoke;
765 res->generic_param_count = sig->generic_param_count;
766 res->sentinelpos = sig->sentinelpos;
767 res->has_type_parameters = is_open;
768 res->is_inflated = 1;
773 mono_metadata_free_type (res->ret);
774 for (i = 0; i < sig->param_count; ++i) {
776 mono_metadata_free_type (res->params [i]);
782 static MonoMethodHeader*
783 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
785 MonoMethodHeader *res;
787 res = g_malloc0 (MONO_SIZEOF_METHOD_HEADER + sizeof (gpointer) * header->num_locals);
788 res->code = header->code;
789 res->code_size = header->code_size;
790 res->max_stack = header->max_stack;
791 res->num_clauses = header->num_clauses;
792 res->init_locals = header->init_locals;
793 res->num_locals = header->num_locals;
794 res->clauses = header->clauses;
795 for (i = 0; i < header->num_locals; ++i)
796 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
797 if (res->num_clauses) {
798 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
799 for (i = 0; i < header->num_clauses; ++i) {
800 MonoExceptionClause *clause = &res->clauses [i];
801 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
803 clause->data.catch_class = mono_class_inflate_generic_class (clause->data.catch_class, context);
810 * token is the method_ref/def/spec token used in a call IL instruction.
813 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
815 int table = mono_metadata_token_table (token);
816 int idx = mono_metadata_token_index (token);
818 guint32 cols [MONO_MEMBERREF_SIZE];
819 MonoMethodSignature *sig;
822 /* !table is for wrappers: we should really assign their own token to them */
823 if (!table || table == MONO_TABLE_METHOD)
824 return mono_method_signature (method);
826 if (table == MONO_TABLE_METHODSPEC) {
827 /* the verifier (do_invoke_method) will turn the NULL into a verifier error */
828 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || !method->is_inflated)
831 return mono_method_signature (method);
834 if (method->klass->generic_class)
835 return mono_method_signature (method);
837 if (image->dynamic) {
838 sig = mono_reflection_lookup_signature (image, method, token);
840 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
841 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
843 sig = find_cached_memberref_sig (image, sig_idx);
845 if (!mono_verifier_verify_memberref_method_signature (image, sig_idx, NULL)) {
846 guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
847 const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
849 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));
853 ptr = mono_metadata_blob_heap (image, sig_idx);
854 mono_metadata_decode_blob_size (ptr, &ptr);
855 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
858 sig = cache_memberref_sig (image, sig_idx, sig);
860 /* FIXME: we probably should verify signature compat in the dynamic case too*/
861 if (!mono_verifier_is_sig_compatible (image, method, sig)) {
862 guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
863 const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
865 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));
873 MonoMethodSignature *cached;
875 /* This signature is not owned by a MonoMethod, so need to cache */
876 sig = inflate_generic_signature_checked (image, sig, context, &error);
877 if (!mono_error_ok (&error)) {/*XXX bubble up this and kill one use of loader errors */
878 mono_loader_set_error_bad_image (g_strdup_printf ("Could not inflate signature %s", mono_error_get_message (&error)));
879 mono_error_cleanup (&error);
883 cached = mono_metadata_get_inflated_signature (sig, context);
885 mono_metadata_free_inflated_signature (sig);
887 inflated_signatures_size += mono_metadata_signature_size (cached);
895 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
897 return mono_method_get_signature_full (method, image, token, NULL);
900 /* this is only for the typespec array methods */
902 mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
906 mono_class_setup_methods (klass);
907 g_assert (!klass->exception_type); /*FIXME this should not fail, right?*/
908 for (i = 0; i < klass->method.count; ++i) {
909 MonoMethod *method = klass->methods [i];
910 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
917 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
918 gboolean *used_context)
920 MonoClass *klass = NULL;
921 MonoMethod *method = NULL;
922 MonoTableInfo *tables = image->tables;
924 guint32 nindex, class, sig_idx;
926 MonoMethodSignature *sig;
929 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
930 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
931 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
932 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
933 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
935 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
938 * Whether we actually used the `typespec_context' or not.
939 * This is used to tell our caller whether or not it's safe to insert the returned
940 * method into a cache.
943 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
946 case MONO_MEMBERREF_PARENT_TYPEREF:
947 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
949 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
950 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
951 mono_loader_set_error_type_load (name, image->assembly_name);
956 case MONO_MEMBERREF_PARENT_TYPESPEC:
958 * Parse the TYPESPEC in the parent's context.
960 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
962 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
963 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
964 mono_loader_set_error_type_load (name, image->assembly_name);
969 case MONO_MEMBERREF_PARENT_TYPEDEF:
970 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
972 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
973 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
974 mono_loader_set_error_type_load (name, image->assembly_name);
979 case MONO_MEMBERREF_PARENT_METHODDEF:
980 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
984 /* This message leaks */
985 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
986 mono_loader_set_error_method_load ("", message);
992 mono_class_init (klass);
994 sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
996 if (!mono_verifier_verify_memberref_method_signature (image, sig_idx, NULL)) {
997 mono_loader_set_error_method_load (klass->name, mname);
1001 ptr = mono_metadata_blob_heap (image, sig_idx);
1002 mono_metadata_decode_blob_size (ptr, &ptr);
1004 sig = find_cached_memberref_sig (image, sig_idx);
1006 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
1010 sig = cache_memberref_sig (image, sig_idx, sig);
1014 case MONO_MEMBERREF_PARENT_TYPEREF:
1015 case MONO_MEMBERREF_PARENT_TYPEDEF:
1016 method = find_method (klass, NULL, mname, sig, klass);
1019 case MONO_MEMBERREF_PARENT_TYPESPEC: {
1022 type = &klass->byval_arg;
1024 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
1025 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
1026 method = find_method (in_class, NULL, mname, sig, klass);
1030 /* we're an array and we created these methods already in klass in mono_class_init () */
1031 method = mono_method_search_in_array_class (klass, mname, sig);
1035 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
1036 g_assert_not_reached ();
1040 char *msig = mono_signature_get_desc (sig, FALSE);
1041 char * class_name = mono_type_get_name (&klass->byval_arg);
1042 GString *s = g_string_new (mname);
1043 if (sig->generic_param_count)
1044 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
1045 g_string_append_printf (s, "(%s)", msig);
1047 msig = g_string_free (s, FALSE);
1050 "Missing method %s::%s in assembly %s, referenced in assembly %s",
1051 class_name, msig, klass->image->name, image->name);
1052 mono_loader_set_error_method_load (class_name, mname);
1054 g_free (class_name);
1061 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
1066 MonoTableInfo *tables = image->tables;
1067 MonoGenericContext new_context;
1068 MonoGenericInst *inst;
1070 guint32 cols [MONO_METHODSPEC_SIZE];
1071 guint32 token, nindex, param_count;
1073 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
1074 token = cols [MONO_METHODSPEC_METHOD];
1075 nindex = token >> MONO_METHODDEFORREF_BITS;
1077 if (!mono_verifier_verify_methodspec_signature (image, cols [MONO_METHODSPEC_SIGNATURE], NULL))
1080 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
1082 mono_metadata_decode_value (ptr, &ptr);
1084 param_count = mono_metadata_decode_value (ptr, &ptr);
1085 g_assert (param_count);
1087 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
1091 if (context && inst->is_open) {
1092 inst = mono_metadata_inflate_generic_inst (inst, context, &error);
1093 if (!mono_error_ok (&error)) {
1094 mono_error_cleanup (&error); /*FIXME don't swallow error message.*/
1099 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
1100 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
1102 method = method_from_memberref (image, nindex, context, NULL);
1107 klass = method->klass;
1109 if (klass->generic_class) {
1110 g_assert (method->is_inflated);
1111 method = ((MonoMethodInflated *) method)->declaring;
1114 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
1115 new_context.method_inst = inst;
1117 return mono_class_inflate_generic_method_full (method, klass, &new_context);
1120 struct _MonoDllMap {
1128 static MonoDllMap *global_dll_map;
1131 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
1139 mono_loader_lock ();
1142 * we use the first entry we find that matches, since entries from
1143 * the config file are prepended to the list and we document that the
1144 * later entries win.
1146 for (; dll_map; dll_map = dll_map->next) {
1147 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
1148 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
1150 } else if (strcmp (dll_map->dll, dll)) {
1153 if (!found && dll_map->target) {
1154 *rdll = dll_map->target;
1156 /* we don't quit here, because we could find a full
1157 * entry that matches also function and that has priority.
1160 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
1161 *rfunc = dll_map->target_func;
1166 mono_loader_unlock ();
1171 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
1174 if (assembly && assembly->dll_map) {
1175 res = mono_dllmap_lookup_list (assembly->dll_map, dll, func, rdll, rfunc);
1179 return mono_dllmap_lookup_list (global_dll_map, dll, func, rdll, rfunc);
1183 * mono_dllmap_insert:
1185 * LOCKING: Acquires the loader lock.
1187 * NOTE: This can be called before the runtime is initialized, for example from
1188 * mono_config_parse ().
1191 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc)
1195 mono_loader_init ();
1197 mono_loader_lock ();
1200 entry = g_malloc0 (sizeof (MonoDllMap));
1201 entry->dll = dll? g_strdup (dll): NULL;
1202 entry->target = tdll? g_strdup (tdll): NULL;
1203 entry->func = func? g_strdup (func): NULL;
1204 entry->target_func = tfunc? g_strdup (tfunc): NULL;
1205 entry->next = global_dll_map;
1206 global_dll_map = entry;
1208 entry = mono_image_alloc0 (assembly, sizeof (MonoDllMap));
1209 entry->dll = dll? mono_image_strdup (assembly, dll): NULL;
1210 entry->target = tdll? mono_image_strdup (assembly, tdll): NULL;
1211 entry->func = func? mono_image_strdup (assembly, func): NULL;
1212 entry->target_func = tfunc? mono_image_strdup (assembly, tfunc): NULL;
1213 entry->next = assembly->dll_map;
1214 assembly->dll_map = entry;
1217 mono_loader_unlock ();
1221 free_dllmap (MonoDllMap *map)
1224 MonoDllMap *next = map->next;
1227 g_free (map->target);
1229 g_free (map->target_func);
1236 dllmap_cleanup (void)
1238 free_dllmap (global_dll_map);
1239 global_dll_map = NULL;
1242 static GHashTable *global_module_map;
1245 cached_module_load (const char *name, int flags, char **err)
1251 mono_loader_lock ();
1252 if (!global_module_map)
1253 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1254 res = g_hash_table_lookup (global_module_map, name);
1256 mono_loader_unlock ();
1259 res = mono_dl_open (name, flags, err);
1261 g_hash_table_insert (global_module_map, g_strdup (name), res);
1262 mono_loader_unlock ();
1267 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1269 MonoImage *image = method->klass->image;
1270 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1271 MonoTableInfo *tables = image->tables;
1272 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1273 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1274 guint32 im_cols [MONO_IMPLMAP_SIZE];
1275 guint32 scope_token;
1276 const char *import = NULL;
1277 const char *orig_scope;
1278 const char *new_scope;
1280 char *full_name, *file_name;
1282 MonoDl *module = NULL;
1284 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1287 return piinfo->addr;
1289 if (method->klass->image->dynamic) {
1290 MonoReflectionMethodAux *method_aux =
1291 g_hash_table_lookup (
1292 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1296 import = method_aux->dllentry;
1297 orig_scope = method_aux->dll;
1300 if (!piinfo->implmap_idx || piinfo->implmap_idx > im->rows)
1303 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1305 if (!im_cols [MONO_IMPLMAP_SCOPE] || im_cols [MONO_IMPLMAP_SCOPE] > mr->rows)
1308 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1309 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1310 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1311 orig_scope = mono_metadata_string_heap (image, scope_token);
1314 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1316 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1317 "DllImport attempting to load: '%s'.", new_scope);
1324 /* we allow a special name to dlopen from the running process namespace */
1325 if (strcmp (new_scope, "__Internal") == 0)
1326 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1329 * Try loading the module using a variety of names
1331 for (i = 0; i < 4; ++i) {
1334 /* Try the original name */
1335 file_name = g_strdup (new_scope);
1338 /* Try trimming the .dll extension */
1339 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1340 file_name = g_strdup (new_scope);
1341 file_name [strlen (new_scope) - 4] = '\0';
1347 if (strstr (new_scope, "lib") != new_scope) {
1348 file_name = g_strdup_printf ("lib%s", new_scope);
1354 #ifndef TARGET_WIN32
1355 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1356 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1357 !g_ascii_strcasecmp ("user32", new_scope) ||
1358 !g_ascii_strcasecmp ("kernel", new_scope)) {
1359 file_name = g_strdup ("libMonoSupportW.so");
1363 #ifndef TARGET_WIN32
1370 char *mdirname = g_path_get_dirname (image->name);
1371 while ((full_name = mono_dl_build_path (mdirname, file_name, &iter))) {
1372 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1373 "DllImport loading library: '%s'.", full_name);
1374 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1376 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1377 "DllImport error loading library '%s'.",
1390 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1391 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1392 "DllImport loading location: '%s'.", full_name);
1393 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1395 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1396 "DllImport error loading library: '%s'.",
1407 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1408 "DllImport loading: '%s'.", file_name);
1409 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1411 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1412 "DllImport error loading library '%s'.",
1424 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1425 "DllImport unable to load library '%s'.",
1430 *exc_class = "DllNotFoundException";
1431 *exc_arg = new_scope;
1437 if (import && import [0] == '#' && isdigit (import [1])) {
1441 id = strtol (import + 1, &end, 10);
1442 if (id > 0 && *end == '\0')
1446 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1447 "Searching for '%s'.", import);
1449 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1450 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1452 char *mangled_name = NULL, *mangled_name2 = NULL;
1455 int mangle_param_count;
1461 * Search using a variety of mangled names
1463 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1464 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1465 gboolean need_param_count = FALSE;
1467 if (mangle_stdcall > 0)
1468 need_param_count = TRUE;
1470 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1475 mangled_name = (char*)import;
1476 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1477 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1478 /* Try the mangled name first */
1479 if (mangle_charset == 0)
1480 mangled_name = g_strconcat (import, "W", NULL);
1482 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1484 if (mangle_charset == 0)
1485 mangled_name = g_strconcat (import, "W", NULL);
1487 /* Try the mangled name last */
1488 if (mangle_charset == 1)
1489 mangled_name = g_strconcat (import, "A", NULL);
1492 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1494 /* Try the mangled name last */
1495 if (mangle_charset == 1)
1496 mangled_name = g_strconcat (import, "A", NULL);
1501 if (mangle_param_count == 0)
1502 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1504 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1505 param_count = mangle_param_count;
1507 /* Try the stdcall mangled name */
1509 * gcc under windows creates mangled names without the underscore, but MS.NET
1510 * doesn't support it, so we doesn't support it either.
1512 if (mangle_stdcall == 1)
1513 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1515 mangled_name2 = mangled_name;
1517 mangled_name2 = mangled_name;
1520 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1521 "Probing '%s'.", mangled_name2);
1523 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1528 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1529 "Found as '%s'.", mangled_name2);
1531 if (mangled_name != mangled_name2)
1532 g_free (mangled_name2);
1533 if (mangled_name != import)
1534 g_free (mangled_name);
1540 if (!piinfo->addr) {
1543 *exc_class = "EntryPointNotFoundException";
1548 return piinfo->addr;
1552 * LOCKING: assumes the loader lock to be taken.
1555 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1556 MonoGenericContext *context, gboolean *used_context)
1559 int table = mono_metadata_token_table (token);
1560 int idx = mono_metadata_token_index (token);
1561 MonoTableInfo *tables = image->tables;
1562 MonoGenericContainer *generic_container = NULL, *container = NULL;
1563 const char *sig = NULL;
1565 guint32 cols [MONO_TYPEDEF_SIZE];
1567 if (image->dynamic) {
1568 MonoClass *handle_class;
1570 result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
1571 // This checks the memberref type as well
1572 if (result && handle_class != mono_defaults.methodhandle_class) {
1573 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1579 if (table != MONO_TABLE_METHOD) {
1580 if (table == MONO_TABLE_METHODSPEC) {
1581 if (used_context) *used_context = TRUE;
1582 return method_from_methodspec (image, context, idx);
1584 if (table != MONO_TABLE_MEMBERREF) {
1585 g_warning ("got wrong token: 0x%08x\n", token);
1586 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1589 return method_from_memberref (image, idx, context, used_context);
1592 if (used_context) *used_context = FALSE;
1594 if (idx > image->tables [MONO_TABLE_METHOD].rows) {
1595 mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
1599 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1601 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1602 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
1603 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
1605 result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethod));
1606 methods_size += sizeof (MonoMethod);
1609 mono_stats.method_count ++;
1611 if (!klass) { /*FIXME put this before the image alloc*/
1612 guint32 type = mono_metadata_typedef_from_method (image, token);
1615 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1621 result->klass = klass;
1622 result->flags = cols [2];
1623 result->iflags = cols [1];
1624 result->token = token;
1625 result->name = mono_metadata_string_heap (image, cols [3]);
1627 if (!sig) /* already taken from the methodref */
1628 sig = mono_metadata_blob_heap (image, cols [4]);
1629 size = mono_metadata_decode_blob_size (sig, &sig);
1631 container = klass->generic_container;
1634 * load_generic_params does a binary search so only call it if the method
1638 generic_container = mono_metadata_load_generic_params (image, token, container);
1639 if (generic_container) {
1640 result->is_generic = TRUE;
1641 generic_container->owner.method = result;
1642 /*FIXME put this before the image alloc*/
1643 if (!mono_metadata_load_generic_param_constraints_full (image, token, generic_container))
1646 container = generic_container;
1649 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1650 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1651 result->string_ctor = 1;
1652 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1653 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1656 /* IJW is P/Invoke with a predefined function pointer. */
1657 if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
1658 piinfo->addr = mono_image_rva_map (image, cols [0]);
1659 g_assert (piinfo->addr);
1662 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1663 /* Native methods can have no map. */
1664 if (piinfo->implmap_idx)
1665 piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1668 if (generic_container)
1669 mono_method_set_generic_container (result, generic_container);
1675 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1677 return mono_get_method_full (image, token, klass, NULL);
1681 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1682 MonoGenericContext *context)
1685 gboolean used_context = FALSE;
1687 /* We do everything inside the lock to prevent creation races */
1689 mono_image_lock (image);
1691 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
1692 if (!image->method_cache)
1693 image->method_cache = g_hash_table_new (NULL, NULL);
1694 result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
1696 if (!image->methodref_cache)
1697 image->methodref_cache = g_hash_table_new (NULL, NULL);
1698 result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1700 mono_image_unlock (image);
1705 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1709 mono_image_lock (image);
1710 if (!used_context && !result->is_inflated) {
1711 MonoMethod *result2;
1712 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1713 result2 = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
1715 result2 = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
1718 mono_image_unlock (image);
1722 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
1723 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)), result);
1725 g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
1728 mono_image_unlock (image);
1734 * mono_get_method_constrained:
1736 * This is used when JITing the `constrained.' opcode.
1738 * This returns two values: the contrained method, which has been inflated
1739 * as the function return value; And the original CIL-stream method as
1740 * declared in cil_method. The later is used for verification.
1743 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1744 MonoGenericContext *context, MonoMethod **cil_method)
1746 MonoMethod *method, *result;
1747 MonoClass *ic = NULL;
1748 MonoGenericContext *method_context = NULL;
1749 MonoMethodSignature *sig, *original_sig;
1751 mono_loader_lock ();
1753 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1755 mono_loader_unlock ();
1759 mono_class_init (constrained_class);
1760 method = *cil_method;
1761 original_sig = sig = mono_method_signature (method);
1763 mono_loader_unlock ();
1767 if (method->is_inflated && sig->generic_param_count) {
1768 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1769 sig = mono_method_signature (imethod->declaring); /*We assume that if the inflated method signature is valid, the declaring method is too*/
1770 method_context = mono_method_get_context (method);
1774 * We must inflate the signature with the class instantiation to work on
1775 * cases where a class inherit from a generic type and the override replaces
1776 * any type argument which a concrete type. See #325283.
1778 if (method_context->class_inst) {
1780 MonoGenericContext ctx;
1781 ctx.method_inst = NULL;
1782 ctx.class_inst = method_context->class_inst;
1783 /*Fixme, property propagate this error*/
1784 sig = inflate_generic_signature_checked (method->klass->image, sig, &ctx, &error);
1785 if (!mono_error_ok (&error)) {
1786 mono_loader_unlock ();
1787 mono_error_cleanup (&error);
1793 if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
1796 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1797 if (sig != original_sig)
1798 mono_metadata_free_inflated_signature (sig);
1801 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1802 method->klass->name, method->name, image->name, token);
1803 mono_loader_unlock ();
1808 result = mono_class_inflate_generic_method (result, method_context);
1810 mono_loader_unlock ();
1815 mono_free_method (MonoMethod *method)
1817 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
1818 mono_profiler_method_free (method);
1820 /* FIXME: This hack will go away when the profiler will support freeing methods */
1821 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1824 if (method->signature) {
1826 * FIXME: This causes crashes because the types inside signatures and
1827 * locals are shared.
1829 /* mono_metadata_free_method_signature (method->signature); */
1830 /* g_free (method->signature); */
1833 if (method->dynamic) {
1834 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1837 mono_marshal_free_dynamic_wrappers (method);
1839 mono_image_property_remove (method->klass->image, method);
1841 g_free ((char*)method->name);
1843 g_free ((char*)mw->header->code);
1844 for (i = 0; i < mw->header->num_locals; ++i)
1845 g_free (mw->header->locals [i]);
1846 g_free (mw->header->clauses);
1847 g_free (mw->header);
1849 g_free (mw->method_data);
1850 g_free (method->signature);
1856 mono_method_get_param_names (MonoMethod *method, const char **names)
1860 MonoTableInfo *methodt;
1861 MonoTableInfo *paramt;
1862 MonoMethodSignature *signature;
1865 if (method->is_inflated)
1866 method = ((MonoMethodInflated *) method)->declaring;
1868 signature = mono_method_signature (method);
1869 /*FIXME this check is somewhat redundant since the caller usally will have to get the signature to figure out the
1870 number of arguments and allocate a properly sized array. */
1871 if (signature == NULL)
1874 if (!signature->param_count)
1877 for (i = 0; i < signature->param_count; ++i)
1880 klass = method->klass;
1884 mono_class_init (klass);
1886 if (klass->image->dynamic) {
1887 MonoReflectionMethodAux *method_aux =
1888 g_hash_table_lookup (
1889 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1890 if (method_aux && method_aux->param_names) {
1891 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1892 if (method_aux->param_names [i + 1])
1893 names [i] = method_aux->param_names [i + 1];
1898 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1899 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1900 idx = mono_method_get_index (method);
1902 guint32 cols [MONO_PARAM_SIZE];
1905 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1907 if (idx < methodt->rows)
1908 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1910 lastp = paramt->rows + 1;
1911 for (i = param_index; i < lastp; ++i) {
1912 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1913 if (cols [MONO_PARAM_SEQUENCE] && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) /* skip return param spec and bounds check*/
1914 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1920 mono_method_get_param_token (MonoMethod *method, int index)
1922 MonoClass *klass = method->klass;
1923 MonoTableInfo *methodt;
1926 mono_class_init (klass);
1928 if (klass->image->dynamic) {
1929 g_assert_not_reached ();
1932 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1933 idx = mono_method_get_index (method);
1935 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1939 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1941 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1948 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1951 MonoClass *klass = method->klass;
1952 MonoTableInfo *methodt;
1953 MonoTableInfo *paramt;
1954 MonoMethodSignature *signature;
1957 signature = mono_method_signature (method);
1958 g_assert (signature); /*FIXME there is no way to signal error from this function*/
1960 for (i = 0; i < signature->param_count + 1; ++i)
1963 if (method->klass->image->dynamic) {
1964 MonoReflectionMethodAux *method_aux =
1965 g_hash_table_lookup (
1966 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1967 if (method_aux && method_aux->param_marshall) {
1968 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1969 for (i = 0; i < signature->param_count + 1; ++i)
1970 if (dyn_specs [i]) {
1971 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1972 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1973 mspecs [i]->data.custom_data.custom_name = g_strdup (dyn_specs [i]->data.custom_data.custom_name);
1974 mspecs [i]->data.custom_data.cookie = g_strdup (dyn_specs [i]->data.custom_data.cookie);
1980 mono_class_init (klass);
1982 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1983 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1984 idx = mono_method_get_index (method);
1986 guint32 cols [MONO_PARAM_SIZE];
1987 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1989 if (idx < methodt->rows)
1990 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1992 lastp = paramt->rows + 1;
1994 for (i = param_index; i < lastp; ++i) {
1995 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1997 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) {
1999 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
2001 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
2010 mono_method_has_marshal_info (MonoMethod *method)
2013 MonoClass *klass = method->klass;
2014 MonoTableInfo *methodt;
2015 MonoTableInfo *paramt;
2018 if (method->klass->image->dynamic) {
2019 MonoReflectionMethodAux *method_aux =
2020 g_hash_table_lookup (
2021 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
2022 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
2024 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
2031 mono_class_init (klass);
2033 methodt = &klass->image->tables [MONO_TABLE_METHOD];
2034 paramt = &klass->image->tables [MONO_TABLE_PARAM];
2035 idx = mono_method_get_index (method);
2037 guint32 cols [MONO_PARAM_SIZE];
2038 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
2040 if (idx + 1 < methodt->rows)
2041 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
2043 lastp = paramt->rows + 1;
2045 for (i = param_index; i < lastp; ++i) {
2046 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
2048 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
2057 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
2060 g_assert (method != NULL);
2061 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
2063 if (method->is_inflated)
2064 method = ((MonoMethodInflated *) method)->declaring;
2065 data = ((MonoMethodWrapper *)method)->method_data;
2066 g_assert (data != NULL);
2067 g_assert (id <= GPOINTER_TO_UINT (*data));
2072 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
2073 g_error ("stack walk not installed");
2076 static MonoStackWalkImpl stack_walk = default_stack_walk;
2079 mono_stack_walk (MonoStackWalk func, gpointer user_data)
2081 stack_walk (func, TRUE, user_data);
2085 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
2087 stack_walk (func, FALSE, user_data);
2091 mono_install_stack_walk (MonoStackWalkImpl func)
2097 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
2099 MonoMethod **dest = data;
2101 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
2107 mono_method_get_last_managed (void)
2109 MonoMethod *m = NULL;
2110 stack_walk (last_managed, FALSE, &m);
2114 static gboolean loader_lock_track_ownership = FALSE;
2119 * See docs/thread-safety.txt for the locking strategy.
2122 mono_loader_lock (void)
2124 mono_locks_acquire (&loader_mutex, LoaderLock);
2125 if (G_UNLIKELY (loader_lock_track_ownership)) {
2126 TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) + 1));
2131 mono_loader_unlock (void)
2133 mono_locks_release (&loader_mutex, LoaderLock);
2134 if (G_UNLIKELY (loader_lock_track_ownership)) {
2135 TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) - 1));
2140 * mono_loader_lock_track_ownership:
2142 * Set whenever the runtime should track ownership of the loader lock. If set to TRUE,
2143 * the mono_loader_lock_is_owned_by_self () can be called to query whenever the current
2144 * thread owns the loader lock.
2147 mono_loader_lock_track_ownership (gboolean track)
2149 loader_lock_track_ownership = track;
2153 * mono_loader_lock_is_owned_by_self:
2155 * Return whenever the current thread owns the loader lock.
2156 * This is useful to avoid blocking operations while holding the loader lock.
2159 mono_loader_lock_is_owned_by_self (void)
2161 g_assert (loader_lock_track_ownership);
2163 return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) > 0;
2167 * mono_loader_lock_if_inited:
2169 * Acquire the loader lock if it has been initialized, no-op otherwise. This can
2170 * be used in runtime initialization code which can be executed before mono_loader_init ().
2173 mono_loader_lock_if_inited (void)
2175 if (loader_lock_inited)
2176 mono_loader_lock ();
2180 mono_loader_unlock_if_inited (void)
2182 if (loader_lock_inited)
2183 mono_loader_unlock ();
2187 * mono_method_signature:
2189 * Return the signature of the method M. On failure, returns NULL, and ERR is set.
2191 MonoMethodSignature*
2192 mono_method_signature_checked (MonoMethod *m, MonoError *error)
2198 gboolean can_cache_signature;
2199 MonoGenericContainer *container;
2200 MonoMethodSignature *signature = NULL;
2203 /* We need memory barriers below because of the double-checked locking pattern */
2205 mono_error_init (error);
2208 return m->signature;
2210 mono_loader_lock ();
2213 mono_loader_unlock ();
2214 return m->signature;
2217 if (m->is_inflated) {
2218 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
2219 /* the lock is recursive */
2220 signature = mono_method_signature (imethod->declaring);
2221 signature = inflate_generic_signature_checked (imethod->declaring->klass->image, signature, mono_method_get_context (m), error);
2222 if (!mono_error_ok (error)) {
2223 mono_loader_unlock ();
2227 inflated_signatures_size += mono_metadata_signature_size (signature);
2229 mono_memory_barrier ();
2230 m->signature = signature;
2231 mono_loader_unlock ();
2232 return m->signature;
2235 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
2236 idx = mono_metadata_token_index (m->token);
2237 img = m->klass->image;
2239 sig = mono_metadata_blob_heap (img, sig_offset = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
2241 g_assert (!m->klass->generic_class);
2242 container = mono_method_get_generic_container (m);
2244 container = m->klass->generic_container;
2246 /* Generic signatures depend on the container so they cannot be cached */
2247 /* icall/pinvoke signatures cannot be cached cause we modify them below */
2248 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
2250 /* If the method has parameter attributes, that can modify the signature */
2251 if (mono_metadata_method_has_param_attrs (img, idx))
2252 can_cache_signature = FALSE;
2254 if (can_cache_signature)
2255 signature = g_hash_table_lookup (img->method_signatures, sig);
2258 const char *sig_body;
2259 /*TODO we should cache the failure result somewhere*/
2260 if (!mono_verifier_verify_method_signature (img, sig_offset, error)) {
2261 mono_loader_unlock ();
2265 size = mono_metadata_decode_blob_size (sig, &sig_body);
2267 signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
2269 mono_loader_clear_error ();
2270 mono_loader_unlock ();
2271 mono_error_set_method_load (error, m->klass, m->name, "");
2275 if (can_cache_signature)
2276 g_hash_table_insert (img->method_signatures, (gpointer)sig, signature);
2278 signatures_size += mono_metadata_signature_size (signature);
2281 /* Verify metadata consistency */
2282 if (signature->generic_param_count) {
2283 if (!container || !container->is_method) {
2284 mono_loader_unlock ();
2285 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);
2288 if (container->type_argc != signature->generic_param_count) {
2289 mono_loader_unlock ();
2290 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);
2293 } else if (container && container->is_method && container->type_argc) {
2294 mono_loader_unlock ();
2295 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);
2298 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
2299 signature->pinvoke = 1;
2300 else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2301 MonoCallConvention conv = 0;
2302 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
2303 signature->pinvoke = 1;
2305 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
2306 case 0: /* no call conv, so using default */
2307 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
2308 conv = MONO_CALL_DEFAULT;
2310 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
2313 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
2314 conv = MONO_CALL_STDCALL;
2316 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
2317 conv = MONO_CALL_THISCALL;
2319 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
2320 conv = MONO_CALL_FASTCALL;
2322 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
2323 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
2325 mono_loader_unlock ();
2326 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);
2329 signature->call_convention = conv;
2332 mono_memory_barrier ();
2333 m->signature = signature;
2335 mono_loader_unlock ();
2336 return m->signature;
2340 * mono_method_signature:
2342 * Return the signature of the method M. On failure, returns NULL.
2344 MonoMethodSignature*
2345 mono_method_signature (MonoMethod *m)
2348 MonoMethodSignature *sig;
2350 sig = mono_method_signature_checked (m, &error);
2352 char *type_name = mono_type_get_full_name (m->klass);
2353 g_warning ("Could not load signature of %s:%s due to: %s", type_name, m->name, mono_error_get_message (&error));
2355 mono_error_cleanup (&error);
2362 mono_method_get_name (MonoMethod *method)
2364 return method->name;
2368 mono_method_get_class (MonoMethod *method)
2370 return method->klass;
2374 mono_method_get_token (MonoMethod *method)
2376 return method->token;
2380 mono_method_get_header (MonoMethod *method)
2386 MonoMethodHeader *header;
2388 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))
2391 if (method->is_inflated) {
2392 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
2393 MonoMethodHeader *header;
2395 mono_loader_lock ();
2397 if (imethod->header) {
2398 mono_loader_unlock ();
2399 return imethod->header;
2402 header = mono_method_get_header (imethod->declaring);
2404 mono_loader_unlock ();
2408 imethod->header = inflate_generic_header (header, mono_method_get_context (method));
2409 mono_loader_unlock ();
2410 mono_metadata_free_mh (header);
2411 return imethod->header;
2414 if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
2415 MonoMethodWrapper *mw = (MonoMethodWrapper *)method;
2416 g_assert (mw->header);
2421 * We don't need locks here: the new header is allocated from malloc memory
2422 * and is not stored anywhere in the runtime, the user needs to free it.
2424 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
2425 idx = mono_metadata_token_index (method->token);
2426 img = method->klass->image;
2427 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
2429 if (!mono_verifier_verify_method_header (img, rva, NULL))
2432 loc = mono_image_rva_map (img, rva);
2436 header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
2442 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
2445 *iflags = method->iflags;
2446 return method->flags;
2450 * Find the method index in the metadata methodDef table.
2453 mono_method_get_index (MonoMethod *method) {
2454 MonoClass *klass = method->klass;
2458 /* constructed array methods are not in the MethodDef table */
2462 return mono_metadata_token_index (method->token);
2464 mono_class_setup_methods (klass);
2465 if (klass->exception_type)
2467 for (i = 0; i < klass->method.count; ++i) {
2468 if (method == klass->methods [i]) {
2469 if (klass->image->uncompressed_metadata)
2470 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2472 return klass->method.first + i + 1;