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.
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/metadata/profiler.h>
37 #include <mono/utils/mono-logger.h>
38 #include <mono/utils/mono-dl.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_TYPEDEF:
333 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | 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_TYPEREF:
346 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
348 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
349 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
353 mono_class_init (klass);
356 field = mono_class_get_field_from_name (klass, fname);
358 case MONO_MEMBERREF_PARENT_TYPESPEC: {
359 /*guint32 bcols [MONO_TYPESPEC_SIZE];
363 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
364 bcols, MONO_TYPESPEC_SIZE);
365 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
366 len = mono_metadata_decode_value (ptr, &ptr);
367 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
369 klass = mono_class_from_mono_type (type);
370 mono_class_init (klass);
371 g_print ("type in sig: %s\n", klass->name);*/
372 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
373 mono_class_init (klass);
376 field = mono_class_get_field_from_name (klass, fname);
380 g_warning ("field load from %x", class);
385 mono_loader_set_error_field_load (klass, fname);
391 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
392 MonoGenericContext *context)
396 MonoClassField *field;
398 if (image->dynamic) {
399 MonoClassField *result = mono_lookup_dynamic_token (image, token);
400 *retklass = result->parent;
405 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
406 *retklass = field->parent;
407 mono_loader_unlock ();
410 mono_loader_unlock ();
412 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
413 field = field_from_memberref (image, token, retklass, context);
415 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
418 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
424 field = mono_class_get_field (k, token);
428 if (field && !field->parent->generic_class)
429 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
430 mono_loader_unlock ();
435 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
439 if (sig1->hasthis != sig2->hasthis ||
440 sig1->sentinelpos != sig2->sentinelpos)
443 for (i = 0; i < sig1->sentinelpos; i++) {
444 MonoType *p1 = sig1->params[i];
445 MonoType *p2 = sig2->params[i];
447 /*if (p1->attrs != p2->attrs)
450 if (!mono_metadata_type_equal (p1, p2))
454 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
460 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
461 MonoMethodSignature *sig, MonoClass *from_class)
465 mono_class_setup_methods (in_class);
466 for (i = 0; i < in_class->method.count; ++i) {
467 MonoMethod *m = in_class->methods [i];
469 if (!((fqname && !strcmp (m->name, fqname)) ||
470 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
473 if (sig->call_convention == MONO_CALL_VARARG) {
474 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
477 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
482 if (i < in_class->method.count) {
483 mono_class_setup_methods (from_class);
484 g_assert (from_class->method.count == in_class->method.count);
485 return from_class->methods [i];
491 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
494 char *qname, *fqname, *class_name;
495 gboolean is_interface;
496 MonoMethod *result = NULL;
498 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
501 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
503 qname = g_strconcat (class_name, ".", name, NULL);
504 if (ic->name_space && ic->name_space [0])
505 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
509 class_name = qname = fqname = NULL;
512 g_assert (from_class);
513 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
517 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
520 g_assert (from_class->interface_count == in_class->interface_count);
521 for (i = 0; i < in_class->interface_count; i++) {
522 MonoClass *ic = in_class->interfaces [i];
523 MonoClass *from_ic = from_class->interfaces [i];
525 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
530 in_class = in_class->parent;
531 from_class = from_class->parent;
533 g_assert (!in_class == !from_class);
536 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
545 static MonoMethodSignature*
546 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
548 MonoMethodSignature *res;
555 res = mono_metadata_signature_alloc (image, sig->param_count);
556 res->ret = mono_class_inflate_generic_type (sig->ret, context);
557 is_open = mono_class_is_open_constructed_type (res->ret);
558 for (i = 0; i < sig->param_count; ++i) {
559 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
561 is_open = mono_class_is_open_constructed_type (res->params [i]);
563 res->hasthis = sig->hasthis;
564 res->explicit_this = sig->explicit_this;
565 res->call_convention = sig->call_convention;
566 res->pinvoke = sig->pinvoke;
567 res->generic_param_count = sig->generic_param_count;
568 res->sentinelpos = sig->sentinelpos;
569 res->has_type_parameters = is_open;
570 res->is_inflated = 1;
574 static MonoMethodHeader*
575 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
577 MonoMethodHeader *res;
579 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
580 res->code = header->code;
581 res->code_size = header->code_size;
582 res->max_stack = header->max_stack;
583 res->num_clauses = header->num_clauses;
584 res->init_locals = header->init_locals;
585 res->num_locals = header->num_locals;
586 res->clauses = header->clauses;
587 for (i = 0; i < header->num_locals; ++i)
588 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
589 if (res->num_clauses) {
590 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
591 for (i = 0; i < header->num_clauses; ++i) {
592 MonoExceptionClause *clause = &res->clauses [i];
594 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
596 t = mono_class_inflate_generic_type (&clause->data.catch_class->byval_arg, context);
597 clause->data.catch_class = mono_class_from_mono_type (t);
604 * token is the method_ref or method_def token used in a call IL instruction.
607 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
609 int table = mono_metadata_token_table (token);
610 int idx = mono_metadata_token_index (token);
611 guint32 cols [MONO_MEMBERREF_SIZE];
612 MonoMethodSignature *sig, *prev_sig;
615 /* !table is for wrappers: we should really assign their own token to them */
616 if (!table || table == MONO_TABLE_METHOD)
617 return mono_method_signature (method);
619 if (table == MONO_TABLE_METHODSPEC) {
620 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
621 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
622 mono_method_signature (method));
623 g_assert (method->is_inflated);
625 return mono_method_signature (method);
628 if (method->klass->generic_class)
629 return mono_method_signature (method);
632 /* FIXME: This might be incorrect for vararg methods */
633 return mono_method_signature (method);
636 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
637 mono_loader_unlock ();
639 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
641 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
642 mono_metadata_decode_blob_size (ptr, &ptr);
643 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
646 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
648 /* Somebody got in before us */
649 /* FIXME: Free sig */
653 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
654 mono_loader_unlock ();
657 sig = inflate_generic_signature (image, sig, context);
663 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
665 return mono_method_get_signature_full (method, image, token, NULL);
668 /* this is only for the typespec array methods */
670 search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
673 for (i = 0; i < klass->method.count; ++i) {
674 MonoMethod *method = klass->methods [i];
675 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
682 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
684 MonoClass *klass = NULL;
685 MonoMethod *method = NULL;
686 MonoTableInfo *tables = image->tables;
688 guint32 nindex, class;
690 MonoMethodSignature *sig;
693 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
694 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
695 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
696 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
697 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
699 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
702 case MONO_MEMBERREF_PARENT_TYPEREF:
703 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
705 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
706 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
707 mono_loader_set_error_method_load (name, mname);
712 case MONO_MEMBERREF_PARENT_TYPESPEC:
714 * Parse the TYPESPEC in the parent's context.
716 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
718 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
719 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
720 mono_loader_set_error_method_load (name, mname);
725 case MONO_MEMBERREF_PARENT_TYPEDEF:
726 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
728 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
729 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
730 mono_loader_set_error_method_load (name, mname);
735 case MONO_MEMBERREF_PARENT_METHODDEF:
736 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
740 /* This message leaks */
741 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
742 mono_loader_set_error_method_load ("", message);
748 mono_class_init (klass);
750 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
751 mono_metadata_decode_blob_size (ptr, &ptr);
753 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
758 case MONO_MEMBERREF_PARENT_TYPEREF:
759 case MONO_MEMBERREF_PARENT_TYPEDEF:
760 method = find_method (klass, NULL, mname, sig, klass);
763 case MONO_MEMBERREF_PARENT_TYPESPEC: {
767 type = &klass->byval_arg;
769 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
770 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
771 method = find_method (in_class, NULL, mname, sig, klass);
775 /* we're an array and we created these methods already in klass in mono_class_init () */
776 result = search_in_array_class (klass, mname, sig);
780 g_assert_not_reached ();
784 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
785 g_assert_not_reached ();
789 char *msig = mono_signature_get_desc (sig, FALSE);
790 char * class_name = mono_type_get_name (&klass->byval_arg);
791 GString *s = g_string_new (mname);
792 if (sig->generic_param_count)
793 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
794 g_string_append_printf (s, "(%s)", msig);
796 msig = g_string_free (s, FALSE);
799 "Missing method %s::%s in assembly %s, referenced in assembly %s",
800 class_name, msig, klass->image->name, image->name);
801 mono_loader_set_error_method_load (class_name, mname);
805 mono_metadata_free_method_signature (sig);
811 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
813 MonoMethod *method, *inflated;
814 MonoTableInfo *tables = image->tables;
815 MonoGenericContext *new_context = NULL;
816 MonoGenericMethod *gmethod;
817 MonoGenericContainer *container = NULL;
819 guint32 cols [MONO_METHODSPEC_SIZE];
820 guint32 token, nindex, param_count;
822 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
823 token = cols [MONO_METHODSPEC_METHOD];
824 nindex = token >> MONO_METHODDEFORREF_BITS;
826 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
828 mono_metadata_decode_value (ptr, &ptr);
830 param_count = mono_metadata_decode_value (ptr, &ptr);
831 g_assert (param_count);
834 * Be careful with the two contexts here:
836 * ----------------------------------------
838 * static void Hello<T> (S s, T t) { }
840 * static void Test<U> (U u) {
841 * Foo<U>.Hello<string> (u, "World");
844 * ----------------------------------------
846 * Let's assume we're currently JITing Foo<int>.Test<long>
847 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
849 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
850 * (MONO_MEMBERREF_PARENT_TYPESPEC).
852 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
853 * context (S=int, U=long) to get the correct `Foo<long>'.
855 * After that, we parse the memberref signature in the new context
856 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
859 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
860 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
862 method = method_from_memberref (image, nindex, context);
864 method = mono_get_inflated_method (method);
866 container = method->generic_container;
867 g_assert (container);
869 gmethod = g_new0 (MonoGenericMethod, 1);
870 if (method->klass->generic_class)
871 gmethod->class_inst = method->klass->generic_class->inst;
872 gmethod->container = container;
874 new_context = g_new0 (MonoGenericContext, 1);
875 new_context->gmethod = gmethod;
876 /* FIXME: Is this correct? */
877 if (container->parent)
878 new_context->class_inst = container->parent->context.class_inst;
881 * When parsing the methodspec signature, we're in the old context again:
883 * ----------------------------------------
885 * static void Hello<T> (T t) { }
887 * static void Test<U> (U u) {
891 * ----------------------------------------
893 * Let's assume we're currently JITing "Foo.Test<float>".
895 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
896 * signature is "<!!0>". This means that we must instantiate the method type parameter
897 * `T' from the new method with the method type parameter `U' from the current context;
898 * ie. instantiate the method as `Foo.Hello<float>.
901 gmethod->inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
903 if (context && gmethod->inst->is_open)
904 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
906 if (!container->method_hash)
907 container->method_hash = g_hash_table_new (
908 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
910 inflated = g_hash_table_lookup (container->method_hash, gmethod);
913 g_free (new_context);
917 context = new_context;
919 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
920 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
922 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
923 g_hash_table_insert (container->method_hash, gmethod, inflated);
928 typedef struct MonoDllMap MonoDllMap;
937 static GHashTable *global_dll_map;
940 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
941 MonoDllMap *map, *tmp;
950 map = g_hash_table_lookup (dll_map, dll);
952 mono_loader_unlock ();
955 *rdll = map->target? map->target: dll;
957 for (tmp = map->next; tmp; tmp = tmp->next) {
958 if (strcmp (func, tmp->name) == 0) {
962 mono_loader_unlock ();
967 mono_loader_unlock ();
972 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
975 if (assembly && assembly->dll_map) {
976 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
980 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
984 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
985 MonoDllMap *map, *entry;
986 GHashTable *dll_map = NULL;
992 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
993 dll_map = global_dll_map;
995 if (!assembly->dll_map)
996 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
997 dll_map = assembly->dll_map;
1000 map = g_hash_table_lookup (dll_map, dll);
1002 map = g_new0 (MonoDllMap, 1);
1003 map->dll = g_strdup (dll);
1005 map->target = g_strdup (tdll);
1006 g_hash_table_insert (dll_map, map->dll, map);
1009 entry = g_new0 (MonoDllMap, 1);
1010 entry->name = g_strdup (func);
1012 entry->target = g_strdup (tfunc);
1013 if (tdll && map->target && strcmp (map->target, tdll))
1014 entry->dll = g_strdup (tdll);
1015 entry->next = map->next;
1019 mono_loader_unlock ();
1022 static GHashTable *global_module_map;
1025 cached_module_load (const char *name, int flags, char **err)
1028 mono_loader_lock ();
1029 if (!global_module_map)
1030 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1031 res = g_hash_table_lookup (global_module_map, name);
1034 mono_loader_unlock ();
1037 res = mono_dl_open (name, flags, err);
1039 g_hash_table_insert (global_module_map, g_strdup (name), res);
1040 mono_loader_unlock ();
1045 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1047 MonoImage *image = method->klass->image;
1048 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1049 MonoTableInfo *tables = image->tables;
1050 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1051 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1052 guint32 im_cols [MONO_IMPLMAP_SIZE];
1053 guint32 scope_token;
1054 const char *import = NULL;
1055 const char *orig_scope;
1056 const char *new_scope;
1058 char *full_name, *file_name;
1060 MonoDl *module = NULL;
1062 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1065 return piinfo->addr;
1067 if (method->klass->image->dynamic) {
1068 MonoReflectionMethodAux *method_aux =
1069 g_hash_table_lookup (
1070 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1074 import = method_aux->dllentry;
1075 orig_scope = method_aux->dll;
1078 if (!piinfo->implmap_idx)
1081 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1083 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1084 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1085 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1086 orig_scope = mono_metadata_string_heap (image, scope_token);
1089 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1091 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1092 "DllImport attempting to load: '%s'.", new_scope);
1099 /* we allow a special name to dlopen from the running process namespace */
1100 if (strcmp (new_scope, "__Internal") == 0)
1101 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1104 * Try loading the module using a variety of names
1106 for (i = 0; i < 4; ++i) {
1109 /* Try the original name */
1110 file_name = g_strdup (new_scope);
1113 /* Try trimming the .dll extension */
1114 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1115 file_name = g_strdup (new_scope);
1116 file_name [strlen (new_scope) - 4] = '\0';
1122 if (strstr (new_scope, "lib") != new_scope) {
1123 file_name = g_strdup_printf ("lib%s", new_scope);
1129 #ifndef PLATFORM_WIN32
1130 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1131 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1132 !g_ascii_strcasecmp ("user32", new_scope) ||
1133 !g_ascii_strcasecmp ("kernel", new_scope)) {
1134 file_name = g_strdup ("libMonoSupportW.so");
1138 #ifndef PLATFORM_WIN32
1145 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1146 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1147 "DllImport loading location: '%s'.", full_name);
1148 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1150 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1151 "DllImport error loading library: '%s'.",
1163 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1164 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1165 "DllImport loading library: '%s'.", full_name);
1166 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1168 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1169 "DllImport error loading library '%s'.",
1180 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1181 "DllImport loading: '%s'.", file_name);
1182 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1184 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1185 "DllImport error loading library '%s'.",
1197 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1198 "DllImport unable to load library '%s'.",
1203 *exc_class = "DllNotFoundException";
1204 *exc_arg = new_scope;
1209 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1210 "Searching for '%s'.", import);
1212 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1213 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1215 char *mangled_name = NULL, *mangled_name2 = NULL;
1218 int mangle_param_count;
1219 #ifdef PLATFORM_WIN32
1224 * Search using a variety of mangled names
1226 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1227 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1228 gboolean need_param_count = FALSE;
1229 #ifdef PLATFORM_WIN32
1230 if (mangle_stdcall > 0)
1231 need_param_count = TRUE;
1233 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1238 mangled_name = (char*)import;
1239 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1240 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1241 /* Try the mangled name first */
1242 if (mangle_charset == 0)
1243 mangled_name = g_strconcat (import, "W", NULL);
1245 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1246 #ifdef PLATFORM_WIN32
1247 if (mangle_charset == 0)
1248 mangled_name = g_strconcat (import, "W", NULL);
1250 /* Try the mangled name last */
1251 if (mangle_charset == 1)
1252 mangled_name = g_strconcat (import, "A", NULL);
1255 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1257 /* Try the mangled name last */
1258 if (mangle_charset == 1)
1259 mangled_name = g_strconcat (import, "A", NULL);
1263 #ifdef PLATFORM_WIN32
1264 if (mangle_param_count == 0)
1265 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1267 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1268 param_count = mangle_param_count;
1270 /* Try the stdcall mangled name */
1272 * gcc under windows creates mangled names without the underscore, but MS.NET
1273 * doesn't support it, so we doesn't support it either.
1275 if (mangle_stdcall == 1)
1276 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1278 mangled_name2 = mangled_name;
1280 mangled_name2 = mangled_name;
1283 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1284 "Probing '%s'.", mangled_name2);
1286 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1289 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1290 "Found as '%s'.", mangled_name2);
1292 if (mangled_name != mangled_name2)
1293 g_free (mangled_name2);
1294 if (mangled_name != import)
1295 g_free (mangled_name);
1301 if (!piinfo->addr) {
1304 *exc_class = "EntryPointNotFoundException";
1309 return piinfo->addr;
1313 mono_get_shared_generic_method (MonoGenericContainer *container)
1315 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1316 gmethod->container = container;
1317 gmethod->class_inst = container->context.class_inst;
1318 gmethod->inst = mono_get_shared_generic_inst (container);
1324 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1325 MonoGenericContext *context)
1328 int table = mono_metadata_token_table (token);
1329 int idx = mono_metadata_token_index (token);
1330 MonoTableInfo *tables = image->tables;
1331 MonoGenericContainer *generic_container = NULL, *container = NULL;
1332 const char *sig = NULL;
1334 guint32 cols [MONO_TYPEDEF_SIZE];
1337 return mono_lookup_dynamic_token (image, token);
1339 if (table != MONO_TABLE_METHOD) {
1340 if (table == MONO_TABLE_METHODSPEC)
1341 return method_from_methodspec (image, context, idx);
1342 if (table != MONO_TABLE_MEMBERREF)
1343 g_print("got wrong token: 0x%08x\n", token);
1344 g_assert (table == MONO_TABLE_MEMBERREF);
1345 result = method_from_memberref (image, idx, context);
1350 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1352 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1353 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1354 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1356 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1358 mono_stats.method_count ++;
1361 guint32 type = mono_metadata_typedef_from_method (image, token);
1362 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1368 result->klass = klass;
1369 result->flags = cols [2];
1370 result->iflags = cols [1];
1371 result->token = token;
1372 result->name = mono_metadata_string_heap (image, cols [3]);
1374 container = klass->generic_container;
1375 generic_container = mono_metadata_load_generic_params (image, token, container);
1376 if (generic_container) {
1377 MonoGenericContext *context;
1379 generic_container->owner.method = result;
1380 context = &generic_container->context;
1382 context->class_inst = container->context.class_inst;
1383 context->gmethod = mono_get_shared_generic_method (generic_container);
1385 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1387 for (i = 0; i < generic_container->type_argc; i++)
1388 mono_class_from_generic_parameter (&generic_container->type_params [i], image, TRUE);
1390 container = generic_container;
1393 if (!sig) /* already taken from the methodref */
1394 sig = mono_metadata_blob_heap (image, cols [4]);
1395 size = mono_metadata_decode_blob_size (sig, &sig);
1397 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1398 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1399 result->string_ctor = 1;
1400 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1401 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1402 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1404 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1405 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1408 /* FIXME: lazyness for generics too, but how? */
1409 if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1410 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1411 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1412 gpointer loc = mono_image_rva_map (image, cols [0]);
1414 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1417 result->generic_container = generic_container;
1423 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1425 return mono_get_method_full (image, token, klass, NULL);
1429 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1430 MonoGenericContext *context)
1434 /* We do everything inside the lock to prevent creation races */
1436 mono_loader_lock ();
1438 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1439 mono_loader_unlock ();
1443 result = mono_get_method_from_token (image, token, klass, context);
1445 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1447 if (!(result && result->is_inflated))
1448 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1450 mono_loader_unlock ();
1456 * mono_get_method_constrained:
1458 * This is used when JITing the `constrained.' opcode.
1460 * This returns two values: the contrained method, which has been inflated
1461 * as the function return value; And the original CIL-stream method as
1462 * declared in cil_method. The later is used for verification.
1465 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1466 MonoGenericContext *context, MonoMethod **cil_method)
1468 MonoMethod *method, *result;
1469 MonoClass *ic = NULL;
1470 MonoGenericContext *class_context = NULL, *method_context = NULL;
1471 MonoMethodSignature *sig;
1473 mono_loader_lock ();
1475 *cil_method = mono_get_method_from_token (image, token, NULL, context);
1477 mono_loader_unlock ();
1481 mono_class_init (constrained_class);
1482 method = mono_get_inflated_method (*cil_method);
1483 sig = mono_method_signature (method);
1485 if (method->is_inflated && sig->generic_param_count) {
1486 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1487 sig = mono_method_signature (imethod->declaring);
1488 method_context = mono_method_get_context (method);
1491 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1494 if (constrained_class->generic_class)
1495 class_context = mono_class_get_context (constrained_class);
1497 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1499 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1500 method->klass->name, method->name, image->name, token);
1501 mono_loader_unlock ();
1506 result = mono_class_inflate_generic_method (result, class_context);
1508 result = mono_class_inflate_generic_method (result, method_context);
1510 mono_loader_unlock ();
1515 mono_free_method (MonoMethod *method)
1517 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1520 if (method->signature) {
1522 * FIXME: This causes crashes because the types inside signatures and
1523 * locals are shared.
1525 /* mono_metadata_free_method_signature (method->signature); */
1526 /* g_free (method->signature); */
1529 if (method->dynamic) {
1530 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1532 g_free ((char*)method->name);
1533 if (mw->method.header)
1534 g_free ((char*)mw->method.header->code);
1535 g_free (mw->method_data);
1538 if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1540 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1541 g_free (((MonoMethodNormal*)method)->header);
1544 if (method->dynamic)
1549 mono_method_get_param_names (MonoMethod *method, const char **names)
1552 MonoClass *klass = method->klass;
1553 MonoTableInfo *methodt;
1554 MonoTableInfo *paramt;
1557 if (!mono_method_signature (method)->param_count)
1559 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1562 if (klass->generic_class || klass->rank) /* copy the names later */
1565 mono_class_init (klass);
1567 if (klass->image->dynamic) {
1568 MonoReflectionMethodAux *method_aux =
1569 g_hash_table_lookup (
1570 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1571 if (method_aux && method_aux->param_names) {
1572 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1573 if (method_aux->param_names [i + 1])
1574 names [i] = method_aux->param_names [i + 1];
1579 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1580 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1581 idx = mono_method_get_index (method);
1583 guint32 cols [MONO_PARAM_SIZE];
1586 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1588 if (idx < methodt->rows)
1589 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1591 lastp = paramt->rows + 1;
1592 for (i = param_index; i < lastp; ++i) {
1593 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1594 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1595 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1602 mono_method_get_param_token (MonoMethod *method, int index)
1604 MonoClass *klass = method->klass;
1605 MonoTableInfo *methodt;
1608 if (klass->generic_class)
1609 g_assert_not_reached ();
1611 mono_class_init (klass);
1613 if (klass->image->dynamic) {
1614 g_assert_not_reached ();
1617 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1618 idx = mono_method_get_index (method);
1620 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1622 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1629 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1632 MonoClass *klass = method->klass;
1633 MonoTableInfo *methodt;
1634 MonoTableInfo *paramt;
1637 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1640 if (method->klass->image->dynamic) {
1641 MonoReflectionMethodAux *method_aux =
1642 g_hash_table_lookup (
1643 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1644 if (method_aux && method_aux->param_marshall) {
1645 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1646 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1647 if (dyn_specs [i]) {
1648 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1649 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1655 mono_class_init (klass);
1657 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1658 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1659 idx = mono_method_get_index (method);
1661 guint32 cols [MONO_PARAM_SIZE];
1662 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1664 if (idx < methodt->rows)
1665 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1667 lastp = paramt->rows + 1;
1669 for (i = param_index; i < lastp; ++i) {
1670 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1672 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1674 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1676 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1685 mono_method_has_marshal_info (MonoMethod *method)
1688 MonoClass *klass = method->klass;
1689 MonoTableInfo *methodt;
1690 MonoTableInfo *paramt;
1693 if (method->klass->image->dynamic) {
1694 MonoReflectionMethodAux *method_aux =
1695 g_hash_table_lookup (
1696 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1697 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1699 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1706 mono_class_init (klass);
1708 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1709 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1710 idx = mono_method_get_index (method);
1712 guint32 cols [MONO_PARAM_SIZE];
1713 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1715 if (idx + 1 < methodt->rows)
1716 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1718 lastp = paramt->rows + 1;
1720 for (i = param_index; i < lastp; ++i) {
1721 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1723 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1732 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1735 g_assert (method != NULL);
1736 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1738 data = ((MonoMethodWrapper *)method)->method_data;
1739 g_assert (data != NULL);
1740 g_assert (id <= GPOINTER_TO_UINT (*data));
1745 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1746 g_error ("stack walk not installed");
1749 static MonoStackWalkImpl stack_walk = default_stack_walk;
1752 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1754 stack_walk (func, TRUE, user_data);
1758 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1760 stack_walk (func, FALSE, user_data);
1764 mono_install_stack_walk (MonoStackWalkImpl func)
1770 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1772 MonoMethod **dest = data;
1774 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1780 mono_method_get_last_managed (void)
1782 MonoMethod *m = NULL;
1783 stack_walk (last_managed, FALSE, &m);
1788 mono_loader_lock (void)
1790 EnterCriticalSection (&loader_mutex);
1794 mono_loader_unlock (void)
1796 LeaveCriticalSection (&loader_mutex);
1800 * mono_method_signature:
1802 * Return the signature of the method M. On failure, returns NULL.
1804 MonoMethodSignature*
1805 mono_method_signature (MonoMethod *m)
1811 gboolean can_cache_signature;
1812 MonoGenericContainer *container;
1816 return m->signature;
1818 mono_loader_lock ();
1821 mono_loader_unlock ();
1822 return m->signature;
1825 if (m->is_inflated) {
1826 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1827 MonoMethodSignature *signature;
1828 /* the lock is recursive */
1829 signature = mono_method_signature (imethod->declaring);
1830 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, mono_method_get_context (m));
1831 mono_loader_unlock ();
1832 return m->signature;
1835 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1836 idx = mono_metadata_token_index (m->token);
1837 img = m->klass->image;
1839 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1841 g_assert (!m->klass->generic_class);
1842 container = m->generic_container;
1844 container = m->klass->generic_container;
1846 /* Generic signatures depend on the container so they cannot be cached */
1847 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1848 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1850 /* If the method has parameter attributes, that can modify the signature */
1851 pattrs = mono_metadata_get_param_attrs (img, idx);
1853 can_cache_signature = FALSE;
1857 if (can_cache_signature)
1858 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1860 if (!m->signature) {
1861 const char *sig_body;
1863 size = mono_metadata_decode_blob_size (sig, &sig_body);
1865 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1866 if (!m->signature) {
1867 mono_loader_unlock ();
1871 if (can_cache_signature)
1872 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1875 /* Verify metadata consistency */
1876 if (m->signature->generic_param_count) {
1877 if (!container || !container->is_method)
1878 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1879 if (container->type_argc != m->signature->generic_param_count)
1880 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1881 m->signature->generic_param_count, container->type_argc);
1882 } else if (container && container->is_method && container->type_argc)
1883 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1885 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1886 m->signature->pinvoke = 1;
1887 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1888 MonoCallConvention conv = 0;
1889 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1890 m->signature->pinvoke = 1;
1892 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1893 case 0: /* no call conv, so using default */
1894 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1895 conv = MONO_CALL_DEFAULT;
1897 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1900 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1901 conv = MONO_CALL_STDCALL;
1903 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1904 conv = MONO_CALL_THISCALL;
1906 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1907 conv = MONO_CALL_FASTCALL;
1909 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1910 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1912 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1913 g_assert_not_reached ();
1915 m->signature->call_convention = conv;
1918 mono_loader_unlock ();
1919 return m->signature;
1923 mono_method_get_name (MonoMethod *method)
1925 return method->name;
1929 mono_method_get_class (MonoMethod *method)
1931 return method->klass;
1935 mono_method_get_token (MonoMethod *method)
1937 return method->token;
1941 mono_method_get_header (MonoMethod *method)
1947 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1949 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))
1953 if (G_LIKELY (mn->header))
1959 mono_loader_lock ();
1962 mono_loader_unlock ();
1966 if (method->is_inflated) {
1967 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1968 MonoMethodHeader *header;
1969 /* the lock is recursive */
1970 header = mono_method_get_header (imethod->declaring);
1971 mn->header = inflate_generic_header (header, mono_method_get_context (method));
1972 mono_loader_unlock ();
1976 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1977 idx = mono_metadata_token_index (method->token);
1978 img = method->klass->image;
1979 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1980 loc = mono_image_rva_map (img, rva);
1984 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1986 mono_loader_unlock ();
1991 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1994 *iflags = method->iflags;
1995 return method->flags;
1999 * Find the method index in the metadata methodDef table.
2002 mono_method_get_index (MonoMethod *method) {
2003 MonoClass *klass = method->klass;
2007 return mono_metadata_token_index (method->token);
2009 mono_class_setup_methods (klass);
2010 for (i = 0; i < klass->method.count; ++i) {
2011 if (method == klass->methods [i]) {
2012 if (klass->image->uncompressed_metadata)
2013 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2015 return klass->method.first + i + 1;