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