2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
11 * This file is used by the interpreter and the JIT engine to locate
12 * assemblies. Used to load AssemblyRef and later to resolve various
16 * 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/cil-coff.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/metadata-internals.h>
32 #include <mono/metadata/loader.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/metadata/reflection.h>
36 #include <mono/utils/mono-logger.h>
37 #include <mono/metadata/exception.h>
39 MonoDefaults mono_defaults;
42 * This lock protects the hash tables inside MonoImage used by the metadata
43 * loading functions in class.c and loader.c.
45 static CRITICAL_SECTION loader_mutex;
49 * This TLS variable contains the last type load error encountered by the loader.
51 guint32 loader_error_thread_id;
56 InitializeCriticalSection (&loader_mutex);
58 loader_error_thread_id = TlsAlloc ();
62 mono_loader_cleanup (void)
64 TlsFree (loader_error_thread_id);
66 DeleteCriticalSection (&loader_mutex);
70 * Handling of type load errors should be done as follows:
72 * If something could not be loaded, the loader should call one of the
73 * mono_loader_set_error_XXX functions ()
74 * with the appropriate arguments, then return NULL to report the failure. The error
75 * should be propagated until it reaches code which can throw managed exceptions. At that
76 * point, an exception should be thrown based on the information returned by
77 * mono_loader_get_error (). Then the error should be cleared by calling
78 * mono_loader_clear_error ().
82 set_loader_error (MonoLoaderError *error)
84 TlsSetValue (loader_error_thread_id, error);
88 * mono_loader_set_error_assembly_load:
90 * Set the loader error for this thread.
93 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
95 MonoLoaderError *error;
97 if (mono_loader_get_last_error ())
100 error = g_new0 (MonoLoaderError, 1);
101 error->kind = MONO_LOADER_ERROR_ASSEMBLY;
102 error->assembly_name = g_strdup (assembly_name);
103 error->ref_only = ref_only;
106 * This is not strictly needed, but some (most) of the loader code still
107 * can't deal with load errors, and this message is more helpful than an
110 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
112 set_loader_error (error);
116 * mono_loader_set_error_type_load:
118 * Set the loader error for this thread.
121 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
123 MonoLoaderError *error;
125 if (mono_loader_get_last_error ())
128 error = g_new0 (MonoLoaderError, 1);
129 error->kind = MONO_LOADER_ERROR_TYPE;
130 error->class_name = g_strdup (class_name);
131 error->assembly_name = g_strdup (assembly_name);
134 * This is not strictly needed, but some (most) of the loader code still
135 * can't deal with load errors, and this message is more helpful than an
138 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
140 set_loader_error (error);
144 * mono_loader_set_error_method_load:
146 * Set the loader error for this thread. MEMBER_NAME should point to a string
150 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
152 MonoLoaderError *error;
154 /* FIXME: Store the signature as well */
155 if (mono_loader_get_last_error ())
158 error = g_new0 (MonoLoaderError, 1);
159 error->kind = MONO_LOADER_ERROR_METHOD;
160 error->class_name = g_strdup (class_name);
161 error->member_name = member_name;
163 set_loader_error (error);
167 * mono_loader_set_error_field_load:
169 * Set the loader error for this thread. MEMBER_NAME should point to a string
173 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
175 MonoLoaderError *error;
177 /* FIXME: Store the signature as well */
178 if (mono_loader_get_last_error ())
181 error = g_new0 (MonoLoaderError, 1);
182 error->kind = MONO_LOADER_ERROR_FIELD;
183 error->klass = klass;
184 error->member_name = member_name;
186 set_loader_error (error);
190 * mono_loader_get_last_error:
192 * Returns information about the last type load exception encountered by the loader, or
193 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
196 mono_loader_get_last_error (void)
198 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
202 * mono_loader_clear_error:
204 * Disposes any loader error messages on this thread
207 mono_loader_clear_error (void)
209 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
211 g_free (ex->class_name);
212 g_free (ex->assembly_name);
215 TlsSetValue (loader_error_thread_id, NULL);
219 * mono_loader_error_prepare_exception:
220 * @error: The MonoLoaderError to turn into an exception
222 * This turns a MonoLoaderError into an exception that can be thrown
223 * and resets the Mono Loader Error state during this process.
227 mono_loader_error_prepare_exception (MonoLoaderError *error)
229 MonoException *ex = NULL;
231 switch (error->kind) {
232 case MONO_LOADER_ERROR_TYPE: {
233 char *cname = g_strdup (error->class_name);
234 char *aname = g_strdup (error->assembly_name);
235 MonoString *class_name;
237 mono_loader_clear_error ();
239 class_name = mono_string_new (mono_domain_get (), cname);
241 ex = mono_get_exception_type_load (class_name, aname);
246 case MONO_LOADER_ERROR_METHOD: {
247 char *cname = g_strdup (error->class_name);
248 char *aname = g_strdup (error->member_name);
250 mono_loader_clear_error ();
251 ex = mono_get_exception_missing_method (cname, aname);
257 case MONO_LOADER_ERROR_FIELD: {
258 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
259 char *cname = g_strdup (error->klass->name);
260 char *cmembername = g_strdup (error->member_name);
263 mono_loader_clear_error ();
264 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
266 ex = mono_get_exception_missing_field (class_name, cmembername);
269 g_free (cmembername);
274 case MONO_LOADER_ERROR_ASSEMBLY: {
278 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);
280 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
282 mono_loader_clear_error ();
283 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), error->assembly_name));
289 g_assert_not_reached ();
295 static MonoClassField*
296 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
297 MonoGenericContext *context)
300 MonoClassField *field;
301 MonoTableInfo *tables = image->tables;
303 guint32 nindex, class;
306 guint32 idx = mono_metadata_token_index (token);
308 if (image->dynamic) {
309 MonoClassField *result = mono_lookup_dynamic_token (image, token);
310 *retklass = result->parent;
314 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
315 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
316 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
318 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
320 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
321 mono_metadata_decode_blob_size (ptr, &ptr);
322 /* we may want to check the signature here... */
325 case MONO_MEMBERREF_PARENT_TYPEREF:
326 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
328 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
329 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
333 mono_class_init (klass);
336 field = mono_class_get_field_from_name (klass, fname);
338 case MONO_MEMBERREF_PARENT_TYPESPEC: {
339 /*guint32 bcols [MONO_TYPESPEC_SIZE];
343 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
344 bcols, MONO_TYPESPEC_SIZE);
345 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
346 len = mono_metadata_decode_value (ptr, &ptr);
347 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
349 klass = mono_class_from_mono_type (type);
350 mono_class_init (klass);
351 g_print ("type in sig: %s\n", klass->name);*/
352 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
353 mono_class_init (klass);
356 field = mono_class_get_field_from_name (klass, fname);
360 g_warning ("field load from %x", class);
365 mono_loader_set_error_field_load (klass, fname);
371 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
372 MonoGenericContext *context)
376 MonoClassField *field;
378 if (image->dynamic) {
379 MonoClassField *result = mono_lookup_dynamic_token (image, token);
380 *retklass = result->parent;
385 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
386 *retklass = field->parent;
387 mono_loader_unlock ();
390 mono_loader_unlock ();
392 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
393 field = field_from_memberref (image, token, retklass, context);
395 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
398 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
404 field = mono_class_get_field (k, token);
408 if (field && !field->parent->generic_class)
409 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
410 mono_loader_unlock ();
415 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
419 if (sig1->hasthis != sig2->hasthis ||
420 sig1->sentinelpos != sig2->sentinelpos)
423 for (i = 0; i < sig1->sentinelpos; i++) {
424 MonoType *p1 = sig1->params[i];
425 MonoType *p2 = sig2->params[i];
427 /*if (p1->attrs != p2->attrs)
430 if (!mono_metadata_type_equal (p1, p2))
434 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
440 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
441 MonoMethodSignature *sig, MonoClass *from_class)
445 mono_class_setup_methods (in_class);
446 for (i = 0; i < in_class->method.count; ++i) {
447 MonoMethod *m = in_class->methods [i];
449 if (!((fqname && !strcmp (m->name, fqname)) ||
450 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
453 if (sig->call_convention == MONO_CALL_VARARG) {
454 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
457 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
462 if (i < in_class->method.count) {
463 mono_class_setup_methods (from_class);
464 g_assert (from_class->method.count == in_class->method.count);
465 return from_class->methods [i];
471 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
474 char *qname, *fqname, *class_name;
475 gboolean is_interface;
476 MonoMethod *result = NULL;
478 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
481 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
483 qname = g_strconcat (class_name, ".", name, NULL);
484 if (ic->name_space && ic->name_space [0])
485 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
489 class_name = qname = fqname = NULL;
492 g_assert (from_class);
493 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
497 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
500 g_assert (from_class->interface_count == in_class->interface_count);
501 for (i = 0; i < in_class->interface_count; i++) {
502 MonoClass *ic = in_class->interfaces [i];
503 MonoClass *from_ic = from_class->interfaces [i];
505 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
510 in_class = in_class->parent;
511 from_class = from_class->parent;
513 g_assert (!in_class == !from_class);
516 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
525 static MonoMethodSignature*
526 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
528 MonoMethodSignature *res;
535 res = mono_metadata_signature_alloc (image, sig->param_count);
536 res->ret = mono_class_inflate_generic_type (sig->ret, context);
537 is_open = mono_class_is_open_constructed_type (res->ret);
538 for (i = 0; i < sig->param_count; ++i) {
539 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
541 is_open = mono_class_is_open_constructed_type (res->params [i]);
543 res->hasthis = sig->hasthis;
544 res->explicit_this = sig->explicit_this;
545 res->call_convention = sig->call_convention;
546 res->pinvoke = sig->pinvoke;
547 res->generic_param_count = sig->generic_param_count;
548 res->sentinelpos = sig->sentinelpos;
549 res->has_type_parameters = is_open;
550 res->is_inflated = 1;
554 static MonoMethodHeader*
555 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
557 MonoMethodHeader *res;
559 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
560 res->code = header->code;
561 res->code_size = header->code_size;
562 res->max_stack = header->max_stack;
563 res->num_clauses = header->num_clauses;
564 res->init_locals = header->init_locals;
565 res->num_locals = header->num_locals;
566 res->clauses = header->clauses;
567 for (i = 0; i < header->num_locals; ++i)
568 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
573 * token is the method_ref or method_def token used in a call IL instruction.
576 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
578 int table = mono_metadata_token_table (token);
579 int idx = mono_metadata_token_index (token);
580 guint32 cols [MONO_MEMBERREF_SIZE];
581 MonoMethodSignature *sig, *prev_sig;
584 /* !table is for wrappers: we should really assign their own token to them */
585 if (!table || table == MONO_TABLE_METHOD)
586 return mono_method_signature (method);
588 if (table == MONO_TABLE_METHODSPEC) {
589 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
590 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
591 mono_method_signature (method));
592 g_assert (method->is_inflated);
594 return mono_method_signature (method);
597 if (method->klass->generic_class)
598 return mono_method_signature (method);
601 /* FIXME: This might be incorrect for vararg methods */
602 return mono_method_signature (method);
605 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
606 mono_loader_unlock ();
608 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
610 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
611 mono_metadata_decode_blob_size (ptr, &ptr);
612 sig = mono_metadata_parse_method_signature_full (
613 image, context ? context->container : NULL, 0, ptr, NULL);
616 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
618 /* Somebody got in before us */
619 /* FIXME: Free sig */
623 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
624 mono_loader_unlock ();
627 sig = inflate_generic_signature (image, sig, context);
633 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
635 return mono_method_get_signature_full (method, image, token, NULL);
639 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
641 MonoClass *klass = NULL;
642 MonoMethod *method = NULL;
643 MonoTableInfo *tables = image->tables;
645 guint32 nindex, class;
647 MonoMethodSignature *sig;
650 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
651 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
652 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
653 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
654 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
656 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
659 case MONO_MEMBERREF_PARENT_TYPEREF:
660 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
662 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
663 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
664 mono_loader_set_error_method_load (name, mname);
669 case MONO_MEMBERREF_PARENT_TYPESPEC:
671 * Parse the TYPESPEC in the parent's context.
673 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
675 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
676 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
677 mono_loader_set_error_method_load (name, mname);
682 case MONO_MEMBERREF_PARENT_TYPEDEF:
683 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
685 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
686 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
687 mono_loader_set_error_method_load (name, mname);
692 case MONO_MEMBERREF_PARENT_METHODDEF:
693 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
697 /* This message leaks */
698 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
699 mono_loader_set_error_method_load ("", message);
705 mono_class_init (klass);
707 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
708 mono_metadata_decode_blob_size (ptr, &ptr);
710 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
715 case MONO_MEMBERREF_PARENT_TYPEREF:
716 case MONO_MEMBERREF_PARENT_TYPEDEF:
717 method = find_method (klass, NULL, mname, sig, klass);
720 case MONO_MEMBERREF_PARENT_TYPESPEC: {
724 type = &klass->byval_arg;
726 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
727 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
728 method = find_method (in_class, NULL, mname, sig, klass);
732 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
733 result->klass = klass;
734 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
735 result->flags = METHOD_ATTRIBUTE_PUBLIC;
736 result->signature = sig;
737 result->name = mname;
739 if (!strcmp (mname, ".ctor")) {
740 /* we special-case this in the runtime. */
744 if (!strcmp (mname, "Set")) {
745 g_assert (sig->hasthis);
746 g_assert (type->data.array->rank + 1 == sig->param_count);
747 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
751 if (!strcmp (mname, "Get")) {
752 g_assert (sig->hasthis);
753 g_assert (type->data.array->rank == sig->param_count);
754 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
758 if (!strcmp (mname, "Address")) {
759 g_assert (sig->hasthis);
760 g_assert (type->data.array->rank == sig->param_count);
761 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
765 g_assert_not_reached ();
769 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
770 g_assert_not_reached ();
774 char *msig = mono_signature_get_desc (sig, FALSE);
775 char * class_name = mono_type_get_name (&klass->byval_arg);
776 GString *s = g_string_new (mname);
777 if (sig->generic_param_count)
778 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
779 g_string_append_printf (s, "(%s)", msig);
781 msig = g_string_free (s, FALSE);
784 "Missing method %s::%s in assembly %s, referenced in assembly %s",
785 class_name, msig, klass->image->name, image->name);
786 mono_loader_set_error_method_load (class_name, mname);
790 mono_metadata_free_method_signature (sig);
796 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
798 MonoMethod *method, *inflated;
799 MonoTableInfo *tables = image->tables;
800 MonoGenericContext *new_context = NULL;
801 MonoGenericMethod *gmethod;
802 MonoGenericContainer *container = NULL;
804 guint32 cols [MONO_METHODSPEC_SIZE];
805 guint32 token, nindex, param_count;
807 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
808 token = cols [MONO_METHODSPEC_METHOD];
809 nindex = token >> MONO_METHODDEFORREF_BITS;
811 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
813 mono_metadata_decode_value (ptr, &ptr);
815 param_count = mono_metadata_decode_value (ptr, &ptr);
816 g_assert (param_count);
819 * Be careful with the two contexts here:
821 * ----------------------------------------
823 * static void Hello<T> (S s, T t) { }
825 * static void Test<U> (U u) {
826 * Foo<U>.Hello<string> (u, "World");
829 * ----------------------------------------
831 * Let's assume we're currently JITing Foo<int>.Test<long>
832 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
834 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
835 * (MONO_MEMBERREF_PARENT_TYPESPEC).
837 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
838 * context (S=int, U=long) to get the correct `Foo<long>'.
840 * After that, we parse the memberref signature in the new context
841 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
844 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
845 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
847 method = method_from_memberref (image, nindex, context);
849 method = mono_get_inflated_method (method);
851 container = method->generic_container;
852 g_assert (container);
854 gmethod = g_new0 (MonoGenericMethod, 1);
855 gmethod->generic_class = method->klass->generic_class;
856 gmethod->container = container;
858 new_context = g_new0 (MonoGenericContext, 1);
859 new_context->container = container;
860 new_context->gmethod = gmethod;
861 if (container->parent)
862 new_context->gclass = container->parent->context.gclass;
865 * When parsing the methodspec signature, we're in the old context again:
867 * ----------------------------------------
869 * static void Hello<T> (T t) { }
871 * static void Test<U> (U u) {
875 * ----------------------------------------
877 * Let's assume we're currently JITing "Foo.Test<float>".
879 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
880 * signature is "<!!0>". This means that we must instantiate the method type parameter
881 * `T' from the new method with the method type parameter `U' from the current context;
882 * ie. instantiate the method as `Foo.Hello<float>.
885 gmethod->inst = mono_metadata_parse_generic_inst (image, context ? context->container : NULL, param_count, ptr, &ptr);
887 if (context && gmethod->inst->is_open)
888 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
890 if (!container->method_hash)
891 container->method_hash = g_hash_table_new (
892 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
894 inflated = g_hash_table_lookup (container->method_hash, gmethod);
897 g_free (new_context);
901 context = new_context;
903 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
904 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
906 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
907 g_hash_table_insert (container->method_hash, gmethod, inflated);
912 typedef struct MonoDllMap MonoDllMap;
921 static GHashTable *global_dll_map;
924 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
925 MonoDllMap *map, *tmp;
934 map = g_hash_table_lookup (dll_map, dll);
936 mono_loader_unlock ();
939 *rdll = map->target? map->target: dll;
941 for (tmp = map->next; tmp; tmp = tmp->next) {
942 if (strcmp (func, tmp->name) == 0) {
946 mono_loader_unlock ();
951 mono_loader_unlock ();
956 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
959 if (assembly && assembly->dll_map) {
960 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
964 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
968 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
969 MonoDllMap *map, *entry;
970 GHashTable *dll_map = NULL;
976 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
977 dll_map = global_dll_map;
979 if (!assembly->dll_map)
980 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
981 dll_map = assembly->dll_map;
984 map = g_hash_table_lookup (dll_map, dll);
986 map = g_new0 (MonoDllMap, 1);
987 map->dll = g_strdup (dll);
989 map->target = g_strdup (tdll);
990 g_hash_table_insert (dll_map, map->dll, map);
993 entry = g_new0 (MonoDllMap, 1);
994 entry->name = g_strdup (func);
996 entry->target = g_strdup (tfunc);
997 if (tdll && map->target && strcmp (map->target, tdll))
998 entry->dll = g_strdup (tdll);
999 entry->next = map->next;
1003 mono_loader_unlock ();
1007 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1009 MonoImage *image = method->klass->image;
1010 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1011 MonoTableInfo *tables = image->tables;
1012 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1013 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1014 guint32 im_cols [MONO_IMPLMAP_SIZE];
1015 guint32 scope_token;
1016 const char *import = NULL;
1017 const char *orig_scope;
1018 const char *new_scope;
1019 char *full_name, *file_name;
1021 GModule *gmodule = NULL;
1023 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1026 return piinfo->addr;
1028 if (method->klass->image->dynamic) {
1029 MonoReflectionMethodAux *method_aux =
1030 g_hash_table_lookup (
1031 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1035 import = method_aux->dllentry;
1036 orig_scope = method_aux->dll;
1039 if (!piinfo->implmap_idx)
1042 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1044 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1045 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1046 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1047 orig_scope = mono_metadata_string_heap (image, scope_token);
1050 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1052 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1053 "DllImport attempting to load: '%s'.", new_scope);
1060 /* we allow a special name to dlopen from the running process namespace */
1061 if (strcmp (new_scope, "__Internal") == 0)
1062 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
1065 * Try loading the module using a variety of names
1067 for (i = 0; i < 4; ++i) {
1070 /* Try the original name */
1071 file_name = g_strdup (new_scope);
1074 /* Try trimming the .dll extension */
1075 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1076 file_name = g_strdup (new_scope);
1077 file_name [strlen (new_scope) - 4] = '\0';
1083 if (strstr (new_scope, "lib") != new_scope) {
1084 file_name = g_strdup_printf ("lib%s", new_scope);
1090 #ifndef PLATFORM_WIN32
1091 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1092 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1093 !g_ascii_strcasecmp ("user32", new_scope) ||
1094 !g_ascii_strcasecmp ("kernel", new_scope)) {
1095 file_name = g_strdup ("libMonoSupportW.so");
1099 #ifndef PLATFORM_WIN32
1105 full_name = g_module_build_path (NULL, file_name);
1106 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1107 "DllImport loading location: '%s'.", full_name);
1108 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1110 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1111 "DllImport error loading library: '%s'.",
1118 full_name = g_module_build_path (".", file_name);
1119 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1120 "DllImport loading library: '%s'.", full_name);
1121 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1123 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1124 "DllImport error loading library '%s'.",
1131 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1132 "DllImport loading: '%s'.", file_name);
1133 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
1135 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1136 "DllImport error loading library '%s'.",
1148 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1149 "DllImport unable to load library '%s'.",
1153 *exc_class = "DllNotFoundException";
1154 *exc_arg = new_scope;
1159 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1160 "Searching for '%s'.", import);
1162 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1163 g_module_symbol (gmodule, import, &piinfo->addr);
1165 char *mangled_name = NULL, *mangled_name2 = NULL;
1168 int mangle_param_count;
1169 #ifdef PLATFORM_WIN32
1174 * Search using a variety of mangled names
1176 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1177 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1178 gboolean need_param_count = FALSE;
1179 #ifdef PLATFORM_WIN32
1180 if (mangle_stdcall > 0)
1181 need_param_count = TRUE;
1183 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1188 mangled_name = (char*)import;
1189 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1190 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1191 /* Try the mangled name first */
1192 if (mangle_charset == 0)
1193 mangled_name = g_strconcat (import, "W", NULL);
1195 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1196 #ifdef PLATFORM_WIN32
1197 if (mangle_charset == 0)
1198 mangled_name = g_strconcat (import, "W", NULL);
1200 /* Try the mangled name last */
1201 if (mangle_charset == 1)
1202 mangled_name = g_strconcat (import, "A", NULL);
1205 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1207 /* Try the mangled name last */
1208 if (mangle_charset == 1)
1209 mangled_name = g_strconcat (import, "A", NULL);
1213 #ifdef PLATFORM_WIN32
1214 if (mangle_param_count == 0)
1215 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1217 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1218 param_count = mangle_param_count;
1220 /* Try the stdcall mangled name */
1222 * gcc under windows creates mangled names without the underscore, but MS.NET
1223 * doesn't support it, so we doesn't support it either.
1225 if (mangle_stdcall == 1)
1226 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1228 mangled_name2 = mangled_name;
1230 mangled_name2 = mangled_name;
1233 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1234 "Probing '%s'.", mangled_name2);
1236 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
1239 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1240 "Found as '%s'.", mangled_name2);
1242 if (mangled_name != mangled_name2)
1243 g_free (mangled_name2);
1244 if (mangled_name != import)
1245 g_free (mangled_name);
1251 if (!piinfo->addr) {
1253 *exc_class = "EntryPointNotFoundException";
1258 return piinfo->addr;
1262 mono_get_shared_generic_method (MonoGenericContainer *container)
1264 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1265 gmethod->container = container;
1266 gmethod->generic_class = container->context.gclass;
1267 gmethod->inst = mono_get_shared_generic_inst (container);
1273 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1274 MonoGenericContext *context)
1277 int table = mono_metadata_token_table (token);
1278 int idx = mono_metadata_token_index (token);
1279 MonoTableInfo *tables = image->tables;
1280 MonoGenericContainer *generic_container = NULL, *container = NULL;
1281 const char *sig = NULL;
1283 guint32 cols [MONO_TYPEDEF_SIZE];
1286 return mono_lookup_dynamic_token (image, token);
1288 if (table != MONO_TABLE_METHOD) {
1289 if (table == MONO_TABLE_METHODSPEC)
1290 return method_from_methodspec (image, context, idx);
1291 if (table != MONO_TABLE_MEMBERREF)
1292 g_print("got wrong token: 0x%08x\n", token);
1293 g_assert (table == MONO_TABLE_MEMBERREF);
1294 result = method_from_memberref (image, idx, context);
1299 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
1301 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1302 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1303 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1305 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1308 guint32 type = mono_metadata_typedef_from_method (image, token);
1309 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1315 result->klass = klass;
1316 result->flags = cols [2];
1317 result->iflags = cols [1];
1318 result->token = token;
1319 result->name = mono_metadata_string_heap (image, cols [3]);
1321 container = klass->generic_container;
1322 generic_container = mono_metadata_load_generic_params (image, token, container);
1323 if (generic_container) {
1324 MonoGenericContext *context = &generic_container->context;
1326 context->gclass = container->context.gclass;
1327 context->gmethod = mono_get_shared_generic_method (generic_container);
1328 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1330 for (i = 0; i < generic_container->type_argc; i++) {
1331 generic_container->type_params [i].method = result;
1333 mono_class_from_generic_parameter (
1334 &generic_container->type_params [i], image, TRUE);
1337 container = generic_container;
1340 if (!sig) /* already taken from the methodref */
1341 sig = mono_metadata_blob_heap (image, cols [4]);
1342 size = mono_metadata_decode_blob_size (sig, &sig);
1344 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1345 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1346 result->string_ctor = 1;
1347 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1348 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1349 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1351 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1352 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1355 /* FIXME: lazyness for generics too, but how? */
1356 if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1357 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1358 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1359 gpointer loc = mono_image_rva_map (image, cols [0]);
1361 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1364 result->generic_container = generic_container;
1370 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1372 return mono_get_method_full (image, token, klass, NULL);
1376 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1377 MonoGenericContext *context)
1381 /* We do everything inside the lock to prevent creation races */
1383 mono_loader_lock ();
1385 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1386 mono_loader_unlock ();
1390 result = mono_get_method_from_token (image, token, klass, context);
1392 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1394 if (!(result && result->is_inflated))
1395 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1397 mono_loader_unlock ();
1403 * mono_get_method_constrained:
1405 * This is used when JITing the `constrained.' opcode.
1408 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1409 MonoGenericContext *context)
1411 MonoMethod *method, *result;
1412 MonoClass *ic = NULL;
1413 MonoGenericContext *class_context = NULL, *method_context = NULL;
1414 MonoMethodSignature *sig;
1416 mono_loader_lock ();
1418 method = mono_get_method_from_token (image, token, NULL, context);
1420 mono_loader_unlock ();
1424 mono_class_init (constrained_class);
1425 method = mono_get_inflated_method (method);
1426 sig = mono_method_signature (method);
1428 if (method->is_inflated && sig->generic_param_count) {
1429 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1430 sig = mono_method_signature (imethod->declaring);
1431 method_context = imethod->context;
1434 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1437 if (constrained_class->generic_class)
1438 class_context = constrained_class->generic_class->context;
1440 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1442 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1443 method->klass->name, method->name, image->name, token);
1444 mono_loader_unlock ();
1449 result = mono_class_inflate_generic_method (result, class_context);
1451 result = mono_class_inflate_generic_method (result, method_context);
1453 mono_loader_unlock ();
1458 mono_free_method (MonoMethod *method)
1460 if (method->signature) {
1462 * FIXME: This causes crashes because the types inside signatures and
1463 * locals are shared.
1465 /* mono_metadata_free_method_signature (method->signature); */
1466 /* g_free (method->signature); */
1469 if (method->dynamic) {
1470 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1472 g_free ((char*)method->name);
1473 if (mw->method.header)
1474 g_free ((char*)mw->method.header->code);
1475 g_free (mw->method_data);
1478 if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1480 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1481 g_free (((MonoMethodNormal*)method)->header);
1484 if (method->dynamic)
1489 mono_method_get_param_names (MonoMethod *method, const char **names)
1492 MonoClass *klass = method->klass;
1493 MonoTableInfo *methodt;
1494 MonoTableInfo *paramt;
1497 if (!mono_method_signature (method)->param_count)
1499 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1502 if (klass->generic_class) /* copy the names later */
1505 mono_class_init (klass);
1507 if (klass->image->dynamic) {
1508 MonoReflectionMethodAux *method_aux =
1509 g_hash_table_lookup (
1510 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1511 if (method_aux && method_aux->param_names) {
1512 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1513 if (method_aux->param_names [i + 1])
1514 names [i] = method_aux->param_names [i + 1];
1519 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1520 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1521 idx = mono_method_get_index (method);
1523 guint32 cols [MONO_PARAM_SIZE];
1524 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1526 if (idx < methodt->rows)
1527 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1529 lastp = paramt->rows + 1;
1530 for (i = param_index; i < lastp; ++i) {
1531 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1532 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1533 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1540 mono_method_get_param_token (MonoMethod *method, int index)
1542 MonoClass *klass = method->klass;
1543 MonoTableInfo *methodt;
1546 if (klass->generic_class)
1547 g_assert_not_reached ();
1549 mono_class_init (klass);
1551 if (klass->image->dynamic) {
1552 g_assert_not_reached ();
1555 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1556 idx = mono_method_get_index (method);
1558 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1560 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1567 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1570 MonoClass *klass = method->klass;
1571 MonoTableInfo *methodt;
1572 MonoTableInfo *paramt;
1575 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1578 if (method->klass->image->dynamic) {
1579 MonoReflectionMethodAux *method_aux =
1580 g_hash_table_lookup (
1581 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1582 if (method_aux && method_aux->param_marshall) {
1583 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1584 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1585 if (dyn_specs [i]) {
1586 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1587 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1593 mono_class_init (klass);
1595 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1596 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1597 idx = mono_method_get_index (method);
1599 guint32 cols [MONO_PARAM_SIZE];
1600 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1602 if (idx < methodt->rows)
1603 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1605 lastp = paramt->rows + 1;
1607 for (i = param_index; i < lastp; ++i) {
1608 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1610 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1612 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1614 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1623 mono_method_has_marshal_info (MonoMethod *method)
1626 MonoClass *klass = method->klass;
1627 MonoTableInfo *methodt;
1628 MonoTableInfo *paramt;
1631 if (method->klass->image->dynamic) {
1632 MonoReflectionMethodAux *method_aux =
1633 g_hash_table_lookup (
1634 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1635 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1637 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1644 mono_class_init (klass);
1646 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1647 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1648 idx = mono_method_get_index (method);
1650 guint32 cols [MONO_PARAM_SIZE];
1651 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1653 if (idx + 1 < methodt->rows)
1654 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1656 lastp = paramt->rows + 1;
1658 for (i = param_index; i < lastp; ++i) {
1659 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1661 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1670 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1673 g_assert (method != NULL);
1674 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1676 data = ((MonoMethodWrapper *)method)->method_data;
1677 g_assert (data != NULL);
1678 g_assert (id <= GPOINTER_TO_UINT (*data));
1683 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1684 g_error ("stack walk not installed");
1687 static MonoStackWalkImpl stack_walk = default_stack_walk;
1690 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1692 stack_walk (func, TRUE, user_data);
1696 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1698 stack_walk (func, FALSE, user_data);
1702 mono_install_stack_walk (MonoStackWalkImpl func)
1708 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1710 MonoMethod **dest = data;
1712 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1718 mono_method_get_last_managed (void)
1720 MonoMethod *m = NULL;
1721 stack_walk (last_managed, FALSE, &m);
1726 mono_loader_lock (void)
1728 EnterCriticalSection (&loader_mutex);
1732 mono_loader_unlock (void)
1734 LeaveCriticalSection (&loader_mutex);
1738 * mono_method_signature:
1740 * Return the signature of the method M. On failure, returns NULL.
1742 MonoMethodSignature*
1743 mono_method_signature (MonoMethod *m)
1749 gboolean can_cache_signature;
1750 MonoGenericContainer *container;
1754 return m->signature;
1756 mono_loader_lock ();
1759 mono_loader_unlock ();
1760 return m->signature;
1763 if (m->is_inflated) {
1764 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1765 MonoMethodSignature *signature;
1766 /* the lock is recursive */
1767 signature = mono_method_signature (imethod->declaring);
1768 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, imethod->context);
1769 mono_loader_unlock ();
1770 return m->signature;
1773 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1774 idx = mono_metadata_token_index (m->token);
1775 img = m->klass->image;
1777 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1779 g_assert (!m->klass->generic_class);
1780 container = m->generic_container;
1782 container = m->klass->generic_container;
1784 /* Generic signatures depend on the container so they cannot be cached */
1785 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1786 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1788 /* If the method has parameter attributes, that can modify the signature */
1789 pattrs = mono_metadata_get_param_attrs (img, idx);
1791 can_cache_signature = FALSE;
1795 if (can_cache_signature)
1796 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1798 if (!m->signature) {
1799 const char *sig_body;
1801 size = mono_metadata_decode_blob_size (sig, &sig_body);
1803 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1804 if (!m->signature) {
1805 mono_loader_unlock ();
1809 if (can_cache_signature)
1810 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1813 /* Verify metadata consistency */
1814 if (m->signature->generic_param_count) {
1815 if (!container || !container->is_method)
1816 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1817 if (container->type_argc != m->signature->generic_param_count)
1818 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1819 m->signature->generic_param_count, container->type_argc);
1820 } else if (container && container->is_method && container->type_argc)
1821 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1823 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1824 m->signature->pinvoke = 1;
1825 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1826 MonoCallConvention conv = 0;
1827 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1828 m->signature->pinvoke = 1;
1830 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1831 case 0: /* no call conv, so using default */
1832 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1833 conv = MONO_CALL_DEFAULT;
1835 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1838 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1839 conv = MONO_CALL_STDCALL;
1841 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1842 conv = MONO_CALL_THISCALL;
1844 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1845 conv = MONO_CALL_FASTCALL;
1847 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1848 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1850 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1851 g_assert_not_reached ();
1853 m->signature->call_convention = conv;
1856 mono_loader_unlock ();
1857 return m->signature;
1861 mono_method_get_name (MonoMethod *method)
1863 return method->name;
1867 mono_method_get_class (MonoMethod *method)
1869 return method->klass;
1873 mono_method_get_token (MonoMethod *method)
1875 return method->token;
1879 mono_method_get_header (MonoMethod *method)
1885 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1887 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))
1891 if (G_LIKELY (mn->header))
1897 mono_loader_lock ();
1900 mono_loader_unlock ();
1904 if (method->is_inflated) {
1905 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1906 MonoMethodHeader *header;
1907 /* the lock is recursive */
1908 header = mono_method_get_header (imethod->declaring);
1909 mn->header = inflate_generic_header (header, imethod->context);
1910 mono_loader_unlock ();
1914 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1915 idx = mono_metadata_token_index (method->token);
1916 img = method->klass->image;
1917 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1918 loc = mono_image_rva_map (img, rva);
1922 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1924 mono_loader_unlock ();
1929 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1932 *iflags = method->iflags;
1933 return method->flags;
1937 * Find the method index in the metadata methodDef table.
1940 mono_method_get_index (MonoMethod *method) {
1941 MonoClass *klass = method->klass;
1945 return mono_metadata_token_index (method->token);
1947 mono_class_setup_methods (klass);
1948 for (i = 0; i < klass->method.count; ++i) {
1949 if (method == klass->methods [i])
1950 return klass->method.first + 1 + i;