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 * Handling of type load errors should be done as follows:
63 * If something could not be loaded, the loader should call one of the
64 * mono_loader_set_error_XXX functions ()
65 * with the appropriate arguments, then return NULL to report the failure. The error
66 * should be propagated until it reaches code which can throw managed exceptions. At that
67 * point, an exception should be thrown based on the information returned by
68 * mono_loader_get_error (). Then the error should be cleared by calling
69 * mono_loader_clear_error ().
73 set_loader_error (MonoLoaderError *error)
75 TlsSetValue (loader_error_thread_id, error);
79 * mono_loader_set_error_type_load:
81 * Set the loader error for this thread. CLASS_NAME and ASSEMBLY_NAME should be
82 * dynamically allocated strings whose ownership is passed to this function.
85 mono_loader_set_error_type_load (char *class_name, char *assembly_name)
87 MonoLoaderError *error;
89 if (mono_loader_get_last_error ()) {
91 g_free (assembly_name);
95 error = g_new0 (MonoLoaderError, 1);
96 error->kind = MONO_LOADER_ERROR_TYPE;
97 error->class_name = class_name;
98 error->assembly_name = assembly_name;
101 * This is not strictly needed, but some (most) of the loader code still
102 * can't deal with load errors, and this message is more helpful than an
105 g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
107 set_loader_error (error);
111 * mono_loader_set_error_method_load:
113 * Set the loader error for this thread. MEMBER_NAME should point to a string
117 mono_loader_set_error_method_load (MonoClass *klass, const char *member_name)
119 MonoLoaderError *error;
121 /* FIXME: Store the signature as well */
122 if (mono_loader_get_last_error ())
125 error = g_new0 (MonoLoaderError, 1);
126 error->kind = MONO_LOADER_ERROR_METHOD;
127 error->klass = klass;
128 error->member_name = member_name;
130 set_loader_error (error);
134 * mono_loader_set_error_field_load:
136 * Set the loader error for this thread. MEMBER_NAME should point to a string
140 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
142 MonoLoaderError *error;
144 /* FIXME: Store the signature as well */
145 if (mono_loader_get_last_error ())
148 error = g_new0 (MonoLoaderError, 1);
149 error->kind = MONO_LOADER_ERROR_FIELD;
150 error->klass = klass;
151 error->member_name = member_name;
153 set_loader_error (error);
157 * mono_loader_get_last_error:
159 * Returns information about the last type load exception encountered by the loader, or
160 * NULL. After use, the exception should be cleared by calling mono_loader_clear_error.
163 mono_loader_get_last_error (void)
165 return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
169 mono_loader_clear_error (void)
171 MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
175 g_free (ex->class_name);
176 g_free (ex->assembly_name);
179 TlsSetValue (loader_error_thread_id, NULL);
182 static MonoClassField*
183 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
184 MonoGenericContext *context)
187 MonoClassField *field;
188 MonoTableInfo *tables = image->tables;
190 guint32 nindex, class;
193 guint32 idx = mono_metadata_token_index (token);
195 if (image->dynamic) {
196 MonoClassField *result = mono_lookup_dynamic_token (image, token);
197 *retklass = result->parent;
201 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
202 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
203 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
205 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
207 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
208 mono_metadata_decode_blob_size (ptr, &ptr);
209 /* we may want to check the signature here... */
212 case MONO_MEMBERREF_PARENT_TYPEREF:
213 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
215 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
216 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
220 mono_class_init (klass);
223 field = mono_class_get_field_from_name (klass, fname);
225 case MONO_MEMBERREF_PARENT_TYPESPEC: {
226 /*guint32 bcols [MONO_TYPESPEC_SIZE];
230 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
231 bcols, MONO_TYPESPEC_SIZE);
232 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
233 len = mono_metadata_decode_value (ptr, &ptr);
234 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
236 klass = mono_class_from_mono_type (type);
237 mono_class_init (klass);
238 g_print ("type in sig: %s\n", klass->name);*/
239 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
240 mono_class_init (klass);
243 field = mono_class_get_field_from_name (klass, fname);
247 g_warning ("field load from %x", class);
252 mono_loader_set_error_field_load (klass, fname);
258 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
259 MonoGenericContext *context)
263 MonoClassField *field;
265 if (image->dynamic) {
266 MonoClassField *result = mono_lookup_dynamic_token (image, token);
267 *retklass = result->parent;
272 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
273 *retklass = field->parent;
274 mono_loader_unlock ();
277 mono_loader_unlock ();
279 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
280 field = field_from_memberref (image, token, retklass, context);
282 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
285 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
291 field = mono_class_get_field (k, token);
295 if (field && !field->parent->generic_class)
296 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
297 mono_loader_unlock ();
302 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
306 if (sig1->hasthis != sig2->hasthis ||
307 sig1->sentinelpos != sig2->sentinelpos)
310 for (i = 0; i < sig1->sentinelpos; i++) {
311 MonoType *p1 = sig1->params[i];
312 MonoType *p2 = sig2->params[i];
314 /*if (p1->attrs != p2->attrs)
317 if (!mono_metadata_type_equal (p1, p2))
321 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
327 find_method_in_class (MonoClass *in_class, const char *name, const char *qname, const char *fqname,
328 MonoMethodSignature *sig, MonoClass *from_class)
332 mono_class_setup_methods (in_class);
333 for (i = 0; i < in_class->method.count; ++i) {
334 MonoMethod *m = in_class->methods [i];
336 if (!((fqname && !strcmp (m->name, fqname)) ||
337 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
340 if (sig->call_convention == MONO_CALL_VARARG) {
341 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
344 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
349 if (i < in_class->method.count) {
350 mono_class_setup_methods (from_class);
351 g_assert (from_class->method.count == in_class->method.count);
352 return from_class->methods [i];
358 find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
361 char *qname, *fqname, *class_name;
362 gboolean is_interface;
363 MonoMethod *result = NULL;
365 is_interface = MONO_CLASS_IS_INTERFACE (in_class);
368 class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
370 qname = g_strconcat (class_name, ".", name, NULL);
371 if (ic->name_space && ic->name_space [0])
372 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
376 class_name = qname = fqname = NULL;
379 g_assert (from_class);
380 result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
384 if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
387 g_assert (from_class->interface_count == in_class->interface_count);
388 for (i = 0; i < in_class->interface_count; i++) {
389 MonoClass *ic = in_class->interfaces [i];
390 MonoClass *from_ic = from_class->interfaces [i];
392 result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
397 in_class = in_class->parent;
398 from_class = from_class->parent;
400 g_assert (!in_class == !from_class);
403 result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
412 static MonoMethodSignature*
413 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
415 MonoMethodSignature *res;
422 res = mono_metadata_signature_alloc (image, sig->param_count);
423 res->ret = mono_class_inflate_generic_type (sig->ret, context);
424 is_open = mono_class_is_open_constructed_type (res->ret);
425 for (i = 0; i < sig->param_count; ++i) {
426 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
428 is_open = mono_class_is_open_constructed_type (res->params [i]);
430 res->hasthis = sig->hasthis;
431 res->explicit_this = sig->explicit_this;
432 res->call_convention = sig->call_convention;
433 res->pinvoke = sig->pinvoke;
434 res->generic_param_count = sig->generic_param_count;
435 res->sentinelpos = sig->sentinelpos;
436 res->has_type_parameters = is_open;
437 res->is_inflated = 1;
441 static MonoMethodHeader*
442 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
444 MonoMethodHeader *res;
446 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
447 res->code = header->code;
448 res->code_size = header->code_size;
449 res->max_stack = header->max_stack;
450 res->num_clauses = header->num_clauses;
451 res->init_locals = header->init_locals;
452 res->num_locals = header->num_locals;
453 res->clauses = header->clauses;
454 for (i = 0; i < header->num_locals; ++i)
455 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
460 * token is the method_ref or method_def token used in a call IL instruction.
463 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
465 int table = mono_metadata_token_table (token);
466 int idx = mono_metadata_token_index (token);
467 guint32 cols [MONO_MEMBERREF_SIZE];
468 MonoMethodSignature *sig, *prev_sig;
471 /* !table is for wrappers: we should really assign their own token to them */
472 if (!table || table == MONO_TABLE_METHOD)
473 return mono_method_signature (method);
475 if (table == MONO_TABLE_METHODSPEC) {
476 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
477 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
478 mono_method_signature (method));
479 g_assert (method->is_inflated);
481 return mono_method_signature (method);
484 if (method->klass->generic_class)
485 return mono_method_signature (method);
488 /* FIXME: This might be incorrect for vararg methods */
489 return mono_method_signature (method);
492 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
493 mono_loader_unlock ();
495 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
497 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
498 mono_metadata_decode_blob_size (ptr, &ptr);
499 sig = mono_metadata_parse_method_signature_full (
500 image, context ? context->container : NULL, 0, ptr, NULL);
503 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
505 /* Somebody got in before us */
506 /* FIXME: Free sig */
510 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
511 mono_loader_unlock ();
514 sig = inflate_generic_signature (image, sig, context);
520 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
522 return mono_method_get_signature_full (method, image, token, NULL);
526 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
528 MonoClass *klass = NULL;
529 MonoMethod *method = NULL;
530 MonoTableInfo *tables = image->tables;
532 guint32 nindex, class;
534 MonoMethodSignature *sig;
537 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
538 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
539 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
540 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
541 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
543 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
546 case MONO_MEMBERREF_PARENT_TYPEREF:
547 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
549 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
550 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
555 case MONO_MEMBERREF_PARENT_TYPESPEC:
557 * Parse the TYPESPEC in the parent's context.
559 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context);
561 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
562 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
567 case MONO_MEMBERREF_PARENT_TYPEDEF:
568 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
570 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
571 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
576 case MONO_MEMBERREF_PARENT_METHODDEF:
577 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
579 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
580 g_assert_not_reached ();
583 mono_class_init (klass);
585 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
586 mono_metadata_decode_blob_size (ptr, &ptr);
588 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
591 case MONO_MEMBERREF_PARENT_TYPEREF:
592 case MONO_MEMBERREF_PARENT_TYPEDEF:
593 method = find_method (klass, NULL, mname, sig, klass);
596 case MONO_MEMBERREF_PARENT_TYPESPEC: {
600 type = &klass->byval_arg;
602 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
603 MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
604 method = find_method (in_class, NULL, mname, sig, klass);
608 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
609 result->klass = klass;
610 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
611 result->signature = sig;
612 result->name = mname;
614 if (!strcmp (mname, ".ctor")) {
615 /* we special-case this in the runtime. */
619 if (!strcmp (mname, "Set")) {
620 g_assert (sig->hasthis);
621 g_assert (type->data.array->rank + 1 == sig->param_count);
622 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
626 if (!strcmp (mname, "Get")) {
627 g_assert (sig->hasthis);
628 g_assert (type->data.array->rank == sig->param_count);
629 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
633 if (!strcmp (mname, "Address")) {
634 g_assert (sig->hasthis);
635 g_assert (type->data.array->rank == sig->param_count);
636 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
640 g_assert_not_reached ();
644 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
645 g_assert_not_reached ();
649 char *msig = mono_signature_get_desc (sig, FALSE);
650 GString *s = g_string_new (mname);
651 if (sig->generic_param_count)
652 g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
653 g_string_append_printf (s, "(%s)", msig);
655 msig = g_string_free (s, FALSE);
658 "Missing method %s::%s in assembly %s, referenced in assembly %s",
659 mono_type_get_name (&klass->byval_arg), msig, klass->image->name, image->name);
661 mono_loader_set_error_method_load (klass, mname);
663 mono_metadata_free_method_signature (sig);
669 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
671 MonoMethod *method, *inflated;
672 MonoTableInfo *tables = image->tables;
673 MonoGenericContext *new_context = NULL;
674 MonoGenericMethod *gmethod;
675 MonoGenericContainer *container = NULL;
677 guint32 cols [MONO_METHODSPEC_SIZE];
678 guint32 token, nindex, param_count;
680 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
681 token = cols [MONO_METHODSPEC_METHOD];
682 nindex = token >> MONO_METHODDEFORREF_BITS;
684 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
686 mono_metadata_decode_value (ptr, &ptr);
688 param_count = mono_metadata_decode_value (ptr, &ptr);
689 g_assert (param_count);
692 * Be careful with the two contexts here:
694 * ----------------------------------------
696 * static void Hello<T> (S s, T t) { }
698 * static void Test<U> (U u) {
699 * Foo<U>.Hello<string> (u, "World");
702 * ----------------------------------------
704 * Let's assume we're currently JITing Foo<int>.Test<long>
705 * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
707 * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
708 * (MONO_MEMBERREF_PARENT_TYPESPEC).
710 * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
711 * context (S=int, U=long) to get the correct `Foo<long>'.
713 * After that, we parse the memberref signature in the new context
714 * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
717 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
718 method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
720 method = method_from_memberref (image, nindex, context);
722 method = mono_get_inflated_method (method);
724 container = method->generic_container;
725 g_assert (container);
727 gmethod = g_new0 (MonoGenericMethod, 1);
728 gmethod->generic_class = method->klass->generic_class;
729 gmethod->container = container;
731 new_context = g_new0 (MonoGenericContext, 1);
732 new_context->container = container;
733 new_context->gmethod = gmethod;
734 if (container->parent)
735 new_context->gclass = container->parent->context.gclass;
738 * When parsing the methodspec signature, we're in the old context again:
740 * ----------------------------------------
742 * static void Hello<T> (T t) { }
744 * static void Test<U> (U u) {
748 * ----------------------------------------
750 * Let's assume we're currently JITing "Foo.Test<float>".
752 * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
753 * signature is "<!!0>". This means that we must instantiate the method type parameter
754 * `T' from the new method with the method type parameter `U' from the current context;
755 * ie. instantiate the method as `Foo.Hello<float>.
758 gmethod->inst = mono_metadata_parse_generic_inst (image, context ? context->container : NULL, param_count, ptr, &ptr);
760 if (context && gmethod->inst->is_open)
761 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
763 if (!container->method_hash)
764 container->method_hash = g_hash_table_new (
765 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
767 inflated = g_hash_table_lookup (container->method_hash, gmethod);
770 g_free (new_context);
774 context = new_context;
776 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
777 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
779 inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
780 g_hash_table_insert (container->method_hash, gmethod, inflated);
785 typedef struct MonoDllMap MonoDllMap;
794 static GHashTable *global_dll_map;
797 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
798 MonoDllMap *map, *tmp;
807 map = g_hash_table_lookup (dll_map, dll);
809 mono_loader_unlock ();
812 *rdll = map->target? map->target: dll;
814 for (tmp = map->next; tmp; tmp = tmp->next) {
815 if (strcmp (func, tmp->name) == 0) {
819 mono_loader_unlock ();
824 mono_loader_unlock ();
829 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
832 if (assembly && assembly->dll_map) {
833 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
837 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
841 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
842 MonoDllMap *map, *entry;
843 GHashTable *dll_map = NULL;
849 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
850 dll_map = global_dll_map;
852 if (!assembly->dll_map)
853 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
854 dll_map = assembly->dll_map;
857 map = g_hash_table_lookup (dll_map, dll);
859 map = g_new0 (MonoDllMap, 1);
860 map->dll = g_strdup (dll);
862 map->target = g_strdup (tdll);
863 g_hash_table_insert (dll_map, map->dll, map);
866 entry = g_new0 (MonoDllMap, 1);
867 entry->name = g_strdup (func);
869 entry->target = g_strdup (tfunc);
870 if (tdll && map->target && strcmp (map->target, tdll))
871 entry->dll = g_strdup (tdll);
872 entry->next = map->next;
876 mono_loader_unlock ();
880 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
882 MonoImage *image = method->klass->image;
883 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
884 MonoTableInfo *tables = image->tables;
885 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
886 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
887 guint32 im_cols [MONO_IMPLMAP_SIZE];
889 const char *import = NULL;
890 const char *orig_scope;
891 const char *new_scope;
892 char *full_name, *file_name;
894 GModule *gmodule = NULL;
896 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
901 if (method->klass->image->dynamic) {
902 MonoReflectionMethodAux *method_aux =
903 g_hash_table_lookup (
904 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
908 import = method_aux->dllentry;
909 orig_scope = method_aux->dll;
912 if (!piinfo->implmap_idx)
915 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
917 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
918 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
919 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
920 orig_scope = mono_metadata_string_heap (image, scope_token);
923 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
925 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
926 "DllImport attempting to load: '%s'.", new_scope);
933 /* we allow a special name to dlopen from the running process namespace */
934 if (strcmp (new_scope, "__Internal") == 0)
935 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
938 * Try loading the module using a variety of names
940 for (i = 0; i < 4; ++i) {
943 /* Try the original name */
944 file_name = g_strdup (new_scope);
947 /* Try trimming the .dll extension */
948 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
949 file_name = g_strdup (new_scope);
950 file_name [strlen (new_scope) - 4] = '\0';
956 if (strstr (new_scope, "lib") != new_scope) {
957 file_name = g_strdup_printf ("lib%s", new_scope);
963 #ifndef PLATFORM_WIN32
964 if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
965 !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
966 !g_ascii_strcasecmp ("user32", new_scope) ||
967 !g_ascii_strcasecmp ("kernel", new_scope)) {
968 file_name = g_strdup ("libMonoSupportW.so");
972 #ifndef PLATFORM_WIN32
978 full_name = g_module_build_path (NULL, file_name);
979 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
980 "DllImport loading location: '%s'.", full_name);
981 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
983 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
984 "DllImport error loading library: '%s'.",
991 full_name = g_module_build_path (".", file_name);
992 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
993 "DllImport loading library: '%s'.", full_name);
994 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
996 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
997 "DllImport error loading library '%s'.",
1004 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1005 "DllImport loading: '%s'.", file_name);
1006 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
1008 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1009 "DllImport error loading library '%s'.",
1021 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
1022 "DllImport unable to load library '%s'.",
1026 *exc_class = "DllNotFoundException";
1027 *exc_arg = new_scope;
1032 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1033 "Searching for '%s'.", import);
1035 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
1036 g_module_symbol (gmodule, import, &piinfo->addr);
1038 char *mangled_name = NULL, *mangled_name2 = NULL;
1041 int mangle_param_count;
1042 #ifdef PLATFORM_WIN32
1047 * Search using a variety of mangled names
1049 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
1050 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
1051 gboolean need_param_count = FALSE;
1052 #ifdef PLATFORM_WIN32
1053 if (mangle_stdcall > 0)
1054 need_param_count = TRUE;
1056 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
1061 mangled_name = (char*)import;
1062 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
1063 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
1064 /* Try the mangled name first */
1065 if (mangle_charset == 0)
1066 mangled_name = g_strconcat (import, "W", NULL);
1068 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
1069 #ifdef PLATFORM_WIN32
1070 if (mangle_charset == 0)
1071 mangled_name = g_strconcat (import, "W", NULL);
1073 /* Try the mangled name last */
1074 if (mangle_charset == 1)
1075 mangled_name = g_strconcat (import, "A", NULL);
1078 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
1080 /* Try the mangled name last */
1081 if (mangle_charset == 1)
1082 mangled_name = g_strconcat (import, "A", NULL);
1086 #ifdef PLATFORM_WIN32
1087 if (mangle_param_count == 0)
1088 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
1090 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1091 param_count = mangle_param_count;
1093 /* Try the stdcall mangled name */
1095 * gcc under windows creates mangled names without the underscore, but MS.NET
1096 * doesn't support it, so we doesn't support it either.
1098 if (mangle_stdcall == 1)
1099 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
1101 mangled_name2 = mangled_name;
1103 mangled_name2 = mangled_name;
1106 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1107 "Probing '%s'.", mangled_name2);
1109 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
1112 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
1113 "Found as '%s'.", mangled_name2);
1115 if (mangled_name != mangled_name2)
1116 g_free (mangled_name2);
1117 if (mangled_name != import)
1118 g_free (mangled_name);
1124 if (!piinfo->addr) {
1126 *exc_class = "EntryPointNotFoundException";
1131 return piinfo->addr;
1135 mono_get_shared_generic_method (MonoGenericContainer *container)
1137 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1138 gmethod->container = container;
1139 gmethod->generic_class = container->context.gclass;
1140 gmethod->inst = mono_get_shared_generic_inst (container);
1146 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
1147 MonoGenericContext *context)
1150 int table = mono_metadata_token_table (token);
1151 int idx = mono_metadata_token_index (token);
1152 MonoTableInfo *tables = image->tables;
1153 MonoGenericContainer *generic_container = NULL, *container = NULL;
1154 const char *sig = NULL;
1156 guint32 cols [MONO_TYPEDEF_SIZE];
1159 return mono_lookup_dynamic_token (image, token);
1161 if (table != MONO_TABLE_METHOD) {
1162 if (table == MONO_TABLE_METHODSPEC)
1163 return method_from_methodspec (image, context, idx);
1164 if (table != MONO_TABLE_MEMBERREF)
1165 g_print("got wrong token: 0x%08x\n", token);
1166 g_assert (table == MONO_TABLE_MEMBERREF);
1167 result = method_from_memberref (image, idx, context);
1172 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
1174 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1175 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1176 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
1178 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
1181 guint32 type = mono_metadata_typedef_from_method (image, token);
1182 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1186 result->klass = klass;
1187 result->flags = cols [2];
1188 result->iflags = cols [1];
1189 result->token = token;
1190 result->name = mono_metadata_string_heap (image, cols [3]);
1192 container = klass->generic_container;
1193 generic_container = mono_metadata_load_generic_params (image, token, container);
1194 if (generic_container) {
1195 MonoGenericContext *context = &generic_container->context;
1197 context->gclass = container->context.gclass;
1198 context->gmethod = mono_get_shared_generic_method (generic_container);
1199 mono_metadata_load_generic_param_constraints (image, token, generic_container);
1201 for (i = 0; i < generic_container->type_argc; i++) {
1202 generic_container->type_params [i].method = result;
1204 mono_class_from_generic_parameter (
1205 &generic_container->type_params [i], image, TRUE);
1208 container = generic_container;
1211 if (!sig) /* already taken from the methodref */
1212 sig = mono_metadata_blob_heap (image, cols [4]);
1213 size = mono_metadata_decode_blob_size (sig, &sig);
1215 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1216 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
1217 result->string_ctor = 1;
1218 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1219 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
1220 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
1222 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
1223 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
1226 /* FIXME: lazyness for generics too, but how? */
1227 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
1228 !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
1229 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
1230 gpointer loc = mono_image_rva_map (image, cols [0]);
1232 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
1235 result->generic_container = generic_container;
1241 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1243 return mono_get_method_full (image, token, klass, NULL);
1247 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1248 MonoGenericContext *context)
1252 /* We do everything inside the lock to prevent creation races */
1254 mono_loader_lock ();
1256 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1257 mono_loader_unlock ();
1261 result = mono_get_method_from_token (image, token, klass, context);
1263 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1265 if (!(result && result->is_inflated))
1266 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1268 mono_loader_unlock ();
1274 * mono_get_method_constrained:
1276 * This is used when JITing the `constrained.' opcode.
1279 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1280 MonoGenericContext *context)
1282 MonoMethod *method, *result;
1283 MonoClass *ic = NULL;
1284 MonoGenericContext *class_context = NULL, *method_context = NULL;
1285 MonoMethodSignature *sig;
1287 mono_loader_lock ();
1289 method = mono_get_method_from_token (image, token, NULL, context);
1291 mono_loader_unlock ();
1295 mono_class_init (constrained_class);
1296 method = mono_get_inflated_method (method);
1297 sig = mono_method_signature (method);
1299 if (method->is_inflated && sig->generic_param_count) {
1300 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1301 sig = mono_method_signature (imethod->declaring);
1302 method_context = imethod->context;
1305 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1308 if (constrained_class->generic_class)
1309 class_context = constrained_class->generic_class->context;
1311 result = find_method (constrained_class, ic, method->name, sig, constrained_class);
1313 g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
1314 method->klass->name, method->name, image->name, token);
1315 mono_loader_unlock ();
1320 result = mono_class_inflate_generic_method (result, class_context);
1322 result = mono_class_inflate_generic_method (result, method_context);
1324 mono_loader_unlock ();
1329 mono_free_method (MonoMethod *method)
1331 if (method->signature) {
1333 * FIXME: This causes crashes because the types inside signatures and
1334 * locals are shared.
1336 /* mono_metadata_free_method_signature (method->signature); */
1337 g_free (method->signature);
1340 if (method->dynamic) {
1341 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1343 g_free ((char*)method->name);
1344 if (mw->method.header)
1345 g_free ((char*)mw->method.header->code);
1346 g_free (mw->method_data);
1349 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1351 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1352 g_free (((MonoMethodNormal*)method)->header);
1359 mono_method_get_param_names (MonoMethod *method, const char **names)
1362 MonoClass *klass = method->klass;
1363 MonoTableInfo *methodt;
1364 MonoTableInfo *paramt;
1367 if (!mono_method_signature (method)->param_count)
1369 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1372 if (klass->generic_class) /* copy the names later */
1375 mono_class_init (klass);
1377 if (klass->image->dynamic) {
1378 MonoReflectionMethodAux *method_aux =
1379 g_hash_table_lookup (
1380 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1381 if (method_aux && method_aux->param_names) {
1382 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1383 if (method_aux->param_names [i + 1])
1384 names [i] = method_aux->param_names [i + 1];
1389 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1390 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1391 idx = mono_method_get_index (method);
1393 guint32 cols [MONO_PARAM_SIZE];
1394 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1396 if (idx < methodt->rows)
1397 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1399 lastp = paramt->rows + 1;
1400 for (i = param_index; i < lastp; ++i) {
1401 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1402 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1403 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1410 mono_method_get_param_token (MonoMethod *method, int index)
1412 MonoClass *klass = method->klass;
1413 MonoTableInfo *methodt;
1416 if (klass->generic_class)
1417 g_assert_not_reached ();
1419 mono_class_init (klass);
1421 if (klass->image->dynamic) {
1422 g_assert_not_reached ();
1425 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1426 idx = mono_method_get_index (method);
1428 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1430 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1437 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1440 MonoClass *klass = method->klass;
1441 MonoTableInfo *methodt;
1442 MonoTableInfo *paramt;
1445 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1448 if (method->klass->image->dynamic) {
1449 MonoReflectionMethodAux *method_aux =
1450 g_hash_table_lookup (
1451 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1452 if (method_aux && method_aux->param_marshall) {
1453 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1454 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1455 if (dyn_specs [i]) {
1456 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1457 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1463 mono_class_init (klass);
1465 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1466 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1467 idx = mono_method_get_index (method);
1469 guint32 cols [MONO_PARAM_SIZE];
1470 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1472 if (idx < methodt->rows)
1473 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1475 lastp = paramt->rows + 1;
1477 for (i = param_index; i < lastp; ++i) {
1478 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1480 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1482 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1484 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1493 mono_method_has_marshal_info (MonoMethod *method)
1496 MonoClass *klass = method->klass;
1497 MonoTableInfo *methodt;
1498 MonoTableInfo *paramt;
1501 if (method->klass->image->dynamic) {
1502 MonoReflectionMethodAux *method_aux =
1503 g_hash_table_lookup (
1504 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1505 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1507 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1514 mono_class_init (klass);
1516 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1517 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1518 idx = mono_method_get_index (method);
1520 guint32 cols [MONO_PARAM_SIZE];
1521 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1523 if (idx + 1 < methodt->rows)
1524 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1526 lastp = paramt->rows + 1;
1528 for (i = param_index; i < lastp; ++i) {
1529 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1531 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1540 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1543 g_assert (method != NULL);
1544 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1546 data = ((MonoMethodWrapper *)method)->method_data;
1547 g_assert (data != NULL);
1548 g_assert (id <= GPOINTER_TO_UINT (*data));
1553 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1554 g_error ("stack walk not installed");
1557 static MonoStackWalkImpl stack_walk = default_stack_walk;
1560 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1562 stack_walk (func, TRUE, user_data);
1566 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1568 stack_walk (func, FALSE, user_data);
1572 mono_install_stack_walk (MonoStackWalkImpl func)
1578 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1580 MonoMethod **dest = data;
1582 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1588 mono_method_get_last_managed (void)
1590 MonoMethod *m = NULL;
1591 stack_walk (last_managed, FALSE, &m);
1596 mono_loader_lock (void)
1598 EnterCriticalSection (&loader_mutex);
1602 mono_loader_unlock (void)
1604 LeaveCriticalSection (&loader_mutex);
1607 MonoMethodSignature*
1608 mono_method_signature (MonoMethod *m)
1614 MonoGenericContainer *container;
1617 return m->signature;
1619 mono_loader_lock ();
1622 mono_loader_unlock ();
1623 return m->signature;
1626 if (m->is_inflated) {
1627 MonoMethodInflated *imethod = (MonoMethodInflated *) m;
1628 MonoMethodSignature *signature;
1629 /* the lock is recursive */
1630 signature = mono_method_signature (imethod->declaring);
1631 m->signature = inflate_generic_signature (imethod->declaring->klass->image, signature, imethod->context);
1632 mono_loader_unlock ();
1633 return m->signature;
1636 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1637 idx = mono_metadata_token_index (m->token);
1638 img = m->klass->image;
1640 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1641 size = mono_metadata_decode_blob_size (sig, &sig);
1643 g_assert (!m->klass->generic_class);
1644 container = m->generic_container;
1646 container = m->klass->generic_container;
1648 m->signature = mono_metadata_parse_method_signature_full (img, container, idx, sig, NULL);
1650 /* Verify metadata consistency */
1651 if (m->signature->generic_param_count) {
1652 if (!container || !container->is_method)
1653 g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
1654 if (container->type_argc != m->signature->generic_param_count)
1655 g_error ("Inconsistent generic parameter count. Signature says %d, generic_params table says %d",
1656 m->signature->generic_param_count, container->type_argc);
1657 } else if (container && container->is_method && container->type_argc)
1658 g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
1660 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1661 m->signature->pinvoke = 1;
1662 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1663 MonoCallConvention conv = 0;
1664 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1665 m->signature->pinvoke = 1;
1667 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1668 case 0: /* no call conv, so using default */
1669 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1670 conv = MONO_CALL_DEFAULT;
1672 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1675 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1676 conv = MONO_CALL_STDCALL;
1678 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1679 conv = MONO_CALL_THISCALL;
1681 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1682 conv = MONO_CALL_FASTCALL;
1684 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1685 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1687 g_warning ("unsupported calling convention : 0x%04x", piinfo->piflags);
1688 g_assert_not_reached ();
1690 m->signature->call_convention = conv;
1693 mono_loader_unlock ();
1694 return m->signature;
1698 mono_method_get_name (MonoMethod *method)
1700 return method->name;
1704 mono_method_get_class (MonoMethod *method)
1706 return method->klass;
1710 mono_method_get_token (MonoMethod *method)
1712 return method->token;
1716 mono_method_get_header (MonoMethod *method)
1722 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1724 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))
1728 if (G_LIKELY (mn->header))
1734 mono_loader_lock ();
1737 mono_loader_unlock ();
1741 if (method->is_inflated) {
1742 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1743 MonoMethodHeader *header;
1744 /* the lock is recursive */
1745 header = mono_method_get_header (imethod->declaring);
1746 mn->header = inflate_generic_header (header, imethod->context);
1747 mono_loader_unlock ();
1751 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1752 idx = mono_metadata_token_index (method->token);
1753 img = method->klass->image;
1754 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1755 loc = mono_image_rva_map (img, rva);
1759 mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
1761 mono_loader_unlock ();
1766 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1769 *iflags = method->iflags;
1770 return method->flags;
1774 * Find the method index in the metadata methodDef table.
1777 mono_method_get_index (MonoMethod *method) {
1778 MonoClass *klass = method->klass;
1782 return mono_metadata_token_index (method->token);
1784 mono_class_setup_methods (klass);
1785 for (i = 0; i < klass->method.count; ++i) {
1786 if (method == klass->methods [i])
1787 return klass->method.first + 1 + i;