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