This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / metadata / image.c
1 /*
2  * image.c: Routines for manipulating an image stored in an
3  * extended PE/COFF file.
4  * 
5  * Authors:
6  *   Miguel de Icaza (miguel@ximian.com)
7  *   Paolo Molaro (lupus@ximian.com)
8  *
9  * (C) 2001-2003 Ximian, Inc.  http://www.ximian.com
10  *
11  */
12 #include <config.h>
13 #include <stdio.h>
14 #include <glib.h>
15 #include <errno.h>
16 #include <time.h>
17 #include <string.h>
18 #include "image.h"
19 #include "cil-coff.h"
20 #include "rawbuffer.h"
21 #include "mono-endian.h"
22 #include "private.h"
23 #include "tabledefs.h"
24 #include "tokentype.h"
25 #include "metadata-internals.h"
26 #include <mono/io-layer/io-layer.h>
27
28 #define INVALID_ADDRESS 0xffffffff
29
30 /*
31  * Keeps track of the various assemblies loaded
32  */
33 static GHashTable *loaded_images_hash;
34 static GHashTable *loaded_images_guid_hash;
35
36 static CRITICAL_SECTION images_mutex;
37
38 guint32
39 mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
40 {
41         const int top = iinfo->cli_section_count;
42         MonoSectionTable *tables = iinfo->cli_section_tables;
43         int i;
44         
45         for (i = 0; i < top; i++){
46                 if ((addr >= tables->st_virtual_address) &&
47                     (addr < tables->st_virtual_address + tables->st_raw_data_size)){
48                         return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
49                 }
50                 tables++;
51         }
52         return INVALID_ADDRESS;
53 }
54
55 char *
56 mono_cli_rva_map (MonoCLIImageInfo *iinfo, guint32 addr)
57 {
58         const int top = iinfo->cli_section_count;
59         MonoSectionTable *tables = iinfo->cli_section_tables;
60         int i;
61         
62         for (i = 0; i < top; i++){
63                 if ((addr >= tables->st_virtual_address) &&
64                     (addr < tables->st_virtual_address + tables->st_raw_data_size)){
65                         return (char*)iinfo->cli_sections [i] +
66                                 (addr - tables->st_virtual_address);
67                 }
68                 tables++;
69         }
70         return NULL;
71 }
72
73 /**
74  * mono_images_init:
75  *
76  *  Initialize the global variables used by this module.
77  */
78 void
79 mono_images_init (void)
80 {
81         InitializeCriticalSection (&images_mutex);
82
83         loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
84         loaded_images_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
85 }
86
87 /**
88  * mono_image_ensure_section_idx:
89  * @image: The image we are operating on
90  * @section: section number that we will load/map into memory
91  *
92  * This routine makes sure that we have an in-memory copy of
93  * an image section (.text, .rsrc, .data).
94  *
95  * Returns: TRUE on success
96  */
97 int
98 mono_image_ensure_section_idx (MonoImage *image, int section)
99 {
100         MonoCLIImageInfo *iinfo = image->image_info;
101         MonoSectionTable *sect;
102         gboolean writable;
103         
104         g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
105
106         if (iinfo->cli_sections [section] != NULL)
107                 return TRUE;
108
109         sect = &iinfo->cli_section_tables [section];
110         
111         writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
112
113         if (!image->f) {
114                 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
115                         return FALSE;
116                 /* FIXME: we ignore the writable flag since we don't patch the binary */
117                 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
118                 return TRUE;
119         }
120         iinfo->cli_sections [section] = mono_raw_buffer_load (
121                 fileno (image->f), writable,
122                 sect->st_raw_data_ptr, sect->st_raw_data_size);
123
124         if (iinfo->cli_sections [section] == NULL)
125                 return FALSE;
126
127         return TRUE;
128 }
129
130 /**
131  * mono_image_ensure_section:
132  * @image: The image we are operating on
133  * @section: section name that we will load/map into memory
134  *
135  * This routine makes sure that we have an in-memory copy of
136  * an image section (.text, .rsrc, .data).
137  *
138  * Returns: TRUE on success
139  */
140 int
141 mono_image_ensure_section (MonoImage *image, const char *section)
142 {
143         MonoCLIImageInfo *ii = image->image_info;
144         int i;
145         
146         for (i = 0; i < ii->cli_section_count; i++){
147                 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
148                         continue;
149                 
150                 return mono_image_ensure_section_idx (image, i);
151         }
152         return FALSE;
153 }
154
155 static int
156 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
157 {
158         const int top = iinfo->cli_header.coff.coff_sections;
159         int i;
160
161         iinfo->cli_section_count = top;
162         iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
163         iinfo->cli_sections = g_new0 (void *, top);
164         
165         for (i = 0; i < top; i++){
166                 MonoSectionTable *t = &iinfo->cli_section_tables [i];
167
168                 if (!image->f) {
169                         if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
170                                 return FALSE;
171                         memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
172                         offset += sizeof (MonoSectionTable);
173                 } else {
174                         if (fread (t, sizeof (MonoSectionTable), 1, image->f) != 1)
175                                 return FALSE;
176                 }
177
178 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
179                 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
180                 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
181                 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
182                 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
183                 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
184                 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
185                 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
186                 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
187                 t->st_flags = GUINT32_FROM_LE (t->st_flags);
188 #endif
189                 /* consistency checks here */
190         }
191
192         for (i = 0; i < top; i++)
193                 if (!mono_image_ensure_section_idx (image, i))
194                         return FALSE;
195         
196         return TRUE;
197 }
198
199 static gboolean
200 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
201 {
202         guint32 offset;
203         int n;
204         
205         offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
206         if (offset == INVALID_ADDRESS)
207                 return FALSE;
208
209         if (!image->f) {
210                 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
211                         return FALSE;
212                 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
213         } else {
214                 if (fseek (image->f, offset, SEEK_SET) != 0)
215                         return FALSE;
216         
217                 if ((n = fread (&iinfo->cli_cli_header, sizeof (MonoCLIHeader), 1, image->f)) != 1)
218                         return FALSE;
219         }
220
221 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
222 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
223 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
224 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
225         SWAP32 (iinfo->cli_cli_header.ch_size);
226         SWAP32 (iinfo->cli_cli_header.ch_flags);
227         SWAP32 (iinfo->cli_cli_header.ch_entry_point);
228         SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
229         SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
230         SWAPPDE (iinfo->cli_cli_header.ch_metadata);
231         SWAPPDE (iinfo->cli_cli_header.ch_resources);
232         SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
233         SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
234         SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
235         SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
236         SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
237         SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
238         SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
239         SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
240         SWAPPDE (iinfo->cli_cli_header.ch_module_image);
241         SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
242         SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
243         SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
244         SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
245 #undef SWAP32
246 #undef SWAP16
247 #undef SWAPPDE
248 #endif
249         /* Catch new uses of the fields that are supposed to be zero */
250
251         if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
252             (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
253             (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
254             (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
255             (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
256             (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
257             (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
258             (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
259             (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
260
261                 /*
262                  * No need to scare people who are testing this, I am just
263                  * labelling this as a LAMESPEC
264                  */
265                 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
266
267         }
268             
269         return TRUE;
270 }
271
272 static gboolean
273 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
274 {
275         guint32 offset, size;
276         guint16 streams;
277         int i;
278         guint32 pad;
279         char *ptr;
280         
281         offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
282         if (offset == INVALID_ADDRESS)
283                 return FALSE;
284
285         size = iinfo->cli_cli_header.ch_metadata.size;
286
287         if (!image->f) {
288                 if (offset + size > image->raw_data_len)
289                         return FALSE;
290                 image->raw_metadata = image->raw_data + offset;
291         } else {
292                 image->raw_metadata = mono_raw_buffer_load (fileno (image->f), FALSE, offset, size);
293                 if (image->raw_metadata == NULL)
294                         return FALSE;
295         }
296
297         ptr = image->raw_metadata;
298
299         if (strncmp (ptr, "BSJB", 4) == 0){
300                 guint32 version_string_len;
301
302                 ptr += 12;
303                 version_string_len = read32 (ptr);
304                 ptr += 4;
305                 image->version = g_strndup (ptr, version_string_len);
306                 ptr += version_string_len;
307                 pad = ptr - image->raw_metadata;
308                 if (pad % 4)
309                         ptr += 4 - (pad % 4);
310         } else
311                 return FALSE;
312
313         /* skip over flags */
314         ptr += 2;
315         
316         streams = read16 (ptr);
317         ptr += 2;
318
319         for (i = 0; i < streams; i++){
320                 if (strncmp (ptr + 8, "#~", 3) == 0){
321                         image->heap_tables.data = image->raw_metadata + read32 (ptr);
322                         image->heap_tables.size = read32 (ptr + 4);
323                         ptr += 8 + 3;
324                 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
325                         image->heap_strings.data = image->raw_metadata + read32 (ptr);
326                         image->heap_strings.size = read32 (ptr + 4);
327                         ptr += 8 + 9;
328                 } else if (strncmp (ptr + 8, "#US", 4) == 0){
329                         image->heap_us.data = image->raw_metadata + read32 (ptr);
330                         image->heap_us.size = read32 (ptr + 4);
331                         ptr += 8 + 4;
332                 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
333                         image->heap_blob.data = image->raw_metadata + read32 (ptr);
334                         image->heap_blob.size = read32 (ptr + 4);
335                         ptr += 8 + 6;
336                 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
337                         image->heap_guid.data = image->raw_metadata + read32 (ptr);
338                         image->heap_guid.size = read32 (ptr + 4);
339                         ptr += 8 + 6;
340                 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
341                         g_print ("Assembly '%s' has the non-standard metadata heap #-.\nRecompile it correctly (without the /incremental switch or in Release mode).", image->name);
342                         return FALSE;
343                 } else {
344                         g_message ("Unknown heap type: %s\n", ptr + 8);
345                         ptr += 8 + strlen (ptr + 8) + 1;
346                 }
347                 pad = ptr - image->raw_metadata;
348                 if (pad % 4)
349                         ptr += 4 - (pad % 4);
350         }
351
352         g_assert (image->heap_guid.data);
353         g_assert (image->heap_guid.size >= 16);
354
355         image->guid = mono_guid_to_string (image->heap_guid.data);
356
357         return TRUE;
358 }
359
360 /*
361  * Load representation of logical metadata tables, from the "#~" stream
362  */
363 static gboolean
364 load_tables (MonoImage *image)
365 {
366         const char *heap_tables = image->heap_tables.data;
367         const guint32 *rows;
368         guint64 valid_mask, sorted_mask;
369         int valid = 0, table;
370         int heap_sizes;
371         
372         heap_sizes = heap_tables [6];
373         image->idx_string_wide = ((heap_sizes & 0x01) == 1);
374         image->idx_guid_wide   = ((heap_sizes & 0x02) == 2);
375         image->idx_blob_wide   = ((heap_sizes & 0x04) == 4);
376         
377         valid_mask = read64 (heap_tables + 8);
378         sorted_mask = read64 (heap_tables + 16);
379         rows = (const guint32 *) (heap_tables + 24);
380         
381         for (table = 0; table < 64; table++){
382                 if ((valid_mask & ((guint64) 1 << table)) == 0){
383                         image->tables [table].rows = 0;
384                         continue;
385                 }
386                 if (table > MONO_TABLE_LAST) {
387                         g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
388                 }
389                 /*if ((sorted_mask & ((guint64) 1 << table)) == 0){
390                         g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
391                 }*/
392                 image->tables [table].rows = read32 (rows);
393                 rows++;
394                 valid++;
395         }
396
397         image->tables_base = (heap_tables + 24) + (4 * valid);
398
399         /* They must be the same */
400         g_assert ((const void *) image->tables_base == (const void *) rows);
401
402         mono_metadata_compute_table_bases (image);
403         return TRUE;
404 }
405
406 static gboolean
407 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
408 {
409         if (!load_metadata_ptrs (image, iinfo))
410                 return FALSE;
411
412         return load_tables (image);
413 }
414
415 void
416 mono_image_add_to_name_cache (MonoImage *image, const char *nspace, 
417                                                           const char *name, guint32 index)
418 {
419         GHashTable *nspace_table;
420         GHashTable *name_cache = image->name_cache;
421
422         if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
423                 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
424                 g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
425         }
426         g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
427 }
428
429 static void
430 load_modules (MonoImage *image, MonoImageOpenStatus *status)
431 {
432         MonoTableInfo *t;
433         int i;
434         char *base_dir;
435
436         if (image->modules)
437                 return;
438
439         t = &image->tables [MONO_TABLE_MODULEREF];
440         image->modules = g_new0 (MonoImage *, t->rows);
441         base_dir = g_path_get_dirname (image->name);
442         for (i = 0; i < t->rows; i++){
443                 char *module_ref;
444                 const char *name;
445                 guint32 cols [MONO_MODULEREF_SIZE];
446
447                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
448                 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
449                 module_ref = g_build_filename (base_dir, name, NULL);
450                 image->modules [i] = mono_image_open (module_ref, status);
451                 if (image->modules [i]) {
452                         /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
453                 }
454                 /* 
455                  * FIXME: what do we do here? it could be a native dll...
456                  * We should probably do lazy-loading of modules.
457                  */
458                 if (status)
459                         *status = MONO_IMAGE_OK;
460                 g_free (module_ref);
461         }
462
463         g_free (base_dir);
464 }
465
466 static void
467 load_class_names (MonoImage *image)
468 {
469         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
470         guint32 cols [MONO_TYPEDEF_SIZE];
471         const char *name;
472         const char *nspace;
473         guint32 i, visib, nspace_index;
474         GHashTable *name_cache2, *nspace_table;
475
476         /* Temporary hash table to avoid lookups in the nspace_table */
477         name_cache2 = g_hash_table_new (NULL, NULL);
478
479         for (i = 1; i <= t->rows; ++i) {
480                 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
481                 /* nested types are accessed from the nesting name */
482                 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
483                 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
484                         continue;
485                 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
486                 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
487
488                 nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
489                 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
490                 if (!nspace_table) {
491                         nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
492                         g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
493                         g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
494                                                                  nspace_table);
495                 }
496                 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (i));
497         }
498
499         /* Load type names from EXPORTEDTYPES table */
500         {
501                 MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
502                 guint32 cols [MONO_EXP_TYPE_SIZE];
503                 int i;
504
505                 for (i = 0; i < t->rows; ++i) {
506                         mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
507                         name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
508                         nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
509
510                         nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
511                         nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
512                         if (!nspace_table) {
513                                 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
514                                 g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
515                                 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
516                                                                          nspace_table);
517                         }
518                         g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
519                 }
520         }
521
522         g_hash_table_destroy (name_cache2);
523 }
524
525 static void
526 register_guid (gpointer key, gpointer value, gpointer user_data)
527 {
528         MonoImage *image = (MonoImage*)value;
529
530         if (!g_hash_table_lookup (loaded_images_guid_hash, image))
531                 g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
532 }
533
534 static void
535 build_guid_table (void)
536 {
537         g_hash_table_foreach (loaded_images_hash, register_guid, NULL);
538 }
539
540 void
541 mono_image_init (MonoImage *image)
542 {
543         image->method_cache = g_hash_table_new (NULL, NULL);
544         image->class_cache = g_hash_table_new (NULL, NULL);
545         image->field_cache = g_hash_table_new (NULL, NULL);
546         image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
547         image->array_cache = g_hash_table_new (NULL, NULL);
548
549         image->delegate_begin_invoke_cache = 
550                 g_hash_table_new ((GHashFunc)mono_signature_hash, 
551                                   (GCompareFunc)mono_metadata_signature_equal);
552         image->delegate_end_invoke_cache = 
553                 g_hash_table_new ((GHashFunc)mono_signature_hash, 
554                                   (GCompareFunc)mono_metadata_signature_equal);
555         image->delegate_invoke_cache = 
556                 g_hash_table_new ((GHashFunc)mono_signature_hash, 
557                                   (GCompareFunc)mono_metadata_signature_equal);
558         image->runtime_invoke_cache  = 
559                 g_hash_table_new ((GHashFunc)mono_signature_hash, 
560                                   (GCompareFunc)mono_metadata_signature_equal);
561         
562         image->managed_wrapper_cache = g_hash_table_new (NULL, NULL);
563         image->native_wrapper_cache = g_hash_table_new (NULL, NULL);
564         image->remoting_invoke_cache = g_hash_table_new (NULL, NULL);
565         image->synchronized_cache = g_hash_table_new (NULL, NULL);
566
567         image->typespec_cache = g_hash_table_new (NULL, NULL);
568         image->memberref_signatures = g_hash_table_new (NULL, NULL);
569
570         image->generic_inst_cache =
571                 g_hash_table_new ((GHashFunc)mono_metadata_generic_inst_hash,
572                                   (GCompareFunc)mono_metadata_generic_inst_equal);
573 }
574
575 static MonoImage *
576 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status)
577 {
578         MonoCLIImageInfo *iinfo;
579         MonoDotNetHeader *header;
580         MonoMSDOSHeader msdos;
581         int n;
582         guint32 offset = 0;
583
584         mono_image_init (image);
585
586         iinfo = image->image_info;
587         header = &iinfo->cli_header;
588                 
589         if (status)
590                 *status = MONO_IMAGE_IMAGE_INVALID;
591
592         if (!image->f) {
593                 if (offset + sizeof (msdos) > image->raw_data_len)
594                         goto invalid_image;
595                 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
596         } else {
597                 if (fread (&msdos, sizeof (msdos), 1, image->f) != 1)
598                         goto invalid_image;
599         }
600         
601         if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
602                 goto invalid_image;
603         
604         msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
605
606         if (msdos.pe_offset != sizeof (msdos)) {
607                 if (image->f)
608                         fseek (image->f, msdos.pe_offset, SEEK_SET);
609         }
610         offset = msdos.pe_offset;
611
612         if (!image->f) {
613                 if (offset + sizeof (MonoDotNetHeader) > image->raw_data_len)
614                         goto invalid_image;
615                 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
616                 offset += sizeof (MonoDotNetHeader);
617         } else {
618                 if ((n = fread (header, sizeof (MonoDotNetHeader), 1, image->f)) != 1)
619                         goto invalid_image;
620         }
621
622 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
623 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
624 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
625 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
626         SWAP32 (header->coff.coff_time);
627         SWAP32 (header->coff.coff_symptr);
628         SWAP32 (header->coff.coff_symcount);
629         SWAP16 (header->coff.coff_machine);
630         SWAP16 (header->coff.coff_sections);
631         SWAP16 (header->coff.coff_opt_header_size);
632         SWAP16 (header->coff.coff_attributes);
633         /* MonoPEHeader */
634         SWAP32 (header->pe.pe_code_size);
635         SWAP32 (header->pe.pe_data_size);
636         SWAP32 (header->pe.pe_uninit_data_size);
637         SWAP32 (header->pe.pe_rva_entry_point);
638         SWAP32 (header->pe.pe_rva_code_base);
639         SWAP32 (header->pe.pe_rva_data_base);
640         SWAP16 (header->pe.pe_magic);
641
642         /* MonoPEHeaderNT: not used yet */
643         SWAP32  (header->nt.pe_image_base);     /* must be 0x400000 */
644         SWAP32  (header->nt.pe_section_align);       /* must be 8192 */
645         SWAP32  (header->nt.pe_file_alignment);      /* must be 512 or 4096 */
646         SWAP16  (header->nt.pe_os_major);            /* must be 4 */
647         SWAP16  (header->nt.pe_os_minor);            /* must be 0 */
648         SWAP16  (header->nt.pe_user_major);
649         SWAP16  (header->nt.pe_user_minor);
650         SWAP16  (header->nt.pe_subsys_major);
651         SWAP16  (header->nt.pe_subsys_minor);
652         SWAP32  (header->nt.pe_reserved_1);
653         SWAP32  (header->nt.pe_image_size);
654         SWAP32  (header->nt.pe_header_size);
655         SWAP32  (header->nt.pe_checksum);
656         SWAP16  (header->nt.pe_subsys_required);
657         SWAP16  (header->nt.pe_dll_flags);
658         SWAP32  (header->nt.pe_stack_reserve);
659         SWAP32  (header->nt.pe_stack_commit);
660         SWAP32  (header->nt.pe_heap_reserve);
661         SWAP32  (header->nt.pe_heap_commit);
662         SWAP32  (header->nt.pe_loader_flags);
663         SWAP32  (header->nt.pe_data_dir_count);
664
665         /* MonoDotNetHeader: mostly unused */
666         SWAPPDE (header->datadir.pe_export_table);
667         SWAPPDE (header->datadir.pe_import_table);
668         SWAPPDE (header->datadir.pe_resource_table);
669         SWAPPDE (header->datadir.pe_exception_table);
670         SWAPPDE (header->datadir.pe_certificate_table);
671         SWAPPDE (header->datadir.pe_reloc_table);
672         SWAPPDE (header->datadir.pe_debug);
673         SWAPPDE (header->datadir.pe_copyright);
674         SWAPPDE (header->datadir.pe_global_ptr);
675         SWAPPDE (header->datadir.pe_tls_table);
676         SWAPPDE (header->datadir.pe_load_config_table);
677         SWAPPDE (header->datadir.pe_bound_import);
678         SWAPPDE (header->datadir.pe_iat);
679         SWAPPDE (header->datadir.pe_delay_import_desc);
680         SWAPPDE (header->datadir.pe_cli_header);
681         SWAPPDE (header->datadir.pe_reserved);
682
683 #undef SWAP32
684 #undef SWAP16
685 #undef SWAPPDE
686 #endif
687
688         if (header->coff.coff_machine != 0x14c)
689                 goto invalid_image;
690
691         if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
692                 goto invalid_image;
693
694         if (header->pesig[0] != 'P' || header->pesig[1] != 'E' || header->pe.pe_magic != 0x10B)
695                 goto invalid_image;
696
697 #if 0
698         /*
699          * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
700          * which produces binaries with 7.0.  From Sergey:
701          *
702          * The reason is that MSVC7 uses traditional compile/link
703          * sequence for CIL executables, and VS.NET (and Framework
704          * SDK) includes linker version 7, that puts 7.0 in this
705          * field.  That's why it's currently not possible to load VC
706          * binaries with Mono.  This field is pretty much meaningless
707          * anyway (what linker?).
708          */
709         if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
710                 goto invalid_image;
711 #endif
712
713         /*
714          * FIXME: byte swap all addresses here for header.
715          */
716         
717         if (!load_section_tables (image, iinfo, offset))
718                 goto invalid_image;
719         
720         /* Load the CLI header */
721         if (!load_cli_header (image, iinfo))
722                 goto invalid_image;
723
724         if (!load_metadata (image, iinfo))
725                 goto invalid_image;
726
727         load_class_names (image);
728
729         /* modules don't have an assembly table row */
730         if (image->tables [MONO_TABLE_ASSEMBLY].rows)
731                 image->assembly_name = mono_metadata_string_heap (image, 
732                         mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
733                                         0, MONO_ASSEMBLY_NAME));
734
735         image->module_name = mono_metadata_string_heap (image, 
736                         mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
737                                         0, MONO_MODULE_NAME));
738
739         load_modules (image, status);
740
741         if (status)
742                 *status = MONO_IMAGE_OK;
743
744         image->ref_count=1;
745
746         return image;
747
748 invalid_image:
749         mono_image_close (image);
750                 return NULL;
751 }
752
753 static MonoImage *
754 do_mono_image_open (const char *fname, MonoImageOpenStatus *status)
755 {
756         MonoCLIImageInfo *iinfo;
757         MonoImage *image;
758         FILE *filed;
759
760         if ((filed = fopen (fname, "rb")) == NULL){
761                 if (status)
762                         *status = MONO_IMAGE_ERROR_ERRNO;
763                 return NULL;
764         }
765
766         image = g_new0 (MonoImage, 1);
767         image->ref_count = 1;
768         image->f = filed;
769         iinfo = g_new0 (MonoCLIImageInfo, 1);
770         image->image_info = iinfo;
771
772         if (g_path_is_absolute (fname))
773                 image->name = g_strdup (fname);
774         else {
775                 gchar *path = g_get_current_dir ();
776                 image->name = g_build_filename (path, fname, NULL);
777                 g_free (path);
778         }
779
780         return do_mono_image_load (image, status);
781 }
782
783 MonoImage *
784 mono_image_loaded (const char *name)
785 {
786         MonoImage *res;
787         
788         EnterCriticalSection (&images_mutex);
789         res = g_hash_table_lookup (loaded_images_hash, name);
790         LeaveCriticalSection (&images_mutex);
791         return res;
792 }
793
794 MonoImage *
795 mono_image_loaded_by_guid (const char *guid)
796 {
797         MonoImage *res;
798
799         EnterCriticalSection (&images_mutex);
800         res = g_hash_table_lookup (loaded_images_guid_hash, guid);
801         LeaveCriticalSection (&images_mutex);
802         return res;
803 }
804
805 MonoImage *
806 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
807 {
808         MonoCLIImageInfo *iinfo;
809         MonoImage *image;
810         char *datac;
811
812         if (!data || !data_len) {
813                 if (status)
814                         *status = MONO_IMAGE_IMAGE_INVALID;
815                 return NULL;
816         }
817         datac = data;
818         if (need_copy) {
819                 datac = g_try_malloc (data_len);
820                 if (!datac) {
821                         if (status)
822                                 *status = MONO_IMAGE_ERROR_ERRNO;
823                         return NULL;
824                 }
825                 memcpy (datac, data, data_len);
826         }
827
828         image = g_new0 (MonoImage, 1);
829         image->ref_count = 1;
830         image->raw_data = datac;
831         image->raw_data_len = data_len;
832         image->raw_data_allocated = need_copy;
833         image->name = g_strdup_printf ("data-%p", datac);
834         iinfo = g_new0 (MonoCLIImageInfo, 1);
835         image->image_info = iinfo;
836
837         return do_mono_image_load (image, status);
838 }
839
840 /**
841  * mono_image_open:
842  * @fname: filename that points to the module we want to open
843  * @status: An error condition is returned in this field
844  *
845  * Retuns: An open image of type %MonoImage or NULL on error.
846  * if NULL, then check the value of @status for details on the error
847  */
848 MonoImage *
849 mono_image_open (const char *fname, MonoImageOpenStatus *status)
850 {
851         MonoImage *image, *image2;
852         const char *absfname;
853         
854         g_return_val_if_fail (fname != NULL, NULL);
855         
856         if (g_path_is_absolute (fname)) 
857                 absfname = fname;
858         else {
859                 gchar *path = g_get_current_dir ();
860                 absfname = g_build_filename (path, fname, NULL);
861                 g_free (path);
862         }
863
864         /*
865          * The easiest solution would be to do all the loading inside the mutex,
866          * but that would lead to scalability problems. So we let the loading
867          * happen outside the mutex, and if multiple threads happen to load
868          * the same image, we discard all but the first copy.
869          */
870         EnterCriticalSection (&images_mutex);
871         image = g_hash_table_lookup (loaded_images_hash, absfname);
872         
873         if (absfname != fname)
874                 g_free (absfname);
875         
876         if (image){
877                 image->ref_count++;
878                 LeaveCriticalSection (&images_mutex);
879                 return image;
880         }
881         LeaveCriticalSection (&images_mutex);
882
883         image = do_mono_image_open (fname, status);
884         if (image == NULL)
885                 return NULL;
886
887         EnterCriticalSection (&images_mutex);
888         image2 = g_hash_table_lookup (loaded_images_hash, fname);
889         if (image2) {
890                 /* Somebody else beat us to it */
891                 image2->ref_count ++;
892                 LeaveCriticalSection (&images_mutex);
893                 mono_image_close (image);
894
895                 return image2;
896         }
897         g_hash_table_insert (loaded_images_hash, image->name, image);
898         if (image->assembly_name)
899                 g_hash_table_insert (loaded_images_hash, (char *) image->assembly_name, image); 
900         g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
901         LeaveCriticalSection (&images_mutex);
902
903         return image;
904 }
905
906 static void
907 free_hash_table (gpointer key, gpointer val, gpointer user_data)
908 {
909         g_hash_table_destroy ((GHashTable*)val);
910 }
911
912 static void
913 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
914 {
915         mono_metadata_free_method_signature ((MonoMethodSignature*)val);
916 }
917
918 /**
919  * mono_image_close:
920  * @image: The image file we wish to add a reference to
921  *
922  *  Increases the reference count of an image.
923  */
924 void
925 mono_image_addref (MonoImage *image)
926 {
927         InterlockedIncrement (&image->ref_count);
928 }       
929
930 /**
931  * mono_image_close:
932  * @image: The image file we wish to close
933  *
934  * Closes an image file, deallocates all memory consumed and
935  * unmaps all possible sections of the file
936  */
937 void
938 mono_image_close (MonoImage *image)
939 {
940         MonoImage *image2;
941
942         g_return_if_fail (image != NULL);
943
944         EnterCriticalSection (&images_mutex);
945         if (--image->ref_count) {
946                 LeaveCriticalSection (&images_mutex);
947                 return;
948         }
949         image2 = g_hash_table_lookup (loaded_images_hash, image->name);
950         if (image == image2) {
951                 /* This is not true if we are called from mono_image_open () */
952                 g_hash_table_remove (loaded_images_hash, image->name);
953                 if (image->assembly_name)
954                         g_hash_table_remove (loaded_images_hash, (char *) image->assembly_name);        
955                 g_hash_table_remove (loaded_images_guid_hash, image->guid);
956                 /* Multiple images might have the same guid */
957                 build_guid_table ();
958         }       
959         LeaveCriticalSection (&images_mutex);
960
961         if (image->f)
962                 fclose (image->f);
963         if (image->raw_data_allocated)
964                 g_free (image->raw_data);
965
966         g_free (image->name);
967         g_free (image->files);
968
969         g_hash_table_destroy (image->method_cache);
970         g_hash_table_destroy (image->class_cache);
971         g_hash_table_destroy (image->field_cache);
972         g_hash_table_destroy (image->array_cache);
973         g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
974         g_hash_table_destroy (image->name_cache);
975         g_hash_table_destroy (image->native_wrapper_cache);
976         g_hash_table_destroy (image->managed_wrapper_cache);
977         g_hash_table_destroy (image->delegate_begin_invoke_cache);
978         g_hash_table_destroy (image->delegate_end_invoke_cache);
979         g_hash_table_destroy (image->delegate_invoke_cache);
980         g_hash_table_destroy (image->remoting_invoke_cache);
981         g_hash_table_destroy (image->runtime_invoke_cache);
982         g_hash_table_destroy (image->typespec_cache);
983         g_hash_table_destroy (image->generic_inst_cache);
984         g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
985         g_hash_table_destroy (image->memberref_signatures);
986         
987         if (image->raw_metadata != NULL)
988                 mono_raw_buffer_free (image->raw_metadata);
989         
990         if (image->image_info){
991                 MonoCLIImageInfo *ii = image->image_info;
992                 int i;
993
994                 for (i = 0; i < ii->cli_section_count; i++){
995                         if (!ii->cli_sections [i])
996                                 continue;
997                         mono_raw_buffer_free (ii->cli_sections [i]);
998                 }
999                 if (ii->cli_section_tables)
1000                         g_free (ii->cli_section_tables);
1001                 if (ii->cli_sections)
1002                         g_free (ii->cli_sections);
1003                 g_free (image->image_info);
1004         }
1005
1006         g_free (image);
1007 }
1008
1009 /** 
1010  * mono_image_strerror:
1011  * @status: an code indicating the result from a recent operation
1012  *
1013  * Returns: a string describing the error
1014  */
1015 const char *
1016 mono_image_strerror (MonoImageOpenStatus status)
1017 {
1018         switch (status){
1019         case MONO_IMAGE_OK:
1020                 return "success";
1021         case MONO_IMAGE_ERROR_ERRNO:
1022                 return strerror (errno);
1023         case MONO_IMAGE_IMAGE_INVALID:
1024                 return "File does not contain a valid CIL image";
1025         case MONO_IMAGE_MISSING_ASSEMBLYREF:
1026                 return "An assembly was referenced, but could not be found";
1027         }
1028         return "Internal error";
1029 }
1030
1031 static gpointer
1032 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
1033                                guint32 lang_id, gunichar2 *name,
1034                                MonoPEResourceDirEntry *entry,
1035                                MonoPEResourceDir *root, guint32 level)
1036 {
1037         gboolean is_string=entry->name_is_string;
1038
1039         /* Level 0 holds a directory entry for each type of resource
1040          * (identified by ID or name).
1041          *
1042          * Level 1 holds a directory entry for each named resource
1043          * item, and each "anonymous" item of a particular type of
1044          * resource.
1045          *
1046          * Level 2 holds a directory entry for each language pointing to
1047          * the actual data.
1048          */
1049
1050         if(level==0) {
1051                 if((is_string==FALSE && entry->name_offset!=res_id) ||
1052                    (is_string==TRUE)) {
1053                         return(NULL);
1054                 }
1055         } else if (level==1) {
1056 #if 0
1057                 if(name!=NULL &&
1058                    is_string==TRUE && name!=lookup (entry->name_offset)) {
1059                         return(NULL);
1060                 }
1061 #endif
1062         } else if (level==2) {
1063                 if((is_string==FALSE && entry->name_offset!=lang_id) ||
1064                    (is_string==TRUE)) {
1065                         return(NULL);
1066                 }
1067         } else {
1068                 g_assert_not_reached ();
1069         }
1070
1071         if(entry->is_dir==TRUE) {
1072                 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+entry->dir_offset);
1073                 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
1074                 guint32 entries, i;
1075                 
1076                 entries=res_dir->res_named_entries + res_dir->res_id_entries;
1077
1078                 for(i=0; i<entries; i++) {
1079                         MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
1080                         gpointer ret;
1081                         
1082                         ret=mono_image_walk_resource_tree (info, res_id,
1083                                                            lang_id, name,
1084                                                            sub_entry, root,
1085                                                            level+1);
1086                         if(ret!=NULL) {
1087                                 return(ret);
1088                         }
1089                 }
1090
1091                 return(NULL);
1092         } else {
1093                 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+entry->dir_offset);
1094                 
1095                 return(data_entry);
1096         }
1097 }
1098
1099 gpointer
1100 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
1101 {
1102         MonoCLIImageInfo *info;
1103         MonoDotNetHeader *header;
1104         MonoPEDatadir *datadir;
1105         MonoPEDirEntry *rsrc;
1106         MonoPEResourceDir *resource_dir;
1107         MonoPEResourceDirEntry *res_entries;
1108         guint32 entries, i;
1109
1110         if(image==NULL) {
1111                 return(NULL);
1112         }
1113
1114         info=image->image_info;
1115         if(info==NULL) {
1116                 return(NULL);
1117         }
1118
1119         header=&info->cli_header;
1120         if(header==NULL) {
1121                 return(NULL);
1122         }
1123
1124         datadir=&header->datadir;
1125         if(datadir==NULL) {
1126                 return(NULL);
1127         }
1128
1129         rsrc=&datadir->pe_resource_table;
1130         if(rsrc==NULL) {
1131                 return(NULL);
1132         }
1133
1134         resource_dir=(MonoPEResourceDir *)mono_cli_rva_map (info, rsrc->rva);
1135         if(resource_dir==NULL) {
1136                 return(NULL);
1137         }
1138         
1139         entries=resource_dir->res_named_entries + resource_dir->res_id_entries;
1140         res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
1141         
1142         for(i=0; i<entries; i++) {
1143                 MonoPEResourceDirEntry *entry=&res_entries[i];
1144                 gpointer ret;
1145                 
1146                 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
1147                                                    name, entry, resource_dir,
1148                                                    0);
1149                 if(ret!=NULL) {
1150                         return(ret);
1151                 }
1152         }
1153
1154         return(NULL);
1155 }
1156
1157 guint32
1158 mono_image_get_entry_point (MonoImage *image)
1159 {
1160         return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
1161 }
1162
1163 const char*
1164 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
1165 {
1166         MonoCLIImageInfo *iinfo = image->image_info;
1167         MonoCLIHeader *ch = &iinfo->cli_cli_header;
1168         const char* data;
1169
1170         if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
1171                 return NULL;
1172         
1173         data = mono_cli_rva_map (iinfo, ch->ch_resources.rva);
1174         if (!data)
1175                 return NULL;
1176         data += offset;
1177         if (size)
1178                 *size = read32 (data);
1179         data += 4;
1180         return data;
1181 }
1182
1183 MonoImage*
1184 mono_image_load_file_for_image (MonoImage *image, int fileidx)
1185 {
1186         char *base_dir, *name;
1187         MonoImage *res;
1188         MonoTableInfo  *t = &image->tables [MONO_TABLE_FILE];
1189         const char *fname;
1190         guint32 fname_id;
1191
1192         if (fileidx < 1 || fileidx > t->rows)
1193                 return NULL;
1194
1195         if (image->files && image->files [fileidx - 1])
1196                 return image->files [fileidx - 1];
1197
1198         if (!image->files)
1199                 image->files = g_new0 (MonoImage*, t->rows);
1200
1201         fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1202         fname = mono_metadata_string_heap (image, fname_id);
1203         base_dir = g_path_get_dirname (image->name);
1204         name = g_build_filename (base_dir, fname, NULL);
1205         res = mono_image_open (name, NULL);
1206         if (res) {
1207                 int i;
1208                 t = &res->tables [MONO_TABLE_MODULEREF];
1209                 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
1210                 res->assembly = image->assembly;
1211                 for (i = 0; i < t->rows; ++i) {
1212                         if (res->modules [i] && !res->modules [i]->assembly)
1213                                 res->modules [i]->assembly = image->assembly;
1214                 }
1215
1216                 image->files [fileidx - 1] = res;
1217         }
1218         g_free (name);
1219         g_free (base_dir);
1220         return res;
1221 }
1222
1223 const char*
1224 mono_image_get_strong_name (MonoImage *image, guint32 *size)
1225 {
1226         MonoCLIImageInfo *iinfo = image->image_info;
1227         MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1228         const char* data;
1229
1230         if (!de->size || !de->rva)
1231                 return NULL;
1232         data = mono_cli_rva_map (iinfo, de->rva);
1233         if (!data)
1234                 return NULL;
1235         if (size)
1236                 *size = de->size;
1237         return data;
1238 }
1239
1240 guint32
1241 mono_image_strong_name_position (MonoImage *image, guint32 *size)
1242 {
1243         MonoCLIImageInfo *iinfo = image->image_info;
1244         MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1245         const int top = iinfo->cli_section_count;
1246         MonoSectionTable *tables = iinfo->cli_section_tables;
1247         int i;
1248         guint32 addr = de->rva;
1249         
1250         if (size)
1251                 *size = de->size;
1252         if (!de->size || !de->rva)
1253                 return 0;
1254         for (i = 0; i < top; i++){
1255                 if ((addr >= tables->st_virtual_address) &&
1256                     (addr < tables->st_virtual_address + tables->st_raw_data_size)){
1257                         return tables->st_raw_data_ptr +
1258                                 (addr - tables->st_virtual_address);
1259                 }
1260                 tables++;
1261         }
1262
1263         return 0;
1264 }
1265
1266 const char*
1267 mono_image_get_public_key (MonoImage *image, guint32 *size)
1268 {
1269         const char *pubkey;
1270         guint32 len, tok;
1271         if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
1272                 return NULL;
1273         tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
1274         if (!tok)
1275                 return NULL;
1276         pubkey = mono_metadata_blob_heap (image, tok);
1277         len = mono_metadata_decode_blob_size (pubkey, &pubkey);
1278         if (size)
1279                 *size = len;
1280         return pubkey;
1281 }
1282
1283 const char*
1284 mono_image_get_name (MonoImage *image)
1285 {
1286         return image->assembly_name;
1287 }
1288
1289 const char*
1290 mono_image_get_filename (MonoImage *image)
1291 {
1292         return image->name;
1293 }
1294
1295 const MonoTableInfo*
1296 mono_image_get_table_info (MonoImage *image, int table_id)
1297 {
1298         if (table_id < 0 || table_id >= 64)
1299                 return NULL;
1300         return &image->tables [table_id];
1301 }
1302
1303 int
1304 mono_image_get_table_rows (MonoImage *image, int table_id)
1305 {
1306         if (table_id < 0 || table_id >= 64)
1307                 return 0;
1308         return image->tables [table_id].rows;
1309 }
1310
1311 int
1312 mono_table_info_get_rows (MonoTableInfo *table)
1313 {
1314         return table->rows;
1315 }
1316
1317 MonoAssembly* 
1318 mono_image_get_assembly (MonoImage *image)
1319 {
1320         return image->assembly;
1321 }
1322
1323 gboolean
1324 mono_image_is_dynamic (MonoImage *image)
1325 {
1326         return image->dynamic;
1327 }
1328
1329 char*
1330 mono_image_rva_map (MonoImage *image, guint32 rva)
1331 {
1332         return mono_cli_rva_map (image->image_info, rva);
1333 }
1334