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->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
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->exception_type = MONO_EXCEPTION_TYPE_LOAD;
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->exception_type = MONO_EXCEPTION_MISSING_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->exception_type = MONO_EXCEPTION_MISSING_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->exception_type) {
239 case MONO_EXCEPTION_TYPE_LOAD: {
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_EXCEPTION_MISSING_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_EXCEPTION_MISSING_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_EXCEPTION_FILE_NOT_FOUND: {
286 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);
288 msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
289 filename = g_strdup (error->assembly_name);
290 /* Has to call this before calling anything which might call mono_class_init () */
291 mono_loader_clear_error ();
292 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
299 g_assert_not_reached ();
305 static MonoClassField*
306 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
307 MonoGenericContext *context)
310 MonoClassField *field;
311 MonoTableInfo *tables = image->tables;
313 guint32 nindex, class;
316 guint32 idx = mono_metadata_token_index (token);
318 if (image->dynamic) {
319 MonoClassField *result = mono_lookup_dynamic_token (image, token);
320 *retklass = result->parent;
324 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
325 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
326 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
328 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
330 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
331 mono_metadata_decode_blob_size (ptr, &ptr);
332 /* we may want to check the signature here... */
335 case MONO_MEMBERREF_PARENT_TYPEDEF:
336 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
338 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
339 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
343 mono_class_init (klass);
346 field = mono_class_get_field_from_name (klass, fname);
348 case MONO_MEMBERREF_PARENT_TYPEREF:
349 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
351 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
352 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
356 mono_class_init (klass);
359 field = mono_class_get_field_from_name (klass, fname);
361 case MONO_MEMBERREF_PARENT_TYPESPEC: {
362 /*guint32 bcols [MONO_TYPESPEC_SIZE];
366 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
367 bcols, MONO_TYPESPEC_SIZE);
368 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
369 len = mono_metadata_decode_value (ptr, &ptr);
370 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
372 klass = mono_class_from_mono_type (type);
373 mono_class_init (klass);
374 g_print ("type in sig: %s\n", klass->name);*/
375 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
376 mono_class_init (klass);
379 field = mono_class_get_field_from_name (klass, fname);
383 g_warning ("field load from %x", class);
388 mono_loader_set_error_field_load (klass, fname);
394 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
395 MonoGenericContext *context)
399 MonoClassField *field;
401 if (image->dynamic) {
402 MonoClassField *result = mono_lookup_dynamic_token (image, token);
403 *retklass = result->parent;
408 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
409 *retklass = field->parent;
410 mono_loader_unlock ();
413 mono_loader_unlock ();
415 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
416 field = field_from_memberref (image, token, retklass, context);
418 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
421 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
427 field = mono_class_get_field (k, token);
431 if (field && !field->parent->generic_class)
432 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
433 mono_loader_unlock ();
438 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
442 if (sig1->hasthis != sig2->hasthis ||
443 sig1->sentinelpos != sig2->sentinelpos)
446 for (i = 0; i < sig1->sentinelpos; i++) {
447 MonoType *p1 = sig1->params[i];
448 MonoType *p2 = sig2->params[i];
450 /*if (p1->attrs != p2->attrs)
453 if (!mono_metadata_type_equal (p1, p2))
457 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
463 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
464 MonoMethodSignature *sig, MonoClass *from_class)
468 mono_class_setup_methods (in_class);
469 for (i = 0; i < in_class->method.count; ++i) {
470 MonoMethod *m = in_class->methods [i];
472 if (!((fqname && !strcmp (m->name, fqname)) ||
473 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
476 if (sig->call_convention == MONO_CALL_VARARG) {
477 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
480 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
485 if (i < in_class->method.count) {
486 mono_class_setup_methods (from_class);
487 g_assert (from_class->method.count == in_class->method.count);
488 return from_class->methods [i];
494 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
497 char *qname, *fqname, *class_name;
498 gboolean is_interface;
499 MonoMethod *result = NULL;
501 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
504 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
506 qname = g_strconcat (class_name, ".", name, NULL);
507 if (ic->name_space && ic->name_space [0])
508 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
512 class_name = qname = fqname = NULL;
515 g_assert (from_class);
516 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
520 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
523 g_assert (from_class->interface_count == in_class->interface_count);
524 for (i = 0; i < in_class->interface_count; i++) {
525 MonoClass *ic = in_class->interfaces [i];
526 MonoClass *from_ic = from_class->interfaces [i];
528 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
533 in_class = in_class->parent;
534 from_class = from_class->parent;
536 g_assert (!in_class == !from_class);
539 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
548 static MonoMethodSignature*
549 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
551 MonoMethodSignature *res;
558 res = mono_metadata_signature_alloc (image, sig->param_count);
559 res->ret = mono_class_inflate_generic_type (sig->ret, context);
560 is_open = mono_class_is_open_constructed_type (res->ret);
561 for (i = 0; i < sig->param_count; ++i) {
562 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
564 is_open = mono_class_is_open_constructed_type (res->params [i]);
566 res->hasthis = sig->hasthis;
567 res->explicit_this = sig->explicit_this;
568 res->call_convention = sig->call_convention;
569 res->pinvoke = sig->pinvoke;
570 res->generic_param_count = sig->generic_param_count;
571 res->sentinelpos = sig->sentinelpos;
572 res->has_type_parameters = is_open;
573 res->is_inflated = 1;
577 static MonoMethodHeader*
578 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
580 MonoMethodHeader *res;
582 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
583 res->code = header->code;
584 res->code_size = header->code_size;
585 res->max_stack = header->max_stack;
586 res->num_clauses = header->num_clauses;
587 res->init_locals = header->init_locals;
588 res->num_locals = header->num_locals;
589 res->clauses = header->clauses;
590 for (i = 0; i < header->num_locals; ++i)
591 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
592 if (res->num_clauses) {
593 res->clauses = g_memdup (header->clauses, sizeof (MonoExceptionClause) * res->num_clauses);
594 for (i = 0; i < header->num_clauses; ++i) {
595 MonoExceptionClause *clause = &res->clauses [i];
597 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
599 t = mono_class_inflate_generic_type (&clause->data.catch_class->byval_arg, context);
600 clause->data.catch_class = mono_class_from_mono_type (t);
607 * token is the method_ref or method_def token used in a call IL instruction.
610 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
612 int table = mono_metadata_token_table (token);
613 int idx = mono_metadata_token_index (token);
614 guint32 cols [MONO_MEMBERREF_SIZE];
615 MonoMethodSignature *sig, *prev_sig;
618 /* !table is for wrappers: we should really assign their own token to them */
619 if (!table || table == MONO_TABLE_METHOD)
620 return mono_method_signature (method);
622 if (table == MONO_TABLE_METHODSPEC) {
623 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
624 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
625 mono_method_signature (method));
626 g_assert (method->is_inflated);
628 return mono_method_signature (method);
631 if (method->klass->generic_class)
632 return mono_method_signature (method);
635 /* FIXME: This might be incorrect for vararg methods */
636 return mono_method_signature (method);
639 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
640 mono_loader_unlock ();
642 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
644 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
645 mono_metadata_decode_blob_size (ptr, &ptr);
646 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
649 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
651 /* Somebody got in before us */
652 /* FIXME: Free sig */
656 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
657 mono_loader_unlock ();
660 sig = inflate_generic_signature (image, sig, context);
666 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
668 return mono_method_get_signature_full (method, image, token, NULL);
671 /* this is only for the typespec array methods */
673 search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig)
676 for (i = 0; i < klass->method.count; ++i) {
677 MonoMethod *method = klass->methods [i];
678 if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
685 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
686 gboolean *used_context)
688 MonoClass *klass = NULL;
689 MonoMethod *method = NULL;
690 MonoTableInfo *tables = image->tables;
692 guint32 nindex, class;
694 MonoMethodSignature *sig;
697 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
698 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
699 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
700 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
701 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
703 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
706 * Whether we actually used the `typespec_context' or not.
707 * This is used to tell our caller whether or not it's safe to insert the returned
708 * method into a cache.
711 *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
714 case MONO_MEMBERREF_PARENT_TYPEREF:
715 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
717 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
718 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
719 mono_loader_set_error_method_load (name, mname);
724 case MONO_MEMBERREF_PARENT_TYPESPEC:
726 * Parse the TYPESPEC in the parent's context.
728 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
730 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
731 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
732 mono_loader_set_error_method_load (name, mname);
737 case MONO_MEMBERREF_PARENT_TYPEDEF:
738 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
740 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
741 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
742 mono_loader_set_error_method_load (name, mname);
747 case MONO_MEMBERREF_PARENT_METHODDEF:
748 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
752 /* This message leaks */
753 char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
754 mono_loader_set_error_method_load ("", message);
760 mono_class_init (klass);
762 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
763 mono_metadata_decode_blob_size (ptr, &ptr);
765 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
770 case MONO_MEMBERREF_PARENT_TYPEREF:
771 case MONO_MEMBERREF_PARENT_TYPEDEF:
772 method = find_method (klass, NULL, mname, sig, klass);
775 case MONO_MEMBERREF_PARENT_TYPESPEC: {
779 type = &klass->byval_arg;
781 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
782 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
783 method = find_method (in_class, NULL, mname, sig, klass);
787 /* we're an array and we created these methods already in klass in mono_class_init () */
788 result = search_in_array_class (klass, mname, sig);
792 g_assert_not_reached ();
796 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
797 g_assert_not_reached ();
801 char *msig = mono_signature_get_desc (sig, FALSE);
802 char * class_name = mono_type_get_name (&klass->byval_arg);
803 GString *s = g_string_new (mname);
804 if (sig->generic_param_count)
805 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
806 g_string_append_printf (s, "(%s)", msig);
808 msig = g_string_free (s, FALSE);
811 "Missing method %s::%s in assembly %s, referenced in assembly %s",
812 class_name, msig, klass->image->name, image->name);
813 mono_loader_set_error_method_load (class_name, mname);
817 mono_metadata_free_method_signature (sig);
823 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
825 MonoMethod *method, *inflated;
827 MonoTableInfo *tables = image->tables;
828 MonoGenericContext new_context;
829 MonoGenericInst *inst;
831 guint32 cols [MONO_METHODSPEC_SIZE];
832 guint32 token, nindex, param_count;
834 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
835 token = cols [MONO_METHODSPEC_METHOD];
836 nindex = token >> MONO_METHODDEFORREF_BITS;
838 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
840 mono_metadata_decode_value (ptr, &ptr);
842 param_count = mono_metadata_decode_value (ptr, &ptr);
843 g_assert (param_count);
846 * Be careful with the two contexts here:
848 * ----------------------------------------
850 * static void Hello<T> (S s, T t) { }
852 * static void Test<U> (U u) {
853 * Foo<U>.Hello<string> (u, "World");
856 * ----------------------------------------
858 * Let's assume we're currently JITing Foo<int>.Test<long>
859 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
861 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
862 * (MONO_MEMBERREF_PARENT_TYPESPEC).
864 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
865 * context (S=int, U=long) to get the correct `Foo<long>'.
867 * After that, we parse the memberref signature in the new context
868 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
871 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
872 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
874 method = method_from_memberref (image, nindex, context, NULL);
876 klass = method->klass;
878 /* FIXME: Is this invalid metadata? Should we throw an exception instead? */
879 g_assert (!klass->generic_container);
881 if (klass->generic_class) {
882 g_assert (method->is_inflated);
883 method = ((MonoMethodInflated *) method)->declaring;
886 new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
889 * When parsing the methodspec signature, we're in the old context again:
891 * ----------------------------------------
893 * static void Hello<T> (T t) { }
895 * static void Test<U> (U u) {
899 * ----------------------------------------
901 * Let's assume we're currently JITing "Foo.Test<float>".
903 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
904 * signature is "<!!0>". This means that we must instantiate the method type parameter
905 * `T' from the new method with the method type parameter `U' from the current context;
906 * ie. instantiate the method as `Foo.Hello<float>.
909 inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
911 if (context && inst->is_open)
912 inst = mono_metadata_inflate_generic_inst (inst, context);
914 new_context.method_inst = inst;
916 inflated = mono_class_inflate_generic_method_full (method, klass, &new_context);
929 static MonoDllMap *global_dll_map;
932 mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
943 * we use the first entry we find that matches, since entries from
944 * the config file are prepended to the list and we document that the
947 for (; dll_map; dll_map = dll_map->next) {
948 if (dll_map->dll [0] == 'i' && dll_map->dll [1] == ':') {
949 if (g_ascii_strcasecmp (dll_map->dll + 2, dll))
951 } else if (strcmp (dll_map->dll, dll)) {
954 if (!found && dll_map->target) {
955 *rdll = dll_map->target;
957 /* we don't quit here, because we could find a full
958 * entry that matches also function and that has priority.
961 if (dll_map->func && strcmp (dll_map->func, func) == 0) {
962 *rfunc = dll_map->target_func;
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_list (assembly->dll_map, dll, func, rdll, rfunc);
980 return mono_dllmap_lookup_list (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) {
990 entry = g_malloc0 (sizeof (MonoDllMap));
991 entry->dll = dll? g_strdup (dll): NULL;
992 entry->target = tdll? g_strdup (tdll): NULL;
993 entry->func = func? g_strdup (func): NULL;
994 entry->target_func = tfunc? g_strdup (tfunc): NULL;
995 entry->next = global_dll_map;
996 global_dll_map = entry;
998 MonoMemPool *mpool = assembly->mempool;
999 entry = mono_mempool_alloc0 (mpool, sizeof (MonoDllMap));
1000 entry->dll = dll? mono_mempool_strdup (mpool, dll): NULL;
1001 entry->target = tdll? mono_mempool_strdup (mpool, tdll): NULL;
1002 entry->func = func? mono_mempool_strdup (mpool, func): NULL;
1003 entry->target_func = tfunc? mono_mempool_strdup (mpool, tfunc): NULL;
1004 entry->next = assembly->dll_map;
1005 assembly->dll_map = entry;
1008 mono_loader_unlock ();
1011 static GHashTable *global_module_map;
1014 cached_module_load (const char *name, int flags, char **err)
1017 mono_loader_lock ();
1018 if (!global_module_map)
1019 global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
1020 res = g_hash_table_lookup (global_module_map, name);
1023 mono_loader_unlock ();
1026 res = mono_dl_open (name, flags, err);
1028 g_hash_table_insert (global_module_map, g_strdup (name), res);
1029 mono_loader_unlock ();
1034 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
1036 MonoImage *image = method->klass->image;
1037 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
1038 MonoTableInfo *tables = image->tables;
1039 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1040 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
1041 guint32 im_cols [MONO_IMPLMAP_SIZE];
1042 guint32 scope_token;
1043 const char *import = NULL;
1044 const char *orig_scope;
1045 const char *new_scope;
1047 char *full_name, *file_name;
1049 MonoDl *module = NULL;
1051 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
1054 return piinfo->addr;
1056 if (method->klass->image->dynamic) {
1057 MonoReflectionMethodAux *method_aux =
1058 g_hash_table_lookup (
1059 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1063 import = method_aux->dllentry;
1064 orig_scope = method_aux->dll;
1067 if (!piinfo->implmap_idx)
1070 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
1072 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
1073 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
1074 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
1075 orig_scope = mono_metadata_string_heap (image, scope_token);
1078 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
1080 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1081 "DllImport attempting to load: '%s'.", new_scope);
1088 /* we allow a special name to dlopen from the running process namespace */
1089 if (strcmp (new_scope, "__Internal") == 0)
1090 module = mono_dl_open (NULL, MONO_DL_LAZY, &error_msg);
1093 * Try loading the module using a variety of names
1095 for (i = 0; i < 4; ++i) {
1098 /* Try the original name */
1099 file_name = g_strdup (new_scope);
1102 /* Try trimming the .dll extension */
1103 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
1104 file_name = g_strdup (new_scope);
1105 file_name [strlen (new_scope) - 4] = '\0';
1111 if (strstr (new_scope, "lib") != new_scope) {
1112 file_name = g_strdup_printf ("lib%s", new_scope);
1118 #ifndef PLATFORM_WIN32
1119 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
1120 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
1121 !g_ascii_strcasecmp ("user32", new_scope) ||
1122 !g_ascii_strcasecmp ("kernel", new_scope)) {
1123 file_name = g_strdup ("libMonoSupportW.so");
1127 #ifndef PLATFORM_WIN32
1134 while ((full_name = mono_dl_build_path (NULL, file_name, &iter))) {
1135 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1136 "DllImport loading location: '%s'.", full_name);
1137 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1139 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1140 "DllImport error loading library: '%s'.",
1152 while ((full_name = mono_dl_build_path (".", file_name, &iter))) {
1153 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1154 "DllImport loading library: '%s'.", full_name);
1155 module = cached_module_load (full_name, MONO_DL_LAZY, &error_msg);
1157 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1158 "DllImport error loading library '%s'.",
1169 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1170 "DllImport loading: '%s'.", file_name);
1171 module = cached_module_load (file_name, MONO_DL_LAZY, &error_msg);
1173 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1174 "DllImport error loading library '%s'.",
1186 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1187 "DllImport unable to load library '%s'.",
1192 *exc_class = "DllNotFoundException";
1193 *exc_arg = new_scope;
1198 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1199 "Searching for '%s'.", import);
1201 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1202 error_msg = mono_dl_symbol (module, import, &piinfo->addr);
1204 char *mangled_name = NULL, *mangled_name2 = NULL;
1207 int mangle_param_count;
1208 #ifdef PLATFORM_WIN32
1213 * Search using a variety of mangled names
1215 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1216 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1217 gboolean need_param_count = FALSE;
1218 #ifdef PLATFORM_WIN32
1219 if (mangle_stdcall > 0)
1220 need_param_count = TRUE;
1222 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1227 mangled_name = (char*)import;
1228 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1229 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1230 /* Try the mangled name first */
1231 if (mangle_charset == 0)
1232 mangled_name = g_strconcat (import, "W", NULL);
1234 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1235 #ifdef PLATFORM_WIN32
1236 if (mangle_charset == 0)
1237 mangled_name = g_strconcat (import, "W", NULL);
1239 /* Try the mangled name last */
1240 if (mangle_charset == 1)
1241 mangled_name = g_strconcat (import, "A", NULL);
1244 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1246 /* Try the mangled name last */
1247 if (mangle_charset == 1)
1248 mangled_name = g_strconcat (import, "A", NULL);
1252 #ifdef PLATFORM_WIN32
1253 if (mangle_param_count == 0)
1254 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1256 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1257 param_count = mangle_param_count;
1259 /* Try the stdcall mangled name */
1261 * gcc under windows creates mangled names without the underscore, but MS.NET
1262 * doesn't support it, so we doesn't support it either.
1264 if (mangle_stdcall == 1)
1265 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1267 mangled_name2 = mangled_name;
1269 mangled_name2 = mangled_name;
1272 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1273 "Probing '%s'.", mangled_name2);
1275 error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
1278 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1279 "Found as '%s'.", mangled_name2);
1281 if (mangled_name != mangled_name2)
1282 g_free (mangled_name2);
1283 if (mangled_name != import)
1284 g_free (mangled_name);
1290 if (!piinfo->addr) {
1293 *exc_class = "EntryPointNotFoundException";
1298 return piinfo->addr;
1302 * LOCKING: assumes the loader lock to be taken.
1305 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1306 MonoGenericContext *context, gboolean *used_context)
1309 int table = mono_metadata_token_table (token);
1310 int idx = mono_metadata_token_index (token);
1311 MonoTableInfo *tables = image->tables;
1312 MonoGenericContainer *generic_container = NULL, *container = NULL;
1313 const char *sig = NULL;
1315 guint32 cols [MONO_TYPEDEF_SIZE];
1318 return mono_lookup_dynamic_token (image, token);
1320 if (table != MONO_TABLE_METHOD) {
1321 if (table == MONO_TABLE_METHODSPEC) {
1322 if (used_context) *used_context = TRUE;
1323 return method_from_methodspec (image, context, idx);
1325 if (table != MONO_TABLE_MEMBERREF)
1326 g_print("got wrong token: 0x%08x\n", token);
1327 g_assert (table == MONO_TABLE_MEMBERREF);
1328 return method_from_memberref (image, idx, context, used_context);
1331 if (used_context) *used_context = FALSE;
1333 mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
1335 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1336 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1337 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1339 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1341 mono_stats.method_count ++;
1344 guint32 type = mono_metadata_typedef_from_method (image, token);
1345 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1351 result->klass = klass;
1352 result->flags = cols [2];
1353 result->iflags = cols [1];
1354 result->token = token;
1355 result->name = mono_metadata_string_heap (image, cols [3]);
1357 container = klass->generic_container;
1358 generic_container = mono_metadata_load_generic_params (image, token, container);
1359 if (generic_container) {
1360 generic_container->owner.method = result;
1362 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1364 for (i = 0; i < generic_container->type_argc; i++)
1365 mono_class_from_generic_parameter (&generic_container->type_params [i], image, TRUE);
1367 container = generic_container;
1371 if (!sig) /* already taken from the methodref */
1372 sig = mono_metadata_blob_heap (image, cols [4]);
1373 size = mono_metadata_decode_blob_size (sig, &sig);
1375 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1376 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1377 result->string_ctor = 1;
1378 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1379 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1380 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1382 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1383 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1386 /* FIXME: lazyness for generics too, but how? */
1387 if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1388 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1389 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1390 gpointer loc = mono_image_rva_map (image, cols [0]);
1392 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1395 result->generic_container = generic_container;
1401 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1403 return mono_get_method_full (image, token, klass, NULL);
1407 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1408 MonoGenericContext *context)
1411 gboolean used_context = FALSE;
1413 /* We do everything inside the lock to prevent creation races */
1415 mono_loader_lock ();
1417 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1418 mono_loader_unlock ();
1422 result = mono_get_method_from_token (image, token, klass, context, &used_context);
1424 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1427 g_message (G_STRLOC ": %s - %d - %d", mono_method_full_name (result, TRUE),
1428 result->is_inflated, used_context);
1432 * `used_context' specifies whether or not mono_get_method_from_token() actually
1433 * used the `context' to get the method. See bug #80969.
1436 if (!used_context && !(result && result->is_inflated))
1437 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1439 mono_loader_unlock ();
1445 * mono_get_method_constrained:
1447 * This is used when JITing the `constrained.' opcode.
1449 * This returns two values: the contrained method, which has been inflated
1450 * as the function return value; And the original CIL-stream method as
1451 * declared in cil_method. The later is used for verification.
1454 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1455 MonoGenericContext *context, MonoMethod **cil_method)
1457 MonoMethod *method, *result;
1458 MonoClass *ic = NULL;
1459 MonoGenericContext *class_context = NULL, *method_context = NULL;
1460 MonoMethodSignature *sig;
1462 mono_loader_lock ();
1464 *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
1466 mono_loader_unlock ();
1470 mono_class_init (constrained_class);
1471 method = *cil_method;
1472 sig = mono_method_signature (method);
1474 if (method->is_inflated && sig->generic_param_count) {
1475 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1476 sig = mono_method_signature (imethod->declaring);
1477 method_context = mono_method_get_context (method);
1480 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1483 if (constrained_class->generic_class)
1484 class_context = mono_class_get_context (constrained_class);
1486 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1488 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1489 method->klass->name, method->name, image->name, token);
1490 mono_loader_unlock ();
1495 result = mono_class_inflate_generic_method (result, class_context);
1497 result = mono_class_inflate_generic_method (result, method_context);
1499 mono_loader_unlock ();
1504 mono_free_method (MonoMethod *method)
1506 if (mono_profiler_get_events () != MONO_PROFILE_NONE)
1509 if (method->signature) {
1511 * FIXME: This causes crashes because the types inside signatures and
1512 * locals are shared.
1514 /* mono_metadata_free_method_signature (method->signature); */
1515 /* g_free (method->signature); */
1518 if (method->dynamic) {
1519 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1522 g_free ((char*)method->name);
1523 if (mw->method.header) {
1524 g_free ((char*)mw->method.header->code);
1525 for (i = 0; i < mw->method.header->num_locals; ++i)
1526 g_free (mw->method.header->locals [i]);
1527 g_free (mw->method.header->clauses);
1528 g_free (mw->method.header);
1530 g_free (mw->method_data);
1531 g_free (method->signature);
1537 mono_method_get_param_names (MonoMethod *method, const char **names)
1540 MonoClass *klass = method->klass;
1541 MonoTableInfo *methodt;
1542 MonoTableInfo *paramt;
1545 if (!mono_method_signature (method)->param_count)
1547 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1550 if (klass->generic_class || klass->rank) /* copy the names later */
1553 mono_class_init (klass);
1555 if (klass->image->dynamic) {
1556 MonoReflectionMethodAux *method_aux =
1557 g_hash_table_lookup (
1558 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1559 if (method_aux && method_aux->param_names) {
1560 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1561 if (method_aux->param_names [i + 1])
1562 names [i] = method_aux->param_names [i + 1];
1567 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1568 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1569 idx = mono_method_get_index (method);
1571 guint32 cols [MONO_PARAM_SIZE];
1574 param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1576 if (idx < methodt->rows)
1577 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1579 lastp = paramt->rows + 1;
1580 for (i = param_index; i < lastp; ++i) {
1581 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1582 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1583 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1590 mono_method_get_param_token (MonoMethod *method, int index)
1592 MonoClass *klass = method->klass;
1593 MonoTableInfo *methodt;
1596 mono_class_init (klass);
1598 if (klass->image->dynamic) {
1599 g_assert_not_reached ();
1602 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1603 idx = mono_method_get_index (method);
1605 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1609 return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
1611 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1618 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1621 MonoClass *klass = method->klass;
1622 MonoTableInfo *methodt;
1623 MonoTableInfo *paramt;
1626 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1629 if (method->klass->image->dynamic) {
1630 MonoReflectionMethodAux *method_aux =
1631 g_hash_table_lookup (
1632 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1633 if (method_aux && method_aux->param_marshall) {
1634 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1635 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1636 if (dyn_specs [i]) {
1637 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1638 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1644 mono_class_init (klass);
1646 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1647 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1648 idx = mono_method_get_index (method);
1650 guint32 cols [MONO_PARAM_SIZE];
1651 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1653 if (idx < methodt->rows)
1654 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1656 lastp = paramt->rows + 1;
1658 for (i = param_index; i < lastp; ++i) {
1659 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1661 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1663 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1665 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1674 mono_method_has_marshal_info (MonoMethod *method)
1677 MonoClass *klass = method->klass;
1678 MonoTableInfo *methodt;
1679 MonoTableInfo *paramt;
1682 if (method->klass->image->dynamic) {
1683 MonoReflectionMethodAux *method_aux =
1684 g_hash_table_lookup (
1685 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1686 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1688 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1695 mono_class_init (klass);
1697 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1698 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1699 idx = mono_method_get_index (method);
1701 guint32 cols [MONO_PARAM_SIZE];
1702 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1704 if (idx + 1 < methodt->rows)
1705 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1707 lastp = paramt->rows + 1;
1709 for (i = param_index; i < lastp; ++i) {
1710 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1712 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1721 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1724 g_assert (method != NULL);
1725 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1727 data = ((MonoMethodWrapper *)method)->method_data;
1728 g_assert (data != NULL);
1729 g_assert (id <= GPOINTER_TO_UINT (*data));
1734 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1735 g_error ("stack walk not installed");
1738 static MonoStackWalkImpl stack_walk = default_stack_walk;
1741 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1743 stack_walk (func, TRUE, user_data);
1747 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1749 stack_walk (func, FALSE, user_data);
1753 mono_install_stack_walk (MonoStackWalkImpl func)
1759 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1761 MonoMethod **dest = data;
1763 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1769 mono_method_get_last_managed (void)
1771 MonoMethod *m = NULL;
1772 stack_walk (last_managed, FALSE, &m);
1777 mono_loader_lock (void)
1779 EnterCriticalSection (&loader_mutex);
1783 mono_loader_unlock (void)
1785 LeaveCriticalSection (&loader_mutex);
1789 * mono_method_signature:
1791 * Return the signature of the method M. On failure, returns NULL.
1793 MonoMethodSignature*
1794 mono_method_signature (MonoMethod *m)
1800 gboolean can_cache_signature;
1801 MonoGenericContainer *container;
1805 return m->signature;
1807 mono_loader_lock ();
1810 mono_loader_unlock ();
1811 return m->signature;
1814 if (m->is_inflated) {
1815 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1816 MonoMethodSignature *signature;
1817 /* the lock is recursive */
1818 signature = mono_method_signature (imethod->declaring);
1819 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, mono_method_get_context (m));
1820 mono_loader_unlock ();
1821 return m->signature;
1824 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1825 idx = mono_metadata_token_index (m->token);
1826 img = m->klass->image;
1828 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1830 g_assert (!m->klass->generic_class);
1831 container = m->generic_container;
1833 container = m->klass->generic_container;
1835 /* Generic signatures depend on the container so they cannot be cached */
1836 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1837 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1839 /* If the method has parameter attributes, that can modify the signature */
1840 pattrs = mono_metadata_get_param_attrs (img, idx);
1842 can_cache_signature = FALSE;
1846 if (can_cache_signature)
1847 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1849 if (!m->signature) {
1850 const char *sig_body;
1852 size = mono_metadata_decode_blob_size (sig, &sig_body);
1854 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1855 if (!m->signature) {
1856 mono_loader_unlock ();
1860 if (can_cache_signature)
1861 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1864 /* Verify metadata consistency */
1865 if (m->signature->generic_param_count) {
1866 if (!container || !container->is_method)
1867 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1868 if (container->type_argc != m->signature->generic_param_count)
1869 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1870 m->signature->generic_param_count, container->type_argc);
1871 } else if (container && container->is_method && container->type_argc)
1872 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1874 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1875 m->signature->pinvoke = 1;
1876 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1877 MonoCallConvention conv = 0;
1878 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1879 m->signature->pinvoke = 1;
1881 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1882 case 0: /* no call conv, so using default */
1883 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1884 conv = MONO_CALL_DEFAULT;
1886 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1889 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1890 conv = MONO_CALL_STDCALL;
1892 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1893 conv = MONO_CALL_THISCALL;
1895 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1896 conv = MONO_CALL_FASTCALL;
1898 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1899 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1901 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1902 g_assert_not_reached ();
1904 m->signature->call_convention = conv;
1907 mono_loader_unlock ();
1908 return m->signature;
1912 mono_method_get_name (MonoMethod *method)
1914 return method->name;
1918 mono_method_get_class (MonoMethod *method)
1920 return method->klass;
1924 mono_method_get_token (MonoMethod *method)
1926 return method->token;
1930 mono_method_get_header (MonoMethod *method)
1936 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1938 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))
1942 if (G_LIKELY (mn->header))
1948 mono_loader_lock ();
1951 mono_loader_unlock ();
1955 if (method->is_inflated) {
1956 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1957 MonoMethodHeader *header;
1958 /* the lock is recursive */
1959 header = mono_method_get_header (imethod->declaring);
1960 mn->header = inflate_generic_header (header, mono_method_get_context (method));
1961 mono_loader_unlock ();
1965 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1966 idx = mono_metadata_token_index (method->token);
1967 img = method->klass->image;
1968 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1969 loc = mono_image_rva_map (img, rva);
1973 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1975 mono_loader_unlock ();
1980 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1983 *iflags = method->iflags;
1984 return method->flags;
1988 * Find the method index in the metadata methodDef table.
1991 mono_method_get_index (MonoMethod *method) {
1992 MonoClass *klass = method->klass;
1996 return mono_metadata_token_index (method->token);
1998 mono_class_setup_methods (klass);
1999 for (i = 0; i < klass->method.count; ++i) {
2000 if (method == klass->methods [i]) {
2001 if (klass->image->uncompressed_metadata)
2002 return mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
2004 return klass->method.first + i + 1;