2003-12-13 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / image.h
1 #ifndef _MONONET_METADATA_IMAGE_H_ 
2 #define _MONONET_METADATA_IMAGE_H_
3
4 #include <stdio.h>
5 #include <glib.h>
6 #include <gmodule.h>
7
8 typedef struct _MonoImage MonoImage;
9
10 typedef struct {
11         const char *name;
12         const char *culture;
13         const char *hash_value;
14         const guint8* public_key;
15         guint32 hash_alg;
16         guint32 hash_len;
17         guint32 flags;
18         guint16 major, minor, build, revision;
19 } MonoAssemblyName;
20
21 typedef struct {
22         int   ref_count;
23         char *basedir;
24         MonoAssemblyName aname;
25         GModule *aot_module;
26         MonoImage *image;
27         /* Load files here */
28         gboolean dynamic;
29 } MonoAssembly;
30
31 typedef struct {
32         const char* data;
33         guint32  size;
34 } MonoStreamHeader;
35
36 typedef struct {
37         guint32   rows, row_size;
38         const char *base;
39
40         /*
41          * Tables contain up to 9 columns and the possible sizes of the
42          * fields in the documentation are 1, 2 and 4 bytes.  So we
43          * can encode in 2 bits the size.
44          *
45          * A 32 bit value can encode the resulting size
46          *
47          * The top eight bits encode the number of columns in the table.
48          * we only need 4, but 8 is aligned no shift required. 
49          */
50         guint32   size_bitfield;
51 } MonoTableInfo;
52
53 struct _MonoImage {
54         int   ref_count;
55         FILE *f;
56         /* if f is NULL the image was loaded from raw data */
57         char *raw_data;
58         guint32 raw_data_len;
59         gboolean raw_data_allocated;
60         char *name;
61         const char *assembly_name;
62         const char *module_name;
63         const char *version;
64         char *guid;
65         void *image_info;
66
67         char                *raw_metadata;
68                             
69         gboolean             idx_string_wide, idx_guid_wide, idx_blob_wide;
70                             
71         MonoStreamHeader     heap_strings;
72         MonoStreamHeader     heap_us;
73         MonoStreamHeader     heap_blob;
74         MonoStreamHeader     heap_guid;
75         MonoStreamHeader     heap_tables;
76                             
77         const char          *tables_base;
78
79         MonoTableInfo        tables [64];
80
81         /*
82          * references is initialized only by using the mono_assembly_open
83          * function, and not by using the lowlevel mono_image_open.
84          *
85          * It is NULL terminated.
86          */
87         MonoAssembly **references;
88
89         MonoImage **modules;
90
91         MonoImage **files;
92
93         /*
94          * The Assembly this image was loaded from.
95          */
96         MonoAssembly *assembly;
97
98         /*
99          * Indexed by method tokens and typedef tokens.
100          */
101         GHashTable *method_cache;
102         GHashTable *class_cache;
103         /*
104          * Indexed by fielddef and memberref tokens
105          */
106         GHashTable *field_cache;
107
108         /* indexed by a generic type instantiation */
109         GHashTable *generics_cache;
110         /* indexed by typespec tokens. */
111         GHashTable *typespec_cache;
112         /*
113          * Indexes namespaces to hash tables that map class name to typedef token.
114          */
115         GHashTable *name_cache;
116
117         /*
118          * Indexed by ((rank << 24) | (typedef & 0xffffff)), which limits us to a
119          * maximal rank of 255
120          */
121         GHashTable *array_cache;
122
123         /*
124          * indexed by MonoMethodSignature 
125          */
126         GHashTable *delegate_begin_invoke_cache;
127         GHashTable *delegate_end_invoke_cache;
128         GHashTable *delegate_invoke_cache;
129
130         /*
131          * indexed by MonoMethod pointers 
132          */
133         GHashTable *runtime_invoke_cache;
134         GHashTable *managed_wrapper_cache;
135         GHashTable *native_wrapper_cache;
136         GHashTable *remoting_invoke_cache;
137         GHashTable *synchronized_cache;
138
139         void *reflection_info;
140
141         /*
142          * user_info is a public field and is not touched by the
143          * metadata engine
144          */
145         void *user_info;
146 };
147
148 typedef enum {
149         MONO_IMAGE_OK,
150         MONO_IMAGE_ERROR_ERRNO,
151         MONO_IMAGE_MISSING_ASSEMBLYREF,
152         MONO_IMAGE_IMAGE_INVALID
153 } MonoImageOpenStatus;
154
155 void          mono_images_init    (void);
156
157 MonoImage    *mono_image_open     (const char *fname,
158                                    MonoImageOpenStatus *status);
159 MonoImage    *mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy,
160                                          MonoImageOpenStatus *status);
161 MonoImage    *mono_image_loaded   (const char *name);
162 MonoImage    *mono_image_loaded_by_guid (const char *guid);
163 void          mono_image_init     (MonoImage *image);
164 void          mono_image_close    (MonoImage *image);
165 const char   *mono_image_strerror (MonoImageOpenStatus status);
166
167 int           mono_image_ensure_section     (MonoImage *image,
168                                              const char *section);
169 int           mono_image_ensure_section_idx (MonoImage *image,
170                                              int section);
171
172 guint32       mono_image_get_entry_point    (MonoImage *image);
173 const char   *mono_image_get_resource       (MonoImage *image, guint32 offset, guint32 *size);
174 MonoImage*    mono_image_load_file_for_image (MonoImage *image, int fileidx);
175
176 /* This actually returns a MonoPEResourceDataEntry *, but declaring it
177  * causes an include file loop.
178  */
179 gpointer      mono_image_lookup_resource (MonoImage *image, guint32 res_id,
180                                           guint32 lang_id, gunichar2 *name);
181
182 const char*   mono_image_get_public_key  (MonoImage *image, guint32 *size);
183 const char*   mono_image_get_strong_name (MonoImage *image, guint32 *size);
184 guint32       mono_image_strong_name_position (MonoImage *image, guint32 *size);
185 void          mono_image_add_to_name_cache (MonoImage *image, 
186                                                                                         const char *nspace, 
187                                                                                         const char *name, guint32 idx);
188
189 #endif