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;
761 * Search using a variety of mangled names
763 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
764 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
765 gboolean need_param_count = FALSE;
766 #ifdef PLATFORM_WIN32
767 if (mangle_stdcall > 0)
768 need_param_count = TRUE;
770 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
775 mangled_name = (char*)import;
776 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
777 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
778 /* Try the mangled name first */
779 if (mangle_charset == 0)
780 mangled_name = g_strconcat (import, "W", NULL);
782 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
783 #ifdef PLATFORM_WIN32
784 if (mangle_charset == 0)
785 mangled_name = g_strconcat (import, "W", NULL);
788 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
790 /* Try the mangled name last */
791 if (mangle_charset == 1)
792 mangled_name = g_strconcat (import, "A", NULL);
796 #ifdef PLATFORM_WIN32
797 if (mangle_param_count == 0)
798 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
800 /* Try brute force, since it would be very hard to compute the stack usage correctly */
801 param_count = mangle_param_count;
803 /* Try the stdcall mangled name */
805 * gcc under windows creates mangled names without the underscore, but MS.NET
806 * doesn't support it, so we doesn't support it either.
808 if (mangle_stdcall == 1)
809 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
811 mangled_name2 = mangled_name;
813 mangled_name2 = mangled_name;
816 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
817 "Probing '%s'.", mangled_name2);
819 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
822 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
823 "Found as '%s'.", mangled_name2);
825 if (mangled_name != mangled_name2)
826 g_free (mangled_name2);
827 if (mangled_name != import)
828 g_free (mangled_name);
836 *exc_class = "EntryPointNotFoundException";
845 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
846 MonoGenericContext *context)
849 int table = mono_metadata_token_table (token);
850 int idx = mono_metadata_token_index (token);
851 MonoTableInfo *tables = image->tables;
852 MonoGenericContainer *generic_container = NULL, *container = NULL;
853 const char *sig = NULL;
855 guint32 cols [MONO_TYPEDEF_SIZE];
858 return mono_lookup_dynamic_token (image, token);
860 if (table != MONO_TABLE_METHOD) {
861 MonoGenericContainer *generic_container = NULL;
863 g_assert (context->container);
864 generic_container = context->container;
866 if (table == MONO_TABLE_METHODSPEC)
867 return method_from_methodspec (image, context, idx);
868 if (table != MONO_TABLE_MEMBERREF)
869 g_print("got wrong token: 0x%08x\n", token);
870 g_assert (table == MONO_TABLE_MEMBERREF);
871 result = method_from_memberref (image, idx, context);
876 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
878 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
879 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
880 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
882 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
885 result->klass = klass;
886 result->flags = cols [2];
887 result->iflags = cols [1];
888 result->token = token;
889 result->name = mono_metadata_string_heap (image, cols [3]);
892 container = klass->generic_container;
894 if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
895 (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
896 generic_container = mono_metadata_load_generic_params (image, token, container);
897 if (generic_container)
898 container = generic_container;
901 if (!sig) /* already taken from the methodref */
902 sig = mono_metadata_blob_heap (image, cols [4]);
903 size = mono_metadata_decode_blob_size (sig, &sig);
905 /* there are generic params, or a container. FIXME: be lazy here for generics*/
906 if (* sig & 0x10 || container)
907 result->signature = mono_metadata_parse_method_signature_full (
908 image, (MonoGenericContext *) container, idx, sig, NULL);
911 if (!result->klass) {
912 guint32 type = mono_metadata_typedef_from_method (image, token);
913 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
916 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
917 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
918 result->string_ctor = 1;
919 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
920 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
921 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
923 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
924 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
926 if (result->signature && result->signature->generic_param_count) {
927 MonoMethodSignature *sig = result->signature;
929 for (i = 0; i < sig->generic_param_count; i++) {
930 generic_container->type_params [i].method = result;
932 mono_class_from_generic_parameter (
933 &generic_container->type_params [i], image, TRUE);
936 if (sig->ret->type == MONO_TYPE_MVAR) {
937 int num = sig->ret->data.generic_param->num;
938 sig->ret->data.generic_param = &generic_container->type_params [num];
941 for (i = 0; i < sig->param_count; i++) {
942 MonoType *t = sig->params [i];
943 if (t->type == MONO_TYPE_MVAR) {
944 int num = t->data.generic_param->num;
945 sig->params [i]->data.generic_param = &generic_container->type_params [num];
950 /* FIXME: lazyness for generics too, but how? */
951 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
952 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
953 gpointer loc = mono_image_rva_map (image, cols [0]);
955 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
956 image, (MonoGenericContext *) container, loc);
959 ((MonoMethodNormal *) result)->generic_container = generic_container;
966 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
968 return mono_get_method_full (image, token, klass, NULL);
972 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
973 MonoGenericContext *context)
977 /* We do everything inside the lock to prevent creation races */
981 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
982 mono_loader_unlock ();
986 result = mono_get_method_from_token (image, token, klass, context);
988 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
990 if (!(result && result->is_inflated))
991 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
993 mono_loader_unlock ();
999 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1000 MonoGenericContext *context)
1002 MonoMethod *method, *result;
1003 MonoClass *ic = NULL;
1004 MonoGenericClass *gclass = NULL;
1006 mono_loader_lock ();
1008 method = mono_get_method_from_token (image, token, NULL, context);
1010 mono_loader_unlock ();
1014 mono_class_init (constrained_class);
1015 method = mono_get_inflated_method (method);
1017 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1020 if (constrained_class->generic_class)
1021 gclass = constrained_class->generic_class;
1023 result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1025 g_warning ("Missing method %s in assembly %s token %x", method->name,
1026 image->name, token);
1029 result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
1031 mono_loader_unlock ();
1036 mono_free_method (MonoMethod *method)
1038 if (method->signature)
1039 mono_metadata_free_method_signature (method->signature);
1040 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1041 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
1048 mono_method_get_param_names (MonoMethod *method, const char **names)
1051 MonoClass *klass = method->klass;
1052 MonoTableInfo *methodt;
1053 MonoTableInfo *paramt;
1056 if (!mono_method_signature (method)->param_count)
1058 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1061 if (klass->generic_class) /* copy the names later */
1064 mono_class_init (klass);
1066 if (klass->image->dynamic) {
1067 MonoReflectionMethodAux *method_aux =
1068 g_hash_table_lookup (
1069 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1070 if (method_aux && method_aux->param_names) {
1071 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1072 if (method_aux->param_names [i + 1])
1073 names [i] = method_aux->param_names [i + 1];
1078 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1079 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1080 idx = mono_method_get_index (method);
1082 guint32 cols [MONO_PARAM_SIZE];
1083 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1085 if (idx < methodt->rows)
1086 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1088 lastp = paramt->rows + 1;
1089 for (i = param_index; i < lastp; ++i) {
1090 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1091 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1092 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1099 mono_method_get_param_token (MonoMethod *method, int index)
1101 MonoClass *klass = method->klass;
1102 MonoTableInfo *methodt;
1105 if (klass->generic_class)
1106 g_assert_not_reached ();
1108 mono_class_init (klass);
1110 if (klass->image->dynamic) {
1111 g_assert_not_reached ();
1114 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1115 idx = mono_method_get_index (method);
1117 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1119 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1126 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1129 MonoClass *klass = method->klass;
1130 MonoTableInfo *methodt;
1131 MonoTableInfo *paramt;
1134 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1137 if (method->klass->image->dynamic) {
1138 MonoReflectionMethodAux *method_aux =
1139 g_hash_table_lookup (
1140 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1141 if (method_aux && method_aux->param_marshall) {
1142 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1143 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1144 if (dyn_specs [i]) {
1145 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1146 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1152 mono_class_init (klass);
1154 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1155 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1156 idx = mono_method_get_index (method);
1158 guint32 cols [MONO_PARAM_SIZE];
1159 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1161 if (idx < methodt->rows)
1162 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1164 lastp = paramt->rows + 1;
1166 for (i = param_index; i < lastp; ++i) {
1167 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1169 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1171 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1173 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1182 mono_method_has_marshal_info (MonoMethod *method)
1185 MonoClass *klass = method->klass;
1186 MonoTableInfo *methodt;
1187 MonoTableInfo *paramt;
1190 if (method->klass->image->dynamic) {
1191 MonoReflectionMethodAux *method_aux =
1192 g_hash_table_lookup (
1193 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1194 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1196 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1203 mono_class_init (klass);
1205 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1206 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1207 idx = mono_method_get_index (method);
1209 guint32 cols [MONO_PARAM_SIZE];
1210 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1212 if (idx + 1 < methodt->rows)
1213 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1215 lastp = paramt->rows + 1;
1217 for (i = param_index; i < lastp; ++i) {
1218 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1220 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1229 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1232 g_assert (method != NULL);
1233 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1235 data = ((MonoMethodWrapper *)method)->method_data;
1236 g_assert (data != NULL);
1237 g_assert (id <= GPOINTER_TO_UINT (*data));
1242 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1243 g_error ("stack walk not installed");
1246 static MonoStackWalkImpl stack_walk = default_stack_walk;
1249 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1251 stack_walk (func, FALSE, user_data);
1255 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1257 stack_walk (func, TRUE, user_data);
1261 mono_install_stack_walk (MonoStackWalkImpl func)
1267 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1269 MonoMethod **dest = data;
1271 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1277 mono_method_get_last_managed (void)
1279 MonoMethod *m = NULL;
1280 stack_walk (last_managed, FALSE, &m);
1285 mono_loader_lock (void)
1287 EnterCriticalSection (&loader_mutex);
1291 mono_loader_unlock (void)
1293 LeaveCriticalSection (&loader_mutex);
1296 MonoMethodSignature*
1297 mono_method_signature (MonoMethod *m)
1305 return m->signature;
1307 mono_loader_lock ();
1310 mono_loader_unlock ();
1311 return m->signature;
1314 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1315 idx = mono_metadata_token_index (m->token);
1316 img = m->klass->image;
1318 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1319 size = mono_metadata_decode_blob_size (sig, &sig);
1321 m->signature = mono_metadata_parse_method_signature_full (img, NULL, idx, sig, NULL);
1323 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1324 m->signature->pinvoke = 1;
1325 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1326 MonoCallConvention conv = 0;
1327 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1328 m->signature->pinvoke = 1;
1330 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1331 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1332 conv = MONO_CALL_DEFAULT;
1334 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1337 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1338 conv = MONO_CALL_STDCALL;
1340 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1341 conv = MONO_CALL_THISCALL;
1343 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1344 conv = MONO_CALL_FASTCALL;
1346 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1347 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1349 g_warning ("unsupported calling convention");
1350 g_assert_not_reached ();
1352 m->signature->call_convention = conv;
1355 mono_loader_unlock ();
1356 return m->signature;
1360 mono_method_get_name (MonoMethod *method)
1362 return method->name;
1366 mono_method_get_class (MonoMethod *method)
1368 return method->klass;
1372 mono_method_get_token (MonoMethod *method)
1374 return method->token;
1378 mono_method_get_header (MonoMethod *method)
1384 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1387 if (G_LIKELY (mn->header))
1393 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))
1396 mono_loader_lock ();
1399 mono_loader_unlock ();
1403 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1404 idx = mono_metadata_token_index (method->token);
1405 img = method->klass->image;
1406 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1407 loc = mono_image_rva_map (img, rva);
1411 mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1413 mono_loader_unlock ();
1418 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1421 *iflags = method->iflags;
1422 return method->flags;
1426 * Find the method index in the metadata methodDef table.
1429 mono_method_get_index (MonoMethod *method) {
1430 MonoClass *klass = method->klass;
1433 mono_class_setup_methods (klass);
1434 for (i = 0; i < klass->method.count; ++i) {
1435 if (method == klass->methods [i])
1436 return klass->method.first + 1 + i;