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;
49 InitializeCriticalSection (&loader_mutex);
52 static MonoClassField*
53 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
54 MonoGenericContext *context)
57 MonoTableInfo *tables = image->tables;
59 guint32 nindex, class;
62 guint32 idx = mono_metadata_token_index (token);
65 MonoClassField *result = mono_lookup_dynamic_token (image, token);
66 *retklass = result->parent;
70 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
71 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
72 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
74 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
76 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
77 mono_metadata_decode_blob_size (ptr, &ptr);
78 /* we may want to check the signature here... */
81 case MONO_MEMBERREF_PARENT_TYPEREF:
82 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
84 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
85 g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
89 mono_class_init (klass);
92 return mono_class_get_field_from_name (klass, fname);
93 case MONO_MEMBERREF_PARENT_TYPESPEC: {
94 /*guint32 bcols [MONO_TYPESPEC_SIZE];
98 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
99 bcols, MONO_TYPESPEC_SIZE);
100 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
101 len = mono_metadata_decode_value (ptr, &ptr);
102 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
104 klass = mono_class_from_mono_type (type);
105 mono_class_init (klass);
106 g_print ("type in sig: %s\n", klass->name);*/
107 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
108 mono_class_init (klass);
111 return mono_class_get_field_from_name (klass, fname);
114 g_warning ("field load from %x", class);
120 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
121 MonoGenericContext *context)
125 MonoClassField *field;
127 if (image->dynamic) {
128 MonoClassField *result = mono_lookup_dynamic_token (image, token);
129 *retklass = result->parent;
134 if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
135 *retklass = field->parent;
136 mono_loader_unlock ();
139 mono_loader_unlock ();
141 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
142 field = field_from_memberref (image, token, retklass, context);
144 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
147 k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
153 field = mono_class_get_field (k, token);
157 if (!field->parent->generic_class)
158 g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
159 mono_loader_unlock ();
164 mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
168 if (sig1->hasthis != sig2->hasthis ||
169 sig1->sentinelpos != sig2->sentinelpos)
172 for (i = 0; i < sig1->sentinelpos; i++) {
173 MonoType *p1 = sig1->params[i];
174 MonoType *p2 = sig2->params[i];
176 /*if (p1->attrs != p2->attrs)
179 if (!mono_metadata_type_equal (p1, p2))
183 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
189 find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignature *sig)
192 char *qname, *fqname;
195 qname = g_strconcat (ic->name, ".", name, NULL);
196 if (ic->name_space && ic->name_space [0])
197 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", name, NULL);
201 qname = fqname = NULL;
204 mono_class_setup_methods (klass);
205 for (i = 0; i < klass->method.count; ++i) {
206 MonoMethod *m = klass->methods [i];
208 if (!((fqname && !strcmp (m->name, fqname)) ||
209 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
212 if (sig->call_convention == MONO_CALL_VARARG) {
213 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
216 if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
221 if (name [0] == '.' && (strcmp (name, ".ctor") == 0 || strcmp (name, ".cctor") == 0))
224 klass = klass->parent;
231 * token is the method_ref or method_def token used in a call IL instruction.
234 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
236 int table = mono_metadata_token_table (token);
237 int idx = mono_metadata_token_index (token);
238 guint32 cols [MONO_MEMBERREF_SIZE];
239 MonoMethodSignature *sig;
242 /* !table is for wrappers: we should really assign their own token to them */
243 if (!table || table == MONO_TABLE_METHOD)
244 return mono_method_signature (method);
246 if (table == MONO_TABLE_METHODSPEC) {
247 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
248 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
249 mono_method_signature (method));
250 g_assert (method->is_inflated);
252 return mono_method_signature (method);
255 if (method->klass->generic_class)
256 return mono_method_signature (method);
259 /* FIXME: This might be incorrect for vararg methods */
260 return mono_method_signature (method);
262 if (!(sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token)))) {
263 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
265 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
266 mono_metadata_decode_blob_size (ptr, &ptr);
267 sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
268 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
271 sig = mono_class_inflate_generic_signature (image, sig, context);
277 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
279 return mono_method_get_signature_full (method, image, token, NULL);
283 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
285 MonoClass *klass = NULL;
286 MonoMethod *method = NULL;
287 MonoTableInfo *tables = image->tables;
289 guint32 nindex, class;
290 MonoGenericClass *gclass = NULL;
291 MonoGenericContainer *container = NULL;
293 MonoMethodSignature *sig;
296 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
297 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
298 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
299 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
300 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
302 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
305 case MONO_MEMBERREF_PARENT_TYPEREF:
306 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
308 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
309 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
314 case MONO_MEMBERREF_PARENT_TYPESPEC:
315 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
317 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
318 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
323 case MONO_MEMBERREF_PARENT_TYPEDEF:
324 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
326 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex, context);
327 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
332 case MONO_MEMBERREF_PARENT_METHODDEF:
333 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
335 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
336 g_assert_not_reached ();
340 if (klass->generic_class) {
341 gclass = klass->generic_class;
342 klass = gclass->container_class;
344 if (klass->generic_container)
345 container = klass->generic_container;
346 mono_class_init (klass);
348 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
349 mono_metadata_decode_blob_size (ptr, &ptr);
350 sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
353 case MONO_MEMBERREF_PARENT_TYPEREF:
354 method = find_method (klass, NULL, mname, sig);
356 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
357 mono_metadata_free_method_signature (sig);
359 case MONO_MEMBERREF_PARENT_TYPESPEC: {
363 type = &klass->byval_arg;
365 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
366 method = find_method (klass, NULL, mname, sig);
368 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
369 else if (klass->generic_class && (klass != method->klass))
370 method = mono_class_inflate_generic_method (
371 method, klass->generic_class->context, klass);
372 mono_metadata_free_method_signature (sig);
376 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
377 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
378 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
379 result->signature = sig;
380 result->name = mname;
382 if (!strcmp (mname, ".ctor")) {
383 /* we special-case this in the runtime. */
387 if (!strcmp (mname, "Set")) {
388 g_assert (sig->hasthis);
389 g_assert (type->data.array->rank + 1 == sig->param_count);
390 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
394 if (!strcmp (mname, "Get")) {
395 g_assert (sig->hasthis);
396 g_assert (type->data.array->rank == sig->param_count);
397 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
401 if (!strcmp (mname, "Address")) {
402 g_assert (sig->hasthis);
403 g_assert (type->data.array->rank == sig->param_count);
404 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
408 g_assert_not_reached ();
411 case MONO_MEMBERREF_PARENT_TYPEDEF:
412 method = find_method (klass, NULL, mname, sig);
414 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
415 mono_metadata_free_method_signature (sig);
418 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
419 g_assert_not_reached ();
423 method = mono_class_inflate_generic_method (method, gclass->context, gclass->klass);
429 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
431 MonoMethod *method, *inflated;
432 MonoTableInfo *tables = image->tables;
433 MonoGenericContext *new_context = NULL;
434 MonoGenericMethod *gmethod;
435 MonoGenericContainer *container = NULL;
437 guint32 cols [MONO_METHODSPEC_SIZE];
438 guint32 token, param_count;
440 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
441 token = cols [MONO_METHODSPEC_METHOD];
442 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
443 token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
445 token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
447 method = mono_get_method (image, token, NULL);
449 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
451 mono_metadata_decode_value (ptr, &ptr);
453 param_count = mono_metadata_decode_value (ptr, &ptr);
455 g_assert (param_count);
456 if (method->is_inflated)
457 container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
459 container = ((MonoMethodNormal *) method)->generic_container;
460 g_assert (container && container->is_method);
463 g_assert (context->container);
464 container->parent = context->container;
465 if (container->parent->is_method)
466 container->parent = container->parent->parent;
469 gmethod = g_new0 (MonoGenericMethod, 1);
470 gmethod->container = container;
472 gmethod->inst = mono_metadata_parse_generic_inst (
473 image, (MonoGenericContext *) container, param_count, ptr, &ptr);
476 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
478 if (!container->method_hash)
479 container->method_hash = g_hash_table_new (
480 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
482 inflated = g_hash_table_lookup (container->method_hash, gmethod);
489 new_context = g_new0 (MonoGenericContext, 1);
490 new_context->container = container;
491 new_context->gmethod = gmethod;
493 context = new_context;
495 new_context = g_new0 (MonoGenericContext, 1);
496 new_context->container = container;
497 new_context->gmethod = gmethod;
498 new_context->gclass = context->gclass;
500 context = new_context;
503 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
504 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
506 inflated = mono_class_inflate_generic_method (method, context, NULL);
507 g_hash_table_insert (container->method_hash, gmethod, inflated);
510 context->gclass = inflated->klass->generic_class;
514 typedef struct MonoDllMap MonoDllMap;
523 static GHashTable *global_dll_map;
526 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
527 MonoDllMap *map, *tmp;
536 map = g_hash_table_lookup (dll_map, dll);
538 mono_loader_unlock ();
541 *rdll = map->target? map->target: dll;
543 for (tmp = map->next; tmp; tmp = tmp->next) {
544 if (strcmp (func, tmp->name) == 0) {
548 mono_loader_unlock ();
553 mono_loader_unlock ();
558 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
561 if (assembly && assembly->dll_map) {
562 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
566 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
570 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
571 MonoDllMap *map, *entry;
572 GHashTable *dll_map = NULL;
578 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
579 dll_map = global_dll_map;
581 if (!assembly->dll_map)
582 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
583 dll_map = assembly->dll_map;
586 map = g_hash_table_lookup (dll_map, dll);
588 map = g_new0 (MonoDllMap, 1);
589 map->dll = g_strdup (dll);
591 map->target = g_strdup (tdll);
592 g_hash_table_insert (dll_map, map->dll, map);
595 entry = g_new0 (MonoDllMap, 1);
596 entry->name = g_strdup (func);
598 entry->target = g_strdup (tfunc);
599 if (tdll && map->target && strcmp (map->target, tdll))
600 entry->dll = g_strdup (tdll);
601 entry->next = map->next;
605 mono_loader_unlock ();
609 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
611 MonoImage *image = method->klass->image;
612 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
613 MonoTableInfo *tables = image->tables;
614 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
615 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
616 guint32 im_cols [MONO_IMPLMAP_SIZE];
618 const char *import = NULL;
619 const char *orig_scope;
620 const char *new_scope;
621 char *full_name, *file_name;
623 GModule *gmodule = NULL;
625 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
630 if (method->klass->image->dynamic) {
631 MonoReflectionMethodAux *method_aux =
632 g_hash_table_lookup (
633 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
637 import = method_aux->dllentry;
638 orig_scope = method_aux->dll;
641 if (!piinfo->implmap_idx)
644 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
646 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
647 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
648 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
649 orig_scope = mono_metadata_string_heap (image, scope_token);
652 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
654 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
655 "DllImport attempting to load: '%s'.", new_scope);
662 /* we allow a special name to dlopen from the running process namespace */
663 if (strcmp (new_scope, "__Internal") == 0)
664 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
667 * Try loading the module using a variety of names
669 for (i = 0; i < 3; ++i) {
672 /* Try the original name */
673 file_name = g_strdup (new_scope);
676 /* Try trimming the .dll extension */
677 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
678 file_name = g_strdup (new_scope);
679 file_name [strlen (new_scope) - 4] = '\0';
685 if (strstr (new_scope, "lib") != new_scope) {
686 file_name = g_strdup_printf ("lib%s", new_scope);
694 full_name = g_module_build_path (NULL, file_name);
695 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
696 "DllImport loading location: '%s'.", full_name);
697 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
699 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
700 "DllImport error loading library: '%s'.",
707 full_name = g_module_build_path (".", file_name);
708 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
709 "DllImport loading library: '%s'.", full_name);
710 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
712 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
713 "DllImport error loading library '%s'.",
720 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
721 "DllImport loading: '%s'.", file_name);
722 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
724 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
725 "DllImport error loading library '%s'.",
737 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
738 "DllImport unable to load library '%s'.",
742 *exc_class = "DllNotFoundException";
743 *exc_arg = new_scope;
748 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
749 "Searching for '%s'.", import);
751 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
752 g_module_symbol (gmodule, import, &piinfo->addr);
754 char *mangled_name = NULL, *mangled_name2 = NULL;
757 int mangle_param_count;
758 #ifdef PLATFORM_WIN32
763 * Search using a variety of mangled names
765 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
766 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
767 gboolean need_param_count = FALSE;
768 #ifdef PLATFORM_WIN32
769 if (mangle_stdcall > 0)
770 need_param_count = TRUE;
772 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
777 mangled_name = (char*)import;
778 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
779 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
780 /* Try the mangled name first */
781 if (mangle_charset == 0)
782 mangled_name = g_strconcat (import, "W", NULL);
784 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
785 #ifdef PLATFORM_WIN32
786 if (mangle_charset == 0)
787 mangled_name = g_strconcat (import, "W", NULL);
790 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
792 /* Try the mangled name last */
793 if (mangle_charset == 1)
794 mangled_name = g_strconcat (import, "A", NULL);
798 #ifdef PLATFORM_WIN32
799 if (mangle_param_count == 0)
800 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
802 /* Try brute force, since it would be very hard to compute the stack usage correctly */
803 param_count = mangle_param_count;
805 /* Try the stdcall mangled name */
807 * gcc under windows creates mangled names without the underscore, but MS.NET
808 * doesn't support it, so we doesn't support it either.
810 if (mangle_stdcall == 1)
811 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
813 mangled_name2 = mangled_name;
815 mangled_name2 = mangled_name;
818 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
819 "Probing '%s'.", mangled_name2);
821 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
824 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
825 "Found as '%s'.", mangled_name2);
827 if (mangled_name != mangled_name2)
828 g_free (mangled_name2);
829 if (mangled_name != import)
830 g_free (mangled_name);
838 *exc_class = "EntryPointNotFoundException";
847 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
848 MonoGenericContext *context)
851 int table = mono_metadata_token_table (token);
852 int idx = mono_metadata_token_index (token);
853 MonoTableInfo *tables = image->tables;
854 MonoGenericContainer *generic_container = NULL, *container = NULL;
855 const char *sig = NULL;
857 guint32 cols [MONO_TYPEDEF_SIZE];
860 return mono_lookup_dynamic_token (image, token);
862 if (table != MONO_TABLE_METHOD) {
863 MonoGenericContainer *generic_container = NULL;
865 g_assert (context->container);
866 generic_container = context->container;
868 if (table == MONO_TABLE_METHODSPEC)
869 return method_from_methodspec (image, context, idx);
870 if (table != MONO_TABLE_MEMBERREF)
871 g_print("got wrong token: 0x%08x\n", token);
872 g_assert (table == MONO_TABLE_MEMBERREF);
873 result = method_from_memberref (image, idx, context);
878 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
880 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
881 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
882 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
884 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
887 result->klass = klass;
888 result->flags = cols [2];
889 result->iflags = cols [1];
890 result->token = token;
891 result->name = mono_metadata_string_heap (image, cols [3]);
894 container = klass->generic_container;
896 if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
897 (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
898 generic_container = mono_metadata_load_generic_params (image, token, container);
899 if (generic_container)
900 container = generic_container;
903 if (!sig) /* already taken from the methodref */
904 sig = mono_metadata_blob_heap (image, cols [4]);
905 size = mono_metadata_decode_blob_size (sig, &sig);
907 /* there are generic params, or a container. FIXME: be lazy here for generics*/
908 if (* sig & 0x10 || container)
909 result->signature = mono_metadata_parse_method_signature_full (
910 image, (MonoGenericContext *) container, idx, sig, NULL);
913 if (!result->klass) {
914 guint32 type = mono_metadata_typedef_from_method (image, token);
915 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
918 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
919 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
920 result->string_ctor = 1;
921 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
922 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
923 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
925 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
926 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
928 if (result->signature && result->signature->generic_param_count) {
929 MonoMethodSignature *sig = result->signature;
931 for (i = 0; i < sig->generic_param_count; i++) {
932 generic_container->type_params [i].method = result;
934 mono_class_from_generic_parameter (
935 &generic_container->type_params [i], image, TRUE);
938 if (sig->ret->type == MONO_TYPE_MVAR) {
939 int num = sig->ret->data.generic_param->num;
940 sig->ret->data.generic_param = &generic_container->type_params [num];
943 for (i = 0; i < sig->param_count; i++) {
944 MonoType *t = sig->params [i];
945 if (t->type == MONO_TYPE_MVAR) {
946 int num = t->data.generic_param->num;
947 sig->params [i]->data.generic_param = &generic_container->type_params [num];
952 /* FIXME: lazyness for generics too, but how? */
953 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
954 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
955 gpointer loc = mono_image_rva_map (image, cols [0]);
957 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
958 image, (MonoGenericContext *) container, loc);
961 ((MonoMethodNormal *) result)->generic_container = generic_container;
968 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
970 return mono_get_method_full (image, token, klass, NULL);
974 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
975 MonoGenericContext *context)
979 /* We do everything inside the lock to prevent creation races */
983 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
984 mono_loader_unlock ();
988 result = mono_get_method_from_token (image, token, klass, context);
990 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
992 if (!(result && result->is_inflated))
993 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
995 mono_loader_unlock ();
1001 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1002 MonoGenericContext *context)
1004 MonoMethod *method, *result;
1005 MonoClass *ic = NULL;
1006 MonoGenericClass *gclass = NULL;
1008 mono_loader_lock ();
1010 method = mono_get_method_from_token (image, token, NULL, context);
1012 mono_loader_unlock ();
1016 mono_class_init (constrained_class);
1017 method = mono_get_inflated_method (method);
1019 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1022 if (constrained_class->generic_class)
1023 gclass = constrained_class->generic_class;
1025 result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1027 g_warning ("Missing method %s in assembly %s token %x", method->name,
1028 image->name, token);
1031 result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
1033 mono_loader_unlock ();
1038 mono_free_method (MonoMethod *method)
1040 if (method->signature) {
1042 * FIXME: This causes crashes because the types inside signatures and
1043 * locals are shared.
1045 /* mono_metadata_free_method_signature (method->signature); */
1046 g_free (method->signature);
1049 if (method->dynamic) {
1050 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1052 g_free ((char*)method->name);
1053 if (mw->method.header)
1054 g_free ((char*)mw->method.header->code);
1055 g_free (mw->method_data);
1058 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1060 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1061 g_free (((MonoMethodNormal*)method)->header);
1068 mono_method_get_param_names (MonoMethod *method, const char **names)
1071 MonoClass *klass = method->klass;
1072 MonoTableInfo *methodt;
1073 MonoTableInfo *paramt;
1076 if (!mono_method_signature (method)->param_count)
1078 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1081 if (klass->generic_class) /* copy the names later */
1084 mono_class_init (klass);
1086 if (klass->image->dynamic) {
1087 MonoReflectionMethodAux *method_aux =
1088 g_hash_table_lookup (
1089 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1090 if (method_aux && method_aux->param_names) {
1091 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1092 if (method_aux->param_names [i + 1])
1093 names [i] = method_aux->param_names [i + 1];
1098 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1099 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1100 idx = mono_method_get_index (method);
1102 guint32 cols [MONO_PARAM_SIZE];
1103 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1105 if (idx < methodt->rows)
1106 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1108 lastp = paramt->rows + 1;
1109 for (i = param_index; i < lastp; ++i) {
1110 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1111 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1112 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1119 mono_method_get_param_token (MonoMethod *method, int index)
1121 MonoClass *klass = method->klass;
1122 MonoTableInfo *methodt;
1125 if (klass->generic_class)
1126 g_assert_not_reached ();
1128 mono_class_init (klass);
1130 if (klass->image->dynamic) {
1131 g_assert_not_reached ();
1134 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1135 idx = mono_method_get_index (method);
1137 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1139 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1146 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1149 MonoClass *klass = method->klass;
1150 MonoTableInfo *methodt;
1151 MonoTableInfo *paramt;
1154 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1157 if (method->klass->image->dynamic) {
1158 MonoReflectionMethodAux *method_aux =
1159 g_hash_table_lookup (
1160 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1161 if (method_aux && method_aux->param_marshall) {
1162 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1163 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1164 if (dyn_specs [i]) {
1165 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1166 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1172 mono_class_init (klass);
1174 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1175 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1176 idx = mono_method_get_index (method);
1178 guint32 cols [MONO_PARAM_SIZE];
1179 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1181 if (idx < methodt->rows)
1182 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1184 lastp = paramt->rows + 1;
1186 for (i = param_index; i < lastp; ++i) {
1187 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1189 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1191 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1193 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1202 mono_method_has_marshal_info (MonoMethod *method)
1205 MonoClass *klass = method->klass;
1206 MonoTableInfo *methodt;
1207 MonoTableInfo *paramt;
1210 if (method->klass->image->dynamic) {
1211 MonoReflectionMethodAux *method_aux =
1212 g_hash_table_lookup (
1213 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1214 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1216 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1223 mono_class_init (klass);
1225 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1226 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1227 idx = mono_method_get_index (method);
1229 guint32 cols [MONO_PARAM_SIZE];
1230 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1232 if (idx + 1 < methodt->rows)
1233 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1235 lastp = paramt->rows + 1;
1237 for (i = param_index; i < lastp; ++i) {
1238 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1240 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1249 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1252 g_assert (method != NULL);
1253 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1255 data = ((MonoMethodWrapper *)method)->method_data;
1256 g_assert (data != NULL);
1257 g_assert (id <= GPOINTER_TO_UINT (*data));
1262 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1263 g_error ("stack walk not installed");
1266 static MonoStackWalkImpl stack_walk = default_stack_walk;
1269 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1271 stack_walk (func, FALSE, user_data);
1275 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1277 stack_walk (func, TRUE, user_data);
1281 mono_install_stack_walk (MonoStackWalkImpl func)
1287 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1289 MonoMethod **dest = data;
1291 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1297 mono_method_get_last_managed (void)
1299 MonoMethod *m = NULL;
1300 stack_walk (last_managed, FALSE, &m);
1305 mono_loader_lock (void)
1307 EnterCriticalSection (&loader_mutex);
1311 mono_loader_unlock (void)
1313 LeaveCriticalSection (&loader_mutex);
1316 MonoMethodSignature*
1317 mono_method_signature (MonoMethod *m)
1325 return m->signature;
1327 mono_loader_lock ();
1330 mono_loader_unlock ();
1331 return m->signature;
1334 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1335 idx = mono_metadata_token_index (m->token);
1336 img = m->klass->image;
1338 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1339 size = mono_metadata_decode_blob_size (sig, &sig);
1341 m->signature = mono_metadata_parse_method_signature_full (img, NULL, idx, sig, NULL);
1343 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1344 m->signature->pinvoke = 1;
1345 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1346 MonoCallConvention conv = 0;
1347 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1348 m->signature->pinvoke = 1;
1350 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1351 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1352 conv = MONO_CALL_DEFAULT;
1354 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1357 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1358 conv = MONO_CALL_STDCALL;
1360 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1361 conv = MONO_CALL_THISCALL;
1363 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1364 conv = MONO_CALL_FASTCALL;
1366 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1367 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1369 g_warning ("unsupported calling convention");
1370 g_assert_not_reached ();
1372 m->signature->call_convention = conv;
1375 mono_loader_unlock ();
1376 return m->signature;
1380 mono_method_get_name (MonoMethod *method)
1382 return method->name;
1386 mono_method_get_class (MonoMethod *method)
1388 return method->klass;
1392 mono_method_get_token (MonoMethod *method)
1394 return method->token;
1398 mono_method_get_header (MonoMethod *method)
1404 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1407 if (G_LIKELY (mn->header))
1413 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))
1416 mono_loader_lock ();
1419 mono_loader_unlock ();
1423 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1424 idx = mono_metadata_token_index (method->token);
1425 img = method->klass->image;
1426 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1427 loc = mono_image_rva_map (img, rva);
1431 mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1433 mono_loader_unlock ();
1438 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1441 *iflags = method->iflags;
1442 return method->flags;
1446 * Find the method index in the metadata methodDef table.
1449 mono_method_get_index (MonoMethod *method) {
1450 MonoClass *klass = method->klass;
1453 mono_class_setup_methods (klass);
1454 for (i = 0; i < klass->method.count; ++i) {
1455 if (method == klass->methods [i])
1456 return klass->method.first + 1 + i;