2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
10 * Copyright (C) 2002-2006 Novell, Inc.
12 * This file is used by the interpreter and the JIT engine to locate
13 * assemblies. Used to load AssemblyRef and later to resolve various
17 * This should keep track of the assembly versions that we are loading.
26 #include <mono/metadata/metadata.h>
27 #include <mono/metadata/image.h>
28 #include <mono/metadata/assembly.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/cil-coff.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/metadata-internals.h>
33 #include <mono/metadata/loader.h>
34 #include <mono/metadata/class-internals.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/metadata/reflection.h>
37 #include <mono/metadata/profiler.h>
38 #include <mono/utils/mono-logger.h>
39 #include <mono/metadata/exception.h>
41 MonoDefaults mono_defaults;
44 * This lock protects the hash tables inside MonoImage used by the metadata
45 * loading functions in class.c and loader.c.
47 static CRITICAL_SECTION loader_mutex;
51 * This TLS variable contains the last type load error encountered by the loader.
53 guint32 loader_error_thread_id;
58 InitializeCriticalSection (&loader_mutex);
60 loader_error_thread_id = TlsAlloc ();
64 mono_loader_cleanup (void)
66 TlsFree (loader_error_thread_id);
68 /*DeleteCriticalSection (&loader_mutex);*/
72 * Handling of type load errors should be done as follows:
74 * If something could not be loaded, the loader should call one of the
75 * mono_loader_set_error_XXX functions ()
76 * with the appropriate arguments, then return NULL to report the failure. The error
77 * should be propagated until it reaches code which can throw managed exceptions. At that
78 * point, an exception should be thrown based on the information returned by
79 * mono_loader_get_error (). Then the error should be cleared by calling
80 * mono_loader_clear_error ().
84 set_loader_error (MonoLoaderError *error)
86 TlsSetValue (loader_error_thread_id, error);
90 * mono_loader_set_error_assembly_load:
92 * Set the loader error for this thread.
95 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only)
97 MonoLoaderError *error;
99 if (mono_loader_get_last_error ())
102 error = g_new0 (MonoLoaderError, 1);
103 error->kind = MONO_LOADER_ERROR_ASSEMBLY;
104 error->assembly_name = g_strdup (assembly_name);
105 error->ref_only = ref_only;
108 * This is not strictly needed, but some (most) of the loader code still
109 * can't deal with load errors, and this message is more helpful than an
113 g_warning ("Cannot resolve dependency to assembly '%s' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.", assembly_name);
115 g_warning ("Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
117 set_loader_error (error);
121 * mono_loader_set_error_type_load:
123 * Set the loader error for this thread.
126 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
128 MonoLoaderError *error;
130 if (mono_loader_get_last_error ())
133 error = g_new0 (MonoLoaderError, 1);
134 error->kind = MONO_LOADER_ERROR_TYPE;
135 error->class_name = g_strdup (class_name);
136 error->assembly_name = g_strdup (assembly_name);
139 * This is not strictly needed, but some (most) of the loader code still
140 * can't deal with load errors, and this message is more helpful than an
143 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
145 set_loader_error (error);
149 * mono_loader_set_error_method_load:
151 * Set the loader error for this thread. MEMBER_NAME should point to a string
155 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
157 MonoLoaderError *error;
159 /* FIXME: Store the signature as well */
160 if (mono_loader_get_last_error ())
163 error = g_new0 (MonoLoaderError, 1);
164 error->kind = MONO_LOADER_ERROR_METHOD;
165 error->class_name = g_strdup (class_name);
166 error->member_name = member_name;
168 set_loader_error (error);
172 * mono_loader_set_error_field_load:
174 * Set the loader error for this thread. MEMBER_NAME should point to a string
178 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
180 MonoLoaderError *error;
182 /* FIXME: Store the signature as well */
183 if (mono_loader_get_last_error ())
186 error = g_new0 (MonoLoaderError, 1);
187 error->kind = MONO_LOADER_ERROR_FIELD;
188 error->klass = klass;
189 error->member_name = member_name;
191 set_loader_error (error);
195 * mono_loader_get_last_error:
197 * Returns information about the last type load exception encountered by the loader, or
198 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
201 mono_loader_get_last_error (void)
203 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
207 * mono_loader_clear_error:
209 * Disposes any loader error messages on this thread
212 mono_loader_clear_error (void)
214 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
217 g_free (ex->class_name);
218 g_free (ex->assembly_name);
221 TlsSetValue (loader_error_thread_id, NULL);
226 * mono_loader_error_prepare_exception:
227 * @error: The MonoLoaderError to turn into an exception
229 * This turns a MonoLoaderError into an exception that can be thrown
230 * and resets the Mono Loader Error state during this process.
234 mono_loader_error_prepare_exception (MonoLoaderError *error)
236 MonoException *ex = NULL;
238 switch (error->kind) {
239 case MONO_LOADER_ERROR_TYPE: {
240 char *cname = g_strdup (error->class_name);
241 char *aname = g_strdup (error->assembly_name);
242 MonoString *class_name;
244 mono_loader_clear_error ();
246 class_name = mono_string_new (mono_domain_get (), cname);
248 ex = mono_get_exception_type_load (class_name, aname);
253 case MONO_LOADER_ERROR_METHOD: {
254 char *cname = g_strdup (error->class_name);
255 char *aname = g_strdup (error->member_name);
257 mono_loader_clear_error ();
258 ex = mono_get_exception_missing_method (cname, aname);
264 case MONO_LOADER_ERROR_FIELD: {
265 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
266 char *cname = g_strdup (error->klass->name);
267 char *cmembername = g_strdup (error->member_name);
270 mono_loader_clear_error ();
271 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
273 ex = mono_get_exception_missing_field (class_name, cmembername);
276 g_free (cmembername);
281 case MONO_LOADER_ERROR_ASSEMBLY: {
285 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);
287 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
289 mono_loader_clear_error ();
290 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), error->assembly_name));
296 g_assert_not_reached ();
302 static MonoClassField*
303 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
304 MonoGenericContext *context)
307 MonoClassField *field;
308 MonoTableInfo *tables = image->tables;
310 guint32 nindex, class;
313 guint32 idx = mono_metadata_token_index (token);
315 if (image->dynamic) {
316 MonoClassField *result = mono_lookup_dynamic_token (image, token);
317 *retklass = result->parent;
321 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
322 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
323 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
325 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
327 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
328 mono_metadata_decode_blob_size (ptr, &ptr);
329 /* we may want to check the signature here... */
332 case MONO_MEMBERREF_PARENT_TYPEREF:
333 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
335 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
336 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
340 mono_class_init (klass);
343 field = mono_class_get_field_from_name (klass, fname);
345 case MONO_MEMBERREF_PARENT_TYPESPEC: {
346 /*guint32 bcols [MONO_TYPESPEC_SIZE];
350 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
351 bcols, MONO_TYPESPEC_SIZE);
352 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
353 len = mono_metadata_decode_value (ptr, &ptr);
354 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
356 klass = mono_class_from_mono_type (type);
357 mono_class_init (klass);
358 g_print ("type in sig: %s\n", klass->name);*/
359 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
360 mono_class_init (klass);
363 field = mono_class_get_field_from_name (klass, fname);
367 g_warning ("field load from %x", class);
372 mono_loader_set_error_field_load (klass, fname);
378 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
379 MonoGenericContext *context)
383 MonoClassField *field;
385 if (image->dynamic) {
386 MonoClassField *result = mono_lookup_dynamic_token (image, token);
387 *retklass = result->parent;
392 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
393 *retklass = field->parent;
394 mono_loader_unlock ();
397 mono_loader_unlock ();
399 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
400 field = field_from_memberref (image, token, retklass, context);
402 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
405 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
411 field = mono_class_get_field (k, token);
415 if (field && !field->parent->generic_class)
416 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
417 mono_loader_unlock ();
422 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
426 if (sig1->hasthis != sig2->hasthis ||
427 sig1->sentinelpos != sig2->sentinelpos)
430 for (i = 0; i < sig1->sentinelpos; i++) {
431 MonoType *p1 = sig1->params[i];
432 MonoType *p2 = sig2->params[i];
434 /*if (p1->attrs != p2->attrs)
437 if (!mono_metadata_type_equal (p1, p2))
441 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
447 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
448 MonoMethodSignature *sig, MonoClass *from_class)
452 mono_class_setup_methods (in_class);
453 for (i = 0; i < in_class->method.count; ++i) {
454 MonoMethod *m = in_class->methods [i];
456 if (!((fqname && !strcmp (m->name, fqname)) ||
457 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
460 if (sig->call_convention == MONO_CALL_VARARG) {
461 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
464 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
469 if (i < in_class->method.count) {
470 mono_class_setup_methods (from_class);
471 g_assert (from_class->method.count == in_class->method.count);
472 return from_class->methods [i];
478 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
481 char *qname, *fqname, *class_name;
482 gboolean is_interface;
483 MonoMethod *result = NULL;
485 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
488 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
490 qname = g_strconcat (class_name, ".", name, NULL);
491 if (ic->name_space && ic->name_space [0])
492 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
496 class_name = qname = fqname = NULL;
499 g_assert (from_class);
500 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
504 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
507 g_assert (from_class->interface_count == in_class->interface_count);
508 for (i = 0; i < in_class->interface_count; i++) {
509 MonoClass *ic = in_class->interfaces [i];
510 MonoClass *from_ic = from_class->interfaces [i];
512 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
517 in_class = in_class->parent;
518 from_class = from_class->parent;
520 g_assert (!in_class == !from_class);
523 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
532 static MonoMethodSignature*
533 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
535 MonoMethodSignature *res;
542 res = mono_metadata_signature_alloc (image, sig->param_count);
543 res->ret = mono_class_inflate_generic_type (sig->ret, context);
544 is_open = mono_class_is_open_constructed_type (res->ret);
545 for (i = 0; i < sig->param_count; ++i) {
546 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
548 is_open = mono_class_is_open_constructed_type (res->params [i]);
550 res->hasthis = sig->hasthis;
551 res->explicit_this = sig->explicit_this;
552 res->call_convention = sig->call_convention;
553 res->pinvoke = sig->pinvoke;
554 res->generic_param_count = sig->generic_param_count;
555 res->sentinelpos = sig->sentinelpos;
556 res->has_type_parameters = is_open;
557 res->is_inflated = 1;
561 static MonoMethodHeader*
562 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
564 MonoMethodHeader *res;
566 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
567 res->code = header->code;
568 res->code_size = header->code_size;
569 res->max_stack = header->max_stack;
570 res->num_clauses = header->num_clauses;
571 res->init_locals = header->init_locals;
572 res->num_locals = header->num_locals;
573 res->clauses = header->clauses;
574 for (i = 0; i < header->num_locals; ++i)
575 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
580 * token is the method_ref or method_def token used in a call IL instruction.
583 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
585 int table = mono_metadata_token_table (token);
586 int idx = mono_metadata_token_index (token);
587 guint32 cols [MONO_MEMBERREF_SIZE];
588 MonoMethodSignature *sig, *prev_sig;
591 /* !table is for wrappers: we should really assign their own token to them */
592 if (!table || table == MONO_TABLE_METHOD)
593 return mono_method_signature (method);
595 if (table == MONO_TABLE_METHODSPEC) {
596 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
597 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
598 mono_method_signature (method));
599 g_assert (method->is_inflated);
601 return mono_method_signature (method);
604 if (method->klass->generic_class)
605 return mono_method_signature (method);
608 /* FIXME: This might be incorrect for vararg methods */
609 return mono_method_signature (method);
612 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
613 mono_loader_unlock ();
615 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
617 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
618 mono_metadata_decode_blob_size (ptr, &ptr);
619 sig = mono_metadata_parse_method_signature_full (
620 image, context ? context->container : NULL, 0, ptr, NULL);
623 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
625 /* Somebody got in before us */
626 /* FIXME: Free sig */
630 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
631 mono_loader_unlock ();
634 sig = inflate_generic_signature (image, sig, context);
640 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
642 return mono_method_get_signature_full (method, image, token, NULL);
646 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
648 MonoClass *klass = NULL;
649 MonoMethod *method = NULL;
650 MonoTableInfo *tables = image->tables;
652 guint32 nindex, class;
654 MonoMethodSignature *sig;
657 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
658 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
659 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
660 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
661 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
663 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
666 case MONO_MEMBERREF_PARENT_TYPEREF:
667 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
669 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
670 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
671 mono_loader_set_error_method_load (name, mname);
676 case MONO_MEMBERREF_PARENT_TYPESPEC:
678 * Parse the TYPESPEC in the parent's context.
680 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
682 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
683 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
684 mono_loader_set_error_method_load (name, mname);
689 case MONO_MEMBERREF_PARENT_TYPEDEF:
690 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
692 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
693 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
694 mono_loader_set_error_method_load (name, mname);
699 case MONO_MEMBERREF_PARENT_METHODDEF:
700 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
704 /* This message leaks */
705 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
706 mono_loader_set_error_method_load ("", message);
712 mono_class_init (klass);
714 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
715 mono_metadata_decode_blob_size (ptr, &ptr);
717 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
722 case MONO_MEMBERREF_PARENT_TYPEREF:
723 case MONO_MEMBERREF_PARENT_TYPEDEF:
724 method = find_method (klass, NULL, mname, sig, klass);
727 case MONO_MEMBERREF_PARENT_TYPESPEC: {
731 type = &klass->byval_arg;
733 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
734 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
735 method = find_method (in_class, NULL, mname, sig, klass);
739 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
740 result->klass = klass;
741 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
742 result->flags = METHOD_ATTRIBUTE_PUBLIC;
743 result->signature = sig;
744 result->name = mname;
746 if (!strcmp (mname, ".ctor")) {
747 /* we special-case this in the runtime. */
751 if (!strcmp (mname, "Set")) {
752 g_assert (sig->hasthis);
753 g_assert (type->data.array->rank + 1 == sig->param_count);
754 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
758 if (!strcmp (mname, "Get")) {
759 g_assert (sig->hasthis);
760 g_assert (type->data.array->rank == sig->param_count);
761 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
765 if (!strcmp (mname, "Address")) {
766 g_assert (sig->hasthis);
767 g_assert (type->data.array->rank == sig->param_count);
768 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
772 g_assert_not_reached ();
776 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
777 g_assert_not_reached ();
781 char *msig = mono_signature_get_desc (sig, FALSE);
782 char * class_name = mono_type_get_name (&klass->byval_arg);
783 GString *s = g_string_new (mname);
784 if (sig->generic_param_count)
785 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
786 g_string_append_printf (s, "(%s)", msig);
788 msig = g_string_free (s, FALSE);
791 "Missing method %s::%s in assembly %s, referenced in assembly %s",
792 class_name, msig, klass->image->name, image->name);
793 mono_loader_set_error_method_load (class_name, mname);
797 mono_metadata_free_method_signature (sig);
803 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
805 MonoMethod *method, *inflated;
806 MonoTableInfo *tables = image->tables;
807 MonoGenericContext *new_context = NULL;
808 MonoGenericMethod *gmethod;
809 MonoGenericContainer *container = NULL;
811 guint32 cols [MONO_METHODSPEC_SIZE];
812 guint32 token, nindex, param_count;
814 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
815 token = cols [MONO_METHODSPEC_METHOD];
816 nindex = token >> MONO_METHODDEFORREF_BITS;
818 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
820 mono_metadata_decode_value (ptr, &ptr);
822 param_count = mono_metadata_decode_value (ptr, &ptr);
823 g_assert (param_count);
826 * Be careful with the two contexts here:
828 * ----------------------------------------
830 * static void Hello<T> (S s, T t) { }
832 * static void Test<U> (U u) {
833 * Foo<U>.Hello<string> (u, "World");
836 * ----------------------------------------
838 * Let's assume we're currently JITing Foo<int>.Test<long>
839 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
841 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
842 * (MONO_MEMBERREF_PARENT_TYPESPEC).
844 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
845 * context (S=int, U=long) to get the correct `Foo<long>'.
847 * After that, we parse the memberref signature in the new context
848 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
851 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
852 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
854 method = method_from_memberref (image, nindex, context);
856 method = mono_get_inflated_method (method);
858 container = method->generic_container;
859 g_assert (container);
861 gmethod = g_new0 (MonoGenericMethod, 1);
862 gmethod->generic_class = method->klass->generic_class;
863 gmethod->container = container;
865 new_context = g_new0 (MonoGenericContext, 1);
866 new_context->container = container;
867 new_context->gmethod = gmethod;
868 if (container->parent)
869 new_context->gclass = container->parent->context.gclass;
872 * When parsing the methodspec signature, we're in the old context again:
874 * ----------------------------------------
876 * static void Hello<T> (T t) { }
878 * static void Test<U> (U u) {
882 * ----------------------------------------
884 * Let's assume we're currently JITing "Foo.Test<float>".
886 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
887 * signature is "<!!0>". This means that we must instantiate the method type parameter
888 * `T' from the new method with the method type parameter `U' from the current context;
889 * ie. instantiate the method as `Foo.Hello<float>.
892 gmethod->inst = mono_metadata_parse_generic_inst (image, context ? context->container : NULL, param_count, ptr, &ptr);
894 if (context && gmethod->inst->is_open)
895 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
897 if (!container->method_hash)
898 container->method_hash = g_hash_table_new (
899 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
901 inflated = g_hash_table_lookup (container->method_hash, gmethod);
904 g_free (new_context);
908 context = new_context;
910 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
911 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
913 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
914 g_hash_table_insert (container->method_hash, gmethod, inflated);
919 typedef struct MonoDllMap MonoDllMap;
928 static GHashTable *global_dll_map;
931 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
932 MonoDllMap *map, *tmp;
941 map = g_hash_table_lookup (dll_map, dll);
943 mono_loader_unlock ();
946 *rdll = map->target? map->target: dll;
948 for (tmp = map->next; tmp; tmp = tmp->next) {
949 if (strcmp (func, tmp->name) == 0) {
953 mono_loader_unlock ();
958 mono_loader_unlock ();
963 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
966 if (assembly && assembly->dll_map) {
967 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
971 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
975 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
976 MonoDllMap *map, *entry;
977 GHashTable *dll_map = NULL;
983 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
984 dll_map = global_dll_map;
986 if (!assembly->dll_map)
987 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
988 dll_map = assembly->dll_map;
991 map = g_hash_table_lookup (dll_map, dll);
993 map = g_new0 (MonoDllMap, 1);
994 map->dll = g_strdup (dll);
996 map->target = g_strdup (tdll);
997 g_hash_table_insert (dll_map, map->dll, map);
1000 entry = g_new0 (MonoDllMap, 1);
1001 entry->name = g_strdup (func);
1003 entry->target = g_strdup (tfunc);
1004 if (tdll && map->target && strcmp (map->target, tdll))
1005 entry->dll = g_strdup (tdll);
1006 entry->next = map->next;
1010 mono_loader_unlock ();
1014 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1016 MonoImage *image = method->klass->image;
1017 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1018 MonoTableInfo *tables = image->tables;
1019 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1020 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1021 guint32 im_cols [MONO_IMPLMAP_SIZE];
1022 guint32 scope_token;
1023 const char *import = NULL;
1024 const char *orig_scope;
1025 const char *new_scope;
1026 char *full_name, *file_name;
1028 GModule *gmodule = NULL;
1030 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1033 return piinfo->addr;
1035 if (method->klass->image->dynamic) {
1036 MonoReflectionMethodAux *method_aux =
1037 g_hash_table_lookup (
1038 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1042 import = method_aux->dllentry;
1043 orig_scope = method_aux->dll;
1046 if (!piinfo->implmap_idx)
1049 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1051 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1052 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1053 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1054 orig_scope = mono_metadata_string_heap (image, scope_token);
1057 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1059 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1060 "DllImport attempting to load: '%s'.", new_scope);
1067 /* we allow a special name to dlopen from the running process namespace */
1068 if (strcmp (new_scope, "__Internal") == 0)
1069 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
1072 * Try loading the module using a variety of names
1074 for (i = 0; i < 4; ++i) {
1077 /* Try the original name */
1078 file_name = g_strdup (new_scope);
1081 /* Try trimming the .dll extension */
1082 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1083 file_name = g_strdup (new_scope);
1084 file_name [strlen (new_scope) - 4] = '\0';
1090 if (strstr (new_scope, "lib") != new_scope) {
1091 file_name = g_strdup_printf ("lib%s", new_scope);
1097 #ifndef PLATFORM_WIN32
1098 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1099 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1100 !g_ascii_strcasecmp ("user32", new_scope) ||
1101 !g_ascii_strcasecmp ("kernel", new_scope)) {
1102 file_name = g_strdup ("libMonoSupportW.so");
1106 #ifndef PLATFORM_WIN32
1112 full_name = g_module_build_path (NULL, file_name);
1113 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1114 "DllImport loading location: '%s'.", full_name);
1115 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1117 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1118 "DllImport error loading library: '%s'.",
1125 full_name = g_module_build_path (".", file_name);
1126 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1127 "DllImport loading library: '%s'.", full_name);
1128 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1130 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1131 "DllImport error loading library '%s'.",
1138 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1139 "DllImport loading: '%s'.", file_name);
1140 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
1142 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1143 "DllImport error loading library '%s'.",
1155 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1156 "DllImport unable to load library '%s'.",
1160 *exc_class = "DllNotFoundException";
1161 *exc_arg = new_scope;
1166 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1167 "Searching for '%s'.", import);
1169 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1170 g_module_symbol (gmodule, import, &piinfo->addr);
1172 char *mangled_name = NULL, *mangled_name2 = NULL;
1175 int mangle_param_count;
1176 #ifdef PLATFORM_WIN32
1181 * Search using a variety of mangled names
1183 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1184 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1185 gboolean need_param_count = FALSE;
1186 #ifdef PLATFORM_WIN32
1187 if (mangle_stdcall > 0)
1188 need_param_count = TRUE;
1190 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1195 mangled_name = (char*)import;
1196 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1197 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1198 /* Try the mangled name first */
1199 if (mangle_charset == 0)
1200 mangled_name = g_strconcat (import, "W", NULL);
1202 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1203 #ifdef PLATFORM_WIN32
1204 if (mangle_charset == 0)
1205 mangled_name = g_strconcat (import, "W", NULL);
1207 /* Try the mangled name last */
1208 if (mangle_charset == 1)
1209 mangled_name = g_strconcat (import, "A", NULL);
1212 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1214 /* Try the mangled name last */
1215 if (mangle_charset == 1)
1216 mangled_name = g_strconcat (import, "A", NULL);
1220 #ifdef PLATFORM_WIN32
1221 if (mangle_param_count == 0)
1222 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1224 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1225 param_count = mangle_param_count;
1227 /* Try the stdcall mangled name */
1229 * gcc under windows creates mangled names without the underscore, but MS.NET
1230 * doesn't support it, so we doesn't support it either.
1232 if (mangle_stdcall == 1)
1233 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1235 mangled_name2 = mangled_name;
1237 mangled_name2 = mangled_name;
1240 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1241 "Probing '%s'.", mangled_name2);
1243 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
1246 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1247 "Found as '%s'.", mangled_name2);
1249 if (mangled_name != mangled_name2)
1250 g_free (mangled_name2);
1251 if (mangled_name != import)
1252 g_free (mangled_name);
1258 if (!piinfo->addr) {
1260 *exc_class = "EntryPointNotFoundException";
1265 return piinfo->addr;
1269 mono_get_shared_generic_method (MonoGenericContainer *container)
1271 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1272 gmethod->container = container;
1273 gmethod->generic_class = container->context.gclass;
1274 gmethod->inst = mono_get_shared_generic_inst (container);
1280 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1281 MonoGenericContext *context)
1284 int table = mono_metadata_token_table (token);
1285 int idx = mono_metadata_token_index (token);
1286 MonoTableInfo *tables = image->tables;
1287 MonoGenericContainer *generic_container = NULL, *container = NULL;
1288 const char *sig = NULL;
1290 guint32 cols [MONO_TYPEDEF_SIZE];
1293 return mono_lookup_dynamic_token (image, token);
1295 if (table != MONO_TABLE_METHOD) {
1296 if (table == MONO_TABLE_METHODSPEC)
1297 return method_from_methodspec (image, context, idx);
1298 if (table != MONO_TABLE_MEMBERREF)
1299 g_print("got wrong token: 0x%08x\n", token);
1300 g_assert (table == MONO_TABLE_MEMBERREF);
1301 result = method_from_memberref (image, idx, context);
1306 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
1308 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1309 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1310 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1312 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1314 mono_stats.method_count ++;
1317 guint32 type = mono_metadata_typedef_from_method (image, token);
1318 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1324 result->klass = klass;
1325 result->flags = cols [2];
1326 result->iflags = cols [1];
1327 result->token = token;
1328 result->name = mono_metadata_string_heap (image, cols [3]);
1330 container = klass->generic_container;
1331 generic_container = mono_metadata_load_generic_params (image, token, container);
1332 if (generic_container) {
1333 MonoGenericContext *context = &generic_container->context;
1335 context->gclass = container->context.gclass;
1336 context->gmethod = mono_get_shared_generic_method (generic_container);
1337 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1339 for (i = 0; i < generic_container->type_argc; i++) {
1340 generic_container->type_params [i].method = result;
1342 mono_class_from_generic_parameter (
1343 &generic_container->type_params [i], image, TRUE);
1346 container = generic_container;
1349 if (!sig) /* already taken from the methodref */
1350 sig = mono_metadata_blob_heap (image, cols [4]);
1351 size = mono_metadata_decode_blob_size (sig, &sig);
1353 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1354 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1355 result->string_ctor = 1;
1356 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1357 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1358 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1360 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1361 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1364 /* FIXME: lazyness for generics too, but how? */
1365 if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1366 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1367 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1368 gpointer loc = mono_image_rva_map (image, cols [0]);
1370 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1373 result->generic_container = generic_container;
1379 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1381 return mono_get_method_full (image, token, klass, NULL);
1385 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1386 MonoGenericContext *context)
1390 /* We do everything inside the lock to prevent creation races */
1392 mono_loader_lock ();
1394 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1395 mono_loader_unlock ();
1399 result = mono_get_method_from_token (image, token, klass, context);
1401 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1403 if (!(result && result->is_inflated))
1404 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1406 mono_loader_unlock ();
1412 * mono_get_method_constrained:
1414 * This is used when JITing the `constrained.' opcode.
1416 * This returns two values: the contrained method, which has been inflated
1417 * as the function return value; And the original CIL-stream method as
1418 * declared in cil_method. The later is used for verification.
1421 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1422 MonoGenericContext *context, MonoMethod **cil_method)
1424 MonoMethod *method, *result;
1425 MonoClass *ic = NULL;
1426 MonoGenericContext *class_context = NULL, *method_context = NULL;
1427 MonoMethodSignature *sig;
1429 mono_loader_lock ();
1431 *cil_method = mono_get_method_from_token (image, token, NULL, context);
1433 mono_loader_unlock ();
1437 mono_class_init (constrained_class);
1438 method = mono_get_inflated_method (*cil_method);
1439 sig = mono_method_signature (method);
1441 if (method->is_inflated && sig->generic_param_count) {
1442 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1443 sig = mono_method_signature (imethod->declaring);
1444 method_context = imethod->context;
1447 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1450 if (constrained_class->generic_class)
1451 class_context = constrained_class->generic_class->context;
1453 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1455 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1456 method->klass->name, method->name, image->name, token);
1457 mono_loader_unlock ();
1462 result = mono_class_inflate_generic_method (result, class_context);
1464 result = mono_class_inflate_generic_method (result, method_context);
1466 mono_loader_unlock ();
1471 mono_free_method (MonoMethod *method)
1473 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1476 if (method->signature) {
1478 * FIXME: This causes crashes because the types inside signatures and
1479 * locals are shared.
1481 /* mono_metadata_free_method_signature (method->signature); */
1482 /* g_free (method->signature); */
1485 if (method->dynamic) {
1486 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1488 g_free ((char*)method->name);
1489 if (mw->method.header)
1490 g_free ((char*)mw->method.header->code);
1491 g_free (mw->method_data);
1494 if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1496 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1497 g_free (((MonoMethodNormal*)method)->header);
1500 if (method->dynamic)
1505 mono_method_get_param_names (MonoMethod *method, const char **names)
1508 MonoClass *klass = method->klass;
1509 MonoTableInfo *methodt;
1510 MonoTableInfo *paramt;
1513 if (!mono_method_signature (method)->param_count)
1515 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1518 if (klass->generic_class) /* copy the names later */
1521 mono_class_init (klass);
1523 if (klass->image->dynamic) {
1524 MonoReflectionMethodAux *method_aux =
1525 g_hash_table_lookup (
1526 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1527 if (method_aux && method_aux->param_names) {
1528 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1529 if (method_aux->param_names [i + 1])
1530 names [i] = method_aux->param_names [i + 1];
1535 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1536 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1537 idx = mono_method_get_index (method);
1539 guint32 cols [MONO_PARAM_SIZE];
1540 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1542 if (idx < methodt->rows)
1543 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1545 lastp = paramt->rows + 1;
1546 for (i = param_index; i < lastp; ++i) {
1547 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1548 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1549 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1556 mono_method_get_param_token (MonoMethod *method, int index)
1558 MonoClass *klass = method->klass;
1559 MonoTableInfo *methodt;
1562 if (klass->generic_class)
1563 g_assert_not_reached ();
1565 mono_class_init (klass);
1567 if (klass->image->dynamic) {
1568 g_assert_not_reached ();
1571 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1572 idx = mono_method_get_index (method);
1574 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1576 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1583 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1586 MonoClass *klass = method->klass;
1587 MonoTableInfo *methodt;
1588 MonoTableInfo *paramt;
1591 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1594 if (method->klass->image->dynamic) {
1595 MonoReflectionMethodAux *method_aux =
1596 g_hash_table_lookup (
1597 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1598 if (method_aux && method_aux->param_marshall) {
1599 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1600 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1601 if (dyn_specs [i]) {
1602 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1603 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1609 mono_class_init (klass);
1611 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1612 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1613 idx = mono_method_get_index (method);
1615 guint32 cols [MONO_PARAM_SIZE];
1616 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1618 if (idx < methodt->rows)
1619 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1621 lastp = paramt->rows + 1;
1623 for (i = param_index; i < lastp; ++i) {
1624 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1626 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1628 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1630 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1639 mono_method_has_marshal_info (MonoMethod *method)
1642 MonoClass *klass = method->klass;
1643 MonoTableInfo *methodt;
1644 MonoTableInfo *paramt;
1647 if (method->klass->image->dynamic) {
1648 MonoReflectionMethodAux *method_aux =
1649 g_hash_table_lookup (
1650 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1651 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1653 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1660 mono_class_init (klass);
1662 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1663 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1664 idx = mono_method_get_index (method);
1666 guint32 cols [MONO_PARAM_SIZE];
1667 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1669 if (idx + 1 < methodt->rows)
1670 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1672 lastp = paramt->rows + 1;
1674 for (i = param_index; i < lastp; ++i) {
1675 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1677 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1686 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1689 g_assert (method != NULL);
1690 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1692 data = ((MonoMethodWrapper *)method)->method_data;
1693 g_assert (data != NULL);
1694 g_assert (id <= GPOINTER_TO_UINT (*data));
1699 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1700 g_error ("stack walk not installed");
1703 static MonoStackWalkImpl stack_walk = default_stack_walk;
1706 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1708 stack_walk (func, TRUE, user_data);
1712 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1714 stack_walk (func, FALSE, user_data);
1718 mono_install_stack_walk (MonoStackWalkImpl func)
1724 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1726 MonoMethod **dest = data;
1728 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1734 mono_method_get_last_managed (void)
1736 MonoMethod *m = NULL;
1737 stack_walk (last_managed, FALSE, &m);
1742 mono_loader_lock (void)
1744 EnterCriticalSection (&loader_mutex);
1748 mono_loader_unlock (void)
1750 LeaveCriticalSection (&loader_mutex);
1754 * mono_method_signature:
1756 * Return the signature of the method M. On failure, returns NULL.
1758 MonoMethodSignature*
1759 mono_method_signature (MonoMethod *m)
1765 gboolean can_cache_signature;
1766 MonoGenericContainer *container;
1770 return m->signature;
1772 mono_loader_lock ();
1775 mono_loader_unlock ();
1776 return m->signature;
1779 if (m->is_inflated) {
1780 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1781 MonoMethodSignature *signature;
1782 /* the lock is recursive */
1783 signature = mono_method_signature (imethod->declaring);
1784 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, imethod->context);
1785 mono_loader_unlock ();
1786 return m->signature;
1789 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1790 idx = mono_metadata_token_index (m->token);
1791 img = m->klass->image;
1793 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1795 g_assert (!m->klass->generic_class);
1796 container = m->generic_container;
1798 container = m->klass->generic_container;
1800 /* Generic signatures depend on the container so they cannot be cached */
1801 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1802 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1804 /* If the method has parameter attributes, that can modify the signature */
1805 pattrs = mono_metadata_get_param_attrs (img, idx);
1807 can_cache_signature = FALSE;
1811 if (can_cache_signature)
1812 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1814 if (!m->signature) {
1815 const char *sig_body;
1817 size = mono_metadata_decode_blob_size (sig, &sig_body);
1819 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1820 if (!m->signature) {
1821 mono_loader_unlock ();
1825 if (can_cache_signature)
1826 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1829 /* Verify metadata consistency */
1830 if (m->signature->generic_param_count) {
1831 if (!container || !container->is_method)
1832 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1833 if (container->type_argc != m->signature->generic_param_count)
1834 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1835 m->signature->generic_param_count, container->type_argc);
1836 } else if (container && container->is_method && container->type_argc)
1837 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1839 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1840 m->signature->pinvoke = 1;
1841 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1842 MonoCallConvention conv = 0;
1843 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1844 m->signature->pinvoke = 1;
1846 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1847 case 0: /* no call conv, so using default */
1848 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1849 conv = MONO_CALL_DEFAULT;
1851 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1854 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1855 conv = MONO_CALL_STDCALL;
1857 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1858 conv = MONO_CALL_THISCALL;
1860 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1861 conv = MONO_CALL_FASTCALL;
1863 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1864 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1866 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1867 g_assert_not_reached ();
1869 m->signature->call_convention = conv;
1872 mono_loader_unlock ();
1873 return m->signature;
1877 mono_method_get_name (MonoMethod *method)
1879 return method->name;
1883 mono_method_get_class (MonoMethod *method)
1885 return method->klass;
1889 mono_method_get_token (MonoMethod *method)
1891 return method->token;
1895 mono_method_get_header (MonoMethod *method)
1901 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1903 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))
1907 if (G_LIKELY (mn->header))
1913 mono_loader_lock ();
1916 mono_loader_unlock ();
1920 if (method->is_inflated) {
1921 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1922 MonoMethodHeader *header;
1923 /* the lock is recursive */
1924 header = mono_method_get_header (imethod->declaring);
1925 mn->header = inflate_generic_header (header, imethod->context);
1926 mono_loader_unlock ();
1930 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1931 idx = mono_metadata_token_index (method->token);
1932 img = method->klass->image;
1933 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1934 loc = mono_image_rva_map (img, rva);
1938 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1940 mono_loader_unlock ();
1945 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1948 *iflags = method->iflags;
1949 return method->flags;
1953 * Find the method index in the metadata methodDef table.
1956 mono_method_get_index (MonoMethod *method) {
1957 MonoClass *klass = method->klass;
1961 return mono_metadata_token_index (method->token);
1963 mono_class_setup_methods (klass);
1964 for (i = 0; i < klass->method.count; ++i) {
1965 if (method == klass->methods [i])
1966 return klass->method.first + 1 + i;