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