In metadata/
[mono.git] / mono / metadata / metadata-internals.h
1
2 #ifndef __MONO_METADATA_INTERNALS_H__
3 #define __MONO_METADATA_INTERNALS_H__
4
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"
15
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)
19
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.
28          */
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 */
45 };
46
47 typedef struct {
48         const char* data;
49         guint32  size;
50 } MonoStreamHeader;
51
52 struct _MonoTableInfo {
53         const char *base;
54         guint       rows     : 24;
55         guint       row_size : 8;
56
57         /*
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.
61          *
62          * A 32 bit value can encode the resulting size
63          *
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. 
66          */
67         guint32   size_bitfield;
68 };
69
70 #define REFERENCE_MISSING ((gpointer) -1)
71
72 typedef struct _MonoDllMap MonoDllMap;
73
74 struct _MonoImage {
75         /*
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 ().
80          */
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;
87
88 #ifdef PLATFORM_WIN32
89         /* Module was loaded using LoadLibrary. */
90         guint8 is_module_handle : 1;
91
92         /* Module entry point is _CorDllMain. */
93         guint8 has_entry_point : 1;
94 #endif
95
96         /* Whenever this is a dynamically emitted module */
97         guint8 dynamic : 1;
98
99         /* Whenever this is a reflection only image */
100         guint8 ref_only : 1;
101
102         /* Whenever this image contains uncompressed metadata */
103         guint8 uncompressed_metadata : 1;
104
105         guint8 checked_module_cctor : 1;
106         guint8 has_module_cctor : 1;
107
108         guint8 idx_string_wide : 1;
109         guint8 idx_guid_wide : 1;
110         guint8 idx_blob_wide : 1;
111                             
112         char *name;
113         const char *assembly_name;
114         const char *module_name;
115         char *version;
116         gint16 md_version_major, md_version_minor;
117         char *guid;
118         void *image_info;
119         MonoMemPool         *mempool; /*protected by the image lock*/
120
121         char                *raw_metadata;
122                             
123         MonoStreamHeader     heap_strings;
124         MonoStreamHeader     heap_us;
125         MonoStreamHeader     heap_blob;
126         MonoStreamHeader     heap_guid;
127         MonoStreamHeader     heap_tables;
128                             
129         const char          *tables_base;
130
131         /**/
132         MonoTableInfo        tables [MONO_TABLE_NUM];
133
134         /*
135          * references is initialized only by using the mono_assembly_open
136          * function, and not by using the lowlevel mono_image_open.
137          *
138          * It is NULL terminated.
139          */
140         MonoAssembly **references;
141
142         MonoImage **modules;
143         guint32 module_count;
144         gboolean *modules_loaded;
145
146         MonoImage **files;
147
148         gpointer aot_module;
149
150         /*
151          * The Assembly this image was loaded from.
152          */
153         MonoAssembly *assembly;
154
155         /*
156          * Indexed by method tokens and typedef tokens.
157          */
158         MonoValueHashTable *method_cache; /*protected by the image lock*/
159         MonoInternalHashTable class_cache;
160
161         /* Indexed by memberref + methodspec tokens */
162         GHashTable *methodref_cache; /*protected by the image lock*/
163
164         /*
165          * Indexed by fielddef and memberref tokens
166          */
167         GHashTable *field_cache;
168
169         /* indexed by typespec tokens. */
170         GHashTable *typespec_cache;
171         /* indexed by token */
172         GHashTable *memberref_signatures;
173         GHashTable *helper_signatures;
174
175         /* Indexed by blob heap indexes */
176         GHashTable *method_signatures;
177
178         /*
179          * Indexes namespaces to hash tables that map class name to typedef token.
180          */
181         GHashTable *name_cache;
182
183         /*
184          * Indexed by MonoClass
185          */
186         GHashTable *array_cache;
187         GHashTable *ptr_cache;
188
189         GHashTable *szarray_cache;
190         /* This has a separate lock to improve scalability */
191         CRITICAL_SECTION szarray_cache_lock;
192
193         /*
194          * indexed by MonoMethodSignature 
195          */
196         GHashTable *delegate_begin_invoke_cache;
197         GHashTable *delegate_end_invoke_cache;
198         GHashTable *delegate_invoke_cache;
199         GHashTable *runtime_invoke_cache;
200
201         /*
202          * indexed by SignatureMethodPair
203          */
204         GHashTable *delegate_abstract_invoke_cache;
205
206         /*
207          * indexed by MonoMethod pointers 
208          */
209         GHashTable *runtime_invoke_direct_cache;
210         GHashTable *runtime_invoke_vcall_cache;
211         GHashTable *managed_wrapper_cache;
212         GHashTable *native_wrapper_cache;
213         GHashTable *native_wrapper_aot_cache;
214         GHashTable *remoting_invoke_cache;
215         GHashTable *synchronized_cache;
216         GHashTable *unbox_wrapper_cache;
217         GHashTable *cominterop_invoke_cache;
218         GHashTable *cominterop_wrapper_cache;
219         GHashTable *static_rgctx_invoke_cache; /* LOCKING: marshal lock */
220         GHashTable *thunk_invoke_cache;
221
222         /*
223          * indexed by MonoClass pointers
224          */
225         GHashTable *ldfld_wrapper_cache;
226         GHashTable *ldflda_wrapper_cache;
227         GHashTable *stfld_wrapper_cache;
228         GHashTable *isinst_cache;
229         GHashTable *castclass_cache;
230         GHashTable *proxy_isinst_cache;
231         GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
232
233         /*
234          * indexed by token and MonoGenericContext pointer
235          */
236         GHashTable *generic_class_cache;
237
238         /* Contains rarely used fields of runtime structures belonging to this image */
239         MonoPropertyHash *property_hash;
240
241         void *reflection_info;
242
243         /*
244          * user_info is a public field and is not touched by the
245          * metadata engine
246          */
247         void *user_info;
248
249         /* dll map entries */
250         MonoDllMap *dll_map;
251
252         /* interfaces IDs from this image */
253         MonoBitSet *interface_bitset;
254
255         /*
256          * No other runtime locks must be taken while holding this lock.
257          * It's meant to be used only to mutate and query structures part of this image.
258          */
259         CRITICAL_SECTION    lock;
260 };
261
262 enum {
263         MONO_SECTION_TEXT,
264         MONO_SECTION_RSRC,
265         MONO_SECTION_RELOC,
266         MONO_SECTION_MAX
267 };
268
269 typedef struct {
270         GHashTable *hash;
271         char *data;
272         guint32 alloc_size; /* malloced bytes */
273         guint32 index;
274         guint32 offset; /* from start of metadata */
275 } MonoDynamicStream;
276
277 typedef struct {
278         guint32 alloc_rows;
279         guint32 rows;
280         guint8  row_size; /*  calculated later with column_sizes */
281         guint8  columns;
282         guint32 next_idx;
283         guint32 *values; /* rows * columns */
284 } MonoDynamicTable;
285
286 struct _MonoDynamicAssembly {
287         MonoAssembly assembly;
288         char *strong_name;
289         guint32 strong_name_size;
290         guint8 run;
291         guint8 save;
292 };
293
294 struct _MonoDynamicImage {
295         MonoImage image;
296         guint32 meta_size;
297         guint32 text_rva;
298         guint32 metadata_rva;
299         guint32 image_base;
300         guint32 cli_header_offset;
301         guint32 iat_offset;
302         guint32 idt_offset;
303         guint32 ilt_offset;
304         guint32 imp_names_offset;
305         struct {
306                 guint32 rva;
307                 guint32 size;
308                 guint32 offset;
309                 guint32 attrs;
310         } sections [MONO_SECTION_MAX];
311         GHashTable *typespec;
312         GHashTable *typeref;
313         GHashTable *handleref;
314         MonoGHashTable *tokens;
315         GHashTable *blob_cache;
316         GHashTable *standalonesig_cache;
317         GList *array_methods;
318         GPtrArray *gen_params;
319         MonoGHashTable *token_fixups;
320         GHashTable *method_to_table_idx;
321         GHashTable *field_to_table_idx;
322         GHashTable *method_aux_hash;
323         MonoGHashTable *generic_def_objects;
324         MonoGHashTable *methodspec;
325         gboolean run;
326         gboolean save;
327         gboolean initial_image;
328         guint32 pe_kind, machine;
329         char *strong_name;
330         guint32 strong_name_size;
331         char *win32_res;
332         guint32 win32_res_size;
333         guint8 *public_key;
334         int public_key_len;
335         MonoDynamicStream sheap;
336         MonoDynamicStream code; /* used to store method headers and bytecode */
337         MonoDynamicStream resources; /* managed embedded resources */
338         MonoDynamicStream us;
339         MonoDynamicStream blob;
340         MonoDynamicStream tstream;
341         MonoDynamicStream guid;
342         MonoDynamicTable tables [MONO_TABLE_NUM];
343 };
344
345 /* Contains information about assembly binding */
346 typedef struct _MonoAssemblyBindingInfo {
347         char *name;
348         char *culture;
349         guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
350         int major;
351         int minor;
352         AssemblyVersionSet old_version_bottom;
353         AssemblyVersionSet old_version_top;
354         AssemblyVersionSet new_version;
355         guint has_old_version_bottom : 1;
356         guint has_old_version_top : 1;
357         guint has_new_version : 1;
358         guint is_valid : 1;
359 } MonoAssemblyBindingInfo;
360
361 struct _MonoMethodHeader {
362         guint32      code_size;
363         const unsigned char  *code;
364         guint16      max_stack;
365         unsigned int num_clauses : 15;
366         /* if num_locals != 0, then the following apply: */
367         unsigned int init_locals : 1;
368         guint16      num_locals;
369         MonoExceptionClause *clauses;
370         MonoType    *locals [MONO_ZERO_LEN_ARRAY];
371 };
372
373 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
374 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
375
376 void
377 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
378
379 gpointer
380 mono_image_alloc  (MonoImage *image, guint size) MONO_INTERNAL;
381
382 gpointer
383 mono_image_alloc0 (MonoImage *image, guint size) MONO_INTERNAL;
384
385 char*
386 mono_image_strdup (MonoImage *image, const char *s) MONO_INTERNAL;
387
388 GList*
389 g_list_prepend_image (MonoImage *image, GList *list, gpointer data) MONO_INTERNAL;
390
391 GSList*
392 g_slist_append_image (MonoImage *image, GSList *list, gpointer data) MONO_INTERNAL;
393
394 void
395 mono_image_lock (MonoImage *image) MONO_INTERNAL;
396
397 void
398 mono_image_unlock (MonoImage *image) MONO_INTERNAL;
399
400 gpointer
401 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property) MONO_INTERNAL;
402
403 void
404 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value) MONO_INTERNAL;
405
406 void
407 mono_image_property_remove (MonoImage *image, gpointer subject) MONO_INTERNAL;
408
409
410 MonoType*
411 mono_metadata_get_shared_type (MonoType *type) MONO_INTERNAL;
412
413 void
414 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
415
416 void
417 mono_metadata_cleanup (void);
418
419 const char *   mono_meta_table_name              (int table) MONO_INTERNAL;
420 void           mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
421
422 gboolean
423 mono_metadata_interfaces_from_typedef_full  (MonoImage             *image,
424                                                                                          guint32                table_index,
425                                                                                          MonoClass           ***interfaces,
426                                                                                          guint                 *count,
427                                                                                          MonoGenericContext    *context) MONO_INTERNAL;
428
429 MonoArrayType *
430 mono_metadata_parse_array_full              (MonoImage             *image,
431                                              MonoGenericContainer  *container,
432                                              const char            *ptr,
433                                              const char           **rptr) MONO_INTERNAL;
434
435 MonoType *
436 mono_metadata_parse_type_full               (MonoImage             *image,
437                                              MonoGenericContainer  *container,
438                                              MonoParseTypeMode      mode,
439                                              short                  opt_attrs,
440                                              const char            *ptr,
441                                              const char           **rptr);
442
443 MonoMethodSignature *
444 mono_metadata_parse_signature_full          (MonoImage             *image,
445                                              MonoGenericContainer  *generic_container,
446                                              guint32                token) MONO_INTERNAL;
447
448 MonoMethodSignature *
449 mono_metadata_parse_method_signature_full   (MonoImage             *image,
450                                              MonoGenericContainer  *generic_container,
451                                              int                     def,
452                                              const char             *ptr,
453                                              const char            **rptr);
454
455 MonoMethodHeader *
456 mono_metadata_parse_mh_full                 (MonoImage             *image,
457                                              MonoGenericContainer  *container,
458                                              const char            *ptr);
459
460 int* mono_metadata_get_param_attrs          (MonoImage *m, int def);
461
462 guint
463 mono_metadata_generic_context_hash          (const MonoGenericContext *context) MONO_INTERNAL;
464
465 gboolean
466 mono_metadata_generic_context_equal         (const MonoGenericContext *g1,
467                                              const MonoGenericContext *g2) MONO_INTERNAL;
468
469 MonoGenericInst *
470 mono_metadata_parse_generic_inst            (MonoImage             *image,
471                                              MonoGenericContainer  *container,
472                                              int                    count,
473                                              const char            *ptr,
474                                              const char           **rptr) MONO_INTERNAL;
475
476 MonoGenericInst *
477 mono_metadata_get_generic_inst              (int                    type_argc,
478                                              MonoType             **type_argv) MONO_INTERNAL;
479
480 MonoGenericClass *
481 mono_metadata_lookup_generic_class          (MonoClass             *gclass,
482                                              MonoGenericInst       *inst,
483                                              gboolean               is_dynamic) MONO_INTERNAL;
484
485 MonoGenericInst *
486 mono_metadata_inflate_generic_inst          (MonoGenericInst       *ginst,
487                                              MonoGenericContext    *context) MONO_INTERNAL;
488
489 void mono_dynamic_stream_reset  (MonoDynamicStream* stream) MONO_INTERNAL;
490 void mono_assembly_addref       (MonoAssembly *assembly) MONO_INTERNAL;
491 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
492 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass) MONO_INTERNAL;
493
494 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
495
496 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
497
498 gboolean
499 mono_assembly_name_parse_full                (const char           *name,
500                                               MonoAssemblyName     *aname,
501                                               gboolean save_public_key,
502                                               gboolean *is_version_defined,
503                                                   gboolean *is_token_defined) MONO_INTERNAL;
504
505 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
506
507 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
508
509
510 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original) MONO_INTERNAL;
511 MonoMethodSignature  *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig) MONO_INTERNAL;
512 MonoMethodSignature  *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig) MONO_INTERNAL;
513
514 MonoGenericInst *
515 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
516
517 int
518 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
519
520 gboolean
521 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
522
523 MonoMarshalSpec *
524 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr) MONO_INTERNAL;
525
526 guint          mono_metadata_generic_inst_hash (gconstpointer data) MONO_INTERNAL;
527 gboolean       mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
528
529 void
530 mono_metadata_field_info_with_mempool (
531                                           MonoImage *meta, 
532                                       guint32       table_index,
533                                       guint32      *offset,
534                                       guint32      *rva,
535                                       MonoMarshalSpec **marshal_spec);
536
537 MonoClassField*
538 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field) MONO_INTERNAL;
539
540 MonoEvent*
541 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event) MONO_INTERNAL;
542
543 MonoProperty*
544 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property) MONO_INTERNAL;
545
546 guint32
547 mono_metadata_signature_size (MonoMethodSignature *sig) MONO_INTERNAL;
548
549 #endif /* __MONO_METADATA_INTERNALS_H__ */
550