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