2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
11 * This file is used by the interpreter and the JIT engine to locate
12 * assemblies. Used to load AssemblyRef and later to resolve various
16 * This should keep track of the assembly versions that we are loading.
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/image.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/cil-coff.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/metadata-internals.h>
32 #include <mono/metadata/loader.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/metadata/reflection.h>
36 #include <mono/utils/mono-logger.h>
38 MonoDefaults mono_defaults;
41 * This lock protects the hash tables inside MonoImage used by the metadata
42 * loading functions in class.c and loader.c.
44 static CRITICAL_SECTION loader_mutex;
48 * This TLS variable contains the last type load error encountered by the loader.
50 guint32 loader_error_thread_id;
55 InitializeCriticalSection (&loader_mutex);
57 loader_error_thread_id = TlsAlloc ();
61 mono_loader_cleanup (void)
63 TlsFree (loader_error_thread_id);
65 DeleteCriticalSection (&loader_mutex);
69 * Handling of type load errors should be done as follows:
71 * If something could not be loaded, the loader should call one of the
72 * mono_loader_set_error_XXX functions ()
73 * with the appropriate arguments, then return NULL to report the failure. The error
74 * should be propagated until it reaches code which can throw managed exceptions. At that
75 * point, an exception should be thrown based on the information returned by
76 * mono_loader_get_error (). Then the error should be cleared by calling
77 * mono_loader_clear_error ().
81 set_loader_error (MonoLoaderError *error)
83 TlsSetValue (loader_error_thread_id, error);
87 * mono_loader_set_error_type_load:
89 * Set the loader error for this thread. CLASS_NAME and ASSEMBLY_NAME should be
90 * dynamically allocated strings whose ownership is passed to this function.
93 mono_loader_set_error_type_load (char *class_name, char *assembly_name)
95 MonoLoaderError *error;
97 if (mono_loader_get_last_error ()) {
99 g_free (assembly_name);
103 error = g_new0 (MonoLoaderError, 1);
104 error->kind = MONO_LOADER_ERROR_TYPE;
105 error->class_name = class_name;
106 error->assembly_name = assembly_name;
109 * This is not strictly needed, but some (most) of the loader code still
110 * can't deal with load errors, and this message is more helpful than an
113 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
115 set_loader_error (error);
119 * mono_loader_set_error_method_load:
121 * Set the loader error for this thread. MEMBER_NAME should point to a string
125 mono_loader_set_error_method_load (MonoClass *klass, const char *member_name)
127 MonoLoaderError *error;
129 /* FIXME: Store the signature as well */
130 if (mono_loader_get_last_error ())
133 error = g_new0 (MonoLoaderError, 1);
134 error->kind = MONO_LOADER_ERROR_METHOD;
135 error->klass = klass;
136 error->member_name = member_name;
138 set_loader_error (error);
142 * mono_loader_set_error_field_load:
144 * Set the loader error for this thread. MEMBER_NAME should point to a string
148 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
150 MonoLoaderError *error;
152 /* FIXME: Store the signature as well */
153 if (mono_loader_get_last_error ())
156 error = g_new0 (MonoLoaderError, 1);
157 error->kind = MONO_LOADER_ERROR_FIELD;
158 error->klass = klass;
159 error->member_name = member_name;
161 set_loader_error (error);
165 * mono_loader_get_last_error:
167 * Returns information about the last type load exception encountered by the loader, or
168 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
171 mono_loader_get_last_error (void)
173 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
177 mono_loader_clear_error (void)
179 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
183 g_free (ex->class_name);
184 g_free (ex->assembly_name);
187 TlsSetValue (loader_error_thread_id, NULL);
190 static MonoClassField*
191 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
192 MonoGenericContext *context)
195 MonoClassField *field;
196 MonoTableInfo *tables = image->tables;
198 guint32 nindex, class;
201 guint32 idx = mono_metadata_token_index (token);
203 if (image->dynamic) {
204 MonoClassField *result = mono_lookup_dynamic_token (image, token);
205 *retklass = result->parent;
209 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
210 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
211 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
213 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
215 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
216 mono_metadata_decode_blob_size (ptr, &ptr);
217 /* we may want to check the signature here... */
220 case MONO_MEMBERREF_PARENT_TYPEREF:
221 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
223 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
224 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
228 mono_class_init (klass);
231 field = mono_class_get_field_from_name (klass, fname);
233 case MONO_MEMBERREF_PARENT_TYPESPEC: {
234 /*guint32 bcols [MONO_TYPESPEC_SIZE];
238 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
239 bcols, MONO_TYPESPEC_SIZE);
240 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
241 len = mono_metadata_decode_value (ptr, &ptr);
242 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
244 klass = mono_class_from_mono_type (type);
245 mono_class_init (klass);
246 g_print ("type in sig: %s\n", klass->name);*/
247 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
248 mono_class_init (klass);
251 field = mono_class_get_field_from_name (klass, fname);
255 g_warning ("field load from %x", class);
260 mono_loader_set_error_field_load (klass, fname);
266 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
267 MonoGenericContext *context)
271 MonoClassField *field;
273 if (image->dynamic) {
274 MonoClassField *result = mono_lookup_dynamic_token (image, token);
275 *retklass = result->parent;
280 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
281 *retklass = field->parent;
282 mono_loader_unlock ();
285 mono_loader_unlock ();
287 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
288 field = field_from_memberref (image, token, retklass, context);
290 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
293 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
299 field = mono_class_get_field (k, token);
303 if (field && !field->parent->generic_class)
304 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
305 mono_loader_unlock ();
310 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
314 if (sig1->hasthis != sig2->hasthis ||
315 sig1->sentinelpos != sig2->sentinelpos)
318 for (i = 0; i < sig1->sentinelpos; i++) {
319 MonoType *p1 = sig1->params[i];
320 MonoType *p2 = sig2->params[i];
322 /*if (p1->attrs != p2->attrs)
325 if (!mono_metadata_type_equal (p1, p2))
329 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
335 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
336 MonoMethodSignature *sig, MonoClass *from_class)
340 mono_class_setup_methods (in_class);
341 for (i = 0; i < in_class->method.count; ++i) {
342 MonoMethod *m = in_class->methods [i];
344 if (!((fqname && !strcmp (m->name, fqname)) ||
345 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
348 if (sig->call_convention == MONO_CALL_VARARG) {
349 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
352 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
357 if (i < in_class->method.count) {
358 mono_class_setup_methods (from_class);
359 g_assert (from_class->method.count == in_class->method.count);
360 return from_class->methods [i];
366 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
369 char *qname, *fqname, *class_name;
370 gboolean is_interface;
371 MonoMethod *result = NULL;
373 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
376 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
378 qname = g_strconcat (class_name, ".", name, NULL);
379 if (ic->name_space && ic->name_space [0])
380 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
384 class_name = qname = fqname = NULL;
387 g_assert (from_class);
388 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
392 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
395 g_assert (from_class->interface_count == in_class->interface_count);
396 for (i = 0; i < in_class->interface_count; i++) {
397 MonoClass *ic = in_class->interfaces [i];
398 MonoClass *from_ic = from_class->interfaces [i];
400 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
405 in_class = in_class->parent;
406 from_class = from_class->parent;
408 g_assert (!in_class == !from_class);
411 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
420 static MonoMethodSignature*
421 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
423 MonoMethodSignature *res;
430 res = mono_metadata_signature_alloc (image, sig->param_count);
431 res->ret = mono_class_inflate_generic_type (sig->ret, context);
432 is_open = mono_class_is_open_constructed_type (res->ret);
433 for (i = 0; i < sig->param_count; ++i) {
434 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
436 is_open = mono_class_is_open_constructed_type (res->params [i]);
438 res->hasthis = sig->hasthis;
439 res->explicit_this = sig->explicit_this;
440 res->call_convention = sig->call_convention;
441 res->pinvoke = sig->pinvoke;
442 res->generic_param_count = sig->generic_param_count;
443 res->sentinelpos = sig->sentinelpos;
444 res->has_type_parameters = is_open;
445 res->is_inflated = 1;
449 static MonoMethodHeader*
450 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
452 MonoMethodHeader *res;
454 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
455 res->code = header->code;
456 res->code_size = header->code_size;
457 res->max_stack = header->max_stack;
458 res->num_clauses = header->num_clauses;
459 res->init_locals = header->init_locals;
460 res->num_locals = header->num_locals;
461 res->clauses = header->clauses;
462 for (i = 0; i < header->num_locals; ++i)
463 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
468 * token is the method_ref or method_def token used in a call IL instruction.
471 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
473 int table = mono_metadata_token_table (token);
474 int idx = mono_metadata_token_index (token);
475 guint32 cols [MONO_MEMBERREF_SIZE];
476 MonoMethodSignature *sig, *prev_sig;
479 /* !table is for wrappers: we should really assign their own token to them */
480 if (!table || table == MONO_TABLE_METHOD)
481 return mono_method_signature (method);
483 if (table == MONO_TABLE_METHODSPEC) {
484 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
485 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
486 mono_method_signature (method));
487 g_assert (method->is_inflated);
489 return mono_method_signature (method);
492 if (method->klass->generic_class)
493 return mono_method_signature (method);
496 /* FIXME: This might be incorrect for vararg methods */
497 return mono_method_signature (method);
500 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
501 mono_loader_unlock ();
503 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
505 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
506 mono_metadata_decode_blob_size (ptr, &ptr);
507 sig = mono_metadata_parse_method_signature_full (
508 image, context ? context->container : NULL, 0, ptr, NULL);
511 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
513 /* Somebody got in before us */
514 /* FIXME: Free sig */
518 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
519 mono_loader_unlock ();
522 sig = inflate_generic_signature (image, sig, context);
528 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
530 return mono_method_get_signature_full (method, image, token, NULL);
534 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
536 MonoClass *klass = NULL;
537 MonoMethod *method = NULL;
538 MonoTableInfo *tables = image->tables;
540 guint32 nindex, class;
542 MonoMethodSignature *sig;
545 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
546 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
547 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
548 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
549 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
551 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
554 case MONO_MEMBERREF_PARENT_TYPEREF:
555 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
557 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
558 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
563 case MONO_MEMBERREF_PARENT_TYPESPEC:
565 * Parse the TYPESPEC in the parent's context.
567 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
569 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
570 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
575 case MONO_MEMBERREF_PARENT_TYPEDEF:
576 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
578 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
579 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
584 case MONO_MEMBERREF_PARENT_METHODDEF:
585 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
587 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
588 g_assert_not_reached ();
591 mono_class_init (klass);
593 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
594 mono_metadata_decode_blob_size (ptr, &ptr);
596 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
599 case MONO_MEMBERREF_PARENT_TYPEREF:
600 case MONO_MEMBERREF_PARENT_TYPEDEF:
601 method = find_method (klass, NULL, mname, sig, klass);
604 case MONO_MEMBERREF_PARENT_TYPESPEC: {
608 type = &klass->byval_arg;
610 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
611 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
612 method = find_method (in_class, NULL, mname, sig, klass);
616 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
617 result->klass = klass;
618 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
619 result->flags = METHOD_ATTRIBUTE_PUBLIC;
620 result->signature = sig;
621 result->name = mname;
623 if (!strcmp (mname, ".ctor")) {
624 /* we special-case this in the runtime. */
628 if (!strcmp (mname, "Set")) {
629 g_assert (sig->hasthis);
630 g_assert (type->data.array->rank + 1 == sig->param_count);
631 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
635 if (!strcmp (mname, "Get")) {
636 g_assert (sig->hasthis);
637 g_assert (type->data.array->rank == sig->param_count);
638 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
642 if (!strcmp (mname, "Address")) {
643 g_assert (sig->hasthis);
644 g_assert (type->data.array->rank == sig->param_count);
645 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
649 g_assert_not_reached ();
653 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
654 g_assert_not_reached ();
658 char *msig = mono_signature_get_desc (sig, FALSE);
659 GString *s = g_string_new (mname);
660 if (sig->generic_param_count)
661 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
662 g_string_append_printf (s, "(%s)", msig);
664 msig = g_string_free (s, FALSE);
667 "Missing method %s::%s in assembly %s, referenced in assembly %s",
668 mono_type_get_name (&klass->byval_arg), msig, klass->image->name, image->name);
670 mono_loader_set_error_method_load (klass, mname);
672 mono_metadata_free_method_signature (sig);
678 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
680 MonoMethod *method, *inflated;
681 MonoTableInfo *tables = image->tables;
682 MonoGenericContext *new_context = NULL;
683 MonoGenericMethod *gmethod;
684 MonoGenericContainer *container = NULL;
686 guint32 cols [MONO_METHODSPEC_SIZE];
687 guint32 token, nindex, param_count;
689 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
690 token = cols [MONO_METHODSPEC_METHOD];
691 nindex = token >> MONO_METHODDEFORREF_BITS;
693 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
695 mono_metadata_decode_value (ptr, &ptr);
697 param_count = mono_metadata_decode_value (ptr, &ptr);
698 g_assert (param_count);
701 * Be careful with the two contexts here:
703 * ----------------------------------------
705 * static void Hello<T> (S s, T t) { }
707 * static void Test<U> (U u) {
708 * Foo<U>.Hello<string> (u, "World");
711 * ----------------------------------------
713 * Let's assume we're currently JITing Foo<int>.Test<long>
714 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
716 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
717 * (MONO_MEMBERREF_PARENT_TYPESPEC).
719 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
720 * context (S=int, U=long) to get the correct `Foo<long>'.
722 * After that, we parse the memberref signature in the new context
723 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
726 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
727 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
729 method = method_from_memberref (image, nindex, context);
731 method = mono_get_inflated_method (method);
733 container = method->generic_container;
734 g_assert (container);
736 gmethod = g_new0 (MonoGenericMethod, 1);
737 gmethod->generic_class = method->klass->generic_class;
738 gmethod->container = container;
740 new_context = g_new0 (MonoGenericContext, 1);
741 new_context->container = container;
742 new_context->gmethod = gmethod;
743 if (container->parent)
744 new_context->gclass = container->parent->context.gclass;
747 * When parsing the methodspec signature, we're in the old context again:
749 * ----------------------------------------
751 * static void Hello<T> (T t) { }
753 * static void Test<U> (U u) {
757 * ----------------------------------------
759 * Let's assume we're currently JITing "Foo.Test<float>".
761 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
762 * signature is "<!!0>". This means that we must instantiate the method type parameter
763 * `T' from the new method with the method type parameter `U' from the current context;
764 * ie. instantiate the method as `Foo.Hello<float>.
767 gmethod->inst = mono_metadata_parse_generic_inst (image, context ? context->container : NULL, param_count, ptr, &ptr);
769 if (context && gmethod->inst->is_open)
770 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
772 if (!container->method_hash)
773 container->method_hash = g_hash_table_new (
774 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
776 inflated = g_hash_table_lookup (container->method_hash, gmethod);
779 g_free (new_context);
783 context = new_context;
785 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
786 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
788 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
789 g_hash_table_insert (container->method_hash, gmethod, inflated);
794 typedef struct MonoDllMap MonoDllMap;
803 static GHashTable *global_dll_map;
806 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
807 MonoDllMap *map, *tmp;
816 map = g_hash_table_lookup (dll_map, dll);
818 mono_loader_unlock ();
821 *rdll = map->target? map->target: dll;
823 for (tmp = map->next; tmp; tmp = tmp->next) {
824 if (strcmp (func, tmp->name) == 0) {
828 mono_loader_unlock ();
833 mono_loader_unlock ();
838 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
841 if (assembly && assembly->dll_map) {
842 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
846 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
850 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
851 MonoDllMap *map, *entry;
852 GHashTable *dll_map = NULL;
858 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
859 dll_map = global_dll_map;
861 if (!assembly->dll_map)
862 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
863 dll_map = assembly->dll_map;
866 map = g_hash_table_lookup (dll_map, dll);
868 map = g_new0 (MonoDllMap, 1);
869 map->dll = g_strdup (dll);
871 map->target = g_strdup (tdll);
872 g_hash_table_insert (dll_map, map->dll, map);
875 entry = g_new0 (MonoDllMap, 1);
876 entry->name = g_strdup (func);
878 entry->target = g_strdup (tfunc);
879 if (tdll && map->target && strcmp (map->target, tdll))
880 entry->dll = g_strdup (tdll);
881 entry->next = map->next;
885 mono_loader_unlock ();
889 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
891 MonoImage *image = method->klass->image;
892 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
893 MonoTableInfo *tables = image->tables;
894 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
895 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
896 guint32 im_cols [MONO_IMPLMAP_SIZE];
898 const char *import = NULL;
899 const char *orig_scope;
900 const char *new_scope;
901 char *full_name, *file_name;
903 GModule *gmodule = NULL;
905 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
910 if (method->klass->image->dynamic) {
911 MonoReflectionMethodAux *method_aux =
912 g_hash_table_lookup (
913 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
917 import = method_aux->dllentry;
918 orig_scope = method_aux->dll;
921 if (!piinfo->implmap_idx)
924 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
926 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
927 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
928 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
929 orig_scope = mono_metadata_string_heap (image, scope_token);
932 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
934 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
935 "DllImport attempting to load: '%s'.", new_scope);
942 /* we allow a special name to dlopen from the running process namespace */
943 if (strcmp (new_scope, "__Internal") == 0)
944 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
947 * Try loading the module using a variety of names
949 for (i = 0; i < 4; ++i) {
952 /* Try the original name */
953 file_name = g_strdup (new_scope);
956 /* Try trimming the .dll extension */
957 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
958 file_name = g_strdup (new_scope);
959 file_name [strlen (new_scope) - 4] = '\0';
965 if (strstr (new_scope, "lib") != new_scope) {
966 file_name = g_strdup_printf ("lib%s", new_scope);
972 #ifndef PLATFORM_WIN32
973 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
974 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
975 !g_ascii_strcasecmp ("user32", new_scope) ||
976 !g_ascii_strcasecmp ("kernel", new_scope)) {
977 file_name = g_strdup ("libMonoSupportW.so");
981 #ifndef PLATFORM_WIN32
987 full_name = g_module_build_path (NULL, file_name);
988 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
989 "DllImport loading location: '%s'.", full_name);
990 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
992 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
993 "DllImport error loading library: '%s'.",
1000 full_name = g_module_build_path (".", file_name);
1001 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1002 "DllImport loading library: '%s'.", full_name);
1003 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
1005 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1006 "DllImport error loading library '%s'.",
1013 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1014 "DllImport loading: '%s'.", file_name);
1015 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
1017 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1018 "DllImport error loading library '%s'.",
1030 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1031 "DllImport unable to load library '%s'.",
1035 *exc_class = "DllNotFoundException";
1036 *exc_arg = new_scope;
1041 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1042 "Searching for '%s'.", import);
1044 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1045 g_module_symbol (gmodule, import, &piinfo->addr);
1047 char *mangled_name = NULL, *mangled_name2 = NULL;
1050 int mangle_param_count;
1051 #ifdef PLATFORM_WIN32
1056 * Search using a variety of mangled names
1058 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1059 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1060 gboolean need_param_count = FALSE;
1061 #ifdef PLATFORM_WIN32
1062 if (mangle_stdcall > 0)
1063 need_param_count = TRUE;
1065 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1070 mangled_name = (char*)import;
1071 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1072 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1073 /* Try the mangled name first */
1074 if (mangle_charset == 0)
1075 mangled_name = g_strconcat (import, "W", NULL);
1077 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1078 #ifdef PLATFORM_WIN32
1079 if (mangle_charset == 0)
1080 mangled_name = g_strconcat (import, "W", NULL);
1082 /* Try the mangled name last */
1083 if (mangle_charset == 1)
1084 mangled_name = g_strconcat (import, "A", NULL);
1087 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1089 /* Try the mangled name last */
1090 if (mangle_charset == 1)
1091 mangled_name = g_strconcat (import, "A", NULL);
1095 #ifdef PLATFORM_WIN32
1096 if (mangle_param_count == 0)
1097 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1099 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1100 param_count = mangle_param_count;
1102 /* Try the stdcall mangled name */
1104 * gcc under windows creates mangled names without the underscore, but MS.NET
1105 * doesn't support it, so we doesn't support it either.
1107 if (mangle_stdcall == 1)
1108 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1110 mangled_name2 = mangled_name;
1112 mangled_name2 = mangled_name;
1115 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1116 "Probing '%s'.", mangled_name2);
1118 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
1121 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1122 "Found as '%s'.", mangled_name2);
1124 if (mangled_name != mangled_name2)
1125 g_free (mangled_name2);
1126 if (mangled_name != import)
1127 g_free (mangled_name);
1133 if (!piinfo->addr) {
1135 *exc_class = "EntryPointNotFoundException";
1140 return piinfo->addr;
1144 mono_get_shared_generic_method (MonoGenericContainer *container)
1146 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1147 gmethod->container = container;
1148 gmethod->generic_class = container->context.gclass;
1149 gmethod->inst = mono_get_shared_generic_inst (container);
1155 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1156 MonoGenericContext *context)
1159 int table = mono_metadata_token_table (token);
1160 int idx = mono_metadata_token_index (token);
1161 MonoTableInfo *tables = image->tables;
1162 MonoGenericContainer *generic_container = NULL, *container = NULL;
1163 const char *sig = NULL;
1165 guint32 cols [MONO_TYPEDEF_SIZE];
1168 return mono_lookup_dynamic_token (image, token);
1170 if (table != MONO_TABLE_METHOD) {
1171 if (table == MONO_TABLE_METHODSPEC)
1172 return method_from_methodspec (image, context, idx);
1173 if (table != MONO_TABLE_MEMBERREF)
1174 g_print("got wrong token: 0x%08x\n", token);
1175 g_assert (table == MONO_TABLE_MEMBERREF);
1176 result = method_from_memberref (image, idx, context);
1181 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
1183 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1184 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1185 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodPInvoke));
1187 result = (MonoMethod *)mono_mempool_alloc0 (image->mempool, sizeof (MonoMethodNormal));
1190 guint32 type = mono_metadata_typedef_from_method (image, token);
1191 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1195 result->klass = klass;
1196 result->flags = cols [2];
1197 result->iflags = cols [1];
1198 result->token = token;
1199 result->name = mono_metadata_string_heap (image, cols [3]);
1201 container = klass->generic_container;
1202 generic_container = mono_metadata_load_generic_params (image, token, container);
1203 if (generic_container) {
1204 MonoGenericContext *context = &generic_container->context;
1206 context->gclass = container->context.gclass;
1207 context->gmethod = mono_get_shared_generic_method (generic_container);
1208 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1210 for (i = 0; i < generic_container->type_argc; i++) {
1211 generic_container->type_params [i].method = result;
1213 mono_class_from_generic_parameter (
1214 &generic_container->type_params [i], image, TRUE);
1217 container = generic_container;
1220 if (!sig) /* already taken from the methodref */
1221 sig = mono_metadata_blob_heap (image, cols [4]);
1222 size = mono_metadata_decode_blob_size (sig, &sig);
1224 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1225 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1226 result->string_ctor = 1;
1227 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1228 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1229 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1231 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1232 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1235 /* FIXME: lazyness for generics too, but how? */
1236 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1237 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1238 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1239 gpointer loc = mono_image_rva_map (image, cols [0]);
1241 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1244 result->generic_container = generic_container;
1250 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1252 return mono_get_method_full (image, token, klass, NULL);
1256 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1257 MonoGenericContext *context)
1261 /* We do everything inside the lock to prevent creation races */
1263 mono_loader_lock ();
1265 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1266 mono_loader_unlock ();
1270 result = mono_get_method_from_token (image, token, klass, context);
1272 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1274 if (!(result && result->is_inflated))
1275 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1277 mono_loader_unlock ();
1283 * mono_get_method_constrained:
1285 * This is used when JITing the `constrained.' opcode.
1288 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1289 MonoGenericContext *context)
1291 MonoMethod *method, *result;
1292 MonoClass *ic = NULL;
1293 MonoGenericContext *class_context = NULL, *method_context = NULL;
1294 MonoMethodSignature *sig;
1296 mono_loader_lock ();
1298 method = mono_get_method_from_token (image, token, NULL, context);
1300 mono_loader_unlock ();
1304 mono_class_init (constrained_class);
1305 method = mono_get_inflated_method (method);
1306 sig = mono_method_signature (method);
1308 if (method->is_inflated && sig->generic_param_count) {
1309 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1310 sig = mono_method_signature (imethod->declaring);
1311 method_context = imethod->context;
1314 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1317 if (constrained_class->generic_class)
1318 class_context = constrained_class->generic_class->context;
1320 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1322 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1323 method->klass->name, method->name, image->name, token);
1324 mono_loader_unlock ();
1329 result = mono_class_inflate_generic_method (result, class_context);
1331 result = mono_class_inflate_generic_method (result, method_context);
1333 mono_loader_unlock ();
1338 mono_free_method (MonoMethod *method)
1340 if (method->signature) {
1342 * FIXME: This causes crashes because the types inside signatures and
1343 * locals are shared.
1345 /* mono_metadata_free_method_signature (method->signature); */
1346 /* g_free (method->signature); */
1349 if (method->dynamic) {
1350 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1352 g_free ((char*)method->name);
1353 if (mw->method.header)
1354 g_free ((char*)mw->method.header->code);
1355 g_free (mw->method_data);
1358 if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1360 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1361 g_free (((MonoMethodNormal*)method)->header);
1364 if (method->dynamic)
1369 mono_method_get_param_names (MonoMethod *method, const char **names)
1372 MonoClass *klass = method->klass;
1373 MonoTableInfo *methodt;
1374 MonoTableInfo *paramt;
1377 if (!mono_method_signature (method)->param_count)
1379 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1382 if (klass->generic_class) /* copy the names later */
1385 mono_class_init (klass);
1387 if (klass->image->dynamic) {
1388 MonoReflectionMethodAux *method_aux =
1389 g_hash_table_lookup (
1390 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1391 if (method_aux && method_aux->param_names) {
1392 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1393 if (method_aux->param_names [i + 1])
1394 names [i] = method_aux->param_names [i + 1];
1399 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1400 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1401 idx = mono_method_get_index (method);
1403 guint32 cols [MONO_PARAM_SIZE];
1404 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1406 if (idx < methodt->rows)
1407 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1409 lastp = paramt->rows + 1;
1410 for (i = param_index; i < lastp; ++i) {
1411 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1412 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1413 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1420 mono_method_get_param_token (MonoMethod *method, int index)
1422 MonoClass *klass = method->klass;
1423 MonoTableInfo *methodt;
1426 if (klass->generic_class)
1427 g_assert_not_reached ();
1429 mono_class_init (klass);
1431 if (klass->image->dynamic) {
1432 g_assert_not_reached ();
1435 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1436 idx = mono_method_get_index (method);
1438 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1440 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1447 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1450 MonoClass *klass = method->klass;
1451 MonoTableInfo *methodt;
1452 MonoTableInfo *paramt;
1455 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1458 if (method->klass->image->dynamic) {
1459 MonoReflectionMethodAux *method_aux =
1460 g_hash_table_lookup (
1461 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1462 if (method_aux && method_aux->param_marshall) {
1463 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1464 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1465 if (dyn_specs [i]) {
1466 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1467 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1473 mono_class_init (klass);
1475 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1476 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1477 idx = mono_method_get_index (method);
1479 guint32 cols [MONO_PARAM_SIZE];
1480 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1482 if (idx < methodt->rows)
1483 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1485 lastp = paramt->rows + 1;
1487 for (i = param_index; i < lastp; ++i) {
1488 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1490 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1492 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1494 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1503 mono_method_has_marshal_info (MonoMethod *method)
1506 MonoClass *klass = method->klass;
1507 MonoTableInfo *methodt;
1508 MonoTableInfo *paramt;
1511 if (method->klass->image->dynamic) {
1512 MonoReflectionMethodAux *method_aux =
1513 g_hash_table_lookup (
1514 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1515 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1517 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1524 mono_class_init (klass);
1526 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1527 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1528 idx = mono_method_get_index (method);
1530 guint32 cols [MONO_PARAM_SIZE];
1531 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1533 if (idx + 1 < methodt->rows)
1534 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1536 lastp = paramt->rows + 1;
1538 for (i = param_index; i < lastp; ++i) {
1539 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1541 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1550 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1553 g_assert (method != NULL);
1554 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1556 data = ((MonoMethodWrapper *)method)->method_data;
1557 g_assert (data != NULL);
1558 g_assert (id <= GPOINTER_TO_UINT (*data));
1563 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1564 g_error ("stack walk not installed");
1567 static MonoStackWalkImpl stack_walk = default_stack_walk;
1570 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1572 stack_walk (func, TRUE, user_data);
1576 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1578 stack_walk (func, FALSE, user_data);
1582 mono_install_stack_walk (MonoStackWalkImpl func)
1588 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1590 MonoMethod **dest = data;
1592 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1598 mono_method_get_last_managed (void)
1600 MonoMethod *m = NULL;
1601 stack_walk (last_managed, FALSE, &m);
1606 mono_loader_lock (void)
1608 EnterCriticalSection (&loader_mutex);
1612 mono_loader_unlock (void)
1614 LeaveCriticalSection (&loader_mutex);
1617 MonoMethodSignature*
1618 mono_method_signature (MonoMethod *m)
1624 gboolean can_cache_signature;
1625 MonoGenericContainer *container;
1629 return m->signature;
1631 mono_loader_lock ();
1634 mono_loader_unlock ();
1635 return m->signature;
1638 if (m->is_inflated) {
1639 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1640 MonoMethodSignature *signature;
1641 /* the lock is recursive */
1642 signature = mono_method_signature (imethod->declaring);
1643 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, imethod->context);
1644 mono_loader_unlock ();
1645 return m->signature;
1648 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1649 idx = mono_metadata_token_index (m->token);
1650 img = m->klass->image;
1652 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1654 g_assert (!m->klass->generic_class);
1655 container = m->generic_container;
1657 container = m->klass->generic_container;
1659 /* Generic signatures depend on the container so they cannot be cached */
1660 /* icall/pinvoke signatures cannot be cached cause we modify them below */
1661 can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
1663 /* If the method has parameter attributes, that can modify the signature */
1664 pattrs = mono_metadata_get_param_attrs (img, idx);
1666 can_cache_signature = FALSE;
1670 if (can_cache_signature)
1671 m->signature = g_hash_table_lookup (img->method_signatures, sig);
1673 if (!m->signature) {
1674 const char *sig_body;
1676 size = mono_metadata_decode_blob_size (sig, &sig_body);
1678 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
1680 if (can_cache_signature)
1681 g_hash_table_insert (img->method_signatures, (gpointer)sig, m->signature);
1684 /* Verify metadata consistency */
1685 if (m->signature->generic_param_count) {
1686 if (!container || !container->is_method)
1687 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1688 if (container->type_argc != m->signature->generic_param_count)
1689 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1690 m->signature->generic_param_count, container->type_argc);
1691 } else if (container && container->is_method && container->type_argc)
1692 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1694 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1695 m->signature->pinvoke = 1;
1696 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1697 MonoCallConvention conv = 0;
1698 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1699 m->signature->pinvoke = 1;
1701 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1702 case 0: /* no call conv, so using default */
1703 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1704 conv = MONO_CALL_DEFAULT;
1706 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1709 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1710 conv = MONO_CALL_STDCALL;
1712 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1713 conv = MONO_CALL_THISCALL;
1715 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1716 conv = MONO_CALL_FASTCALL;
1718 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1719 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1721 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1722 g_assert_not_reached ();
1724 m->signature->call_convention = conv;
1727 mono_loader_unlock ();
1728 return m->signature;
1732 mono_method_get_name (MonoMethod *method)
1734 return method->name;
1738 mono_method_get_class (MonoMethod *method)
1740 return method->klass;
1744 mono_method_get_token (MonoMethod *method)
1746 return method->token;
1750 mono_method_get_header (MonoMethod *method)
1756 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1758 if (method->klass->dummy || (method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1762 if (G_LIKELY (mn->header))
1768 mono_loader_lock ();
1771 mono_loader_unlock ();
1775 if (method->is_inflated) {
1776 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1777 MonoMethodHeader *header;
1778 /* the lock is recursive */
1779 header = mono_method_get_header (imethod->declaring);
1780 mn->header = inflate_generic_header (header, imethod->context);
1781 mono_loader_unlock ();
1785 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1786 idx = mono_metadata_token_index (method->token);
1787 img = method->klass->image;
1788 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1789 loc = mono_image_rva_map (img, rva);
1793 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1795 mono_loader_unlock ();
1800 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1803 *iflags = method->iflags;
1804 return method->flags;
1808 * Find the method index in the metadata methodDef table.
1811 mono_method_get_index (MonoMethod *method) {
1812 MonoClass *klass = method->klass;
1816 return mono_metadata_token_index (method->token);
1818 mono_class_setup_methods (klass);
1819 for (i = 0; i < klass->method.count; ++i) {
1820 if (method == klass->methods [i])
1821 return klass->method.first + 1 + i;