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);
89 * mono_loader_set_error_type_load:
91 * Set the loader error for this thread.
94 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)
96 MonoLoaderError *error;
98 if (mono_loader_get_last_error ())
101 error = g_new0 (MonoLoaderError, 1);
102 error->kind = MONO_LOADER_ERROR_TYPE;
103 error->class_name = g_strdup (class_name);
104 error->assembly_name = g_strdup (assembly_name);
107 * This is not strictly needed, but some (most) of the loader code still
108 * can't deal with load errors, and this message is more helpful than an
111 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
113 set_loader_error (error);
117 * mono_loader_set_error_method_load:
119 * Set the loader error for this thread. MEMBER_NAME should point to a string
123 mono_loader_set_error_method_load (const char *class_name, const char *member_name)
125 MonoLoaderError *error;
127 /* FIXME: Store the signature as well */
128 if (mono_loader_get_last_error ())
131 error = g_new0 (MonoLoaderError, 1);
132 error->kind = MONO_LOADER_ERROR_METHOD;
133 error->class_name = g_strdup (class_name);
134 error->member_name = member_name;
136 set_loader_error (error);
140 * mono_loader_set_error_field_load:
142 * Set the loader error for this thread. MEMBER_NAME should point to a string
146 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
148 MonoLoaderError *error;
150 /* FIXME: Store the signature as well */
151 if (mono_loader_get_last_error ())
154 error = g_new0 (MonoLoaderError, 1);
155 error->kind = MONO_LOADER_ERROR_FIELD;
156 error->klass = klass;
157 error->member_name = member_name;
159 set_loader_error (error);
163 * mono_loader_get_last_error:
165 * Returns information about the last type load exception encountered by the loader, or
166 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
169 mono_loader_get_last_error (void)
171 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
175 * mono_loader_clear_error:
177 * Disposes any loader error messages on this thread
180 mono_loader_clear_error (void)
182 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
184 g_free (ex->class_name);
185 g_free (ex->assembly_name);
188 TlsSetValue (loader_error_thread_id, NULL);
192 * mono_loader_error_prepare_exception:
193 * @error: The MonoLoaderError to turn into an exception
195 * This turns a MonoLoaderError into an exception that can be thrown
196 * and resets the Mono Loader Error state during this process.
200 mono_loader_error_prepare_exception (MonoLoaderError *error)
202 MonoException *ex = NULL;
204 switch (error->kind) {
205 case MONO_LOADER_ERROR_TYPE: {
206 char *cname = g_strdup (error->class_name);
207 char *aname = g_strdup (error->assembly_name);
208 MonoString *class_name;
210 mono_loader_clear_error ();
212 class_name = mono_string_new (mono_domain_get (), cname);
214 ex = mono_get_exception_type_load (class_name, aname);
219 case MONO_LOADER_ERROR_METHOD: {
220 char *cname = g_strdup (error->class_name);
221 char *aname = g_strdup (error->member_name);
223 mono_loader_clear_error ();
224 ex = mono_get_exception_missing_method (cname, aname);
230 case MONO_LOADER_ERROR_FIELD: {
231 char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
232 char *cname = g_strdup (error->klass->name);
233 char *cmembername = g_strdup (error->member_name);
236 mono_loader_clear_error ();
237 class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
239 ex = mono_get_exception_missing_field (class_name, cmembername);
242 g_free (cmembername);
247 g_assert_not_reached ();
253 static MonoClassField*
254 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
255 MonoGenericContext *context)
258 MonoClassField *field;
259 MonoTableInfo *tables = image->tables;
261 guint32 nindex, class;
264 guint32 idx = mono_metadata_token_index (token);
266 if (image->dynamic) {
267 MonoClassField *result = mono_lookup_dynamic_token (image, token);
268 *retklass = result->parent;
272 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
273 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
274 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
276 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
278 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
279 mono_metadata_decode_blob_size (ptr, &ptr);
280 /* we may want to check the signature here... */
283 case MONO_MEMBERREF_PARENT_TYPEREF:
284 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
286 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
287 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
291 mono_class_init (klass);
294 field = mono_class_get_field_from_name (klass, fname);
296 case MONO_MEMBERREF_PARENT_TYPESPEC: {
297 /*guint32 bcols [MONO_TYPESPEC_SIZE];
301 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
302 bcols, MONO_TYPESPEC_SIZE);
303 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
304 len = mono_metadata_decode_value (ptr, &ptr);
305 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
307 klass = mono_class_from_mono_type (type);
308 mono_class_init (klass);
309 g_print ("type in sig: %s\n", klass->name);*/
310 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
311 mono_class_init (klass);
314 field = mono_class_get_field_from_name (klass, fname);
318 g_warning ("field load from %x", class);
323 mono_loader_set_error_field_load (klass, fname);
329 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
330 MonoGenericContext *context)
334 MonoClassField *field;
336 if (image->dynamic) {
337 MonoClassField *result = mono_lookup_dynamic_token (image, token);
338 *retklass = result->parent;
343 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
344 *retklass = field->parent;
345 mono_loader_unlock ();
348 mono_loader_unlock ();
350 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
351 field = field_from_memberref (image, token, retklass, context);
353 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
356 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
362 field = mono_class_get_field (k, token);
366 if (field && !field->parent->generic_class)
367 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
368 mono_loader_unlock ();
373 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
377 if (sig1->hasthis != sig2->hasthis ||
378 sig1->sentinelpos != sig2->sentinelpos)
381 for (i = 0; i < sig1->sentinelpos; i++) {
382 MonoType *p1 = sig1->params[i];
383 MonoType *p2 = sig2->params[i];
385 /*if (p1->attrs != p2->attrs)
388 if (!mono_metadata_type_equal (p1, p2))
392 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
398 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
399 MonoMethodSignature *sig, MonoClass *from_class)
403 mono_class_setup_methods (in_class);
404 for (i = 0; i < in_class->method.count; ++i) {
405 MonoMethod *m = in_class->methods [i];
407 if (!((fqname && !strcmp (m->name, fqname)) ||
408 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
411 if (sig->call_convention == MONO_CALL_VARARG) {
412 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
415 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
420 if (i < in_class->method.count) {
421 mono_class_setup_methods (from_class);
422 g_assert (from_class->method.count == in_class->method.count);
423 return from_class->methods [i];
429 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
432 char *qname, *fqname, *class_name;
433 gboolean is_interface;
434 MonoMethod *result = NULL;
436 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
439 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
441 qname = g_strconcat (class_name, ".", name, NULL);
442 if (ic->name_space && ic->name_space [0])
443 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
447 class_name = qname = fqname = NULL;
450 g_assert (from_class);
451 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
455 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
458 g_assert (from_class->interface_count == in_class->interface_count);
459 for (i = 0; i < in_class->interface_count; i++) {
460 MonoClass *ic = in_class->interfaces [i];
461 MonoClass *from_ic = from_class->interfaces [i];
463 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
468 in_class = in_class->parent;
469 from_class = from_class->parent;
471 g_assert (!in_class == !from_class);
474 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
483 static MonoMethodSignature*
484 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
486 MonoMethodSignature *res;
493 res = mono_metadata_signature_alloc (image, sig->param_count);
494 res->ret = mono_class_inflate_generic_type (sig->ret, context);
495 is_open = mono_class_is_open_constructed_type (res->ret);
496 for (i = 0; i < sig->param_count; ++i) {
497 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
499 is_open = mono_class_is_open_constructed_type (res->params [i]);
501 res->hasthis = sig->hasthis;
502 res->explicit_this = sig->explicit_this;
503 res->call_convention = sig->call_convention;
504 res->pinvoke = sig->pinvoke;
505 res->generic_param_count = sig->generic_param_count;
506 res->sentinelpos = sig->sentinelpos;
507 res->has_type_parameters = is_open;
508 res->is_inflated = 1;
512 static MonoMethodHeader*
513 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
515 MonoMethodHeader *res;
517 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
518 res->code = header->code;
519 res->code_size = header->code_size;
520 res->max_stack = header->max_stack;
521 res->num_clauses = header->num_clauses;
522 res->init_locals = header->init_locals;
523 res->num_locals = header->num_locals;
524 res->clauses = header->clauses;
525 for (i = 0; i < header->num_locals; ++i)
526 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
531 * token is the method_ref or method_def token used in a call IL instruction.
534 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
536 int table = mono_metadata_token_table (token);
537 int idx = mono_metadata_token_index (token);
538 guint32 cols [MONO_MEMBERREF_SIZE];
539 MonoMethodSignature *sig, *prev_sig;
542 /* !table is for wrappers: we should really assign their own token to them */
543 if (!table || table == MONO_TABLE_METHOD)
544 return mono_method_signature (method);
546 if (table == MONO_TABLE_METHODSPEC) {
547 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
548 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
549 mono_method_signature (method));
550 g_assert (method->is_inflated);
552 return mono_method_signature (method);
555 if (method->klass->generic_class)
556 return mono_method_signature (method);
559 /* FIXME: This might be incorrect for vararg methods */
560 return mono_method_signature (method);
563 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
564 mono_loader_unlock ();
566 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
568 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
569 mono_metadata_decode_blob_size (ptr, &ptr);
570 sig = mono_metadata_parse_method_signature_full (
571 image, context ? context->container : NULL, 0, ptr, NULL);
574 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
576 /* Somebody got in before us */
577 /* FIXME: Free sig */
581 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
582 mono_loader_unlock ();
585 sig = inflate_generic_signature (image, sig, context);
591 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
593 return mono_method_get_signature_full (method, image, token, NULL);
597 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
599 MonoClass *klass = NULL;
600 MonoMethod *method = NULL;
601 MonoTableInfo *tables = image->tables;
603 guint32 nindex, class;
605 MonoMethodSignature *sig;
608 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
609 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
610 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
611 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
612 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
614 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
617 case MONO_MEMBERREF_PARENT_TYPEREF:
618 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
620 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
621 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
622 mono_loader_set_error_method_load (name, mname);
627 case MONO_MEMBERREF_PARENT_TYPESPEC:
629 * Parse the TYPESPEC in the parent's context.
631 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
633 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
634 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
635 mono_loader_set_error_method_load (name, mname);
640 case MONO_MEMBERREF_PARENT_TYPEDEF:
641 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
643 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
644 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
645 mono_loader_set_error_method_load (name, mname);
650 case MONO_MEMBERREF_PARENT_METHODDEF:
651 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
655 /* This message leaks */
656 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
657 mono_loader_set_error_method_load ("", message);
663 mono_class_init (klass);
665 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
666 mono_metadata_decode_blob_size (ptr, &ptr);
668 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
673 case MONO_MEMBERREF_PARENT_TYPEREF:
674 case MONO_MEMBERREF_PARENT_TYPEDEF:
675 method = find_method (klass, NULL, mname, sig, klass);
678 case MONO_MEMBERREF_PARENT_TYPESPEC: {
682 type = &klass->byval_arg;
684 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
685 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
686 method = find_method (in_class, NULL, mname, sig, klass);
690 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
691 result->klass = klass;
692 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
693 result->flags = METHOD_ATTRIBUTE_PUBLIC;
694 result->signature = sig;
695 result->name = mname;
697 if (!strcmp (mname, ".ctor")) {
698 /* we special-case this in the runtime. */
702 if (!strcmp (mname, "Set")) {
703 g_assert (sig->hasthis);
704 g_assert (type->data.array->rank + 1 == sig->param_count);
705 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
709 if (!strcmp (mname, "Get")) {
710 g_assert (sig->hasthis);
711 g_assert (type->data.array->rank == sig->param_count);
712 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
716 if (!strcmp (mname, "Address")) {
717 g_assert (sig->hasthis);
718 g_assert (type->data.array->rank == sig->param_count);
719 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
723 g_assert_not_reached ();
727 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
728 g_assert_not_reached ();
732 char *msig = mono_signature_get_desc (sig, FALSE);
733 char * class_name = mono_type_get_name (&klass->byval_arg);
734 GString *s = g_string_new (mname);
735 if (sig->generic_param_count)
736 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
737 g_string_append_printf (s, "(%s)", msig);
739 msig = g_string_free (s, FALSE);
742 "Missing method %s::%s in assembly %s, referenced in assembly %s",
743 class_name, msig, klass->image->name, image->name);
744 mono_loader_set_error_method_load (class_name, mname);
748 mono_metadata_free_method_signature (sig);
754 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
756 MonoMethod *method, *inflated;
757 MonoTableInfo *tables = image->tables;
758 MonoGenericContext *new_context = NULL;
759 MonoGenericMethod *gmethod;
760 MonoGenericContainer *container = NULL;
762 guint32 cols [MONO_METHODSPEC_SIZE];
763 guint32 token, nindex, param_count;
765 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
766 token = cols [MONO_METHODSPEC_METHOD];
767 nindex = token >> MONO_METHODDEFORREF_BITS;
769 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
771 mono_metadata_decode_value (ptr, &ptr);
773 param_count = mono_metadata_decode_value (ptr, &ptr);
774 g_assert (param_count);
777 * Be careful with the two contexts here:
779 * ----------------------------------------
781 * static void Hello<T> (S s, T t) { }
783 * static void Test<U> (U u) {
784 * Foo<U>.Hello<string> (u, "World");
787 * ----------------------------------------
789 * Let's assume we're currently JITing Foo<int>.Test<long>
790 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
792 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
793 * (MONO_MEMBERREF_PARENT_TYPESPEC).
795 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
796 * context (S=int, U=long) to get the correct `Foo<long>'.
798 * After that, we parse the memberref signature in the new context
799 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
802 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
803 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
805 method = method_from_memberref (image, nindex, context);
807 method = mono_get_inflated_method (method);
809 container = method->generic_container;
810 g_assert (container);
812 gmethod = g_new0 (MonoGenericMethod, 1);
813 gmethod->generic_class = method->klass->generic_class;
814 gmethod->container = container;
816 new_context = g_new0 (MonoGenericContext, 1);
817 new_context->container = container;
818 new_context->gmethod = gmethod;
819 if (container->parent)
820 new_context->gclass = container->parent->context.gclass;
823 * When parsing the methodspec signature, we're in the old context again:
825 * ----------------------------------------
827 * static void Hello<T> (T t) { }
829 * static void Test<U> (U u) {
833 * ----------------------------------------
835 * Let's assume we're currently JITing "Foo.Test<float>".
837 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
838 * signature is "<!!0>". This means that we must instantiate the method type parameter
839 * `T' from the new method with the method type parameter `U' from the current context;
840 * ie. instantiate the method as `Foo.Hello<float>.
843 gmethod->inst = mono_metadata_parse_generic_inst (image, context ? context->container : NULL, param_count, ptr, &ptr);
845 if (context && gmethod->inst->is_open)
846 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
848 if (!container->method_hash)
849 container->method_hash = g_hash_table_new (
850 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
852 inflated = g_hash_table_lookup (container->method_hash, gmethod);
855 g_free (new_context);
859 context = new_context;
861 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
862 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
864 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
865 g_hash_table_insert (container->method_hash, gmethod, inflated);
870 typedef struct MonoDllMap MonoDllMap;
879 static GHashTable *global_dll_map;
882 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
883 MonoDllMap *map, *tmp;
892 map = g_hash_table_lookup (dll_map, dll);
894 mono_loader_unlock ();
897 *rdll = map->target? map->target: dll;
899 for (tmp = map->next; tmp; tmp = tmp->next) {
900 if (strcmp (func, tmp->name) == 0) {
904 mono_loader_unlock ();
909 mono_loader_unlock ();
914 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
917 if (assembly && assembly->dll_map) {
918 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
922 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
926 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
927 MonoDllMap *map, *entry;
928 GHashTable *dll_map = NULL;
934 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
935 dll_map = global_dll_map;
937 if (!assembly->dll_map)
938 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
939 dll_map = assembly->dll_map;
942 map = g_hash_table_lookup (dll_map, dll);
944 map = g_new0 (MonoDllMap, 1);
945 map->dll = g_strdup (dll);
947 map->target = g_strdup (tdll);
948 g_hash_table_insert (dll_map, map->dll, map);
951 entry = g_new0 (MonoDllMap, 1);
952 entry->name = g_strdup (func);
954 entry->target = g_strdup (tfunc);
955 if (tdll && map->target && strcmp (map->target, tdll))
956 entry->dll = g_strdup (tdll);
957 entry->next = map->next;
961 mono_loader_unlock ();
965 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
967 MonoImage *image = method->klass->image;
968 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
969 MonoTableInfo *tables = image->tables;
970 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
971 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
972 guint32 im_cols [MONO_IMPLMAP_SIZE];
974 const char *import = NULL;
975 const char *orig_scope;
976 const char *new_scope;
977 char *full_name, *file_name;
979 GModule *gmodule = NULL;
981 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
986 if (method->klass->image->dynamic) {
987 MonoReflectionMethodAux *method_aux =
988 g_hash_table_lookup (
989 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
993 import = method_aux->dllentry;
994 orig_scope = method_aux->dll;
997 if (!piinfo->implmap_idx)
1000 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1002 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1003 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1004 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1005 orig_scope = mono_metadata_string_heap (image, scope_token);
1008 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1010 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1011 "DllImport attempting to load: '%s'.", new_scope);
1018 /* we allow a special name to dlopen from the running process namespace */
1019 if (strcmp (new_scope, "__Internal") == 0)
1020 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
1023 * Try loading the module using a variety of names
1025 for (i = 0; i < 4; ++i) {
1028 /* Try the original name */
1029 file_name = g_strdup (new_scope);
1032 /* Try trimming the .dll extension */
1033 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1034 file_name = g_strdup (new_scope);
1035 file_name [strlen (new_scope) - 4] = '\0';
1041 if (strstr (new_scope, "lib") != new_scope) {
1042 file_name = g_strdup_printf ("lib%s", new_scope);
1048 #ifndef PLATFORM_WIN32
1049 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1050 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1051 !g_ascii_strcasecmp ("user32", new_scope) ||
1052 !g_ascii_strcasecmp ("kernel", new_scope)) {
1053 file_name = g_strdup ("libMonoSupportW.so");
1057 #ifndef PLATFORM_WIN32
1063 full_name = g_module_build_path (NULL, file_name);
1064 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1065 "DllImport loading location: '%s'.", full_name);
1066 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1068 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1069 "DllImport error loading library: '%s'.",
1076 full_name = g_module_build_path (".", file_name);
1077 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1078 "DllImport loading library: '%s'.", full_name);
1079 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1081 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1082 "DllImport error loading library '%s'.",
1089 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1090 "DllImport loading: '%s'.", file_name);
1091 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
1093 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1094 "DllImport error loading library '%s'.",
1106 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1107 "DllImport unable to load library '%s'.",
1111 *exc_class = "DllNotFoundException";
1112 *exc_arg = new_scope;
1117 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1118 "Searching for '%s'.", import);
1120 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1121 g_module_symbol (gmodule, import, &piinfo->addr);
1123 char *mangled_name = NULL, *mangled_name2 = NULL;
1126 int mangle_param_count;
1127 #ifdef PLATFORM_WIN32
1132 * Search using a variety of mangled names
1134 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1135 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1136 gboolean need_param_count = FALSE;
1137 #ifdef PLATFORM_WIN32
1138 if (mangle_stdcall > 0)
1139 need_param_count = TRUE;
1141 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1146 mangled_name = (char*)import;
1147 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1148 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1149 /* Try the mangled name first */
1150 if (mangle_charset == 0)
1151 mangled_name = g_strconcat (import, "W", NULL);
1153 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1154 #ifdef PLATFORM_WIN32
1155 if (mangle_charset == 0)
1156 mangled_name = g_strconcat (import, "W", NULL);
1158 /* Try the mangled name last */
1159 if (mangle_charset == 1)
1160 mangled_name = g_strconcat (import, "A", NULL);
1163 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1165 /* Try the mangled name last */
1166 if (mangle_charset == 1)
1167 mangled_name = g_strconcat (import, "A", NULL);
1171 #ifdef PLATFORM_WIN32
1172 if (mangle_param_count == 0)
1173 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1175 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1176 param_count = mangle_param_count;
1178 /* Try the stdcall mangled name */
1180 * gcc under windows creates mangled names without the underscore, but MS.NET
1181 * doesn't support it, so we doesn't support it either.
1183 if (mangle_stdcall == 1)
1184 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1186 mangled_name2 = mangled_name;
1188 mangled_name2 = mangled_name;
1191 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1192 "Probing '%s'.", mangled_name2);
1194 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
1197 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1198 "Found as '%s'.", mangled_name2);
1200 if (mangled_name != mangled_name2)
1201 g_free (mangled_name2);
1202 if (mangled_name != import)
1203 g_free (mangled_name);
1209 if (!piinfo->addr) {
1211 *exc_class = "EntryPointNotFoundException";
1216 return piinfo->addr;
1220 mono_get_shared_generic_method (MonoGenericContainer *container)
1222 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1223 gmethod->container = container;
1224 gmethod->generic_class = container->context.gclass;
1225 gmethod->inst = mono_get_shared_generic_inst (container);
1231 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1232 MonoGenericContext *context)
1235 int table = mono_metadata_token_table (token);
1236 int idx = mono_metadata_token_index (token);
1237 MonoTableInfo *tables = image->tables;
1238 MonoGenericContainer *generic_container = NULL, *container = NULL;
1239 const char *sig = NULL;
1241 guint32 cols [MONO_TYPEDEF_SIZE];
1244 return mono_lookup_dynamic_token (image, token);
1246 if (table != MONO_TABLE_METHOD) {
1247 if (table == MONO_TABLE_METHODSPEC)
1248 return method_from_methodspec (image, context, idx);
1249 if (table != MONO_TABLE_MEMBERREF)
1250 g_print("got wrong token: 0x%08x\n", token);
1251 g_assert (table == MONO_TABLE_MEMBERREF);
1252 result = method_from_memberref (image, idx, context);
1257 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
1259 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1260 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1261 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1263 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1266 guint32 type = mono_metadata_typedef_from_method (image, token);
1267 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1273 result->klass = klass;
1274 result->flags = cols [2];
1275 result->iflags = cols [1];
1276 result->token = token;
1277 result->name = mono_metadata_string_heap (image, cols [3]);
1279 container = klass->generic_container;
1280 generic_container = mono_metadata_load_generic_params (image, token, container);
1281 if (generic_container) {
1282 MonoGenericContext *context = &generic_container->context;
1284 context->gclass = container->context.gclass;
1285 context->gmethod = mono_get_shared_generic_method (generic_container);
1286 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1288 for (i = 0; i < generic_container->type_argc; i++) {
1289 generic_container->type_params [i].method = result;
1291 mono_class_from_generic_parameter (
1292 &generic_container->type_params [i], image, TRUE);
1295 container = generic_container;
1298 if (!sig) /* already taken from the methodref */
1299 sig = mono_metadata_blob_heap (image, cols [4]);
1300 size = mono_metadata_decode_blob_size (sig, &sig);
1302 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1303 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1304 result->string_ctor = 1;
1305 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1306 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1307 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1309 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1310 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1313 /* FIXME: lazyness for generics too, but how? */
1314 if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1315 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1316 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1317 gpointer loc = mono_image_rva_map (image, cols [0]);
1319 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1322 result->generic_container = generic_container;
1328 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1330 return mono_get_method_full (image, token, klass, NULL);
1334 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1335 MonoGenericContext *context)
1339 /* We do everything inside the lock to prevent creation races */
1341 mono_loader_lock ();
1343 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1344 mono_loader_unlock ();
1348 result = mono_get_method_from_token (image, token, klass, context);
1350 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1352 if (!(result && result->is_inflated))
1353 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1355 mono_loader_unlock ();
1361 * mono_get_method_constrained:
1363 * This is used when JITing the `constrained.' opcode.
1366 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1367 MonoGenericContext *context)
1369 MonoMethod *method, *result;
1370 MonoClass *ic = NULL;
1371 MonoGenericContext *class_context = NULL, *method_context = NULL;
1372 MonoMethodSignature *sig;
1374 mono_loader_lock ();
1376 method = mono_get_method_from_token (image, token, NULL, context);
1378 mono_loader_unlock ();
1382 mono_class_init (constrained_class);
1383 method = mono_get_inflated_method (method);
1384 sig = mono_method_signature (method);
1386 if (method->is_inflated && sig->generic_param_count) {
1387 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1388 sig = mono_method_signature (imethod->declaring);
1389 method_context = imethod->context;
1392 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1395 if (constrained_class->generic_class)
1396 class_context = constrained_class->generic_class->context;
1398 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1400 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1401 method->klass->name, method->name, image->name, token);
1402 mono_loader_unlock ();
1407 result = mono_class_inflate_generic_method (result, class_context);
1409 result = mono_class_inflate_generic_method (result, method_context);
1411 mono_loader_unlock ();
1416 mono_free_method (MonoMethod *method)
1418 if (method->signature) {
1420 * FIXME: This causes crashes because the types inside signatures and
1421 * locals are shared.
1423 /* mono_metadata_free_method_signature (method->signature); */
1424 /* g_free (method->signature); */
1427 if (method->dynamic) {
1428 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1430 g_free ((char*)method->name);
1431 if (mw->method.header)
1432 g_free ((char*)mw->method.header->code);
1433 g_free (mw->method_data);
1436 if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1438 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1439 g_free (((MonoMethodNormal*)method)->header);
1442 if (method->dynamic)
1447 mono_method_get_param_names (MonoMethod *method, const char **names)
1450 MonoClass *klass = method->klass;
1451 MonoTableInfo *methodt;
1452 MonoTableInfo *paramt;
1455 if (!mono_method_signature (method)->param_count)
1457 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1460 if (klass->generic_class) /* copy the names later */
1463 mono_class_init (klass);
1465 if (klass->image->dynamic) {
1466 MonoReflectionMethodAux *method_aux =
1467 g_hash_table_lookup (
1468 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1469 if (method_aux && method_aux->param_names) {
1470 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1471 if (method_aux->param_names [i + 1])
1472 names [i] = method_aux->param_names [i + 1];
1477 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1478 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1479 idx = mono_method_get_index (method);
1481 guint32 cols [MONO_PARAM_SIZE];
1482 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1484 if (idx < methodt->rows)
1485 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1487 lastp = paramt->rows + 1;
1488 for (i = param_index; i < lastp; ++i) {
1489 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1490 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1491 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1498 mono_method_get_param_token (MonoMethod *method, int index)
1500 MonoClass *klass = method->klass;
1501 MonoTableInfo *methodt;
1504 if (klass->generic_class)
1505 g_assert_not_reached ();
1507 mono_class_init (klass);
1509 if (klass->image->dynamic) {
1510 g_assert_not_reached ();
1513 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1514 idx = mono_method_get_index (method);
1516 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1518 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1525 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1528 MonoClass *klass = method->klass;
1529 MonoTableInfo *methodt;
1530 MonoTableInfo *paramt;
1533 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1536 if (method->klass->image->dynamic) {
1537 MonoReflectionMethodAux *method_aux =
1538 g_hash_table_lookup (
1539 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1540 if (method_aux && method_aux->param_marshall) {
1541 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1542 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1543 if (dyn_specs [i]) {
1544 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1545 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1551 mono_class_init (klass);
1553 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1554 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1555 idx = mono_method_get_index (method);
1557 guint32 cols [MONO_PARAM_SIZE];
1558 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1560 if (idx < methodt->rows)
1561 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1563 lastp = paramt->rows + 1;
1565 for (i = param_index; i < lastp; ++i) {
1566 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1568 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1570 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1572 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1581 mono_method_has_marshal_info (MonoMethod *method)
1584 MonoClass *klass = method->klass;
1585 MonoTableInfo *methodt;
1586 MonoTableInfo *paramt;
1589 if (method->klass->image->dynamic) {
1590 MonoReflectionMethodAux *method_aux =
1591 g_hash_table_lookup (
1592 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1593 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1595 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1602 mono_class_init (klass);
1604 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1605 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1606 idx = mono_method_get_index (method);
1608 guint32 cols [MONO_PARAM_SIZE];
1609 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1611 if (idx + 1 < methodt->rows)
1612 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1614 lastp = paramt->rows + 1;
1616 for (i = param_index; i < lastp; ++i) {
1617 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1619 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1628 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1631 g_assert (method != NULL);
1632 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1634 data = ((MonoMethodWrapper *)method)->method_data;
1635 g_assert (data != NULL);
1636 g_assert (id <= GPOINTER_TO_UINT (*data));
1641 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1642 g_error ("stack walk not installed");
1645 static MonoStackWalkImpl stack_walk = default_stack_walk;
1648 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1650 stack_walk (func, TRUE, user_data);
1654 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1656 stack_walk (func, FALSE, user_data);
1660 mono_install_stack_walk (MonoStackWalkImpl func)
1666 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1668 MonoMethod **dest = data;
1670 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1676 mono_method_get_last_managed (void)
1678 MonoMethod *m = NULL;
1679 stack_walk (last_managed, FALSE, &m);
1684 mono_loader_lock (void)
1686 EnterCriticalSection (&loader_mutex);
1690 mono_loader_unlock (void)
1692 LeaveCriticalSection (&loader_mutex);
1695 MonoMethodSignature*
1696 mono_method_signature (MonoMethod *m)
1702 gboolean can_cache_signature;
1703 MonoGenericContainer *container;
1707 return m->signature;
1709 mono_loader_lock ();
1712 mono_loader_unlock ();
1713 return m->signature;
1716 if (m->is_inflated) {
1717 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1718 MonoMethodSignature *signature;
1719 /* the lock is recursive */
1720 signature = mono_method_signature (imethod->declaring);
1721 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, imethod->context);
1722 mono_loader_unlock ();
1723 return m->signature;
1726 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1727 idx = mono_metadata_token_index (m->token);
1728 img = m->klass->image;
1730 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1732 g_assert (!m->klass->generic_class);
1733 container = m->generic_container;
1735 container = m->klass->generic_container;
1737 /* Generic signatures depend on the container so they cannot be cached */
1738 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1739 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1741 /* If the method has parameter attributes, that can modify the signature */
1742 pattrs = mono_metadata_get_param_attrs (img, idx);
1744 can_cache_signature = FALSE;
1748 if (can_cache_signature)
1749 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1751 if (!m->signature) {
1752 const char *sig_body;
1754 size = mono_metadata_decode_blob_size (sig, &sig_body);
1756 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1758 if (can_cache_signature)
1759 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1762 /* Verify metadata consistency */
1763 if (m->signature->generic_param_count) {
1764 if (!container || !container->is_method)
1765 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1766 if (container->type_argc != m->signature->generic_param_count)
1767 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1768 m->signature->generic_param_count, container->type_argc);
1769 } else if (container && container->is_method && container->type_argc)
1770 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1772 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1773 m->signature->pinvoke = 1;
1774 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1775 MonoCallConvention conv = 0;
1776 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1777 m->signature->pinvoke = 1;
1779 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1780 case 0: /* no call conv, so using default */
1781 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1782 conv = MONO_CALL_DEFAULT;
1784 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1787 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1788 conv = MONO_CALL_STDCALL;
1790 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1791 conv = MONO_CALL_THISCALL;
1793 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1794 conv = MONO_CALL_FASTCALL;
1796 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1797 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1799 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1800 g_assert_not_reached ();
1802 m->signature->call_convention = conv;
1805 mono_loader_unlock ();
1806 return m->signature;
1810 mono_method_get_name (MonoMethod *method)
1812 return method->name;
1816 mono_method_get_class (MonoMethod *method)
1818 return method->klass;
1822 mono_method_get_token (MonoMethod *method)
1824 return method->token;
1828 mono_method_get_header (MonoMethod *method)
1834 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1836 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))
1840 if (G_LIKELY (mn->header))
1846 mono_loader_lock ();
1849 mono_loader_unlock ();
1853 if (method->is_inflated) {
1854 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1855 MonoMethodHeader *header;
1856 /* the lock is recursive */
1857 header = mono_method_get_header (imethod->declaring);
1858 mn->header = inflate_generic_header (header, imethod->context);
1859 mono_loader_unlock ();
1863 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1864 idx = mono_metadata_token_index (method->token);
1865 img = method->klass->image;
1866 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1867 loc = mono_image_rva_map (img, rva);
1871 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1873 mono_loader_unlock ();
1878 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1881 *iflags = method->iflags;
1882 return method->flags;
1886 * Find the method index in the metadata methodDef table.
1889 mono_method_get_index (MonoMethod *method) {
1890 MonoClass *klass = method->klass;
1894 return mono_metadata_token_index (method->token);
1896 mono_class_setup_methods (klass);
1897 for (i = 0; i < klass->method.count; ++i) {
1898 if (method == klass->methods [i])
1899 return klass->method.first + 1 + i;