2009-05-15 Geoff Norton <gnorton@novell.com>
[mono-project.git] / mono / metadata / metadata-internals.h
blob9d5fb128907fde8268cda3e64347de6e087fff50
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/utils/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"
16 #define MONO_SECMAN_FLAG_INIT(x) (x & 0x2)
17 #define MONO_SECMAN_FLAG_GET_VALUE(x) (x & 0x1)
18 #define MONO_SECMAN_FLAG_SET_VALUE(x,y) do { x = ((y) ? 0x3 : 0x2); } while (0)
20 struct _MonoAssembly {
21 /*
22 * The number of appdomains which have this assembly loaded plus the number of
23 * assemblies referencing this assembly through an entry in their image->references
24 * arrays. The later is needed because entries in the image->references array
25 * might point to assemblies which are only loaded in some appdomains, and without
26 * the additional reference, they can be freed at any time.
27 * The ref_count is initially 0.
29 int ref_count; /* use atomic operations only */
30 char *basedir;
31 MonoAssemblyName aname;
32 MonoImage *image;
33 GSList *friend_assembly_names; /* Computed by mono_assembly_load_friends () */
34 guint8 friend_assembly_names_inited;
35 guint8 in_gac;
36 guint8 dynamic;
37 guint8 corlib_internal;
38 gboolean ref_only;
39 /* security manager flags (one bit is for lazy initialization) */
40 guint32 ecma:2; /* Has the ECMA key */
41 guint32 aptc:2; /* Has the [AllowPartiallyTrustedCallers] attributes */
42 guint32 fulltrust:2; /* Has FullTrust permission */
43 guint32 unmanaged:2; /* Has SecurityPermissionFlag.UnmanagedCode permission */
44 guint32 skipverification:2; /* Has SecurityPermissionFlag.SkipVerification permission */
47 typedef struct {
48 const char* data;
49 guint32 size;
50 } MonoStreamHeader;
52 struct _MonoTableInfo {
53 const char *base;
54 guint rows : 24;
55 guint row_size : 8;
58 * Tables contain up to 9 columns and the possible sizes of the
59 * fields in the documentation are 1, 2 and 4 bytes. So we
60 * can encode in 2 bits the size.
62 * A 32 bit value can encode the resulting size
64 * The top eight bits encode the number of columns in the table.
65 * we only need 4, but 8 is aligned no shift required.
67 guint32 size_bitfield;
70 #define REFERENCE_MISSING ((gpointer) -1)
72 typedef struct _MonoDllMap MonoDllMap;
74 struct _MonoImage {
76 * The number of assemblies which reference this MonoImage though their 'image'
77 * field plus the number of images which reference this MonoImage through their
78 * 'modules' field, plus the number of threads holding temporary references to
79 * this image between calls of mono_image_open () and mono_image_close ().
81 int ref_count;
82 void *raw_data_handle;
83 char *raw_data;
84 guint32 raw_data_len;
85 guint8 raw_buffer_used : 1;
86 guint8 raw_data_allocated : 1;
88 #ifdef PLATFORM_WIN32
89 /* Module was loaded using LoadLibrary. */
90 guint8 is_module_handle : 1;
92 /* Module entry point is _CorDllMain. */
93 guint8 has_entry_point : 1;
94 #endif
96 /* Whenever this is a dynamically emitted module */
97 guint8 dynamic : 1;
99 /* Whenever this is a reflection only image */
100 guint8 ref_only : 1;
102 /* Whenever this image contains uncompressed metadata */
103 guint8 uncompressed_metadata : 1;
105 guint8 checked_module_cctor : 1;
106 guint8 has_module_cctor : 1;
108 guint8 idx_string_wide : 1;
109 guint8 idx_guid_wide : 1;
110 guint8 idx_blob_wide : 1;
112 /* Whenever this image is considered as platform code for the CoreCLR security model */
113 guint8 core_clr_platform_code : 1;
115 char *name;
116 const char *assembly_name;
117 const char *module_name;
118 char *version;
119 gint16 md_version_major, md_version_minor;
120 char *guid;
121 void *image_info;
122 MonoMemPool *mempool; /*protected by the image lock*/
124 char *raw_metadata;
126 MonoStreamHeader heap_strings;
127 MonoStreamHeader heap_us;
128 MonoStreamHeader heap_blob;
129 MonoStreamHeader heap_guid;
130 MonoStreamHeader heap_tables;
132 const char *tables_base;
134 /**/
135 MonoTableInfo tables [MONO_TABLE_NUM];
138 * references is initialized only by using the mono_assembly_open
139 * function, and not by using the lowlevel mono_image_open.
141 * It is NULL terminated.
143 MonoAssembly **references;
145 MonoImage **modules;
146 guint32 module_count;
147 gboolean *modules_loaded;
149 MonoImage **files;
151 gpointer aot_module;
154 * The Assembly this image was loaded from.
156 MonoAssembly *assembly;
159 * Indexed by method tokens and typedef tokens.
161 MonoValueHashTable *method_cache; /*protected by the image lock*/
162 MonoInternalHashTable class_cache;
164 /* Indexed by memberref + methodspec tokens */
165 GHashTable *methodref_cache; /*protected by the image lock*/
168 * Indexed by fielddef and memberref tokens
170 GHashTable *field_cache;
172 /* indexed by typespec tokens. */
173 GHashTable *typespec_cache;
174 /* indexed by token */
175 GHashTable *memberref_signatures;
176 GHashTable *helper_signatures;
178 /* Indexed by blob heap indexes */
179 GHashTable *method_signatures;
182 * Indexes namespaces to hash tables that map class name to typedef token.
184 GHashTable *name_cache;
187 * Indexed by MonoClass
189 GHashTable *array_cache;
190 GHashTable *ptr_cache;
192 GHashTable *szarray_cache;
193 /* This has a separate lock to improve scalability */
194 CRITICAL_SECTION szarray_cache_lock;
197 * indexed by MonoMethodSignature
199 GHashTable *delegate_begin_invoke_cache;
200 GHashTable *delegate_end_invoke_cache;
201 GHashTable *delegate_invoke_cache;
202 GHashTable *runtime_invoke_cache;
205 * indexed by SignatureMethodPair
207 GHashTable *delegate_abstract_invoke_cache;
210 * indexed by MonoMethod pointers
212 GHashTable *runtime_invoke_direct_cache;
213 GHashTable *runtime_invoke_vcall_cache;
214 GHashTable *managed_wrapper_cache;
215 GHashTable *native_wrapper_cache;
216 GHashTable *native_wrapper_aot_cache;
217 GHashTable *remoting_invoke_cache;
218 GHashTable *synchronized_cache;
219 GHashTable *unbox_wrapper_cache;
220 GHashTable *cominterop_invoke_cache;
221 GHashTable *cominterop_wrapper_cache;
222 GHashTable *static_rgctx_invoke_cache; /* LOCKING: marshal lock */
223 GHashTable *thunk_invoke_cache;
226 * indexed by MonoClass pointers
228 GHashTable *ldfld_wrapper_cache;
229 GHashTable *ldflda_wrapper_cache;
230 GHashTable *stfld_wrapper_cache;
231 GHashTable *isinst_cache;
232 GHashTable *castclass_cache;
233 GHashTable *proxy_isinst_cache;
234 GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
237 * indexed by token and MonoGenericContext pointer
239 GHashTable *generic_class_cache;
241 /* Contains rarely used fields of runtime structures belonging to this image */
242 MonoPropertyHash *property_hash;
244 void *reflection_info;
247 * user_info is a public field and is not touched by the
248 * metadata engine
250 void *user_info;
252 /* dll map entries */
253 MonoDllMap *dll_map;
255 /* interfaces IDs from this image */
256 MonoBitSet *interface_bitset;
259 * No other runtime locks must be taken while holding this lock.
260 * It's meant to be used only to mutate and query structures part of this image.
262 CRITICAL_SECTION lock;
265 enum {
266 MONO_SECTION_TEXT,
267 MONO_SECTION_RSRC,
268 MONO_SECTION_RELOC,
269 MONO_SECTION_MAX
272 typedef struct {
273 GHashTable *hash;
274 char *data;
275 guint32 alloc_size; /* malloced bytes */
276 guint32 index;
277 guint32 offset; /* from start of metadata */
278 } MonoDynamicStream;
280 typedef struct {
281 guint32 alloc_rows;
282 guint32 rows;
283 guint8 row_size; /* calculated later with column_sizes */
284 guint8 columns;
285 guint32 next_idx;
286 guint32 *values; /* rows * columns */
287 } MonoDynamicTable;
289 struct _MonoDynamicAssembly {
290 MonoAssembly assembly;
291 char *strong_name;
292 guint32 strong_name_size;
293 guint8 run;
294 guint8 save;
297 struct _MonoDynamicImage {
298 MonoImage image;
299 guint32 meta_size;
300 guint32 text_rva;
301 guint32 metadata_rva;
302 guint32 image_base;
303 guint32 cli_header_offset;
304 guint32 iat_offset;
305 guint32 idt_offset;
306 guint32 ilt_offset;
307 guint32 imp_names_offset;
308 struct {
309 guint32 rva;
310 guint32 size;
311 guint32 offset;
312 guint32 attrs;
313 } sections [MONO_SECTION_MAX];
314 GHashTable *typespec;
315 GHashTable *typeref;
316 GHashTable *handleref;
317 MonoGHashTable *tokens;
318 GHashTable *blob_cache;
319 GHashTable *standalonesig_cache;
320 GList *array_methods;
321 GPtrArray *gen_params;
322 MonoGHashTable *token_fixups;
323 GHashTable *method_to_table_idx;
324 GHashTable *field_to_table_idx;
325 GHashTable *method_aux_hash;
326 MonoGHashTable *generic_def_objects;
327 MonoGHashTable *methodspec;
328 gboolean run;
329 gboolean save;
330 gboolean initial_image;
331 guint32 pe_kind, machine;
332 char *strong_name;
333 guint32 strong_name_size;
334 char *win32_res;
335 guint32 win32_res_size;
336 guint8 *public_key;
337 int public_key_len;
338 MonoDynamicStream sheap;
339 MonoDynamicStream code; /* used to store method headers and bytecode */
340 MonoDynamicStream resources; /* managed embedded resources */
341 MonoDynamicStream us;
342 MonoDynamicStream blob;
343 MonoDynamicStream tstream;
344 MonoDynamicStream guid;
345 MonoDynamicTable tables [MONO_TABLE_NUM];
348 /* Contains information about assembly binding */
349 typedef struct _MonoAssemblyBindingInfo {
350 char *name;
351 char *culture;
352 guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
353 int major;
354 int minor;
355 AssemblyVersionSet old_version_bottom;
356 AssemblyVersionSet old_version_top;
357 AssemblyVersionSet new_version;
358 guint has_old_version_bottom : 1;
359 guint has_old_version_top : 1;
360 guint has_new_version : 1;
361 guint is_valid : 1;
362 } MonoAssemblyBindingInfo;
364 struct _MonoMethodHeader {
365 guint32 code_size;
366 const unsigned char *code;
367 guint16 max_stack;
368 unsigned int num_clauses : 15;
369 /* if num_locals != 0, then the following apply: */
370 unsigned int init_locals : 1;
371 guint16 num_locals;
372 MonoExceptionClause *clauses;
373 MonoType *locals [MONO_ZERO_LEN_ARRAY];
376 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
377 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
379 void
380 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
382 gpointer
383 mono_image_alloc (MonoImage *image, guint size) MONO_INTERNAL;
385 gpointer
386 mono_image_alloc0 (MonoImage *image, guint size) MONO_INTERNAL;
388 char*
389 mono_image_strdup (MonoImage *image, const char *s) MONO_INTERNAL;
391 GList*
392 g_list_prepend_image (MonoImage *image, GList *list, gpointer data) MONO_INTERNAL;
394 GSList*
395 g_slist_append_image (MonoImage *image, GSList *list, gpointer data) MONO_INTERNAL;
397 void
398 mono_image_lock (MonoImage *image) MONO_INTERNAL;
400 void
401 mono_image_unlock (MonoImage *image) MONO_INTERNAL;
403 gpointer
404 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property) MONO_INTERNAL;
406 void
407 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value) MONO_INTERNAL;
409 void
410 mono_image_property_remove (MonoImage *image, gpointer subject) MONO_INTERNAL;
413 MonoType*
414 mono_metadata_get_shared_type (MonoType *type) MONO_INTERNAL;
416 void
417 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
419 void
420 mono_metadata_cleanup (void);
422 const char * mono_meta_table_name (int table) MONO_INTERNAL;
423 void mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
425 gboolean
426 mono_metadata_interfaces_from_typedef_full (MonoImage *image,
427 guint32 table_index,
428 MonoClass ***interfaces,
429 guint *count,
430 MonoGenericContext *context) MONO_INTERNAL;
432 MonoArrayType *
433 mono_metadata_parse_array_full (MonoImage *image,
434 MonoGenericContainer *container,
435 const char *ptr,
436 const char **rptr) MONO_INTERNAL;
438 MonoType *
439 mono_metadata_parse_type_full (MonoImage *image,
440 MonoGenericContainer *container,
441 MonoParseTypeMode mode,
442 short opt_attrs,
443 const char *ptr,
444 const char **rptr);
446 MonoMethodSignature *
447 mono_metadata_parse_signature_full (MonoImage *image,
448 MonoGenericContainer *generic_container,
449 guint32 token) MONO_INTERNAL;
451 MonoMethodSignature *
452 mono_metadata_parse_method_signature_full (MonoImage *image,
453 MonoGenericContainer *generic_container,
454 int def,
455 const char *ptr,
456 const char **rptr);
458 MonoMethodHeader *
459 mono_metadata_parse_mh_full (MonoImage *image,
460 MonoGenericContainer *container,
461 const char *ptr);
463 int* mono_metadata_get_param_attrs (MonoImage *m, int def);
465 guint
466 mono_metadata_generic_context_hash (const MonoGenericContext *context) MONO_INTERNAL;
468 gboolean
469 mono_metadata_generic_context_equal (const MonoGenericContext *g1,
470 const MonoGenericContext *g2) MONO_INTERNAL;
472 MonoGenericInst *
473 mono_metadata_parse_generic_inst (MonoImage *image,
474 MonoGenericContainer *container,
475 int count,
476 const char *ptr,
477 const char **rptr) MONO_INTERNAL;
479 MonoGenericInst *
480 mono_metadata_get_generic_inst (int type_argc,
481 MonoType **type_argv) MONO_INTERNAL;
483 MonoGenericClass *
484 mono_metadata_lookup_generic_class (MonoClass *gclass,
485 MonoGenericInst *inst,
486 gboolean is_dynamic) MONO_INTERNAL;
488 MonoGenericInst *
489 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst,
490 MonoGenericContext *context) MONO_INTERNAL;
492 void mono_dynamic_stream_reset (MonoDynamicStream* stream) MONO_INTERNAL;
493 void mono_assembly_addref (MonoAssembly *assembly) MONO_INTERNAL;
494 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
495 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass) MONO_INTERNAL;
497 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
499 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
501 gboolean
502 mono_assembly_name_parse_full (const char *name,
503 MonoAssemblyName *aname,
504 gboolean save_public_key,
505 gboolean *is_version_defined,
506 gboolean *is_token_defined) MONO_INTERNAL;
508 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
510 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
513 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original) MONO_INTERNAL;
514 MonoMethodSignature *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig) MONO_INTERNAL;
515 MonoMethodSignature *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig) MONO_INTERNAL;
517 MonoGenericInst *
518 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
521 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
523 gboolean
524 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
526 MonoMarshalSpec *
527 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr) MONO_INTERNAL;
529 guint mono_metadata_generic_inst_hash (gconstpointer data) MONO_INTERNAL;
530 gboolean mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
532 void
533 mono_metadata_field_info_with_mempool (
534 MonoImage *meta,
535 guint32 table_index,
536 guint32 *offset,
537 guint32 *rva,
538 MonoMarshalSpec **marshal_spec);
540 MonoClassField*
541 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field) MONO_INTERNAL;
543 MonoEvent*
544 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event) MONO_INTERNAL;
546 MonoProperty*
547 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property) MONO_INTERNAL;
549 guint32
550 mono_metadata_signature_size (MonoMethodSignature *sig) MONO_INTERNAL;
552 gboolean mono_image_load_pe_data (MonoImage *image) MONO_INTERNAL;
554 gboolean mono_image_load_cli_data (MonoImage *image) MONO_INTERNAL;
556 MonoImage *mono_image_open_raw (const char *fname, MonoImageOpenStatus *status) MONO_INTERNAL;
558 #endif /* __MONO_METADATA_INTERNALS_H__ */