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