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