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, *class_name;
193 MonoMethod *result = NULL;
196 class_name = mono_class_get_name_full (ic, FALSE, FALSE, FALSE);
198 qname = g_strconcat (class_name, ".", name, NULL);
199 if (ic->name_space && ic->name_space [0])
200 fqname = g_strconcat (ic->name_space, ".", class_name, ".", name, NULL);
204 class_name = qname = fqname = NULL;
207 MonoGenericContext *context = NULL;
209 if (klass->generic_container)
210 context = &klass->generic_container->context;
211 else if (klass->generic_class)
212 context = klass->generic_class->context;
214 mono_class_setup_methods (klass);
215 for (i = 0; i < klass->method.count; ++i) {
216 MonoMethod *m = klass->methods [i];
218 if (!((fqname && !strcmp (m->name, fqname)) ||
219 (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
222 if (sig->call_convention == MONO_CALL_VARARG) {
223 if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m))) {
228 MonoMethodSignature *msig = mono_method_signature_full (m, context);
229 if (mono_metadata_signature_equal (sig, msig)) {
236 if (name [0] == '.' && (strcmp (name, ".ctor") == 0 || strcmp (name, ".cctor") == 0))
239 klass = klass->parent;
250 * token is the method_ref or method_def token used in a call IL instruction.
253 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
255 int table = mono_metadata_token_table (token);
256 int idx = mono_metadata_token_index (token);
257 guint32 cols [MONO_MEMBERREF_SIZE];
258 MonoMethodSignature *sig, *prev_sig;
261 /* !table is for wrappers: we should really assign their own token to them */
262 if (!table || table == MONO_TABLE_METHOD)
263 return mono_method_signature (method);
265 if (table == MONO_TABLE_METHODSPEC) {
266 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
267 !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
268 mono_method_signature (method));
269 g_assert (method->is_inflated);
271 return mono_method_signature (method);
274 if (method->klass->generic_class)
275 return mono_method_signature (method);
278 /* FIXME: This might be incorrect for vararg methods */
279 return mono_method_signature (method);
282 sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
283 mono_loader_unlock ();
285 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
287 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
288 mono_metadata_decode_blob_size (ptr, &ptr);
289 sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
292 prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
294 /* Somebody got in before us */
295 /* FIXME: Free sig */
299 g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
300 mono_loader_unlock ();
303 sig = mono_class_inflate_generic_signature (image, sig, context);
309 mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
311 return mono_method_get_signature_full (method, image, token, NULL);
315 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
317 MonoClass *klass = NULL;
318 MonoMethod *method = NULL;
319 MonoTableInfo *tables = image->tables;
321 guint32 nindex, class;
322 MonoGenericClass *gclass = NULL;
323 MonoGenericContainer *container = NULL;
325 MonoMethodSignature *sig;
328 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
329 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
330 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
331 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
332 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
334 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
337 case MONO_MEMBERREF_PARENT_TYPEREF:
338 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
340 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex, context);
341 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
346 case MONO_MEMBERREF_PARENT_TYPESPEC:
347 klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
349 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
350 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
355 case MONO_MEMBERREF_PARENT_TYPEDEF:
356 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
358 char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex, context);
359 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
364 case MONO_MEMBERREF_PARENT_METHODDEF:
365 return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
367 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
368 g_assert_not_reached ();
372 if (klass->generic_class) {
373 gclass = klass->generic_class;
374 klass = gclass->container_class;
376 if (klass->generic_container)
377 container = klass->generic_container;
378 mono_class_init (klass);
380 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
381 mono_metadata_decode_blob_size (ptr, &ptr);
382 sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
385 case MONO_MEMBERREF_PARENT_TYPEREF:
386 method = find_method (klass, NULL, mname, sig);
388 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
389 mono_metadata_free_method_signature (sig);
391 case MONO_MEMBERREF_PARENT_TYPESPEC: {
395 type = &klass->byval_arg;
397 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
398 method = find_method (klass, NULL, mname, sig);
400 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
401 else if (klass->generic_class && (klass != method->klass))
402 method = mono_class_inflate_generic_method (
403 method, klass->generic_class->context, klass);
404 mono_metadata_free_method_signature (sig);
408 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
409 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
410 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
411 result->signature = sig;
412 result->name = mname;
414 if (!strcmp (mname, ".ctor")) {
415 /* we special-case this in the runtime. */
419 if (!strcmp (mname, "Set")) {
420 g_assert (sig->hasthis);
421 g_assert (type->data.array->rank + 1 == sig->param_count);
422 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
426 if (!strcmp (mname, "Get")) {
427 g_assert (sig->hasthis);
428 g_assert (type->data.array->rank == sig->param_count);
429 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
433 if (!strcmp (mname, "Address")) {
434 g_assert (sig->hasthis);
435 g_assert (type->data.array->rank == sig->param_count);
436 result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
440 g_assert_not_reached ();
443 case MONO_MEMBERREF_PARENT_TYPEDEF:
444 method = find_method (klass, NULL, mname, sig);
446 g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, mono_class_get_name (klass));
447 mono_metadata_free_method_signature (sig);
450 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
451 g_assert_not_reached ();
455 method = mono_class_inflate_generic_method (method, gclass->context, gclass->klass);
461 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
463 MonoMethod *method, *inflated;
464 MonoTableInfo *tables = image->tables;
465 MonoGenericContext *new_context = NULL;
466 MonoGenericMethod *gmethod;
467 MonoGenericContainer *container = NULL;
469 guint32 cols [MONO_METHODSPEC_SIZE];
470 guint32 token, param_count;
472 mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
473 token = cols [MONO_METHODSPEC_METHOD];
474 if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
475 token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
477 token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
479 method = mono_get_method (image, token, NULL);
481 ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
483 mono_metadata_decode_value (ptr, &ptr);
485 param_count = mono_metadata_decode_value (ptr, &ptr);
487 g_assert (param_count);
488 if (method->is_inflated)
489 container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
491 container = ((MonoMethodNormal *) method)->generic_container;
492 g_assert (container && container->is_method);
495 g_assert (context->container);
496 container->parent = context->container;
497 if (container->parent->is_method)
498 container->parent = container->parent->parent;
501 gmethod = g_new0 (MonoGenericMethod, 1);
502 gmethod->container = container;
504 gmethod->inst = mono_metadata_parse_generic_inst (
505 image, (MonoGenericContext *) container, param_count, ptr, &ptr);
508 gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
510 if (!container->method_hash)
511 container->method_hash = g_hash_table_new (
512 (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
514 inflated = g_hash_table_lookup (container->method_hash, gmethod);
521 new_context = g_new0 (MonoGenericContext, 1);
522 new_context->container = container;
523 new_context->gmethod = gmethod;
525 context = new_context;
527 new_context = g_new0 (MonoGenericContext, 1);
528 new_context->container = container;
529 new_context->gmethod = gmethod;
530 new_context->gclass = context->gclass;
532 context = new_context;
535 mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
536 sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
538 inflated = mono_class_inflate_generic_method (method, context, NULL);
539 g_hash_table_insert (container->method_hash, gmethod, inflated);
542 context->gclass = inflated->klass->generic_class;
546 typedef struct MonoDllMap MonoDllMap;
555 static GHashTable *global_dll_map;
558 mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
559 MonoDllMap *map, *tmp;
568 map = g_hash_table_lookup (dll_map, dll);
570 mono_loader_unlock ();
573 *rdll = map->target? map->target: dll;
575 for (tmp = map->next; tmp; tmp = tmp->next) {
576 if (strcmp (func, tmp->name) == 0) {
580 mono_loader_unlock ();
585 mono_loader_unlock ();
590 mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
593 if (assembly && assembly->dll_map) {
594 res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
598 return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
602 mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
603 MonoDllMap *map, *entry;
604 GHashTable *dll_map = NULL;
610 global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
611 dll_map = global_dll_map;
613 if (!assembly->dll_map)
614 assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
615 dll_map = assembly->dll_map;
618 map = g_hash_table_lookup (dll_map, dll);
620 map = g_new0 (MonoDllMap, 1);
621 map->dll = g_strdup (dll);
623 map->target = g_strdup (tdll);
624 g_hash_table_insert (dll_map, map->dll, map);
627 entry = g_new0 (MonoDllMap, 1);
628 entry->name = g_strdup (func);
630 entry->target = g_strdup (tfunc);
631 if (tdll && map->target && strcmp (map->target, tdll))
632 entry->dll = g_strdup (tdll);
633 entry->next = map->next;
637 mono_loader_unlock ();
641 mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
643 MonoImage *image = method->klass->image;
644 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
645 MonoTableInfo *tables = image->tables;
646 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
647 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
648 guint32 im_cols [MONO_IMPLMAP_SIZE];
650 const char *import = NULL;
651 const char *orig_scope;
652 const char *new_scope;
653 char *full_name, *file_name;
655 GModule *gmodule = NULL;
657 g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
662 if (method->klass->image->dynamic) {
663 MonoReflectionMethodAux *method_aux =
664 g_hash_table_lookup (
665 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
669 import = method_aux->dllentry;
670 orig_scope = method_aux->dll;
673 if (!piinfo->implmap_idx)
676 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
678 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
679 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
680 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
681 orig_scope = mono_metadata_string_heap (image, scope_token);
684 mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
686 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
687 "DllImport attempting to load: '%s'.", new_scope);
694 /* we allow a special name to dlopen from the running process namespace */
695 if (strcmp (new_scope, "__Internal") == 0)
696 gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
699 * Try loading the module using a variety of names
701 for (i = 0; i < 3; ++i) {
704 /* Try the original name */
705 file_name = g_strdup (new_scope);
708 /* Try trimming the .dll extension */
709 if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
710 file_name = g_strdup (new_scope);
711 file_name [strlen (new_scope) - 4] = '\0';
717 if (strstr (new_scope, "lib") != new_scope) {
718 file_name = g_strdup_printf ("lib%s", new_scope);
726 full_name = g_module_build_path (NULL, file_name);
727 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
728 "DllImport loading location: '%s'.", full_name);
729 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
731 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
732 "DllImport error loading library: '%s'.",
739 full_name = g_module_build_path (".", file_name);
740 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
741 "DllImport loading library: '%s'.", full_name);
742 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
744 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
745 "DllImport error loading library '%s'.",
752 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
753 "DllImport loading: '%s'.", file_name);
754 gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
756 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
757 "DllImport error loading library '%s'.",
769 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
770 "DllImport unable to load library '%s'.",
774 *exc_class = "DllNotFoundException";
775 *exc_arg = new_scope;
780 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
781 "Searching for '%s'.", import);
783 if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
784 g_module_symbol (gmodule, import, &piinfo->addr);
786 char *mangled_name = NULL, *mangled_name2 = NULL;
789 int mangle_param_count;
790 #ifdef PLATFORM_WIN32
795 * Search using a variety of mangled names
797 for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
798 for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
799 gboolean need_param_count = FALSE;
800 #ifdef PLATFORM_WIN32
801 if (mangle_stdcall > 0)
802 need_param_count = TRUE;
804 for (mangle_param_count = 0; mangle_param_count <= (need_param_count ? 256 : 0); mangle_param_count += 4) {
809 mangled_name = (char*)import;
810 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
811 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
812 /* Try the mangled name first */
813 if (mangle_charset == 0)
814 mangled_name = g_strconcat (import, "W", NULL);
816 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
817 #ifdef PLATFORM_WIN32
818 if (mangle_charset == 0)
819 mangled_name = g_strconcat (import, "W", NULL);
822 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
824 /* Try the mangled name last */
825 if (mangle_charset == 1)
826 mangled_name = g_strconcat (import, "A", NULL);
830 #ifdef PLATFORM_WIN32
831 if (mangle_param_count == 0)
832 param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
834 /* Try brute force, since it would be very hard to compute the stack usage correctly */
835 param_count = mangle_param_count;
837 /* Try the stdcall mangled name */
839 * gcc under windows creates mangled names without the underscore, but MS.NET
840 * doesn't support it, so we doesn't support it either.
842 if (mangle_stdcall == 1)
843 mangled_name2 = g_strdup_printf ("_%s@%d", mangled_name, param_count);
845 mangled_name2 = mangled_name;
847 mangled_name2 = mangled_name;
850 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
851 "Probing '%s'.", mangled_name2);
853 g_module_symbol (gmodule, mangled_name2, &piinfo->addr);
856 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
857 "Found as '%s'.", mangled_name2);
859 if (mangled_name != mangled_name2)
860 g_free (mangled_name2);
861 if (mangled_name != import)
862 g_free (mangled_name);
870 *exc_class = "EntryPointNotFoundException";
879 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
880 MonoGenericContext *context)
883 int table = mono_metadata_token_table (token);
884 int idx = mono_metadata_token_index (token);
885 MonoTableInfo *tables = image->tables;
886 MonoGenericContainer *generic_container = NULL, *container = NULL;
887 const char *sig = NULL;
889 guint32 cols [MONO_TYPEDEF_SIZE];
892 return mono_lookup_dynamic_token (image, token);
894 if (table != MONO_TABLE_METHOD) {
895 MonoGenericContainer *generic_container = NULL;
897 g_assert (context->container);
898 generic_container = context->container;
900 if (table == MONO_TABLE_METHODSPEC)
901 return method_from_methodspec (image, context, idx);
902 if (table != MONO_TABLE_MEMBERREF)
903 g_print("got wrong token: 0x%08x\n", token);
904 g_assert (table == MONO_TABLE_MEMBERREF);
905 result = method_from_memberref (image, idx, context);
910 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
912 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
913 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
914 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
916 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
919 result->klass = klass;
920 result->flags = cols [2];
921 result->iflags = cols [1];
922 result->token = token;
923 result->name = mono_metadata_string_heap (image, cols [3]);
926 container = klass->generic_container;
928 if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
929 (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
930 generic_container = mono_metadata_load_generic_params (image, token, container);
931 if (generic_container)
932 container = generic_container;
935 if (!sig) /* already taken from the methodref */
936 sig = mono_metadata_blob_heap (image, cols [4]);
937 size = mono_metadata_decode_blob_size (sig, &sig);
939 /* there are generic params, or a container. FIXME: be lazy here for generics*/
940 if (* sig & 0x10 || container)
941 result->signature = mono_metadata_parse_method_signature_full (
942 image, (MonoGenericContext *) container, idx, sig, NULL);
945 if (!result->klass) {
946 guint32 type = mono_metadata_typedef_from_method (image, token);
947 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
950 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
951 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
952 result->string_ctor = 1;
953 } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
954 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
955 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
957 piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
958 piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
960 if (result->signature && result->signature->generic_param_count) {
961 MonoMethodSignature *sig = result->signature;
963 for (i = 0; i < sig->generic_param_count; i++) {
964 generic_container->type_params [i].method = result;
966 mono_class_from_generic_parameter (
967 &generic_container->type_params [i], image, TRUE);
970 if (sig->ret->type == MONO_TYPE_MVAR) {
971 int num = sig->ret->data.generic_param->num;
972 sig->ret->data.generic_param = &generic_container->type_params [num];
975 for (i = 0; i < sig->param_count; i++) {
976 MonoType *t = sig->params [i];
977 if (t->type == MONO_TYPE_MVAR) {
978 int num = t->data.generic_param->num;
979 sig->params [i]->data.generic_param = &generic_container->type_params [num];
984 /* FIXME: lazyness for generics too, but how? */
985 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
986 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
987 gpointer loc = mono_image_rva_map (image, cols [0]);
989 ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
990 image, (MonoGenericContext *) container, loc);
993 ((MonoMethodNormal *) result)->generic_container = generic_container;
1000 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
1002 return mono_get_method_full (image, token, klass, NULL);
1006 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
1007 MonoGenericContext *context)
1011 /* We do everything inside the lock to prevent creation races */
1013 mono_loader_lock ();
1015 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
1016 mono_loader_unlock ();
1020 result = mono_get_method_from_token (image, token, klass, context);
1022 //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
1024 if (!(result && result->is_inflated))
1025 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
1027 mono_loader_unlock ();
1033 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
1034 MonoGenericContext *context)
1036 MonoMethod *method, *result;
1037 MonoClass *ic = NULL;
1038 MonoGenericClass *gclass = NULL;
1040 mono_loader_lock ();
1042 method = mono_get_method_from_token (image, token, NULL, context);
1044 mono_loader_unlock ();
1048 mono_class_init (constrained_class);
1049 method = mono_get_inflated_method (method);
1051 if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
1054 if (constrained_class->generic_class)
1055 gclass = constrained_class->generic_class;
1057 result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
1059 g_warning ("Missing method %s in assembly %s token %x", method->name,
1060 image->name, token);
1063 result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
1065 mono_loader_unlock ();
1070 mono_free_method (MonoMethod *method)
1072 if (method->signature) {
1074 * FIXME: This causes crashes because the types inside signatures and
1075 * locals are shared.
1077 /* mono_metadata_free_method_signature (method->signature); */
1078 g_free (method->signature);
1081 if (method->dynamic) {
1082 MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
1084 g_free ((char*)method->name);
1085 if (mw->method.header)
1086 g_free ((char*)mw->method.header->code);
1087 g_free (mw->method_data);
1090 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
1092 /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
1093 g_free (((MonoMethodNormal*)method)->header);
1100 mono_method_get_param_names (MonoMethod *method, const char **names)
1103 MonoClass *klass = method->klass;
1104 MonoTableInfo *methodt;
1105 MonoTableInfo *paramt;
1108 if (!mono_method_signature (method)->param_count)
1110 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1113 if (klass->generic_class) /* copy the names later */
1116 mono_class_init (klass);
1118 if (klass->image->dynamic) {
1119 MonoReflectionMethodAux *method_aux =
1120 g_hash_table_lookup (
1121 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1122 if (method_aux && method_aux->param_names) {
1123 for (i = 0; i < mono_method_signature (method)->param_count; ++i)
1124 if (method_aux->param_names [i + 1])
1125 names [i] = method_aux->param_names [i + 1];
1130 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1131 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1132 idx = mono_method_get_index (method);
1134 guint32 cols [MONO_PARAM_SIZE];
1135 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1137 if (idx < methodt->rows)
1138 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1140 lastp = paramt->rows + 1;
1141 for (i = param_index; i < lastp; ++i) {
1142 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1143 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
1144 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
1151 mono_method_get_param_token (MonoMethod *method, int index)
1153 MonoClass *klass = method->klass;
1154 MonoTableInfo *methodt;
1157 if (klass->generic_class)
1158 g_assert_not_reached ();
1160 mono_class_init (klass);
1162 if (klass->image->dynamic) {
1163 g_assert_not_reached ();
1166 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1167 idx = mono_method_get_index (method);
1169 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1171 return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
1178 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
1181 MonoClass *klass = method->klass;
1182 MonoTableInfo *methodt;
1183 MonoTableInfo *paramt;
1186 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1189 if (method->klass->image->dynamic) {
1190 MonoReflectionMethodAux *method_aux =
1191 g_hash_table_lookup (
1192 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1193 if (method_aux && method_aux->param_marshall) {
1194 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1195 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1196 if (dyn_specs [i]) {
1197 mspecs [i] = g_new0 (MonoMarshalSpec, 1);
1198 memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
1204 mono_class_init (klass);
1206 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1207 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1208 idx = mono_method_get_index (method);
1210 guint32 cols [MONO_PARAM_SIZE];
1211 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1213 if (idx < methodt->rows)
1214 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1216 lastp = paramt->rows + 1;
1218 for (i = param_index; i < lastp; ++i) {
1219 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1221 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
1223 tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
1225 mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
1234 mono_method_has_marshal_info (MonoMethod *method)
1237 MonoClass *klass = method->klass;
1238 MonoTableInfo *methodt;
1239 MonoTableInfo *paramt;
1242 if (method->klass->image->dynamic) {
1243 MonoReflectionMethodAux *method_aux =
1244 g_hash_table_lookup (
1245 ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1246 MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
1248 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
1255 mono_class_init (klass);
1257 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1258 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1259 idx = mono_method_get_index (method);
1261 guint32 cols [MONO_PARAM_SIZE];
1262 guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
1264 if (idx + 1 < methodt->rows)
1265 lastp = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1267 lastp = paramt->rows + 1;
1269 for (i = param_index; i < lastp; ++i) {
1270 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
1272 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
1281 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
1284 g_assert (method != NULL);
1285 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
1287 data = ((MonoMethodWrapper *)method)->method_data;
1288 g_assert (data != NULL);
1289 g_assert (id <= GPOINTER_TO_UINT (*data));
1294 default_stack_walk (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
1295 g_error ("stack walk not installed");
1298 static MonoStackWalkImpl stack_walk = default_stack_walk;
1301 mono_stack_walk (MonoStackWalk func, gpointer user_data)
1303 stack_walk (func, TRUE, user_data);
1307 mono_stack_walk_no_il (MonoStackWalk func, gpointer user_data)
1309 stack_walk (func, FALSE, user_data);
1313 mono_install_stack_walk (MonoStackWalkImpl func)
1319 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1321 MonoMethod **dest = data;
1323 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
1329 mono_method_get_last_managed (void)
1331 MonoMethod *m = NULL;
1332 stack_walk (last_managed, FALSE, &m);
1337 mono_loader_lock (void)
1339 EnterCriticalSection (&loader_mutex);
1343 mono_loader_unlock (void)
1345 LeaveCriticalSection (&loader_mutex);
1348 MonoMethodSignature*
1349 mono_method_signature (MonoMethod *m)
1351 return mono_method_signature_full (m, NULL);
1354 MonoMethodSignature*
1355 mono_method_signature_full (MonoMethod *m, MonoGenericContext *context)
1363 return m->signature;
1365 mono_loader_lock ();
1368 mono_loader_unlock ();
1369 return m->signature;
1372 g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
1373 idx = mono_metadata_token_index (m->token);
1374 img = m->klass->image;
1376 sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
1377 size = mono_metadata_decode_blob_size (sig, &sig);
1379 m->signature = mono_metadata_parse_method_signature_full (img, context, idx, sig, NULL);
1381 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
1382 m->signature->pinvoke = 1;
1383 else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
1384 MonoCallConvention conv = 0;
1385 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
1386 m->signature->pinvoke = 1;
1388 switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
1389 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
1390 conv = MONO_CALL_DEFAULT;
1392 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
1395 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
1396 conv = MONO_CALL_STDCALL;
1398 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
1399 conv = MONO_CALL_THISCALL;
1401 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
1402 conv = MONO_CALL_FASTCALL;
1404 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
1405 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
1407 g_warning ("unsupported calling convention");
1408 g_assert_not_reached ();
1410 m->signature->call_convention = conv;
1413 mono_loader_unlock ();
1414 return m->signature;
1418 mono_method_get_name (MonoMethod *method)
1420 return method->name;
1424 mono_method_get_class (MonoMethod *method)
1426 return method->klass;
1430 mono_method_get_token (MonoMethod *method)
1432 return method->token;
1436 mono_method_get_header (MonoMethod *method)
1442 MonoMethodNormal* mn = (MonoMethodNormal*) method;
1445 if (G_LIKELY (mn->header))
1451 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))
1454 mono_loader_lock ();
1457 mono_loader_unlock ();
1461 g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
1462 idx = mono_metadata_token_index (method->token);
1463 img = method->klass->image;
1464 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
1465 loc = mono_image_rva_map (img, rva);
1469 mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
1471 mono_loader_unlock ();
1476 mono_method_get_flags (MonoMethod *method, guint32 *iflags)
1479 *iflags = method->iflags;
1480 return method->flags;
1484 * Find the method index in the metadata methodDef table.
1487 mono_method_get_index (MonoMethod *method) {
1488 MonoClass *klass = method->klass;
1491 mono_class_setup_methods (klass);
1492 for (i = 0; i < klass->method.count; ++i) {
1493 if (method == klass->methods [i])
1494 return klass->method.first + 1 + i;