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