2 #ifndef __MONO_METADATA_INTERNALS_H__
3 #define __MONO_METADATA_INTERNALS_H__
5 #include "mono/metadata/image.h"
6 #include "mono/metadata/blob.h"
7 #include "mono/metadata/mempool.h"
8 #include "mono/metadata/domain-internals.h"
9 #include "mono/metadata/mono-hash.h"
10 #include "mono/utils/mono-compiler.h"
11 #include "mono/utils/mono-dl.h"
12 #include "mono/utils/monobitset.h"
13 #include "mono/utils/mono-property-hash.h"
14 #include "mono/utils/mono-value-hash.h"
15 #include <mono/utils/mono-error.h>
19 MonoClass *klass; /* for VALUETYPE and CLASS */
20 MonoType *type; /* for PTR */
21 MonoArrayType *array; /* for ARRAY */
22 MonoMethodSignature *method;
23 MonoGenericParam *generic_param; /* for VAR and MVAR */
24 MonoGenericClass *generic_class; /* for GENERICINST */
26 unsigned int attrs : 16; /* param attributes or field flags */
27 MonoTypeEnum type : 8;
28 unsigned int num_mods : 6; /* max 64 modifiers follow at the end */
29 unsigned int byref : 1;
30 unsigned int pinned : 1; /* valid when included in a local var signature */
31 MonoCustomMod modifiers [MONO_ZERO_LEN_ARRAY]; /* this may grow */
34 #define MONO_SIZEOF_TYPE (offsetof (struct _MonoType, modifiers))
36 #define MONO_SECMAN_FLAG_INIT(x) (x & 0x2)
37 #define MONO_SECMAN_FLAG_GET_VALUE(x) (x & 0x1)
38 #define MONO_SECMAN_FLAG_SET_VALUE(x,y) do { x = ((y) ? 0x3 : 0x2); } while (0)
40 #define MONO_PUBLIC_KEY_TOKEN_LENGTH 17
42 #define MONO_PROCESSOR_ARCHITECTURE_NONE 0
43 #define MONO_PROCESSOR_ARCHITECTURE_MSIL 1
44 #define MONO_PROCESSOR_ARCHITECTURE_X86 2
45 #define MONO_PROCESSOR_ARCHITECTURE_IA64 3
46 #define MONO_PROCESSOR_ARCHITECTURE_AMD64 4
48 struct _MonoAssemblyName {
51 const char *hash_value;
52 const mono_byte* public_key;
53 // string of 16 hex chars + 1 NULL
54 mono_byte public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
58 uint16_t major, minor, build, revision, arch;
61 struct MonoTypeNameParse {
64 MonoAssemblyName assembly;
65 GList *modifiers; /* 0 -> byref, -1 -> pointer, > 0 -> array rank */
66 GPtrArray *type_arguments;
70 struct _MonoAssembly {
72 * The number of appdomains which have this assembly loaded plus the number of
73 * assemblies referencing this assembly through an entry in their image->references
74 * arrays. The later is needed because entries in the image->references array
75 * might point to assemblies which are only loaded in some appdomains, and without
76 * the additional reference, they can be freed at any time.
77 * The ref_count is initially 0.
79 int ref_count; /* use atomic operations only */
81 MonoAssemblyName aname;
83 GSList *friend_assembly_names; /* Computed by mono_assembly_load_friends () */
84 guint8 friend_assembly_names_inited;
87 guint8 corlib_internal;
89 guint8 wrap_non_exception_throws;
90 guint8 wrap_non_exception_throws_inited;
91 guint8 jit_optimizer_disabled;
92 guint8 jit_optimizer_disabled_inited;
93 /* security manager flags (one bit is for lazy initialization) */
94 guint32 ecma:2; /* Has the ECMA key */
95 guint32 aptc:2; /* Has the [AllowPartiallyTrustedCallers] attributes */
96 guint32 fulltrust:2; /* Has FullTrust permission */
97 guint32 unmanaged:2; /* Has SecurityPermissionFlag.UnmanagedCode permission */
98 guint32 skipverification:2; /* Has SecurityPermissionFlag.SkipVerification permission */
106 struct _MonoTableInfo {
112 * Tables contain up to 9 columns and the possible sizes of the
113 * fields in the documentation are 1, 2 and 4 bytes. So we
114 * can encode in 2 bits the size.
116 * A 32 bit value can encode the resulting size
118 * The top eight bits encode the number of columns in the table.
119 * we only need 4, but 8 is aligned no shift required.
121 guint32 size_bitfield;
124 #define REFERENCE_MISSING ((gpointer) -1)
126 typedef struct _MonoDllMap MonoDllMap;
130 * The number of assemblies which reference this MonoImage though their 'image'
131 * field plus the number of images which reference this MonoImage through their
132 * 'modules' field, plus the number of threads holding temporary references to
133 * this image between calls of mono_image_open () and mono_image_close ().
136 void *raw_data_handle;
138 guint32 raw_data_len;
139 guint8 raw_buffer_used : 1;
140 guint8 raw_data_allocated : 1;
141 guint8 fileio_used : 1;
144 /* Module was loaded using LoadLibrary. */
145 guint8 is_module_handle : 1;
147 /* Module entry point is _CorDllMain. */
148 guint8 has_entry_point : 1;
151 /* Whenever this is a dynamically emitted module */
154 /* Whenever this is a reflection only image */
157 /* Whenever this image contains uncompressed metadata */
158 guint8 uncompressed_metadata : 1;
160 guint8 checked_module_cctor : 1;
161 guint8 has_module_cctor : 1;
163 guint8 idx_string_wide : 1;
164 guint8 idx_guid_wide : 1;
165 guint8 idx_blob_wide : 1;
167 /* Whenever this image is considered as platform code for the CoreCLR security model */
168 guint8 core_clr_platform_code : 1;
171 const char *assembly_name;
172 const char *module_name;
174 gint16 md_version_major, md_version_minor;
177 MonoMemPool *mempool; /*protected by the image lock*/
181 MonoStreamHeader heap_strings;
182 MonoStreamHeader heap_us;
183 MonoStreamHeader heap_blob;
184 MonoStreamHeader heap_guid;
185 MonoStreamHeader heap_tables;
187 const char *tables_base;
190 MonoTableInfo tables [MONO_TABLE_NUM];
193 * references is initialized only by using the mono_assembly_open
194 * function, and not by using the lowlevel mono_image_open.
196 * It is NULL terminated.
198 MonoAssembly **references;
201 guint32 module_count;
202 gboolean *modules_loaded;
209 * The Assembly this image was loaded from.
211 MonoAssembly *assembly;
214 * Indexed by method tokens and typedef tokens.
216 GHashTable *method_cache; /*protected by the image lock*/
217 MonoInternalHashTable class_cache;
219 /* Indexed by memberref + methodspec tokens */
220 GHashTable *methodref_cache; /*protected by the image lock*/
223 * Indexed by fielddef and memberref tokens
225 GHashTable *field_cache;
227 /* indexed by typespec tokens. */
228 GHashTable *typespec_cache;
229 /* indexed by token */
230 GHashTable *memberref_signatures;
231 GHashTable *helper_signatures;
233 /* Indexed by blob heap indexes */
234 GHashTable *method_signatures;
237 * Indexes namespaces to hash tables that map class name to typedef token.
239 GHashTable *name_cache; /*protected by the image lock*/
242 * Indexed by MonoClass
244 GHashTable *array_cache;
245 GHashTable *ptr_cache;
247 GHashTable *szarray_cache;
248 /* This has a separate lock to improve scalability */
249 CRITICAL_SECTION szarray_cache_lock;
252 * indexed by MonoMethodSignature
254 GHashTable *delegate_begin_invoke_cache;
255 GHashTable *delegate_end_invoke_cache;
256 GHashTable *delegate_invoke_cache;
257 GHashTable *runtime_invoke_cache;
260 * indexed by SignatureMethodPair
262 GHashTable *delegate_abstract_invoke_cache;
265 * indexed by SignatureMethodPair
267 GHashTable *delegate_bound_static_invoke_cache;
269 * indexed by MonoMethod pointers
271 GHashTable *runtime_invoke_direct_cache;
272 GHashTable *runtime_invoke_vcall_cache;
273 GHashTable *managed_wrapper_cache;
274 GHashTable *native_wrapper_cache;
275 GHashTable *native_wrapper_aot_cache;
276 GHashTable *native_func_wrapper_aot_cache;
277 GHashTable *remoting_invoke_cache;
278 GHashTable *synchronized_cache;
279 GHashTable *unbox_wrapper_cache;
280 GHashTable *cominterop_invoke_cache;
281 GHashTable *cominterop_wrapper_cache; /* LOCKING: marshal lock */
282 GHashTable *thunk_invoke_cache;
283 GHashTable *wrapper_param_names;
286 * indexed by MonoClass pointers
288 GHashTable *ldfld_wrapper_cache;
289 GHashTable *ldflda_wrapper_cache;
290 GHashTable *stfld_wrapper_cache;
291 GHashTable *isinst_cache;
292 GHashTable *castclass_cache;
293 GHashTable *proxy_isinst_cache;
294 GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
296 /* Contains rarely used fields of runtime structures belonging to this image */
297 MonoPropertyHash *property_hash;
299 void *reflection_info;
302 * user_info is a public field and is not touched by the
307 /* dll map entries */
310 /* interfaces IDs from this image */
311 MonoBitSet *interface_bitset;
313 /* when the image is being closed, this is abused as a list of
314 malloc'ed regions to be freed. */
315 GSList *reflection_info_unregister_classes;
317 /* List of image sets containing this image */
320 /* Caches for MonoClass-es representing anon generic params */
321 MonoClass **var_cache_fast;
322 MonoClass **mvar_cache_fast;
323 GHashTable *var_cache_slow;
324 GHashTable *mvar_cache_slow;
326 /* Maps malloc-ed char* pinvoke scope -> MonoDl* */
327 GHashTable *pinvoke_scopes;
329 /* Maps malloc-ed char* pinvoke scope -> malloced-ed char* filename */
330 GHashTable *pinvoke_scope_filenames;
333 * No other runtime locks must be taken while holding this lock.
334 * It's meant to be used only to mutate and query structures part of this image.
336 CRITICAL_SECTION lock;
340 * Generic instances depend on many images, and they need to be deleted if one
341 * of the images they depend on is unloaded. For example,
342 * List<Foo> depends on both List's image and Foo's image.
343 * A MonoImageSet is the owner of all generic instances depending on the same set of
350 GHashTable *gclass_cache, *ginst_cache, *gmethod_cache, *gsignature_cache;
352 CRITICAL_SECTION lock;
355 * Memory for generic instances owned by this image set should be allocated from
356 * this mempool, using the mono_image_set_alloc family of functions.
358 MonoMemPool *mempool;
371 guint32 alloc_size; /* malloced bytes */
373 guint32 offset; /* from start of metadata */
379 guint8 row_size; /* calculated later with column_sizes */
382 guint32 *values; /* rows * columns */
385 struct _MonoDynamicAssembly {
386 MonoAssembly assembly;
388 guint32 strong_name_size;
394 struct _MonoDynamicImage {
398 guint32 metadata_rva;
400 guint32 cli_header_offset;
404 guint32 imp_names_offset;
410 } sections [MONO_SECTION_MAX];
411 GHashTable *typespec;
413 GHashTable *handleref;
414 MonoGHashTable *handleref_managed;
415 MonoGHashTable *tokens;
416 GHashTable *blob_cache;
417 GHashTable *standalonesig_cache;
418 GList *array_methods;
419 GPtrArray *gen_params;
420 MonoGHashTable *token_fixups;
421 GHashTable *method_to_table_idx;
422 GHashTable *field_to_table_idx;
423 GHashTable *method_aux_hash;
424 GHashTable *vararg_aux_hash;
425 MonoGHashTable *generic_def_objects;
426 MonoGHashTable *methodspec;
428 * Maps final token values to the object they describe.
430 MonoGHashTable *remapped_tokens;
433 gboolean initial_image;
434 guint32 pe_kind, machine;
436 guint32 strong_name_size;
438 guint32 win32_res_size;
441 MonoDynamicStream sheap;
442 MonoDynamicStream code; /* used to store method headers and bytecode */
443 MonoDynamicStream resources; /* managed embedded resources */
444 MonoDynamicStream us;
445 MonoDynamicStream blob;
446 MonoDynamicStream tstream;
447 MonoDynamicStream guid;
448 MonoDynamicTable tables [MONO_TABLE_NUM];
449 MonoClass *wrappers_type; /*wrappers are bound to this type instead of <Module>*/
452 /* Contains information about assembly binding */
453 typedef struct _MonoAssemblyBindingInfo {
456 guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
459 AssemblyVersionSet old_version_bottom;
460 AssemblyVersionSet old_version_top;
461 AssemblyVersionSet new_version;
462 guint has_old_version_bottom : 1;
463 guint has_old_version_top : 1;
464 guint has_new_version : 1;
466 gint32 domain_id; /*Needed to unload per-domain binding*/
467 } MonoAssemblyBindingInfo;
469 struct _MonoMethodHeader {
470 const unsigned char *code;
471 #ifdef MONO_SMALL_CONFIG
476 guint16 max_stack : 15;
477 unsigned int is_transient: 1; /* mono_metadata_free_mh () will actually free this header */
478 unsigned int num_clauses : 15;
479 /* if num_locals != 0, then the following apply: */
480 unsigned int init_locals : 1;
482 MonoExceptionClause *clauses;
483 MonoType *locals [MONO_ZERO_LEN_ARRAY];
488 gboolean has_clauses;
489 } MonoMethodHeaderSummary;
491 #define MONO_SIZEOF_METHOD_HEADER (sizeof (struct _MonoMethodHeader) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
493 struct _MonoMethodSignature {
495 #ifdef MONO_SMALL_CONFIG
498 unsigned int generic_param_count : 5;
502 unsigned int generic_param_count : 16;
504 unsigned int call_convention : 6;
505 unsigned int hasthis : 1;
506 unsigned int explicit_this : 1;
507 unsigned int pinvoke : 1;
508 unsigned int is_inflated : 1;
509 unsigned int has_type_parameters : 1;
510 MonoType *params [MONO_ZERO_LEN_ARRAY];
513 #define MONO_SIZEOF_METHOD_SIGNATURE (sizeof (struct _MonoMethodSignature) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
515 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
516 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
519 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
522 mono_image_alloc (MonoImage *image, guint size) MONO_INTERNAL;
525 mono_image_alloc0 (MonoImage *image, guint size) MONO_INTERNAL;
527 #define mono_image_new0(image,type,size) ((type *) mono_image_alloc0 (image, sizeof (type)* (size)))
530 mono_image_strdup (MonoImage *image, const char *s) MONO_INTERNAL;
533 g_list_prepend_image (MonoImage *image, GList *list, gpointer data) MONO_INTERNAL;
536 g_slist_append_image (MonoImage *image, GSList *list, gpointer data) MONO_INTERNAL;
539 mono_image_lock (MonoImage *image) MONO_INTERNAL;
542 mono_image_unlock (MonoImage *image) MONO_INTERNAL;
545 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property) MONO_INTERNAL;
548 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value) MONO_INTERNAL;
551 mono_image_property_remove (MonoImage *image, gpointer subject) MONO_INTERNAL;
554 mono_image_close_except_pools (MonoImage *image) MONO_INTERNAL;
557 mono_image_close_finish (MonoImage *image) MONO_INTERNAL;
559 typedef void (*MonoImageUnloadFunc) (MonoImage *image, gpointer user_data);
562 mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data) MONO_INTERNAL;
565 mono_remove_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data) MONO_INTERNAL;
568 mono_image_set_alloc (MonoImageSet *set, guint size) MONO_INTERNAL;
571 mono_image_set_alloc0 (MonoImageSet *set, guint size) MONO_INTERNAL;
574 mono_image_set_strdup (MonoImageSet *set, const char *s) MONO_INTERNAL;
576 #define mono_image_set_new0(image,type,size) ((type *) mono_image_set_alloc0 (image, sizeof (type)* (size)))
579 mono_metadata_get_shared_type (MonoType *type) MONO_INTERNAL;
582 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
585 mono_metadata_clean_generic_classes_for_image (MonoImage *image) MONO_INTERNAL;
588 mono_metadata_cleanup (void);
590 const char * mono_meta_table_name (int table) MONO_INTERNAL;
591 void mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
594 mono_metadata_interfaces_from_typedef_full (MonoImage *image,
596 MonoClass ***interfaces,
598 gboolean heap_alloc_result,
599 MonoGenericContext *context) MONO_INTERNAL;
602 mono_metadata_parse_array_full (MonoImage *image,
603 MonoGenericContainer *container,
605 const char **rptr) MONO_INTERNAL;
608 mono_metadata_parse_type_full (MonoImage *image,
609 MonoGenericContainer *container,
610 MonoParseTypeMode mode,
615 MonoMethodSignature *
616 mono_metadata_parse_signature_full (MonoImage *image,
617 MonoGenericContainer *generic_container,
618 guint32 token) MONO_INTERNAL;
620 MonoMethodSignature *
621 mono_metadata_parse_method_signature_full (MonoImage *image,
622 MonoGenericContainer *generic_container,
628 mono_metadata_parse_mh_full (MonoImage *image,
629 MonoGenericContainer *container,
633 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary) MONO_INTERNAL;
635 int* mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count) MONO_INTERNAL;
636 gboolean mono_metadata_method_has_param_attrs (MonoImage *m, int def) MONO_INTERNAL;
639 mono_metadata_generic_context_hash (const MonoGenericContext *context) MONO_INTERNAL;
642 mono_metadata_generic_context_equal (const MonoGenericContext *g1,
643 const MonoGenericContext *g2) MONO_INTERNAL;
646 mono_metadata_parse_generic_inst (MonoImage *image,
647 MonoGenericContainer *container,
650 const char **rptr) MONO_INTERNAL;
653 mono_metadata_get_generic_inst (int type_argc,
654 MonoType **type_argv) MONO_INTERNAL;
657 mono_metadata_lookup_generic_class (MonoClass *gclass,
658 MonoGenericInst *inst,
659 gboolean is_dynamic) MONO_INTERNAL;
661 MonoGenericInst * mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error) MONO_INTERNAL;
663 void mono_dynamic_stream_reset (MonoDynamicStream* stream) MONO_INTERNAL;
664 void mono_assembly_addref (MonoAssembly *assembly) MONO_INTERNAL;
665 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
666 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass) MONO_INTERNAL;
668 void mono_assembly_release_gc_roots (MonoAssembly *assembly) MONO_INTERNAL;
669 gboolean mono_assembly_close_except_image_pools (MonoAssembly *assembly) MONO_INTERNAL;
670 void mono_assembly_close_finish (MonoAssembly *assembly) MONO_INTERNAL;
673 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
675 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
676 void mono_config_parse_assembly_bindings (const char *filename, int major, int minor, void *user_data,
677 void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data)) MONO_INTERNAL;
680 mono_assembly_name_parse_full (const char *name,
681 MonoAssemblyName *aname,
682 gboolean save_public_key,
683 gboolean *is_version_defined,
684 gboolean *is_token_defined) MONO_INTERNAL;
686 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
688 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
691 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original) MONO_INTERNAL;
692 MonoMethodSignature *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig) MONO_INTERNAL;
693 MonoMethodSignature *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig) MONO_INTERNAL;
696 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
699 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
701 void mono_type_get_desc (GString *res, MonoType *type, mono_bool include_namespace);
704 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
707 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr) MONO_INTERNAL;
709 guint mono_metadata_generic_inst_hash (gconstpointer data) MONO_INTERNAL;
710 gboolean mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
713 mono_metadata_field_info_with_mempool (
718 MonoMarshalSpec **marshal_spec);
721 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field) MONO_INTERNAL;
724 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event) MONO_INTERNAL;
727 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property) MONO_INTERNAL;
730 mono_metadata_signature_size (MonoMethodSignature *sig) MONO_INTERNAL;
732 guint mono_metadata_str_hash (gconstpointer v1) MONO_INTERNAL;
734 gboolean mono_image_load_pe_data (MonoImage *image) MONO_INTERNAL;
736 gboolean mono_image_load_cli_data (MonoImage *image) MONO_INTERNAL;
738 void mono_image_load_names (MonoImage *image) MONO_INTERNAL;
740 MonoImage *mono_image_open_raw (const char *fname, MonoImageOpenStatus *status) MONO_INTERNAL;
742 MonoException *mono_get_exception_field_access_msg (const char *msg) MONO_INTERNAL;
744 MonoException *mono_get_exception_method_access_msg (const char *msg) MONO_INTERNAL;
746 MonoMethod* method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context) MONO_INTERNAL;
748 MonoMethod *mono_get_method_constrained_with_method (MonoImage *image, MonoMethod *method, MonoClass *constrained_class, MonoGenericContext *context) MONO_INTERNAL;
750 #endif /* __MONO_METADATA_INTERNALS_H__ */