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