6 * Paolo Molaro (lupus@ximian.com)
7 * Miguel de Icaza (miguel@ximian.com)
8 * Patrik Torstensson (patrik.torstensson@labs2.com)
10 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
14 * This file is used by the interpreter and the JIT engine to locate
15 * assemblies. Used to load AssemblyRef and later to resolve various
19 * This should keep track of the assembly versions that we are loading.
21 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
28 #include <mono/metadata/metadata.h>
29 #include <mono/metadata/image.h>
30 #include <mono/metadata/assembly.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/metadata-internals.h>
34 #include <mono/metadata/loader.h>
35 #include <mono/metadata/loader-internals.h>
36 #include <mono/metadata/class-init.h>
37 #include <mono/metadata/class-internals.h>
38 #include <mono/metadata/debug-helpers.h>
39 #include <mono/metadata/reflection.h>
40 #include <mono/metadata/profiler-private.h>
41 #include <mono/metadata/exception.h>
42 #include <mono/metadata/marshal.h>
43 #include <mono/metadata/lock-tracer.h>
44 #include <mono/metadata/verify-internals.h>
45 #include <mono/metadata/exception-internals.h>
46 #include <mono/utils/mono-logger-internals.h>
47 #include <mono/utils/mono-dl.h>
48 #include <mono/utils/mono-membar.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-error-internals.h>
51 #include <mono/utils/mono-tls.h>
52 #include <mono/utils/mono-path.h>
54 MonoDefaults mono_defaults
;
57 * This lock protects the hash tables inside MonoImage used by the metadata
58 * loading functions in class.c and loader.c.
60 * See domain-internals.h for locking policy in combination with the
63 static MonoCoopMutex loader_mutex
;
64 static mono_mutex_t global_loader_data_mutex
;
65 static gboolean loader_lock_inited
;
68 static gint32 inflated_signatures_size
;
69 static gint32 memberref_sig_cache_size
;
70 static gint32 methods_size
;
71 static gint32 signatures_size
;
74 * This TLS variable holds how many times the current thread has acquired the loader
77 static MonoNativeTlsKey loader_lock_nest_id
;
80 static int pinvoke_search_directories_count
;
81 static char **pinvoke_search_directories
;
84 static void dllmap_cleanup (void);
85 static void cached_module_cleanup(void);
87 static void dllmap_insert_global (const char *dll
, const char *func
, const char *tdll
, const char *tfunc
);
88 static void dllmap_insert_image (MonoImage
*assembly
, const char *dll
, const char *func
, const char *tdll
, const char *tfun
);
91 /* Class lazy loading functions */
92 GENERATE_GET_CLASS_WITH_CACHE (appdomain_unloaded_exception
, "System", "AppDomainUnloadedException")
93 GENERATE_TRY_GET_CLASS_WITH_CACHE (appdomain_unloaded_exception
, "System", "AppDomainUnloadedException")
96 global_loader_data_lock (void)
98 mono_locks_os_acquire (&global_loader_data_mutex
, LoaderGlobalDataLock
);
102 global_loader_data_unlock (void)
104 mono_locks_os_release (&global_loader_data_mutex
, LoaderGlobalDataLock
);
110 static gboolean inited
;
113 mono_coop_mutex_init_recursive (&loader_mutex
);
114 mono_os_mutex_init_recursive (&global_loader_data_mutex
);
115 loader_lock_inited
= TRUE
;
117 mono_native_tls_alloc (&loader_lock_nest_id
, NULL
);
119 mono_counters_init ();
120 mono_counters_register ("Inflated signatures size",
121 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_signatures_size
);
122 mono_counters_register ("Memberref signature cache size",
123 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &memberref_sig_cache_size
);
124 mono_counters_register ("MonoMethod size",
125 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &methods_size
);
126 mono_counters_register ("MonoMethodSignature size",
127 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &signatures_size
);
134 mono_loader_cleanup (void)
137 cached_module_cleanup ();
139 mono_native_tls_free (loader_lock_nest_id
);
141 mono_coop_mutex_destroy (&loader_mutex
);
142 mono_os_mutex_destroy (&global_loader_data_mutex
);
143 loader_lock_inited
= FALSE
;
147 * find_cached_memberref_sig:
149 * Return a cached copy of the memberref signature identified by SIG_IDX.
150 * We use a gpointer since the cache stores both MonoTypes and MonoMethodSignatures.
151 * A cache is needed since the type/signature parsing routines allocate everything
152 * from a mempool, so without a cache, multiple requests for the same signature would
153 * lead to unbounded memory growth. For normal methods/fields this is not a problem
154 * since the resulting methods/fields are cached, but inflated methods/fields cannot
156 * LOCKING: Acquires the loader lock.
159 find_cached_memberref_sig (MonoImage
*image
, guint32 sig_idx
)
163 mono_image_lock (image
);
164 res
= g_hash_table_lookup (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
));
165 mono_image_unlock (image
);
171 cache_memberref_sig (MonoImage
*image
, guint32 sig_idx
, gpointer sig
)
175 mono_image_lock (image
);
176 prev_sig
= g_hash_table_lookup (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
));
178 /* Somebody got in before us */
182 g_hash_table_insert (image
->memberref_signatures
, GUINT_TO_POINTER (sig_idx
), sig
);
183 /* An approximation based on glib 2.18 */
184 mono_atomic_fetch_add_i32 (&memberref_sig_cache_size
, sizeof (gpointer
) * 4);
186 mono_image_unlock (image
);
191 static MonoClassField
*
192 field_from_memberref (MonoImage
*image
, guint32 token
, MonoClass
**retklass
,
193 MonoGenericContext
*context
, MonoError
*error
)
195 MonoClass
*klass
= NULL
;
196 MonoClassField
*field
;
197 MonoTableInfo
*tables
= image
->tables
;
200 guint32 nindex
, class_index
;
203 guint32 idx
= mono_metadata_token_index (token
);
207 mono_metadata_decode_row (&tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, MONO_MEMBERREF_SIZE
);
208 nindex
= cols
[MONO_MEMBERREF_CLASS
] >> MONO_MEMBERREF_PARENT_BITS
;
209 class_index
= cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
211 fname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
213 if (!mono_verifier_verify_memberref_field_signature (image
, cols
[MONO_MEMBERREF_SIGNATURE
], error
))
216 switch (class_index
) {
217 case MONO_MEMBERREF_PARENT_TYPEDEF
:
218 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
| nindex
, error
);
220 case MONO_MEMBERREF_PARENT_TYPEREF
:
221 klass
= mono_class_from_typeref_checked (image
, MONO_TOKEN_TYPE_REF
| nindex
, error
);
223 case MONO_MEMBERREF_PARENT_TYPESPEC
:
224 klass
= mono_class_get_and_inflate_typespec_checked (image
, MONO_TOKEN_TYPE_SPEC
| nindex
, context
, error
);
227 mono_error_set_bad_image (error
, image
, "Bad field field '%u' signature 0x%08x", class_index
, token
);
233 ptr
= mono_metadata_blob_heap (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
234 mono_metadata_decode_blob_size (ptr
, &ptr
);
235 /* we may want to check the signature here... */
238 mono_error_set_field_missing (error
, klass
, fname
, NULL
, "Bad field signature class token %08x field token %08x", class_index
, token
);
242 /* FIXME: This needs a cache, especially for generic instances, since
243 * we ask mono_metadata_parse_type_checked () to allocates everything from a mempool.
244 * FIXME part2, mono_metadata_parse_type_checked actually allows for a transient type instead.
245 * FIXME part3, transient types are not 100% transient, so we need to take care of that first.
247 sig_type
= (MonoType
*)find_cached_memberref_sig (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
249 ERROR_DECL (inner_error
);
250 sig_type
= mono_metadata_parse_type_checked (image
, NULL
, 0, FALSE
, ptr
, &ptr
, inner_error
);
251 if (sig_type
== NULL
) {
252 mono_error_set_field_missing (error
, klass
, fname
, NULL
, "Could not parse field signature %08x due to: %s", token
, mono_error_get_message (inner_error
));
253 mono_error_cleanup (inner_error
);
256 sig_type
= (MonoType
*)cache_memberref_sig (image
, cols
[MONO_MEMBERREF_SIGNATURE
], sig_type
);
259 mono_class_init_internal (klass
); /*FIXME is this really necessary?*/
262 field
= mono_class_get_field_from_name_full (klass
, fname
, sig_type
);
265 mono_error_set_field_missing (error
, klass
, fname
, sig_type
, "Could not find field in class");
272 * mono_field_from_token:
273 * \deprecated use the \c _checked variant
274 * Notes: runtime code MUST not use this function
277 mono_field_from_token (MonoImage
*image
, guint32 token
, MonoClass
**retklass
, MonoGenericContext
*context
)
280 MonoClassField
*res
= mono_field_from_token_checked (image
, token
, retklass
, context
, error
);
281 mono_error_assert_ok (error
);
286 mono_field_from_token_checked (MonoImage
*image
, guint32 token
, MonoClass
**retklass
, MonoGenericContext
*context
, MonoError
*error
)
290 MonoClassField
*field
;
294 if (image_is_dynamic (image
)) {
295 MonoClassField
*result
;
296 MonoClass
*handle_class
;
299 ERROR_DECL (inner_error
);
300 result
= (MonoClassField
*)mono_lookup_dynamic_token_class (image
, token
, TRUE
, &handle_class
, context
, inner_error
);
301 mono_error_cleanup (inner_error
);
302 // This checks the memberref type as well
303 if (!result
|| handle_class
!= mono_defaults
.fieldhandle_class
) {
304 mono_error_set_bad_image (error
, image
, "Bad field token 0x%08x", token
);
307 *retklass
= result
->parent
;
311 if ((field
= (MonoClassField
*)mono_conc_hashtable_lookup (image
->field_cache
, GUINT_TO_POINTER (token
)))) {
312 *retklass
= field
->parent
;
316 if (mono_metadata_token_table (token
) == MONO_TABLE_MEMBERREF
) {
317 field
= field_from_memberref (image
, token
, retklass
, context
, error
);
319 type
= mono_metadata_typedef_from_field (image
, mono_metadata_token_index (token
));
321 mono_error_set_bad_image (error
, image
, "Invalid field token 0x%08x", token
);
324 k
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
| type
, error
);
328 mono_class_init_internal (k
);
331 if (mono_class_has_failure (k
)) {
332 ERROR_DECL (causedby_error
);
333 mono_error_set_for_class_failure (causedby_error
, k
);
334 mono_error_set_bad_image (error
, image
, "Could not resolve field token 0x%08x, due to: %s", token
, mono_error_get_message (causedby_error
));
335 mono_error_cleanup (causedby_error
);
337 field
= mono_class_get_field (k
, token
);
339 mono_error_set_bad_image (error
, image
, "Could not resolve field token 0x%08x", token
);
344 if (field
&& field
->parent
&& !mono_class_is_ginst (field
->parent
) && !mono_class_is_gtd (field
->parent
)) {
345 mono_image_lock (image
);
346 mono_conc_hashtable_insert (image
->field_cache
, GUINT_TO_POINTER (token
), field
);
347 mono_image_unlock (image
);
354 mono_metadata_signature_vararg_match (MonoMethodSignature
*sig1
, MonoMethodSignature
*sig2
)
358 if (sig1
->hasthis
!= sig2
->hasthis
||
359 sig1
->sentinelpos
!= sig2
->sentinelpos
)
362 for (i
= 0; i
< sig1
->sentinelpos
; i
++) {
363 MonoType
*p1
= sig1
->params
[i
];
364 MonoType
*p2
= sig2
->params
[i
];
366 /*if (p1->attrs != p2->attrs)
369 if (!mono_metadata_type_equal (p1
, p2
))
373 if (!mono_metadata_type_equal (sig1
->ret
, sig2
->ret
))
379 find_method_in_class (MonoClass
*klass
, const char *name
, const char *qname
, const char *fqname
,
380 MonoMethodSignature
*sig
, MonoClass
*from_class
, MonoError
*error
)
384 /* Search directly in the metadata to avoid calling setup_methods () */
387 MonoImage
*klass_image
= m_class_get_image (klass
);
388 /* FIXME: !mono_class_is_ginst (from_class) condition causes test failures. */
389 if (m_class_get_type_token (klass
) && !image_is_dynamic (klass_image
) && !m_class_get_methods (klass
) && !m_class_get_rank (klass
) && klass
== from_class
&& !mono_class_is_ginst (from_class
)) {
390 int first_idx
= mono_class_get_first_method_idx (klass
);
391 int mcount
= mono_class_get_method_count (klass
);
392 for (i
= 0; i
< mcount
; ++i
) {
393 guint32 cols
[MONO_METHOD_SIZE
];
396 MonoMethodSignature
*other_sig
;
398 mono_metadata_decode_table_row (klass_image
, MONO_TABLE_METHOD
, first_idx
+ i
, cols
, MONO_METHOD_SIZE
);
400 m_name
= mono_metadata_string_heap (klass_image
, cols
[MONO_METHOD_NAME
]);
402 if (!((fqname
&& !strcmp (m_name
, fqname
)) ||
403 (qname
&& !strcmp (m_name
, qname
)) ||
404 (name
&& !strcmp (m_name
, name
))))
407 method
= mono_get_method_checked (klass_image
, MONO_TOKEN_METHOD_DEF
| (first_idx
+ i
+ 1), klass
, NULL
, error
);
408 if (!is_ok (error
)) //bail out if we hit a loader error
411 other_sig
= mono_method_signature_checked (method
, error
);
412 if (!is_ok (error
)) //bail out if we hit a loader error
414 if (other_sig
&& (sig
->call_convention
!= MONO_CALL_VARARG
) && mono_metadata_signature_equal (sig
, other_sig
))
420 mono_class_setup_methods (klass
); /* FIXME don't swallow the error here. */
422 We can't fail lookup of methods otherwise the runtime will fail with MissingMethodException instead of TypeLoadException.
423 See mono/tests/generic-type-load-exception.2.il
424 FIXME we should better report this error to the caller
426 if (!m_class_get_methods (klass
) || mono_class_has_failure (klass
)) {
427 ERROR_DECL (cause_error
);
428 mono_error_set_for_class_failure (cause_error
, klass
);
429 mono_error_set_type_load_class (error
, klass
, "Could not find method '%s' due to a type load error: %s", name
, mono_error_get_message (cause_error
));
430 mono_error_cleanup (cause_error
);
433 int mcount
= mono_class_get_method_count (klass
);
434 MonoMethod
**klass_methods
= m_class_get_methods (klass
);
435 for (i
= 0; i
< mcount
; ++i
) {
436 MonoMethod
*m
= klass_methods
[i
];
437 MonoMethodSignature
*msig
;
439 /* We must cope with failing to load some of the types. */
443 if (!((fqname
&& !strcmp (m
->name
, fqname
)) ||
444 (qname
&& !strcmp (m
->name
, qname
)) ||
445 (name
&& !strcmp (m
->name
, name
))))
447 msig
= mono_method_signature_checked (m
, error
);
448 if (!is_ok (error
)) //bail out if we hit a loader error
454 if (sig
->call_convention
== MONO_CALL_VARARG
) {
455 if (mono_metadata_signature_vararg_match (sig
, msig
))
458 if (mono_metadata_signature_equal (sig
, msig
))
464 return mono_class_get_method_by_index (from_class
, i
);
469 find_method (MonoClass
*in_class
, MonoClass
*ic
, const char* name
, MonoMethodSignature
*sig
, MonoClass
*from_class
, MonoError
*error
)
472 char *qname
, *fqname
, *class_name
;
473 gboolean is_interface
;
474 MonoMethod
*result
= NULL
;
475 MonoClass
*initial_class
= in_class
;
478 is_interface
= MONO_CLASS_IS_INTERFACE_INTERNAL (in_class
);
481 class_name
= mono_type_get_name_full (m_class_get_byval_arg (ic
), MONO_TYPE_NAME_FORMAT_IL
);
483 qname
= g_strconcat (class_name
, ".", name
, (const char*)NULL
);
484 const char *ic_name_space
= m_class_get_name_space (ic
);
485 if (ic_name_space
&& ic_name_space
[0])
486 fqname
= g_strconcat (ic_name_space
, ".", class_name
, ".", name
, (const char*)NULL
);
490 class_name
= qname
= fqname
= NULL
;
493 g_assert (from_class
);
494 result
= find_method_in_class (in_class
, name
, qname
, fqname
, sig
, from_class
, error
);
495 if (result
|| !is_ok (error
))
498 if (name
[0] == '.' && (!strcmp (name
, ".ctor") || !strcmp (name
, ".cctor")))
502 * This happens when we fail to lazily load the interfaces of one of the types.
503 * On such case we can't just bail out since user code depends on us trying harder.
505 if (m_class_get_interface_offsets_count (from_class
) != m_class_get_interface_offsets_count (in_class
)) {
506 in_class
= m_class_get_parent (in_class
);
507 from_class
= m_class_get_parent (from_class
);
511 int in_class_interface_offsets_count
= m_class_get_interface_offsets_count (in_class
);
512 MonoClass
**in_class_interfaces_packed
= m_class_get_interfaces_packed (in_class
);
513 MonoClass
**from_class_interfaces_packed
= m_class_get_interfaces_packed (from_class
);
514 for (i
= 0; i
< in_class_interface_offsets_count
; i
++) {
515 MonoClass
*in_ic
= in_class_interfaces_packed
[i
];
516 MonoClass
*from_ic
= from_class_interfaces_packed
[i
];
517 char *ic_qname
, *ic_fqname
, *ic_class_name
;
519 ic_class_name
= mono_type_get_name_full (m_class_get_byval_arg (in_ic
), MONO_TYPE_NAME_FORMAT_IL
);
520 ic_qname
= g_strconcat (ic_class_name
, ".", name
, (const char*)NULL
);
521 const char *in_ic_name_space
= m_class_get_name_space (in_ic
);
522 if (in_ic_name_space
&& in_ic_name_space
[0])
523 ic_fqname
= g_strconcat (in_ic_name_space
, ".", ic_class_name
, ".", name
, (const char*)NULL
);
527 result
= find_method_in_class (in_ic
, ic
? name
: NULL
, ic_qname
, ic_fqname
, sig
, from_ic
, error
);
528 g_free (ic_class_name
);
531 if (result
|| !is_ok (error
))
535 in_class
= m_class_get_parent (in_class
);
536 from_class
= m_class_get_parent (from_class
);
538 g_assert (!in_class
== !from_class
);
541 result
= find_method_in_class (mono_defaults
.object_class
, name
, qname
, fqname
, sig
, mono_defaults
.object_class
, error
);
543 //we did not find the method
544 if (!result
&& is_ok (error
))
545 mono_error_set_method_missing (error
, initial_class
, name
, sig
, NULL
);
554 static MonoMethodSignature
*
555 inflate_generic_signature_checked (MonoImage
*image
, MonoMethodSignature
*sig
, MonoGenericContext
*context
, MonoError
*error
)
557 MonoMethodSignature
*res
;
565 res
= (MonoMethodSignature
*)g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE
+ ((gint32
)sig
->param_count
) * sizeof (MonoType
*));
566 res
->param_count
= sig
->param_count
;
567 res
->sentinelpos
= -1;
568 res
->ret
= mono_class_inflate_generic_type_checked (sig
->ret
, context
, error
);
571 is_open
= mono_class_is_open_constructed_type (res
->ret
);
572 for (i
= 0; i
< sig
->param_count
; ++i
) {
573 res
->params
[i
] = mono_class_inflate_generic_type_checked (sig
->params
[i
], context
, error
);
578 is_open
= mono_class_is_open_constructed_type (res
->params
[i
]);
580 res
->hasthis
= sig
->hasthis
;
581 res
->explicit_this
= sig
->explicit_this
;
582 res
->call_convention
= sig
->call_convention
;
583 res
->pinvoke
= sig
->pinvoke
;
584 res
->generic_param_count
= sig
->generic_param_count
;
585 res
->sentinelpos
= sig
->sentinelpos
;
586 res
->has_type_parameters
= is_open
;
587 res
->is_inflated
= 1;
592 mono_metadata_free_type (res
->ret
);
593 for (i
= 0; i
< sig
->param_count
; ++i
) {
595 mono_metadata_free_type (res
->params
[i
]);
602 * mono_inflate_generic_signature:
604 * Inflate \p sig with \p context, and return a canonical copy. On error, set \p error, and return NULL.
607 mono_inflate_generic_signature (MonoMethodSignature
*sig
, MonoGenericContext
*context
, MonoError
*error
)
609 MonoMethodSignature
*res
, *cached
;
611 res
= inflate_generic_signature_checked (NULL
, sig
, context
, error
);
614 cached
= mono_metadata_get_inflated_signature (res
, context
);
616 mono_metadata_free_inflated_signature (res
);
620 static MonoMethodHeader
*
621 inflate_generic_header (MonoMethodHeader
*header
, MonoGenericContext
*context
, MonoError
*error
)
623 size_t locals_size
= sizeof (gpointer
) * header
->num_locals
;
624 size_t clauses_size
= header
->num_clauses
* sizeof (MonoExceptionClause
);
625 size_t header_size
= MONO_SIZEOF_METHOD_HEADER
+ locals_size
+ clauses_size
;
626 MonoMethodHeader
*res
= (MonoMethodHeader
*)g_malloc0 (header_size
);
627 res
->num_locals
= header
->num_locals
;
628 res
->clauses
= (MonoExceptionClause
*) &res
->locals
[res
->num_locals
] ;
629 memcpy (res
->clauses
, header
->clauses
, clauses_size
);
631 res
->code
= header
->code
;
632 res
->code_size
= header
->code_size
;
633 res
->max_stack
= header
->max_stack
;
634 res
->num_clauses
= header
->num_clauses
;
635 res
->init_locals
= header
->init_locals
;
637 res
->is_transient
= TRUE
;
641 for (int i
= 0; i
< header
->num_locals
; ++i
) {
642 res
->locals
[i
] = mono_class_inflate_generic_type_checked (header
->locals
[i
], context
, error
);
643 goto_if_nok (error
, fail
);
645 if (res
->num_clauses
) {
646 for (int i
= 0; i
< header
->num_clauses
; ++i
) {
647 MonoExceptionClause
*clause
= &res
->clauses
[i
];
648 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
650 clause
->data
.catch_class
= mono_class_inflate_generic_class_checked (clause
->data
.catch_class
, context
, error
);
651 goto_if_nok (error
, fail
);
661 * mono_method_get_signature_full:
662 * \p token is the method ref/def/spec token used in a \c call IL instruction.
663 * \deprecated use the \c _checked variant
664 * Notes: runtime code MUST not use this function
667 mono_method_get_signature_full (MonoMethod
*method
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
670 MonoMethodSignature
*res
= mono_method_get_signature_checked (method
, image
, token
, context
, error
);
671 mono_error_cleanup (error
);
676 mono_method_get_signature_checked (MonoMethod
*method
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
, MonoError
*error
)
678 int table
= mono_metadata_token_table (token
);
679 int idx
= mono_metadata_token_index (token
);
681 guint32 cols
[MONO_MEMBERREF_SIZE
];
682 MonoMethodSignature
*sig
;
687 /* !table is for wrappers: we should really assign their own token to them */
688 if (!table
|| table
== MONO_TABLE_METHOD
)
689 return mono_method_signature_checked (method
, error
);
691 if (table
== MONO_TABLE_METHODSPEC
) {
692 /* the verifier (do_invoke_method) will turn the NULL into a verifier error */
693 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) || !method
->is_inflated
) {
694 mono_error_set_bad_image (error
, image
, "Method is a pinvoke or open generic");
698 return mono_method_signature_checked (method
, error
);
701 if (mono_class_is_ginst (method
->klass
))
702 return mono_method_signature_checked (method
, error
);
704 if (image_is_dynamic (image
)) {
705 sig
= mono_reflection_lookup_signature (image
, method
, token
, error
);
709 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, MONO_MEMBERREF_SIZE
);
710 sig_idx
= cols
[MONO_MEMBERREF_SIGNATURE
];
712 sig
= (MonoMethodSignature
*)find_cached_memberref_sig (image
, sig_idx
);
714 if (!mono_verifier_verify_memberref_method_signature (image
, sig_idx
, error
))
717 ptr
= mono_metadata_blob_heap (image
, sig_idx
);
718 mono_metadata_decode_blob_size (ptr
, &ptr
);
720 sig
= mono_metadata_parse_method_signature_full (image
, NULL
, 0, ptr
, NULL
, error
);
724 sig
= (MonoMethodSignature
*)cache_memberref_sig (image
, sig_idx
, sig
);
726 /* FIXME: we probably should verify signature compat in the dynamic case too*/
727 if (!mono_verifier_is_sig_compatible (image
, method
, sig
)) {
728 guint32 klass
= cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
729 const char *fname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
731 mono_error_set_bad_image (error
, image
, "Incompatible method signature class token 0x%08x field name %s token 0x%08x", klass
, fname
, token
);
737 MonoMethodSignature
*cached
;
739 /* This signature is not owned by a MonoMethod, so need to cache */
740 sig
= inflate_generic_signature_checked (image
, sig
, context
, error
);
744 cached
= mono_metadata_get_inflated_signature (sig
, context
);
746 mono_metadata_free_inflated_signature (sig
);
748 mono_atomic_fetch_add_i32 (&inflated_signatures_size
, mono_metadata_signature_size (cached
));
752 g_assert (is_ok (error
));
757 * mono_method_get_signature:
758 * \p token is the method_ref/def/spec token used in a call IL instruction.
759 * \deprecated use the \c _checked variant
760 * Notes: runtime code MUST not use this function
763 mono_method_get_signature (MonoMethod
*method
, MonoImage
*image
, guint32 token
)
766 MonoMethodSignature
*res
= mono_method_get_signature_checked (method
, image
, token
, NULL
, error
);
767 mono_error_cleanup (error
);
771 /* this is only for the typespec array methods */
773 mono_method_search_in_array_class (MonoClass
*klass
, const char *name
, MonoMethodSignature
*sig
)
777 mono_class_setup_methods (klass
);
778 g_assert (!mono_class_has_failure (klass
)); /*FIXME this should not fail, right?*/
779 int mcount
= mono_class_get_method_count (klass
);
780 MonoMethod
**klass_methods
= m_class_get_methods (klass
);
781 for (i
= 0; i
< mcount
; ++i
) {
782 MonoMethod
*method
= klass_methods
[i
];
783 if (strcmp (method
->name
, name
) == 0 && sig
->param_count
== method
->signature
->param_count
)
790 method_from_memberref (MonoImage
*image
, guint32 idx
, MonoGenericContext
*typespec_context
,
791 gboolean
*used_context
, MonoError
*error
)
793 MonoClass
*klass
= NULL
;
794 MonoMethod
*method
= NULL
;
795 MonoTableInfo
*tables
= image
->tables
;
797 guint32 nindex
, class_index
, sig_idx
;
799 MonoMethodSignature
*sig
;
804 mono_metadata_decode_row (&tables
[MONO_TABLE_MEMBERREF
], idx
-1, cols
, 3);
805 nindex
= cols
[MONO_MEMBERREF_CLASS
] >> MONO_MEMBERREF_PARENT_BITS
;
806 class_index
= cols
[MONO_MEMBERREF_CLASS
] & MONO_MEMBERREF_PARENT_MASK
;
807 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
808 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
810 mname
= mono_metadata_string_heap (image
, cols
[MONO_MEMBERREF_NAME
]);
813 * Whether we actually used the `typespec_context' or not.
814 * This is used to tell our caller whether or not it's safe to insert the returned
815 * method into a cache.
818 *used_context
= class_index
== MONO_MEMBERREF_PARENT_TYPESPEC
;
820 switch (class_index
) {
821 case MONO_MEMBERREF_PARENT_TYPEREF
:
822 klass
= mono_class_from_typeref_checked (image
, MONO_TOKEN_TYPE_REF
| nindex
, error
);
826 case MONO_MEMBERREF_PARENT_TYPESPEC
:
828 * Parse the TYPESPEC in the parent's context.
830 klass
= mono_class_get_and_inflate_typespec_checked (image
, MONO_TOKEN_TYPE_SPEC
| nindex
, typespec_context
, error
);
834 case MONO_MEMBERREF_PARENT_TYPEDEF
:
835 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
| nindex
, error
);
839 case MONO_MEMBERREF_PARENT_METHODDEF
: {
840 method
= mono_get_method_checked (image
, MONO_TOKEN_METHOD_DEF
| nindex
, NULL
, NULL
, error
);
846 mono_error_set_bad_image (error
, image
, "Memberref parent unknown: class: %d, index %d", class_index
, nindex
);
851 mono_class_init_internal (klass
);
853 sig_idx
= cols
[MONO_MEMBERREF_SIGNATURE
];
855 if (!mono_verifier_verify_memberref_method_signature (image
, sig_idx
, error
))
858 ptr
= mono_metadata_blob_heap (image
, sig_idx
);
859 mono_metadata_decode_blob_size (ptr
, &ptr
);
861 sig
= (MonoMethodSignature
*)find_cached_memberref_sig (image
, sig_idx
);
863 sig
= mono_metadata_parse_method_signature_full (image
, NULL
, 0, ptr
, NULL
, error
);
867 sig
= (MonoMethodSignature
*)cache_memberref_sig (image
, sig_idx
, sig
);
870 switch (class_index
) {
871 case MONO_MEMBERREF_PARENT_TYPEREF
:
872 case MONO_MEMBERREF_PARENT_TYPEDEF
:
873 method
= find_method (klass
, NULL
, mname
, sig
, klass
, error
);
876 case MONO_MEMBERREF_PARENT_TYPESPEC
: {
879 type
= m_class_get_byval_arg (klass
);
881 if (type
->type
!= MONO_TYPE_ARRAY
&& type
->type
!= MONO_TYPE_SZARRAY
) {
882 MonoClass
*in_class
= mono_class_is_ginst (klass
) ? mono_class_get_generic_class (klass
)->container_class
: klass
;
883 method
= find_method (in_class
, NULL
, mname
, sig
, klass
, error
);
887 /* we're an array and we created these methods already in klass in mono_class_init_internal () */
888 method
= mono_method_search_in_array_class (klass
, mname
, sig
);
892 mono_error_set_bad_image (error
, image
,"Memberref parent unknown: class: %d, index %d", class_index
, nindex
);
896 if (!method
&& is_ok (error
))
897 mono_error_set_method_missing (error
, klass
, mname
, sig
, "Failed to load due to unknown reasons");
902 g_assert (!is_ok (error
));
907 method_from_methodspec (MonoImage
*image
, MonoGenericContext
*context
, guint32 idx
, MonoError
*error
)
911 MonoTableInfo
*tables
= image
->tables
;
912 MonoGenericContext new_context
;
913 MonoGenericInst
*inst
;
915 guint32 cols
[MONO_METHODSPEC_SIZE
];
916 guint32 token
, nindex
, param_count
;
920 mono_metadata_decode_row (&tables
[MONO_TABLE_METHODSPEC
], idx
- 1, cols
, MONO_METHODSPEC_SIZE
);
921 token
= cols
[MONO_METHODSPEC_METHOD
];
922 nindex
= token
>> MONO_METHODDEFORREF_BITS
;
924 if (!mono_verifier_verify_methodspec_signature (image
, cols
[MONO_METHODSPEC_SIGNATURE
], error
))
927 ptr
= mono_metadata_blob_heap (image
, cols
[MONO_METHODSPEC_SIGNATURE
]);
929 mono_metadata_decode_value (ptr
, &ptr
);
931 param_count
= mono_metadata_decode_value (ptr
, &ptr
);
933 inst
= mono_metadata_parse_generic_inst (image
, NULL
, param_count
, ptr
, &ptr
, error
);
937 if (context
&& inst
->is_open
) {
938 inst
= mono_metadata_inflate_generic_inst (inst
, context
, error
);
943 if ((token
& MONO_METHODDEFORREF_MASK
) == MONO_METHODDEFORREF_METHODDEF
) {
944 method
= mono_get_method_checked (image
, MONO_TOKEN_METHOD_DEF
| nindex
, NULL
, context
, error
);
948 method
= method_from_memberref (image
, nindex
, context
, NULL
, error
);
954 klass
= method
->klass
;
956 if (mono_class_is_ginst (klass
)) {
957 g_assert (method
->is_inflated
);
958 method
= ((MonoMethodInflated
*) method
)->declaring
;
961 new_context
.class_inst
= mono_class_is_ginst (klass
) ? mono_class_get_generic_class (klass
)->context
.class_inst
: NULL
;
962 new_context
.method_inst
= inst
;
964 method
= mono_class_inflate_generic_method_full_checked (method
, klass
, &new_context
, error
);
976 static MonoDllMap
*global_dll_map
;
979 mono_dllmap_lookup_list (MonoDllMap
*dll_map
, const char *dll
, const char* func
, const char **rdll
, const char **rfunc
) {
987 global_loader_data_lock ();
990 * we use the first entry we find that matches, since entries from
991 * the config file are prepended to the list and we document that the
994 for (; dll_map
; dll_map
= dll_map
->next
) {
995 if (dll_map
->dll
[0] == 'i' && dll_map
->dll
[1] == ':') {
996 if (g_ascii_strcasecmp (dll_map
->dll
+ 2, dll
))
998 } else if (strcmp (dll_map
->dll
, dll
)) {
1001 if (!found
&& dll_map
->target
) {
1002 *rdll
= dll_map
->target
;
1004 /* we don't quit here, because we could find a full
1005 * entry that matches also function and that has priority.
1008 if (dll_map
->func
&& strcmp (dll_map
->func
, func
) == 0) {
1009 *rdll
= dll_map
->target
;
1010 *rfunc
= dll_map
->target_func
;
1015 global_loader_data_unlock ();
1020 mono_dllmap_lookup (MonoImage
*assembly
, const char *dll
, const char* func
, const char **rdll
, const char **rfunc
)
1023 if (assembly
&& assembly
->dll_map
) {
1024 res
= mono_dllmap_lookup_list (assembly
->dll_map
, dll
, func
, rdll
, rfunc
);
1028 return mono_dllmap_lookup_list (global_dll_map
, dll
, func
, rdll
, rfunc
);
1032 * mono_dllmap_insert:
1033 * \param assembly if NULL, this is a global mapping, otherwise the remapping of the dynamic library will only apply to the specified assembly
1034 * \param dll The name of the external library, as it would be found in the \c DllImport declaration. If prefixed with <code>i:</code> the matching of the library name is done without case sensitivity
1035 * \param func if not null, the mapping will only applied to the named function (the value of <code>EntryPoint</code>)
1036 * \param tdll The name of the library to map the specified \p dll if it matches.
1037 * \param tfunc The name of the function that replaces the invocation. If NULL, it is replaced with a copy of \p func.
1039 * LOCKING: Acquires the loader lock.
1041 * This function is used to programatically add \c DllImport remapping in either
1042 * a specific assembly, or as a global remapping. This is done by remapping
1043 * references in a \c DllImport attribute from the \p dll library name into the \p tdll
1044 * name. If the \p dll name contains the prefix <code>i:</code>, the comparison of the
1045 * library name is done without case sensitivity.
1047 * If you pass \p func, this is the name of the \c EntryPoint in a \c DllImport if specified
1048 * or the name of the function as determined by \c DllImport. If you pass \p func, you
1049 * must also pass \p tfunc which is the name of the target function to invoke on a match.
1053 * <code>mono_dllmap_insert (NULL, "i:libdemo.dll", NULL, relocated_demo_path, NULL);</code>
1055 * The above will remap \c DllImport statements for \c libdemo.dll and \c LIBDEMO.DLL to
1056 * the contents of \c relocated_demo_path for all assemblies in the Mono process.
1058 * NOTE: This can be called before the runtime is initialized, for example from
1059 * \c mono_config_parse.
1062 mono_dllmap_insert (MonoImage
*assembly
, const char *dll
, const char *func
, const char *tdll
, const char *tfunc
)
1065 dllmap_insert_global (dll
, func
, tdll
, tfunc
);
1067 MONO_ENTER_GC_UNSAFE
;
1068 dllmap_insert_image (assembly
, dll
, func
, tdll
, tfunc
);
1069 MONO_EXIT_GC_UNSAFE
;
1074 dllmap_insert_global (const char *dll
, const char *func
, const char *tdll
, const char *tfunc
)
1078 mono_loader_init ();
1080 entry
= (MonoDllMap
*)g_malloc0 (sizeof (MonoDllMap
));
1081 entry
->dll
= dll
? g_strdup (dll
): NULL
;
1082 entry
->target
= tdll
? g_strdup (tdll
): NULL
;
1083 entry
->func
= func
? g_strdup (func
): NULL
;
1084 entry
->target_func
= tfunc
? g_strdup (tfunc
): (func
? g_strdup (func
): NULL
);
1086 global_loader_data_lock ();
1087 entry
->next
= global_dll_map
;
1088 global_dll_map
= entry
;
1089 global_loader_data_unlock ();
1093 dllmap_insert_image (MonoImage
*assembly
, const char *dll
, const char *func
, const char *tdll
, const char *tfunc
)
1096 g_assert (assembly
!= NULL
);
1098 mono_loader_init ();
1100 entry
= (MonoDllMap
*)mono_image_alloc0 (assembly
, sizeof (MonoDllMap
));
1101 entry
->dll
= dll
? mono_image_strdup (assembly
, dll
): NULL
;
1102 entry
->target
= tdll
? mono_image_strdup (assembly
, tdll
): NULL
;
1103 entry
->func
= func
? mono_image_strdup (assembly
, func
): NULL
;
1104 entry
->target_func
= tfunc
? mono_image_strdup (assembly
, tfunc
): (func
? mono_image_strdup (assembly
, func
): NULL
);
1106 mono_image_lock (assembly
);
1107 entry
->next
= assembly
->dll_map
;
1108 assembly
->dll_map
= entry
;
1109 mono_image_unlock (assembly
);
1113 free_dllmap (MonoDllMap
*map
)
1116 MonoDllMap
*next
= map
->next
;
1119 g_free (map
->target
);
1121 g_free (map
->target_func
);
1128 dllmap_cleanup (void)
1130 free_dllmap (global_dll_map
);
1131 global_dll_map
= NULL
;
1134 static GHashTable
*global_module_map
;
1137 cached_module_load (const char *name
, int flags
, char **err
)
1143 global_loader_data_lock ();
1144 if (!global_module_map
)
1145 global_module_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
1146 res
= (MonoDl
*)g_hash_table_lookup (global_module_map
, name
);
1148 global_loader_data_unlock ();
1151 res
= mono_dl_open (name
, flags
, err
);
1153 g_hash_table_insert (global_module_map
, g_strdup (name
), res
);
1154 global_loader_data_unlock ();
1159 mono_loader_register_module (const char *name
, MonoDl
*module
)
1161 if (!global_module_map
)
1162 global_module_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
1163 g_hash_table_insert (global_module_map
, g_strdup (name
), module
);
1167 remove_cached_module(gpointer key
, gpointer value
, gpointer user_data
)
1169 mono_dl_close((MonoDl
*)value
);
1173 cached_module_cleanup(void)
1175 if (global_module_map
!= NULL
) {
1176 g_hash_table_foreach(global_module_map
, remove_cached_module
, NULL
);
1178 g_hash_table_destroy(global_module_map
);
1179 global_module_map
= NULL
;
1183 static MonoDl
*internal_module
;
1186 is_absolute_path (const char *path
)
1189 if (!strncmp (path
, "@executable_path/", 17) || !strncmp (path
, "@loader_path/", 13) ||
1190 !strncmp (path
, "@rpath/", 7))
1193 return g_path_is_absolute (path
);
1197 LOOKUP_PINVOKE_ERR_OK
= 0, /* No error */
1198 LOOKUP_PINVOKE_ERR_NO_LIB
, /* DllNotFoundException */
1199 LOOKUP_PINVOKE_ERR_NO_SYM
, /* EntryPointNotFoundException */
1200 } MonoLookupPInvokeErr
;
1202 /* We should just use a MonoError, but mono_lookup_pinvoke_call has this legacy
1203 * error reporting mechanism where it returns an exception class and a string
1204 * message. So instead we return an error code and message, and for internal
1205 * callers convert it to a MonoError.
1207 * Don't expose this type to the runtime. It's just an implementation
1208 * detail for backward compatability.
1210 typedef struct MonoLookupPInvokeStatus
{
1211 MonoLookupPInvokeErr err_code
;
1213 } MonoLookupPInvokeStatus
;
1216 lookup_pinvoke_call_impl (MonoMethod
*method
, MonoLookupPInvokeStatus
*status_out
);
1219 pinvoke_probe_convert_status_for_api (MonoLookupPInvokeStatus
*status
, const char **exc_class
, const char **exc_arg
)
1223 switch (status
->err_code
) {
1224 case LOOKUP_PINVOKE_ERR_OK
:
1228 case LOOKUP_PINVOKE_ERR_NO_LIB
:
1229 *exc_class
= "DllNotFoundException";
1230 *exc_arg
= status
->err_arg
;
1231 status
->err_arg
= NULL
;
1233 case LOOKUP_PINVOKE_ERR_NO_SYM
:
1234 *exc_class
= "EntryPointNotFoundException";
1235 *exc_arg
= status
->err_arg
;
1236 status
->err_arg
= NULL
;
1239 g_assert_not_reached ();
1244 pinvoke_probe_convert_status_to_error (MonoLookupPInvokeStatus
*status
, MonoError
*error
)
1246 /* Note: this has to return a MONO_ERROR_GENERIC because mono_mb_emit_exception_for_error only knows how to decode generic errors. */
1247 switch (status
->err_code
) {
1248 case LOOKUP_PINVOKE_ERR_OK
:
1250 case LOOKUP_PINVOKE_ERR_NO_LIB
:
1251 mono_error_set_generic_error (error
, "System", "DllNotFoundException", "%s", status
->err_arg
);
1252 g_free (status
->err_arg
);
1253 status
->err_arg
= NULL
;
1255 case LOOKUP_PINVOKE_ERR_NO_SYM
:
1256 mono_error_set_generic_error (error
, "System", "EntryPointNotFoundException", "%s", status
->err_arg
);
1257 g_free (status
->err_arg
);
1258 status
->err_arg
= NULL
;
1261 g_assert_not_reached ();
1266 * mono_lookup_pinvoke_call:
1269 mono_lookup_pinvoke_call (MonoMethod
*method
, const char **exc_class
, const char **exc_arg
)
1272 MONO_ENTER_GC_UNSAFE
;
1273 MonoLookupPInvokeStatus status
;
1274 memset (&status
, 0, sizeof (status
));
1275 result
= lookup_pinvoke_call_impl (method
, &status
);
1276 pinvoke_probe_convert_status_for_api (&status
, exc_class
, exc_arg
);
1277 MONO_EXIT_GC_UNSAFE
;
1282 pinvoke_probe_for_module (MonoImage
*image
, const char*new_scope
, const char *import
, char **found_name_out
, char **error_msg_out
);
1285 pinvoke_probe_for_module_relative_directories (MonoImage
*image
, const char *file_name
, char **found_name_out
);
1288 pinvoke_probe_for_symbol (MonoDl
*module
, MonoMethodPInvoke
*piinfo
, const char *import
, char **error_msg_out
);
1291 mono_lookup_pinvoke_call_internal (MonoMethod
*method
, MonoError
*error
)
1294 MonoLookupPInvokeStatus status
;
1295 memset (&status
, 0, sizeof (status
));
1296 result
= lookup_pinvoke_call_impl (method
, &status
);
1297 if (status
.err_code
)
1298 pinvoke_probe_convert_status_to_error (&status
, error
);
1303 lookup_pinvoke_call_impl (MonoMethod
*method
, MonoLookupPInvokeStatus
*status_out
)
1305 MonoImage
*image
= m_class_get_image (method
->klass
);
1306 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)method
;
1307 MonoTableInfo
*tables
= image
->tables
;
1308 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
1309 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
1310 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
1311 guint32 scope_token
;
1312 const char *import
= NULL
;
1313 const char *orig_scope
;
1314 const char *new_scope
;
1316 char *found_name
= NULL
;
1317 MonoDl
*module
= NULL
;
1318 gboolean cached
= FALSE
;
1319 gpointer addr
= NULL
;
1321 g_assert (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
);
1323 g_assert (status_out
);
1326 return piinfo
->addr
;
1328 if (image_is_dynamic (m_class_get_image (method
->klass
))) {
1329 MonoReflectionMethodAux
*method_aux
=
1330 (MonoReflectionMethodAux
*)g_hash_table_lookup (
1331 ((MonoDynamicImage
*)m_class_get_image (method
->klass
))->method_aux_hash
, method
);
1335 import
= method_aux
->dllentry
;
1336 orig_scope
= method_aux
->dll
;
1339 if (!piinfo
->implmap_idx
|| piinfo
->implmap_idx
> im
->rows
)
1342 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
1344 if (!im_cols
[MONO_IMPLMAP_SCOPE
] || im_cols
[MONO_IMPLMAP_SCOPE
] > mr
->rows
)
1347 piinfo
->piflags
= im_cols
[MONO_IMPLMAP_FLAGS
];
1348 import
= mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]);
1349 scope_token
= mono_metadata_decode_row_col (mr
, im_cols
[MONO_IMPLMAP_SCOPE
] - 1, MONO_MODULEREF_NAME
);
1350 orig_scope
= mono_metadata_string_heap (image
, scope_token
);
1353 #ifndef ENABLE_NETCORE
1354 // FIXME: The dllmap remaps System.Native to mono-native
1355 mono_dllmap_lookup (image
, orig_scope
, import
, &new_scope
, &import
);
1357 /* AK: FIXME: dllmap, above doesn't strdup the results, so these leak
1358 * since there's no free() */
1359 new_scope
= g_strdup (orig_scope
);
1360 import
= g_strdup (import
);
1364 mono_image_lock (image
);
1365 if (!image
->pinvoke_scopes
) {
1366 image
->pinvoke_scopes
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
1367 image
->pinvoke_scope_filenames
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
1369 module
= (MonoDl
*)g_hash_table_lookup (image
->pinvoke_scopes
, new_scope
);
1370 found_name
= (char *)g_hash_table_lookup (image
->pinvoke_scope_filenames
, new_scope
);
1371 mono_image_unlock (image
);
1375 found_name
= g_strdup (found_name
);
1379 module
= pinvoke_probe_for_module (image
, new_scope
, import
, &found_name
, &error_msg
);
1382 mono_trace (G_LOG_LEVEL_WARNING
, MONO_TRACE_DLLIMPORT
,
1383 "DllImport unable to load library '%s'.",
1387 status_out
->err_code
= LOOKUP_PINVOKE_ERR_NO_LIB
;
1388 status_out
->err_arg
= g_strdup (new_scope
);
1393 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1394 "DllImport loaded library '%s'.", found_name
);
1395 mono_image_lock (image
);
1396 if (!g_hash_table_lookup (image
->pinvoke_scopes
, new_scope
)) {
1397 g_hash_table_insert (image
->pinvoke_scopes
, g_strdup (new_scope
), module
);
1398 g_hash_table_insert (image
->pinvoke_scope_filenames
, g_strdup (new_scope
), g_strdup (found_name
));
1400 mono_image_unlock (image
);
1403 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1404 "DllImport searching in: '%s' ('%s').", new_scope
, found_name
);
1405 g_free (found_name
);
1407 addr
= pinvoke_probe_for_symbol (module
, piinfo
, import
, &error_msg
);
1411 status_out
->err_code
= LOOKUP_PINVOKE_ERR_NO_SYM
;
1412 status_out
->err_arg
= g_strdup (import
);
1415 piinfo
->addr
= addr
;
1420 * pinvoke_probe_transform_path:
1422 * Try transforming the library path given in \p new_scope in different ways
1423 * depending on \p phase
1425 * \returns \c TRUE if a transformation was applied and the transformed path
1426 * components are written to the out arguments, or \c FALSE if a transformation
1430 pinvoke_probe_transform_path (const char *new_scope
, int phase
, char **file_name_out
, char **base_name_out
, char **dir_name_out
, gboolean
*is_absolute_out
)
1432 char *file_name
= NULL
, *base_name
= NULL
, *dir_name
= NULL
;
1433 gboolean changed
= FALSE
;
1434 gboolean is_absolute
= is_absolute_path (new_scope
);
1437 /* Try the original name */
1438 file_name
= g_strdup (new_scope
);
1442 /* Try trimming the .dll extension */
1443 if (strstr (new_scope
, ".dll") == (new_scope
+ strlen (new_scope
) - 4)) {
1444 file_name
= g_strdup (new_scope
);
1445 file_name
[strlen (new_scope
) - 4] = '\0';
1451 dir_name
= g_path_get_dirname (new_scope
);
1452 base_name
= g_path_get_basename (new_scope
);
1453 if (strstr (base_name
, "lib") != base_name
) {
1454 char *tmp
= g_strdup_printf ("lib%s", base_name
);
1457 file_name
= g_strdup_printf ("%s%s%s", dir_name
, G_DIR_SEPARATOR_S
, base_name
);
1460 } else if (strstr (new_scope
, "lib") != new_scope
) {
1461 file_name
= g_strdup_printf ("lib%s", new_scope
);
1466 if (!is_absolute
&& mono_dl_get_system_dir ()) {
1467 dir_name
= (char*)mono_dl_get_system_dir ();
1468 file_name
= g_path_get_basename (new_scope
);
1474 #ifndef TARGET_WIN32
1475 if (!g_ascii_strcasecmp ("user32.dll", new_scope
) ||
1476 !g_ascii_strcasecmp ("kernel32.dll", new_scope
) ||
1477 !g_ascii_strcasecmp ("user32", new_scope
) ||
1478 !g_ascii_strcasecmp ("kernel", new_scope
)) {
1479 file_name
= g_strdup ("libMonoSupportW.so");
1485 if (changed
&& is_absolute
) {
1487 dir_name
= g_path_get_dirname (file_name
);
1489 base_name
= g_path_get_basename (file_name
);
1491 *file_name_out
= file_name
;
1492 *base_name_out
= base_name
;
1493 *dir_name_out
= dir_name
;
1494 *is_absolute_out
= is_absolute
;
1499 pinvoke_probe_for_module (MonoImage
*image
, const char*new_scope
, const char *import
, char **found_name_out
, char **error_msg_out
)
1501 char *full_name
, *file_name
;
1502 char *error_msg
= NULL
;
1503 char *found_name
= NULL
;
1505 MonoDl
*module
= NULL
;
1507 g_assert (found_name_out
);
1508 g_assert (error_msg_out
);
1511 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1512 "DllImport attempting to load: '%s'.", new_scope
);
1514 /* we allow a special name to dlopen from the running process namespace */
1515 if (strcmp (new_scope
, "__Internal") == 0){
1516 if (internal_module
== NULL
)
1517 internal_module
= mono_dl_open (NULL
, MONO_DL_LAZY
, &error_msg
);
1518 module
= internal_module
;
1523 * Try loading the module using a variety of names
1525 for (i
= 0; i
< 5; ++i
) {
1526 char *base_name
= NULL
, *dir_name
= NULL
;
1527 gboolean is_absolute
;
1529 gboolean changed
= pinvoke_probe_transform_path (new_scope
, i
, &file_name
, &base_name
, &dir_name
, &is_absolute
);
1533 if (!module
&& is_absolute
) {
1534 module
= cached_module_load (file_name
, MONO_DL_LAZY
, &error_msg
);
1536 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1537 "DllImport error loading library '%s': '%s'.",
1538 file_name
, error_msg
);
1541 found_name
= g_strdup (file_name
);
1545 if (!module
&& !is_absolute
) {
1546 module
= pinvoke_probe_for_module_relative_directories (image
, file_name
, &found_name
);
1551 char *file_or_base
= is_absolute
? base_name
: file_name
;
1552 while ((full_name
= mono_dl_build_path (dir_name
, file_or_base
, &iter
))) {
1553 module
= cached_module_load (full_name
, MONO_DL_LAZY
, &error_msg
);
1555 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1556 "DllImport error loading library '%s': '%s'.",
1557 full_name
, error_msg
);
1560 found_name
= g_strdup (full_name
);
1569 module
= cached_module_load (file_name
, MONO_DL_LAZY
, &error_msg
);
1571 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1572 "DllImport error loading library '%s': '%s'.",
1573 file_name
, error_msg
);
1575 found_name
= g_strdup (file_name
);
1589 *found_name_out
= found_name
;
1590 *error_msg_out
= error_msg
;
1596 mono_set_pinvoke_search_directories (int dir_count
, char **dirs
)
1598 pinvoke_search_directories_count
= dir_count
;
1599 pinvoke_search_directories
= dirs
;
1604 pinvoke_probe_for_module_in_directory (const char *mdirname
, const char *file_name
, char **found_name_out
)
1608 MonoDl
* module
= NULL
;
1610 while ((full_name
= mono_dl_build_path (mdirname
, file_name
, &iter
)) && module
== NULL
) {
1612 module
= cached_module_load (full_name
, MONO_DL_LAZY
, &error_msg
);
1614 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
, "DllImport error loading library '%s': '%s'.", full_name
, error_msg
);
1617 *found_name_out
= g_strdup (full_name
);
1627 pinvoke_probe_for_module_relative_directories (MonoImage
*image
, const char *file_name
, char **found_name_out
)
1629 char *found_name
= NULL
;
1630 MonoDl
* module
= NULL
;
1632 g_assert (found_name_out
);
1635 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_DLLIMPORT
, "netcore DllImport handler: wanted '%s'", file_name
);
1637 // Search in predefined directories first
1638 for (int j
= 0; j
< pinvoke_search_directories_count
&& module
== NULL
; ++j
) {
1639 module
= pinvoke_probe_for_module_in_directory (pinvoke_search_directories
[j
], file_name
, &found_name
);
1642 // Fallback to image directory
1643 if (module
== NULL
) {
1644 // TODO: Check DefaultDllImportSearchPathsAttribute, NativeLibrary callback
1645 char *mdirname
= g_path_get_dirname (image
->name
);
1647 module
= pinvoke_probe_for_module_in_directory (mdirname
, file_name
, &found_name
);
1651 for (int j
= 0; j
< 3; ++j
) {
1652 char *mdirname
= NULL
;
1655 mdirname
= g_path_get_dirname (image
->name
);
1657 case 1: /* @executable_path@/../lib */
1661 binl
= mono_dl_get_executable_path (buf
, sizeof (buf
));
1663 char *base
, *newbase
;
1666 resolvedname
= mono_path_resolve_symlinks (buf
);
1668 base
= g_path_get_dirname (resolvedname
);
1669 newbase
= g_path_get_dirname(base
);
1671 // On Android the executable for the application is going to be /system/bin/app_process{32,64} depending on
1672 // the application's architecture. However, libraries for the different architectures live in different
1673 // subdirectories of `/system`: `lib` for 32-bit apps and `lib64` for 64-bit ones. Thus appending `/lib` below
1674 // will fail to load the DSO for a 64-bit app, even if it exists there, because it will have a different
1675 // architecture. This is the cause of https://github.com/xamarin/xamarin-android/issues/2780 and the ifdef
1676 // below is the fix.
1677 mdirname
= g_strdup_printf (
1678 #if defined(TARGET_ANDROID) && (defined(TARGET_ARM64) || defined(TARGET_AMD64))
1684 g_free (resolvedname
);
1691 case 2: /* @executable_path@/../Libraries */
1695 binl
= mono_dl_get_executable_path (buf
, sizeof (buf
));
1697 char *base
, *newbase
;
1700 resolvedname
= mono_path_resolve_symlinks (buf
);
1702 base
= g_path_get_dirname (resolvedname
);
1703 newbase
= g_path_get_dirname(base
);
1704 mdirname
= g_strdup_printf ("%s/Libraries", newbase
);
1706 g_free (resolvedname
);
1718 module
= pinvoke_probe_for_module_in_directory (mdirname
, file_name
, &found_name
);
1725 *found_name_out
= found_name
;
1731 pinvoke_probe_for_symbol (MonoDl
*module
, MonoMethodPInvoke
*piinfo
, const char *import
, char **error_msg_out
)
1733 char *error_msg
= NULL
;
1734 gpointer addr
= NULL
;
1736 g_assert (error_msg_out
);
1739 if (import
&& import
[0] == '#' && isdigit (import
[1])) {
1743 id
= strtol (import
+ 1, &end
, 10);
1744 if (id
> 0 && *end
== '\0')
1748 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1749 "Searching for '%s'.", import
);
1751 if (piinfo
->piflags
& PINVOKE_ATTRIBUTE_NO_MANGLE
) {
1752 error_msg
= mono_dl_symbol (module
, import
, &addr
);
1755 * Search using a variety of mangled names
1757 for (int mangle_stdcall
= 0; mangle_stdcall
<= 1 && addr
== NULL
; mangle_stdcall
++) {
1758 #if HOST_WIN32 && HOST_X86
1759 const int max_managle_param_count
= (mangle_stdcall
== 0) ? 0 : 256;
1761 const int max_managle_param_count
= 0;
1763 for (int mangle_charset
= 0; mangle_charset
<= 1 && addr
== NULL
; mangle_charset
++) {
1764 for (int mangle_param_count
= 0; mangle_param_count
<= max_managle_param_count
&& addr
== NULL
; mangle_param_count
+= 4) {
1766 char *mangled_name
= (char*)import
;
1767 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
1768 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
1769 /* Try the mangled name first */
1770 if (mangle_charset
== 0)
1771 mangled_name
= g_strconcat (import
, "W", (const char*)NULL
);
1773 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
1775 if (mangle_charset
== 0)
1776 mangled_name
= g_strconcat (import
, "W", (const char*)NULL
);
1778 /* Try the mangled name last */
1779 if (mangle_charset
== 1)
1780 mangled_name
= g_strconcat (import
, "A", (const char*)NULL
);
1783 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
1785 /* Try the mangled name last */
1786 if (mangle_charset
== 1)
1787 mangled_name
= g_strconcat (import
, "A", (const char*)NULL
);
1791 #if HOST_WIN32 && HOST_X86
1792 /* Try the stdcall mangled name */
1794 * gcc under windows creates mangled names without the underscore, but MS.NET
1795 * doesn't support it, so we doesn't support it either.
1797 if (mangle_stdcall
== 1) {
1798 MonoMethod
*method
= &piinfo
->method
;
1800 if (mangle_param_count
== 0)
1801 param_count
= mono_method_signature_internal (method
)->param_count
* sizeof (gpointer
);
1803 /* Try brute force, since it would be very hard to compute the stack usage correctly */
1804 param_count
= mangle_param_count
;
1806 char *mangled_stdcall_name
= g_strdup_printf ("_%s@%d", mangled_name
, param_count
);
1808 if (mangled_name
!= import
)
1809 g_free (mangled_name
);
1811 mangled_name
= mangled_stdcall_name
;
1814 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1815 "Probing '%s'.", mangled_name
);
1817 error_msg
= mono_dl_symbol (module
, mangled_name
, &addr
);
1820 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1821 "Found as '%s'.", mangled_name
);
1823 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_DLLIMPORT
,
1824 "Could not find '%s' due to '%s'.", mangled_name
, error_msg
);
1829 if (mangled_name
!= import
)
1830 g_free (mangled_name
);
1836 *error_msg_out
= error_msg
;
1841 * LOCKING: assumes the loader lock to be taken.
1844 mono_get_method_from_token (MonoImage
*image
, guint32 token
, MonoClass
*klass
,
1845 MonoGenericContext
*context
, gboolean
*used_context
, MonoError
*error
)
1848 int table
= mono_metadata_token_table (token
);
1849 int idx
= mono_metadata_token_index (token
);
1850 MonoTableInfo
*tables
= image
->tables
;
1851 MonoGenericContainer
*generic_container
= NULL
, *container
= NULL
;
1852 const char *sig
= NULL
;
1853 guint32 cols
[MONO_TYPEDEF_SIZE
];
1857 if (image_is_dynamic (image
)) {
1858 MonoClass
*handle_class
;
1860 result
= (MonoMethod
*)mono_lookup_dynamic_token_class (image
, token
, TRUE
, &handle_class
, context
, error
);
1861 return_val_if_nok (error
, NULL
);
1863 // This checks the memberref type as well
1864 if (result
&& handle_class
!= mono_defaults
.methodhandle_class
) {
1865 mono_error_set_bad_image (error
, image
, "Bad method token 0x%08x on dynamic image", token
);
1871 if (table
!= MONO_TABLE_METHOD
) {
1872 if (table
== MONO_TABLE_METHODSPEC
) {
1873 if (used_context
) *used_context
= TRUE
;
1874 return method_from_methodspec (image
, context
, idx
, error
);
1876 if (table
!= MONO_TABLE_MEMBERREF
) {
1877 mono_error_set_bad_image (error
, image
, "Bad method token 0x%08x.", token
);
1880 return method_from_memberref (image
, idx
, context
, used_context
, error
);
1883 if (used_context
) *used_context
= FALSE
;
1885 if (idx
> image
->tables
[MONO_TABLE_METHOD
].rows
) {
1886 mono_error_set_bad_image (error
, image
, "Bad method token 0x%08x (out of bounds).", token
);
1891 guint32 type
= mono_metadata_typedef_from_method (image
, token
);
1893 mono_error_set_bad_image (error
, image
, "Bad method token 0x%08x (could not find corresponding typedef).", token
);
1896 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
| type
, error
);
1901 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_METHOD
], idx
- 1, cols
, 6);
1903 if ((cols
[2] & METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
1904 (cols
[1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
1905 result
= (MonoMethod
*)mono_image_alloc0 (image
, sizeof (MonoMethodPInvoke
));
1907 result
= (MonoMethod
*)mono_image_alloc0 (image
, sizeof (MonoMethod
));
1908 mono_atomic_fetch_add_i32 (&methods_size
, sizeof (MonoMethod
));
1911 mono_atomic_inc_i32 (&mono_stats
.method_count
);
1914 result
->klass
= klass
;
1915 result
->flags
= cols
[2];
1916 result
->iflags
= cols
[1];
1917 result
->token
= token
;
1918 result
->name
= mono_metadata_string_heap (image
, cols
[3]);
1920 /* If a method is abstract and marked as an icall, silently ignore the
1921 * icall attribute so that we don't later emit a warning that the icall
1924 if ((result
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) &&
1925 (result
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
))
1926 result
->iflags
&= ~METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
;
1928 if (!sig
) /* already taken from the methodref */
1929 sig
= mono_metadata_blob_heap (image
, cols
[4]);
1930 /* size = */ mono_metadata_decode_blob_size (sig
, &sig
);
1932 container
= mono_class_try_get_generic_container (klass
);
1935 * load_generic_params does a binary search so only call it if the method
1939 generic_container
= mono_metadata_load_generic_params (image
, token
, container
, result
);
1941 if (generic_container
) {
1942 result
->is_generic
= TRUE
;
1943 /*FIXME put this before the image alloc*/
1944 if (!mono_metadata_load_generic_param_constraints_checked (image
, token
, generic_container
, error
))
1947 container
= generic_container
;
1950 if (cols
[1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
1951 if (result
->klass
== mono_defaults
.string_class
&& !strcmp (result
->name
, ".ctor"))
1952 result
->string_ctor
= 1;
1953 } else if (cols
[2] & METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
1954 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)result
;
1957 /* IJW is P/Invoke with a predefined function pointer. */
1958 if (m_image_is_module_handle (image
) && (cols
[1] & METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
1959 piinfo
->addr
= mono_image_rva_map (image
, cols
[0]);
1960 g_assert (piinfo
->addr
);
1963 piinfo
->implmap_idx
= mono_metadata_implmap_from_method (image
, idx
- 1);
1964 /* Native methods can have no map. */
1965 if (piinfo
->implmap_idx
)
1966 piinfo
->piflags
= mono_metadata_decode_row_col (&tables
[MONO_TABLE_IMPLMAP
], piinfo
->implmap_idx
- 1, MONO_IMPLMAP_FLAGS
);
1969 if (generic_container
)
1970 mono_method_set_generic_container (result
, generic_container
);
1979 mono_get_method (MonoImage
*image
, guint32 token
, MonoClass
*klass
)
1982 MonoMethod
*result
= mono_get_method_checked (image
, token
, klass
, NULL
, error
);
1983 mono_error_cleanup (error
);
1988 * mono_get_method_full:
1991 mono_get_method_full (MonoImage
*image
, guint32 token
, MonoClass
*klass
,
1992 MonoGenericContext
*context
)
1995 MonoMethod
*result
= mono_get_method_checked (image
, token
, klass
, context
, error
);
1996 mono_error_cleanup (error
);
2001 mono_get_method_checked (MonoImage
*image
, guint32 token
, MonoClass
*klass
, MonoGenericContext
*context
, MonoError
*error
)
2003 MonoMethod
*result
= NULL
;
2004 gboolean used_context
= FALSE
;
2006 /* We do everything inside the lock to prevent creation races */
2010 mono_image_lock (image
);
2012 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
) {
2013 if (!image
->method_cache
)
2014 image
->method_cache
= g_hash_table_new (NULL
, NULL
);
2015 result
= (MonoMethod
*)g_hash_table_lookup (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)));
2016 } else if (!image_is_dynamic (image
)) {
2017 if (!image
->methodref_cache
)
2018 image
->methodref_cache
= g_hash_table_new (NULL
, NULL
);
2019 result
= (MonoMethod
*)g_hash_table_lookup (image
->methodref_cache
, GINT_TO_POINTER (token
));
2021 mono_image_unlock (image
);
2027 result
= mono_get_method_from_token (image
, token
, klass
, context
, &used_context
, error
);
2031 mono_image_lock (image
);
2032 if (!used_context
&& !result
->is_inflated
) {
2033 MonoMethod
*result2
= NULL
;
2035 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
)
2036 result2
= (MonoMethod
*)g_hash_table_lookup (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)));
2037 else if (!image_is_dynamic (image
))
2038 result2
= (MonoMethod
*)g_hash_table_lookup (image
->methodref_cache
, GINT_TO_POINTER (token
));
2041 mono_image_unlock (image
);
2045 if (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
)
2046 g_hash_table_insert (image
->method_cache
, GINT_TO_POINTER (mono_metadata_token_index (token
)), result
);
2047 else if (!image_is_dynamic (image
))
2048 g_hash_table_insert (image
->methodref_cache
, GINT_TO_POINTER (token
), result
);
2051 mono_image_unlock (image
);
2057 get_method_constrained (MonoImage
*image
, MonoMethod
*method
, MonoClass
*constrained_class
, MonoGenericContext
*context
, MonoError
*error
)
2059 MonoClass
*base_class
= method
->klass
;
2063 if (!mono_class_is_assignable_from_internal (base_class
, constrained_class
)) {
2064 char *base_class_name
= mono_type_get_full_name (base_class
);
2065 char *constrained_class_name
= mono_type_get_full_name (constrained_class
);
2066 mono_error_set_invalid_operation (error
, "constrained call: %s is not assignable from %s", base_class_name
, constrained_class_name
);
2067 g_free (base_class_name
);
2068 g_free (constrained_class_name
);
2072 /* If the constraining class is actually an interface, we don't learn
2073 * anything new by constraining.
2075 if (MONO_CLASS_IS_INTERFACE_INTERNAL (constrained_class
))
2078 mono_class_setup_vtable (base_class
);
2079 if (mono_class_has_failure (base_class
)) {
2080 mono_error_set_for_class_failure (error
, base_class
);
2084 MonoGenericContext inflated_method_ctx
;
2085 memset (&inflated_method_ctx
, 0, sizeof (inflated_method_ctx
));
2086 inflated_method_ctx
.class_inst
= NULL
;
2087 inflated_method_ctx
.method_inst
= NULL
;
2088 gboolean inflated_generic_method
= FALSE
;
2089 if (method
->is_inflated
) {
2090 MonoGenericContext
*method_ctx
= mono_method_get_context (method
);
2091 /* If method is an instantiation of a generic method definition, ie
2092 * class H<T> { void M<U> (...) { ... } }
2093 * and method is H<C>.M<D>
2094 * we will get at the end a refined HSubclass<...>.M<U> and we will need to re-instantiate it with D.
2095 * to get HSubclass<...>.M<D>
2098 if (method_ctx
->method_inst
!= NULL
) {
2099 inflated_generic_method
= TRUE
;
2100 inflated_method_ctx
.method_inst
= method_ctx
->method_inst
;
2103 int vtable_slot
= 0;
2104 if (!MONO_CLASS_IS_INTERFACE_INTERNAL (base_class
)) {
2105 /*if the base class isn't an interface and the method isn't
2106 * virtual, there's nothing to do, we're already on the method
2107 * we want to call. */
2108 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) == 0)
2110 /* if this isn't an interface method, get the vtable slot and
2111 * find the corresponding method in the constrained class,
2112 * which is a subclass of the base class. */
2113 vtable_slot
= mono_method_get_vtable_index (method
);
2115 mono_class_setup_vtable (constrained_class
);
2116 if (mono_class_has_failure (constrained_class
)) {
2117 mono_error_set_for_class_failure (error
, constrained_class
);
2121 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) == 0)
2123 mono_class_setup_vtable (constrained_class
);
2124 if (mono_class_has_failure (constrained_class
)) {
2125 mono_error_set_for_class_failure (error
, constrained_class
);
2129 /* Get the slot of the method in the interface. Then get the
2130 * interface base in constrained_class */
2131 int itf_slot
= mono_method_get_vtable_index (method
);
2132 g_assert (itf_slot
>= 0);
2133 gboolean variant
= FALSE
;
2134 int itf_base
= mono_class_interface_offset_with_variance (constrained_class
, base_class
, &variant
);
2135 vtable_slot
= itf_slot
+ itf_base
;
2137 g_assert (vtable_slot
>= 0);
2139 MonoMethod
*res
= mono_class_get_vtable_entry (constrained_class
, vtable_slot
);
2140 if (res
== NULL
&& mono_class_is_abstract (constrained_class
) ) {
2141 /* Constraining class is abstract, there may not be a refined method. */
2144 g_assert (res
!= NULL
);
2145 if (inflated_generic_method
) {
2146 g_assert (res
->is_generic
|| res
->is_inflated
);
2147 res
= mono_class_inflate_generic_method_checked (res
, &inflated_method_ctx
, error
);
2148 return_val_if_nok (error
, NULL
);
2154 mono_get_method_constrained_with_method (MonoImage
*image
, MonoMethod
*method
, MonoClass
*constrained_class
,
2155 MonoGenericContext
*context
, MonoError
*error
)
2159 return get_method_constrained (image
, method
, constrained_class
, context
, error
);
2163 * mono_get_method_constrained:
2164 * This is used when JITing the <code>constrained.</code> opcode.
2165 * \returns The contrained method, which has been inflated
2166 * as the function return value; and the original CIL-stream method as
2167 * declared in \p cil_method. The latter is used for verification.
2170 mono_get_method_constrained (MonoImage
*image
, guint32 token
, MonoClass
*constrained_class
,
2171 MonoGenericContext
*context
, MonoMethod
**cil_method
)
2174 MonoMethod
*result
= mono_get_method_constrained_checked (image
, token
, constrained_class
, context
, cil_method
, error
);
2175 mono_error_cleanup (error
);
2180 mono_get_method_constrained_checked (MonoImage
*image
, guint32 token
, MonoClass
*constrained_class
, MonoGenericContext
*context
, MonoMethod
**cil_method
, MonoError
*error
)
2184 *cil_method
= mono_get_method_checked (image
, token
, NULL
, context
, error
);
2188 return get_method_constrained (image
, *cil_method
, constrained_class
, context
, error
);
2195 mono_free_method (MonoMethod
*method
)
2200 MONO_PROFILER_RAISE (method_free
, (method
));
2202 /* FIXME: This hack will go away when the profiler will support freeing methods */
2203 if (G_UNLIKELY (mono_profiler_installed ()))
2206 if (method
->signature
) {
2208 * FIXME: This causes crashes because the types inside signatures and
2209 * locals are shared.
2211 /* mono_metadata_free_method_signature (method->signature); */
2212 /* g_free (method->signature); */
2215 if (method_is_dynamic (method
)) {
2216 MonoMethodWrapper
*mw
= (MonoMethodWrapper
*)method
;
2219 mono_marshal_free_dynamic_wrappers (method
);
2221 mono_image_property_remove (m_class_get_image (method
->klass
), method
);
2223 g_free ((char*)method
->name
);
2225 g_free ((char*)mw
->header
->code
);
2226 for (i
= 0; i
< mw
->header
->num_locals
; ++i
)
2227 g_free (mw
->header
->locals
[i
]);
2228 g_free (mw
->header
->clauses
);
2229 g_free (mw
->header
);
2231 g_free (mw
->method_data
);
2232 g_free (method
->signature
);
2238 * mono_method_get_param_names:
2241 mono_method_get_param_names (MonoMethod
*method
, const char **names
)
2245 MonoTableInfo
*methodt
;
2246 MonoTableInfo
*paramt
;
2247 MonoMethodSignature
*signature
;
2250 if (method
->is_inflated
)
2251 method
= ((MonoMethodInflated
*) method
)->declaring
;
2253 signature
= mono_method_signature_internal (method
);
2254 /*FIXME this check is somewhat redundant since the caller usally will have to get the signature to figure out the
2255 number of arguments and allocate a properly sized array. */
2256 if (signature
== NULL
)
2259 if (!signature
->param_count
)
2262 for (i
= 0; i
< signature
->param_count
; ++i
)
2265 klass
= method
->klass
;
2266 if (m_class_get_rank (klass
))
2269 mono_class_init_internal (klass
);
2271 MonoImage
*klass_image
= m_class_get_image (klass
);
2272 if (image_is_dynamic (klass_image
)) {
2273 MonoReflectionMethodAux
*method_aux
=
2274 (MonoReflectionMethodAux
*)g_hash_table_lookup (
2275 ((MonoDynamicImage
*)m_class_get_image (method
->klass
))->method_aux_hash
, method
);
2276 if (method_aux
&& method_aux
->param_names
) {
2277 for (i
= 0; i
< mono_method_signature_internal (method
)->param_count
; ++i
)
2278 if (method_aux
->param_names
[i
+ 1])
2279 names
[i
] = method_aux
->param_names
[i
+ 1];
2284 if (method
->wrapper_type
) {
2285 char **pnames
= NULL
;
2287 mono_image_lock (klass_image
);
2288 if (klass_image
->wrapper_param_names
)
2289 pnames
= (char **)g_hash_table_lookup (klass_image
->wrapper_param_names
, method
);
2290 mono_image_unlock (klass_image
);
2293 for (i
= 0; i
< signature
->param_count
; ++i
)
2294 names
[i
] = pnames
[i
];
2299 methodt
= &klass_image
->tables
[MONO_TABLE_METHOD
];
2300 paramt
= &klass_image
->tables
[MONO_TABLE_PARAM
];
2301 idx
= mono_method_get_index (method
);
2303 guint32 cols
[MONO_PARAM_SIZE
];
2306 param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
2308 if (idx
< methodt
->rows
)
2309 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
2311 lastp
= paramt
->rows
+ 1;
2312 for (i
= param_index
; i
< lastp
; ++i
) {
2313 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
2314 if (cols
[MONO_PARAM_SEQUENCE
] && cols
[MONO_PARAM_SEQUENCE
] <= signature
->param_count
) /* skip return param spec and bounds check*/
2315 names
[cols
[MONO_PARAM_SEQUENCE
] - 1] = mono_metadata_string_heap (klass_image
, cols
[MONO_PARAM_NAME
]);
2321 * mono_method_get_param_token:
2324 mono_method_get_param_token (MonoMethod
*method
, int index
)
2326 MonoClass
*klass
= method
->klass
;
2327 MonoTableInfo
*methodt
;
2330 mono_class_init_internal (klass
);
2332 MonoImage
*klass_image
= m_class_get_image (klass
);
2333 g_assert (!image_is_dynamic (klass_image
));
2335 methodt
= &klass_image
->tables
[MONO_TABLE_METHOD
];
2336 idx
= mono_method_get_index (method
);
2338 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
2342 return mono_metadata_make_token (MONO_TABLE_PARAM
, 0);
2344 return mono_metadata_make_token (MONO_TABLE_PARAM
, param_index
+ index
);
2351 * mono_method_get_marshal_info:
2354 mono_method_get_marshal_info (MonoMethod
*method
, MonoMarshalSpec
**mspecs
)
2357 MonoClass
*klass
= method
->klass
;
2358 MonoTableInfo
*methodt
;
2359 MonoTableInfo
*paramt
;
2360 MonoMethodSignature
*signature
;
2363 signature
= mono_method_signature_internal (method
);
2364 g_assert (signature
); /*FIXME there is no way to signal error from this function*/
2366 for (i
= 0; i
< signature
->param_count
+ 1; ++i
)
2369 if (image_is_dynamic (m_class_get_image (method
->klass
))) {
2370 MonoReflectionMethodAux
*method_aux
=
2371 (MonoReflectionMethodAux
*)g_hash_table_lookup (
2372 ((MonoDynamicImage
*)m_class_get_image (method
->klass
))->method_aux_hash
, method
);
2373 if (method_aux
&& method_aux
->param_marshall
) {
2374 MonoMarshalSpec
**dyn_specs
= method_aux
->param_marshall
;
2375 for (i
= 0; i
< signature
->param_count
+ 1; ++i
)
2376 if (dyn_specs
[i
]) {
2377 mspecs
[i
] = g_new0 (MonoMarshalSpec
, 1);
2378 memcpy (mspecs
[i
], dyn_specs
[i
], sizeof (MonoMarshalSpec
));
2379 mspecs
[i
]->data
.custom_data
.custom_name
= g_strdup (dyn_specs
[i
]->data
.custom_data
.custom_name
);
2380 mspecs
[i
]->data
.custom_data
.cookie
= g_strdup (dyn_specs
[i
]->data
.custom_data
.cookie
);
2386 /* dynamic method added to non-dynamic image */
2387 if (method
->dynamic
)
2390 mono_class_init_internal (klass
);
2392 MonoImage
*klass_image
= m_class_get_image (klass
);
2393 methodt
= &klass_image
->tables
[MONO_TABLE_METHOD
];
2394 paramt
= &klass_image
->tables
[MONO_TABLE_PARAM
];
2395 idx
= mono_method_get_index (method
);
2397 guint32 cols
[MONO_PARAM_SIZE
];
2398 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
2400 if (idx
< methodt
->rows
)
2401 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
2403 lastp
= paramt
->rows
+ 1;
2405 for (i
= param_index
; i
< lastp
; ++i
) {
2406 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
2408 if (cols
[MONO_PARAM_FLAGS
] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL
&& cols
[MONO_PARAM_SEQUENCE
] <= signature
->param_count
) {
2410 tp
= mono_metadata_get_marshal_info (klass_image
, i
- 1, FALSE
);
2412 mspecs
[cols
[MONO_PARAM_SEQUENCE
]]= mono_metadata_parse_marshal_spec (klass_image
, tp
);
2421 * mono_method_has_marshal_info:
2424 mono_method_has_marshal_info (MonoMethod
*method
)
2427 MonoClass
*klass
= method
->klass
;
2428 MonoTableInfo
*methodt
;
2429 MonoTableInfo
*paramt
;
2432 if (image_is_dynamic (m_class_get_image (method
->klass
))) {
2433 MonoReflectionMethodAux
*method_aux
=
2434 (MonoReflectionMethodAux
*)g_hash_table_lookup (
2435 ((MonoDynamicImage
*)m_class_get_image (method
->klass
))->method_aux_hash
, method
);
2436 MonoMarshalSpec
**dyn_specs
= method_aux
->param_marshall
;
2438 for (i
= 0; i
< mono_method_signature_internal (method
)->param_count
+ 1; ++i
)
2445 mono_class_init_internal (klass
);
2447 methodt
= &m_class_get_image (klass
)->tables
[MONO_TABLE_METHOD
];
2448 paramt
= &m_class_get_image (klass
)->tables
[MONO_TABLE_PARAM
];
2449 idx
= mono_method_get_index (method
);
2451 guint32 cols
[MONO_PARAM_SIZE
];
2452 guint param_index
= mono_metadata_decode_row_col (methodt
, idx
- 1, MONO_METHOD_PARAMLIST
);
2454 if (idx
+ 1 < methodt
->rows
)
2455 lastp
= mono_metadata_decode_row_col (methodt
, idx
, MONO_METHOD_PARAMLIST
);
2457 lastp
= paramt
->rows
+ 1;
2459 for (i
= param_index
; i
< lastp
; ++i
) {
2460 mono_metadata_decode_row (paramt
, i
-1, cols
, MONO_PARAM_SIZE
);
2462 if (cols
[MONO_PARAM_FLAGS
] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL
)
2471 mono_method_get_wrapper_data (MonoMethod
*method
, guint32 id
)
2474 g_assert (method
!= NULL
);
2475 g_assert (method
->wrapper_type
!= MONO_WRAPPER_NONE
);
2477 data
= (void **)((MonoMethodWrapper
*)method
)->method_data
;
2478 g_assert (data
!= NULL
);
2479 g_assert (id
<= GPOINTER_TO_UINT (*data
));
2486 } StackWalkUserData
;
2489 stack_walk_adapter (MonoStackFrameInfo
*frame
, MonoContext
*ctx
, gpointer data
)
2491 StackWalkUserData
*d
= (StackWalkUserData
*)data
;
2493 switch (frame
->type
) {
2494 case FRAME_TYPE_DEBUGGER_INVOKE
:
2495 case FRAME_TYPE_MANAGED_TO_NATIVE
:
2496 case FRAME_TYPE_TRAMPOLINE
:
2497 case FRAME_TYPE_INTERP_TO_MANAGED
:
2498 case FRAME_TYPE_INTERP_TO_MANAGED_WITH_CTX
:
2500 case FRAME_TYPE_MANAGED
:
2501 case FRAME_TYPE_INTERP
:
2502 g_assert (frame
->ji
);
2503 return d
->func (frame
->actual_method
, frame
->native_offset
, frame
->il_offset
, frame
->managed
, d
->user_data
);
2506 g_assert_not_reached ();
2512 mono_stack_walk (MonoStackWalk func
, gpointer user_data
)
2514 StackWalkUserData ud
= { func
, user_data
};
2515 mono_get_eh_callbacks ()->mono_walk_stack_with_ctx (stack_walk_adapter
, NULL
, MONO_UNWIND_LOOKUP_ALL
, &ud
);
2519 * mono_stack_walk_no_il:
2522 mono_stack_walk_no_il (MonoStackWalk func
, gpointer user_data
)
2524 StackWalkUserData ud
= { func
, user_data
};
2525 mono_get_eh_callbacks ()->mono_walk_stack_with_ctx (stack_walk_adapter
, NULL
, MONO_UNWIND_DEFAULT
, &ud
);
2529 MonoStackWalkAsyncSafe func
;
2531 } AsyncStackWalkUserData
;
2535 async_stack_walk_adapter (MonoStackFrameInfo
*frame
, MonoContext
*ctx
, gpointer data
)
2537 AsyncStackWalkUserData
*d
= (AsyncStackWalkUserData
*)data
;
2539 switch (frame
->type
) {
2540 case FRAME_TYPE_DEBUGGER_INVOKE
:
2541 case FRAME_TYPE_MANAGED_TO_NATIVE
:
2542 case FRAME_TYPE_TRAMPOLINE
:
2543 case FRAME_TYPE_INTERP_TO_MANAGED
:
2544 case FRAME_TYPE_INTERP_TO_MANAGED_WITH_CTX
:
2546 case FRAME_TYPE_MANAGED
:
2547 case FRAME_TYPE_INTERP
:
2552 method
= frame
->ji
->async
? NULL
: frame
->actual_method
;
2554 return d
->func (method
, frame
->domain
, frame
->ji
->code_start
, frame
->native_offset
, d
->user_data
);
2556 g_assert_not_reached ();
2563 * mono_stack_walk_async_safe:
2564 * Async safe version callable from signal handlers.
2567 mono_stack_walk_async_safe (MonoStackWalkAsyncSafe func
, void *initial_sig_context
, void *user_data
)
2570 AsyncStackWalkUserData ud
= { func
, user_data
};
2572 mono_sigctx_to_monoctx (initial_sig_context
, &ctx
);
2573 mono_get_eh_callbacks ()->mono_walk_stack_with_ctx (async_stack_walk_adapter
, &ctx
, MONO_UNWIND_SIGNAL_SAFE
, &ud
);
2577 last_managed (MonoMethod
*m
, gint no
, gint ilo
, gboolean managed
, gpointer data
)
2579 MonoMethod
**dest
= (MonoMethod
**)data
;
2581 /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
2587 * mono_method_get_last_managed:
2590 mono_method_get_last_managed (void)
2592 MonoMethod
*m
= NULL
;
2593 mono_stack_walk_no_il (last_managed
, &m
);
2597 static gboolean loader_lock_track_ownership
= FALSE
;
2602 * See \c docs/thread-safety.txt for the locking strategy.
2605 mono_loader_lock (void)
2607 mono_locks_coop_acquire (&loader_mutex
, LoaderLock
);
2608 if (G_UNLIKELY (loader_lock_track_ownership
)) {
2609 mono_native_tls_set_value (loader_lock_nest_id
, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id
)) + 1));
2614 * mono_loader_unlock:
2617 mono_loader_unlock (void)
2619 mono_locks_coop_release (&loader_mutex
, LoaderLock
);
2620 if (G_UNLIKELY (loader_lock_track_ownership
)) {
2621 mono_native_tls_set_value (loader_lock_nest_id
, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id
)) - 1));
2626 * mono_loader_lock_track_ownership:
2628 * Set whenever the runtime should track ownership of the loader lock. If set to TRUE,
2629 * the mono_loader_lock_is_owned_by_self () can be called to query whenever the current
2630 * thread owns the loader lock.
2633 mono_loader_lock_track_ownership (gboolean track
)
2635 loader_lock_track_ownership
= track
;
2639 * mono_loader_lock_is_owned_by_self:
2641 * Return whenever the current thread owns the loader lock.
2642 * This is useful to avoid blocking operations while holding the loader lock.
2645 mono_loader_lock_is_owned_by_self (void)
2647 g_assert (loader_lock_track_ownership
);
2649 return GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id
)) > 0;
2653 * mono_loader_lock_if_inited:
2655 * Acquire the loader lock if it has been initialized, no-op otherwise. This can
2656 * be used in runtime initialization code which can be executed before mono_loader_init ().
2659 mono_loader_lock_if_inited (void)
2661 if (loader_lock_inited
)
2662 mono_loader_lock ();
2666 mono_loader_unlock_if_inited (void)
2668 if (loader_lock_inited
)
2669 mono_loader_unlock ();
2673 * mono_method_signature_checked_slow:
2675 * Return the signature of the method M. On failure, returns NULL, and ERR is set.
2676 * Call mono_method_signature_checked instead.
2678 MonoMethodSignature
*
2679 mono_method_signature_checked_slow (MonoMethod
*m
, MonoError
*error
)
2684 gboolean can_cache_signature
;
2685 MonoGenericContainer
*container
;
2686 MonoMethodSignature
*signature
= NULL
, *sig2
;
2689 /* We need memory barriers below because of the double-checked locking pattern */
2694 return m
->signature
;
2696 img
= m_class_get_image (m
->klass
);
2698 if (m
->is_inflated
) {
2699 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) m
;
2700 /* the lock is recursive */
2701 signature
= mono_method_signature_internal (imethod
->declaring
);
2702 signature
= inflate_generic_signature_checked (m_class_get_image (imethod
->declaring
->klass
), signature
, mono_method_get_context (m
), error
);
2706 mono_atomic_fetch_add_i32 (&inflated_signatures_size
, mono_metadata_signature_size (signature
));
2708 mono_image_lock (img
);
2710 mono_memory_barrier ();
2712 m
->signature
= signature
;
2714 mono_image_unlock (img
);
2716 return m
->signature
;
2719 g_assert (mono_metadata_token_table (m
->token
) == MONO_TABLE_METHOD
);
2720 idx
= mono_metadata_token_index (m
->token
);
2722 sig
= mono_metadata_blob_heap (img
, sig_offset
= mono_metadata_decode_row_col (&img
->tables
[MONO_TABLE_METHOD
], idx
- 1, MONO_METHOD_SIGNATURE
));
2724 g_assert (!mono_class_is_ginst (m
->klass
));
2725 container
= mono_method_get_generic_container (m
);
2727 container
= mono_class_try_get_generic_container (m
->klass
);
2729 /* Generic signatures depend on the container so they cannot be cached */
2730 /* icall/pinvoke signatures cannot be cached cause we modify them below */
2731 can_cache_signature
= !(m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) && !(m
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) && !container
;
2733 /* If the method has parameter attributes, that can modify the signature */
2734 if (mono_metadata_method_has_param_attrs (img
, idx
))
2735 can_cache_signature
= FALSE
;
2737 if (can_cache_signature
) {
2738 mono_image_lock (img
);
2739 signature
= (MonoMethodSignature
*)g_hash_table_lookup (img
->method_signatures
, sig
);
2740 mono_image_unlock (img
);
2744 const char *sig_body
;
2745 /*TODO we should cache the failure result somewhere*/
2746 if (!mono_verifier_verify_method_signature (img
, sig_offset
, error
))
2749 /* size = */ mono_metadata_decode_blob_size (sig
, &sig_body
);
2751 signature
= mono_metadata_parse_method_signature_full (img
, container
, idx
, sig_body
, NULL
, error
);
2755 if (can_cache_signature
) {
2756 mono_image_lock (img
);
2757 sig2
= (MonoMethodSignature
*)g_hash_table_lookup (img
->method_signatures
, sig
);
2759 g_hash_table_insert (img
->method_signatures
, (gpointer
)sig
, signature
);
2760 mono_image_unlock (img
);
2763 mono_atomic_fetch_add_i32 (&signatures_size
, mono_metadata_signature_size (signature
));
2766 /* Verify metadata consistency */
2767 if (signature
->generic_param_count
) {
2768 if (!container
|| !container
->is_method
) {
2769 mono_error_set_method_missing (error
, m
->klass
, m
->name
, signature
, "Signature claims method has generic parameters, but generic_params table says it doesn't for method 0x%08x from image %s", idx
, img
->name
);
2772 if (container
->type_argc
!= signature
->generic_param_count
) {
2773 mono_error_set_method_missing (error
, m
->klass
, m
->name
, signature
, "Inconsistent generic parameter count. Signature says %d, generic_params table says %d for method 0x%08x from image %s", signature
->generic_param_count
, container
->type_argc
, idx
, img
->name
);
2776 } else if (container
&& container
->is_method
&& container
->type_argc
) {
2777 mono_error_set_method_missing (error
, m
->klass
, m
->name
, signature
, "generic_params table claims method has generic parameters, but signature says it doesn't for method 0x%08x from image %s", idx
, img
->name
);
2780 if (m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
2781 signature
->pinvoke
= 1;
2784 * On Windows the default pinvoke calling convention is STDCALL but
2785 * we need CDECL since this is actually an icall.
2787 signature
->call_convention
= MONO_CALL_C
;
2789 } else if (m
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2790 MonoCallConvention conv
= (MonoCallConvention
)0;
2791 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)m
;
2792 signature
->pinvoke
= 1;
2794 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CALL_CONV_MASK
) {
2795 case 0: /* no call conv, so using default */
2796 case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI
:
2797 conv
= MONO_CALL_DEFAULT
;
2799 case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL
:
2802 case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL
:
2803 conv
= MONO_CALL_STDCALL
;
2805 case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL
:
2806 conv
= MONO_CALL_THISCALL
;
2808 case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL
:
2809 conv
= MONO_CALL_FASTCALL
;
2811 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC
:
2812 case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST
:
2814 mono_error_set_method_missing (error
, m
->klass
, m
->name
, signature
, "Unsupported calling convention : 0x%04x for method 0x%08x from image %s", piinfo
->piflags
, idx
, img
->name
);
2818 signature
->call_convention
= conv
;
2821 mono_image_lock (img
);
2823 mono_memory_barrier ();
2825 m
->signature
= signature
;
2827 mono_image_unlock (img
);
2829 return m
->signature
;
2833 * mono_method_signature_internal_slow:
2834 * \returns the signature of the method \p m. On failure, returns NULL.
2835 * Call mono_method_signature_internal instead.
2837 MonoMethodSignature
*
2838 mono_method_signature_internal_slow (MonoMethod
*m
)
2841 MonoMethodSignature
*sig
= mono_method_signature_checked (m
, error
);
2844 char *type_name
= mono_type_get_full_name (m
->klass
);
2845 g_warning ("Could not load signature of %s:%s due to: %s", type_name
, m
->name
, mono_error_get_message (error
));
2847 mono_error_cleanup (error
);
2852 * mono_method_signature:
2853 * \returns the signature of the method \p m. On failure, returns NULL.
2855 MonoMethodSignature
*
2856 mono_method_signature (MonoMethod
*m
)
2858 MonoMethodSignature
*sig
;
2859 MONO_ENTER_GC_UNSAFE
;
2860 sig
= mono_method_signature_internal (m
);
2861 MONO_EXIT_GC_UNSAFE
;
2866 * mono_method_get_name:
2869 mono_method_get_name (MonoMethod
*method
)
2871 return method
->name
;
2875 * mono_method_get_class:
2878 mono_method_get_class (MonoMethod
*method
)
2880 return method
->klass
;
2884 * mono_method_get_token:
2887 mono_method_get_token (MonoMethod
*method
)
2889 return method
->token
;
2893 mono_method_has_no_body (MonoMethod
*method
)
2895 return ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
2896 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
2897 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
2898 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
));
2901 // FIXME Replace all internal callers of mono_method_get_header_checked with
2902 // mono_method_get_header_internal; the difference is in error initialization.
2904 mono_method_get_header_internal (MonoMethod
*method
, MonoError
*error
)
2910 MonoGenericContainer
*container
;
2913 img
= m_class_get_image (method
->klass
);
2915 // FIXME: for internal callers maybe it makes sense to do this check at the call site, not
2917 if (mono_method_has_no_body (method
)) {
2918 if (method
->is_reabstracted
== 1)
2919 mono_error_set_generic_error (error
, "System", "EntryPointNotFoundException", "%s", method
->name
);
2921 mono_error_set_bad_image (error
, img
, "Method has no body");
2925 if (method
->is_inflated
) {
2926 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
2927 MonoMethodHeader
*header
, *iheader
;
2929 header
= mono_method_get_header_checked (imethod
->declaring
, error
);
2933 iheader
= inflate_generic_header (header
, mono_method_get_context (method
), error
);
2934 mono_metadata_free_mh (header
);
2942 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
|| method
->sre_method
) {
2943 MonoMethodWrapper
*mw
= (MonoMethodWrapper
*)method
;
2944 g_assert (mw
->header
);
2949 * We don't need locks here: the new header is allocated from malloc memory
2950 * and is not stored anywhere in the runtime, the user needs to free it.
2952 g_assert (mono_metadata_token_table (method
->token
) == MONO_TABLE_METHOD
);
2953 idx
= mono_metadata_token_index (method
->token
);
2954 rva
= mono_metadata_decode_row_col (&img
->tables
[MONO_TABLE_METHOD
], idx
- 1, MONO_METHOD_RVA
);
2956 if (!mono_verifier_verify_method_header (img
, rva
, error
))
2959 loc
= mono_image_rva_map (img
, rva
);
2961 mono_error_set_bad_image (error
, img
, "Method has zero rva");
2966 * When parsing the types of local variables, we must pass any container available
2967 * to ensure that both VAR and MVAR will get the right owner.
2969 container
= mono_method_get_generic_container (method
);
2971 container
= mono_class_try_get_generic_container (method
->klass
);
2972 return mono_metadata_parse_mh_full (img
, container
, (const char *)loc
, error
);
2976 mono_method_get_header_checked (MonoMethod
*method
, MonoError
*error
)
2977 // Public function that must initialize MonoError for compatibility.
2979 MONO_API_ERROR_INIT (error
);
2980 return mono_method_get_header_internal (method
, error
);
2983 * mono_method_get_header:
2986 mono_method_get_header (MonoMethod
*method
)
2989 MonoMethodHeader
*header
= mono_method_get_header_checked (method
, error
);
2990 mono_error_cleanup (error
);
2996 * mono_method_get_flags:
2999 mono_method_get_flags (MonoMethod
*method
, guint32
*iflags
)
3002 *iflags
= method
->iflags
;
3003 return method
->flags
;
3007 * mono_method_get_index:
3008 * Find the method index in the metadata \c MethodDef table.
3011 mono_method_get_index (MonoMethod
*method
)
3013 MonoClass
*klass
= method
->klass
;
3016 if (m_class_get_rank (klass
))
3017 /* constructed array methods are not in the MethodDef table */
3021 return mono_metadata_token_index (method
->token
);
3023 mono_class_setup_methods (klass
);
3024 if (mono_class_has_failure (klass
))
3026 int first_idx
= mono_class_get_first_method_idx (klass
);
3027 int mcount
= mono_class_get_method_count (klass
);
3028 MonoMethod
**klass_methods
= m_class_get_methods (klass
);
3029 for (i
= 0; i
< mcount
; ++i
) {
3030 if (method
== klass_methods
[i
]) {
3031 if (m_class_get_image (klass
)->uncompressed_metadata
)
3032 return mono_metadata_translate_token_index (m_class_get_image (klass
), MONO_TABLE_METHOD
, first_idx
+ i
+ 1);
3034 return first_idx
+ i
+ 1;