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