2008-04-28 Mark Probst <mark.probst@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
14 struct _MonoAssembly {
15         /* 
16          * The number of appdomains which have this assembly loaded plus the number of 
17          * assemblies referencing this assembly through an entry in their image->references
18          * arrays. The later is needed because entries in the image->references array
19          * might point to assemblies which are only loaded in some appdomains, and without
20          * the additional reference, they can be freed at any time.
21          * The ref_count is initially 0.
22          */
23         int ref_count; /* use atomic operations only */
24         char *basedir;
25         MonoAssemblyName aname;
26         MonoDl *aot_module;
27         MonoImage *image;
28         GSList *friend_assembly_names;
29         guint8 in_gac;
30         guint8 dynamic;
31         guint8 corlib_internal;
32         gboolean ref_only;
33         /* security manager flags (one bit is for lazy initialization) */
34         guint32 ecma:2;         /* Has the ECMA key */
35         guint32 aptc:2;         /* Has the [AllowPartiallyTrustedCallers] attributes */
36         guint32 fulltrust:2;    /* Has FullTrust permission */
37         guint32 unmanaged:2;    /* Has SecurityPermissionFlag.UnmanagedCode permission */
38 };
39
40 typedef struct {
41         const char* data;
42         guint32  size;
43 } MonoStreamHeader;
44
45 struct _MonoTableInfo {
46         const char *base;
47         guint       rows     : 24;
48         guint       row_size : 8;
49
50         /*
51          * Tables contain up to 9 columns and the possible sizes of the
52          * fields in the documentation are 1, 2 and 4 bytes.  So we
53          * can encode in 2 bits the size.
54          *
55          * A 32 bit value can encode the resulting size
56          *
57          * The top eight bits encode the number of columns in the table.
58          * we only need 4, but 8 is aligned no shift required. 
59          */
60         guint32   size_bitfield;
61 };
62
63 #define REFERENCE_MISSING ((gpointer) -1)
64
65 typedef struct _MonoDllMap MonoDllMap;
66
67 struct _MonoImage {
68         /*
69          * The number of assemblies which reference this MonoImage though their 'image'
70          * field plus the number of images which reference this MonoImage through their 
71          * 'modules' field, plus the number of threads holding temporary references to
72          * this image between calls of mono_image_open () and mono_image_close ().
73          */
74         int   ref_count;
75         char *raw_data;
76         guint32 raw_data_len;
77         guint8 raw_buffer_used    : 1;
78         guint8 raw_data_allocated : 1;
79
80         /* Whenever this is a dynamically emitted module */
81         guint8 dynamic : 1;
82
83         /* Whenever this is a reflection only image */
84         guint8 ref_only : 1;
85
86         /* Whenever this image contains uncompressed metadata */
87         guint8 uncompressed_metadata : 1;
88
89         guint8 checked_module_cctor : 1;
90         guint8 has_module_cctor : 1;
91
92         guint8 idx_string_wide : 1;
93         guint8 idx_guid_wide : 1;
94         guint8 idx_blob_wide : 1;
95                             
96         char *name;
97         const char *assembly_name;
98         const char *module_name;
99         char *version;
100         gint16 md_version_major, md_version_minor;
101         char *guid;
102         void *image_info;
103         MonoMemPool         *mempool;
104
105         char                *raw_metadata;
106                             
107         MonoStreamHeader     heap_strings;
108         MonoStreamHeader     heap_us;
109         MonoStreamHeader     heap_blob;
110         MonoStreamHeader     heap_guid;
111         MonoStreamHeader     heap_tables;
112                             
113         const char          *tables_base;
114
115         /**/
116         MonoTableInfo        tables [MONO_TABLE_NUM];
117
118         /*
119          * references is initialized only by using the mono_assembly_open
120          * function, and not by using the lowlevel mono_image_open.
121          *
122          * It is NULL terminated.
123          */
124         MonoAssembly **references;
125
126         MonoImage **modules;
127         guint32 module_count;
128         gboolean *modules_loaded;
129
130         MonoImage **files;
131
132         /*
133          * The Assembly this image was loaded from.
134          */
135         MonoAssembly *assembly;
136
137         /*
138          * Indexed by method tokens and typedef tokens.
139          */
140         GHashTable *method_cache;
141         MonoInternalHashTable class_cache;
142         /*
143          * Indexed by fielddef and memberref tokens
144          */
145         GHashTable *field_cache;
146
147         /* indexed by typespec tokens. */
148         GHashTable *typespec_cache;
149         /* indexed by token */
150         GHashTable *memberref_signatures;
151         GHashTable *helper_signatures;
152
153         /* Indexed by blob heap indexes */
154         GHashTable *method_signatures;
155
156         /*
157          * Indexes namespaces to hash tables that map class name to typedef token.
158          */
159         GHashTable *name_cache;
160
161         /*
162          * Indexed by MonoClass
163          */
164         GHashTable *array_cache;
165         GHashTable *ptr_cache;
166
167         /*
168          * indexed by MonoMethodSignature 
169          */
170         GHashTable *delegate_begin_invoke_cache;
171         GHashTable *delegate_end_invoke_cache;
172         GHashTable *delegate_invoke_cache;
173         GHashTable *runtime_invoke_cache;
174
175         /*
176          * indexed by SignatureMethodPair
177          */
178         GHashTable *delegate_abstract_invoke_cache;
179
180         /*
181          * indexed by MonoMethod pointers 
182          */
183         GHashTable *runtime_invoke_direct_cache;
184         GHashTable *managed_wrapper_cache;
185         GHashTable *native_wrapper_cache;
186         GHashTable *remoting_invoke_cache;
187         GHashTable *synchronized_cache;
188         GHashTable *unbox_wrapper_cache;
189         GHashTable *cominterop_invoke_cache;
190         GHashTable *cominterop_wrapper_cache;
191         GHashTable *static_rgctx_invoke_cache; /* LOCKING: marshal lock */
192
193         /*
194          * indexed by MonoClass pointers
195          */
196         GHashTable *ldfld_wrapper_cache;
197         GHashTable *ldflda_wrapper_cache;
198         GHashTable *stfld_wrapper_cache;
199         GHashTable *isinst_cache;
200         GHashTable *castclass_cache;
201         GHashTable *proxy_isinst_cache;
202         GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
203
204         /*
205          * indexed by token and MonoGenericContext pointer
206          */
207         GHashTable *generic_class_cache;
208
209         void *reflection_info;
210
211         /*
212          * user_info is a public field and is not touched by the
213          * metadata engine
214          */
215         void *user_info;
216
217         /* dll map entries */
218         MonoDllMap *dll_map;
219
220         /* interfaces IDs from this image */
221         MonoBitSet *interface_bitset;
222 };
223
224 enum {
225         MONO_SECTION_TEXT,
226         MONO_SECTION_RSRC,
227         MONO_SECTION_RELOC,
228         MONO_SECTION_MAX
229 };
230
231 typedef struct {
232         GHashTable *hash;
233         char *data;
234         guint32 alloc_size; /* malloced bytes */
235         guint32 index;
236         guint32 offset; /* from start of metadata */
237 } MonoDynamicStream;
238
239 typedef struct {
240         guint32 alloc_rows;
241         guint32 rows;
242         guint8  row_size; /*  calculated later with column_sizes */
243         guint8  columns;
244         guint32 next_idx;
245         guint32 *values; /* rows * columns */
246 } MonoDynamicTable;
247
248 struct _MonoDynamicAssembly {
249         MonoAssembly assembly;
250         char *strong_name;
251         guint32 strong_name_size;
252         guint8 run;
253         guint8 save;
254 };
255
256 struct _MonoDynamicImage {
257         MonoImage image;
258         guint32 meta_size;
259         guint32 text_rva;
260         guint32 metadata_rva;
261         guint32 image_base;
262         guint32 cli_header_offset;
263         guint32 iat_offset;
264         guint32 idt_offset;
265         guint32 ilt_offset;
266         guint32 imp_names_offset;
267         struct {
268                 guint32 rva;
269                 guint32 size;
270                 guint32 offset;
271                 guint32 attrs;
272         } sections [MONO_SECTION_MAX];
273         GHashTable *typespec;
274         GHashTable *typeref;
275         GHashTable *handleref;
276         MonoGHashTable *tokens;
277         GHashTable *blob_cache;
278         GList *array_methods;
279         GPtrArray *gen_params;
280         MonoGHashTable *token_fixups;
281         GHashTable *method_to_table_idx;
282         GHashTable *field_to_table_idx;
283         GHashTable *method_aux_hash;
284         MonoGHashTable *generic_def_objects;
285         gboolean run;
286         gboolean save;
287         gboolean initial_image;
288         guint32 pe_kind, machine;
289         char *strong_name;
290         guint32 strong_name_size;
291         char *win32_res;
292         guint32 win32_res_size;
293         guint8 *public_key;
294         int public_key_len;
295         MonoDynamicStream sheap;
296         MonoDynamicStream code; /* used to store method headers and bytecode */
297         MonoDynamicStream resources; /* managed embedded resources */
298         MonoDynamicStream us;
299         MonoDynamicStream blob;
300         MonoDynamicStream tstream;
301         MonoDynamicStream guid;
302         MonoDynamicTable tables [MONO_TABLE_NUM];
303 };
304
305 /* Contains information about assembly binding */
306 typedef struct _MonoAssemblyBindingInfo {
307         char *name;
308         char *culture;
309         guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
310         int major;
311         int minor;
312         AssemblyVersionSet old_version_bottom;
313         AssemblyVersionSet old_version_top;
314         AssemblyVersionSet new_version;
315         guint has_old_version_bottom : 1;
316         guint has_old_version_top : 1;
317         guint has_new_version : 1;
318         guint is_valid : 1;
319 } MonoAssemblyBindingInfo;
320
321 struct _MonoMethodHeader {
322         guint32      code_size;
323         const unsigned char  *code;
324         guint16      max_stack;
325         unsigned int num_clauses : 15;
326         /* if num_locals != 0, then the following apply: */
327         unsigned int init_locals : 1;
328         guint16      num_locals;
329         MonoExceptionClause *clauses;
330         MonoType    *locals [MONO_ZERO_LEN_ARRAY];
331 };
332
333 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
334 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
335
336 void
337 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
338
339 void
340 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
341
342 void
343 mono_metadata_cleanup (void);
344
345 const char *   mono_meta_table_name              (int table) MONO_INTERNAL;
346 void           mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
347
348 gboolean
349 mono_metadata_interfaces_from_typedef_full  (MonoImage             *image,
350                                                                                          guint32                table_index,
351                                                                                          MonoClass           ***interfaces,
352                                                                                          guint                 *count,
353                                                                                          MonoGenericContext    *context) MONO_INTERNAL;
354
355 MonoArrayType *
356 mono_metadata_parse_array_full              (MonoImage             *image,
357                                              MonoGenericContainer  *container,
358                                              const char            *ptr,
359                                              const char           **rptr) MONO_INTERNAL;
360
361 MonoType *
362 mono_metadata_parse_type_full               (MonoImage             *image,
363                                              MonoGenericContainer  *container,
364                                              MonoParseTypeMode      mode,
365                                              short                  opt_attrs,
366                                              const char            *ptr,
367                                              const char           **rptr);
368
369 MonoMethodSignature *
370 mono_metadata_parse_signature_full          (MonoImage             *image,
371                                              MonoGenericContainer  *generic_container,
372                                              guint32                token) MONO_INTERNAL;
373
374 MonoMethodSignature *
375 mono_metadata_parse_method_signature_full   (MonoImage             *image,
376                                              MonoGenericContainer  *generic_container,
377                                              int                     def,
378                                              const char             *ptr,
379                                              const char            **rptr);
380
381 MonoMethodHeader *
382 mono_metadata_parse_mh_full                 (MonoImage             *image,
383                                              MonoGenericContainer  *container,
384                                              const char            *ptr);
385
386 int* mono_metadata_get_param_attrs          (MonoImage *m, int def);
387
388 guint
389 mono_metadata_generic_context_hash          (const MonoGenericContext *context) MONO_INTERNAL;
390
391 gboolean
392 mono_metadata_generic_context_equal         (const MonoGenericContext *g1,
393                                              const MonoGenericContext *g2) MONO_INTERNAL;
394
395 MonoGenericInst *
396 mono_metadata_parse_generic_inst            (MonoImage             *image,
397                                              MonoGenericContainer  *container,
398                                              int                    count,
399                                              const char            *ptr,
400                                              const char           **rptr) MONO_INTERNAL;
401
402 MonoGenericInst *
403 mono_metadata_get_generic_inst              (int                    type_argc,
404                                              MonoType             **type_argv) MONO_INTERNAL;
405
406 MonoGenericClass *
407 mono_metadata_lookup_generic_class          (MonoClass             *gclass,
408                                              MonoGenericInst       *inst,
409                                              gboolean               is_dynamic) MONO_INTERNAL;
410
411 MonoGenericInst *
412 mono_metadata_inflate_generic_inst          (MonoGenericInst       *ginst,
413                                              MonoGenericContext    *context) MONO_INTERNAL;
414
415 void mono_dynamic_stream_reset  (MonoDynamicStream* stream) MONO_INTERNAL;
416 void mono_assembly_addref       (MonoAssembly *assembly) MONO_INTERNAL;
417 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
418
419 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
420
421 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
422
423 gboolean
424 mono_assembly_name_parse_full                (const char           *name,
425                                               MonoAssemblyName     *aname,
426                                               gboolean save_public_key,
427                                               gboolean *is_version_defined,
428                                                   gboolean *is_token_defined) MONO_INTERNAL;
429
430 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
431
432 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
433
434
435 MonoType *mono_metadata_type_dup (MonoMemPool *mp, const MonoType *original) MONO_INTERNAL;
436
437 MonoGenericInst *
438 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
439
440 int
441 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
442
443 gboolean
444 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
445
446
447 #endif /* __MONO_METADATA_INTERNALS_H__ */
448