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