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