2009-05-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / metadata / image.c
index adba56959e9ee21656e7345ee94d895ac2181023..da552e6fa65c971a1674302873f03a585e4633f8 100644 (file)
@@ -6,7 +6,8 @@
  *   Miguel de Icaza (miguel@ximian.com)
  *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2001-2003 Ximian, Inc.  http://www.ximian.com
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  *
  */
 #include <config.h>
 #include <string.h>
 #include "image.h"
 #include "cil-coff.h"
-#include "rawbuffer.h"
 #include "mono-endian.h"
 #include "tabledefs.h"
 #include "tokentype.h"
 #include "metadata-internals.h"
 #include "profiler-private.h"
 #include "loader.h"
+#include "marshal.h"
+#include "coree.h"
 #include <mono/io-layer/io-layer.h>
 #include <mono/utils/mono-logger.h>
 #include <mono/utils/mono-path.h>
+#include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-io-portability.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/object-internals.h>
+#include <mono/metadata/security-core-clr.h>
+#include <mono/metadata/verify-internals.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifdef HAVE_UNISTD_H
@@ -51,9 +56,11 @@ static gboolean debug_assembly_unload = FALSE;
 #define mono_images_unlock() LeaveCriticalSection (&images_mutex)
 static CRITICAL_SECTION images_mutex;
 
+/* returns offset relative to image->raw_data */
 guint32
-mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
+mono_cli_rva_image_map (MonoImage *image, guint32 addr)
 {
+       MonoCLIImageInfo *iinfo = image->image_info;
        const int top = iinfo->cli_section_count;
        MonoSectionTable *tables = iinfo->cli_section_tables;
        int i;
@@ -61,6 +68,10 @@ mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
        for (i = 0; i < top; i++){
                if ((addr >= tables->st_virtual_address) &&
                    (addr < tables->st_virtual_address + tables->st_raw_data_size)){
+#ifdef PLATFORM_WIN32
+                       if (image->is_module_handle)
+                               return addr;
+#endif
                        return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
                }
                tables++;
@@ -94,6 +105,10 @@ mono_image_rva_map (MonoImage *image, guint32 addr)
                                if (!mono_image_ensure_section_idx (image, i))
                                        return NULL;
                        }
+#ifdef PLATFORM_WIN32
+                       if (image->is_module_handle)
+                               return image->raw_data + addr;
+#endif
                        return (char*)iinfo->cli_sections [i] +
                                (addr - tables->st_virtual_address);
                }
@@ -115,7 +130,7 @@ mono_images_init (void)
        loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
        loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
-       debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
+       debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
 }
 
 /**
@@ -160,6 +175,11 @@ mono_image_ensure_section_idx (MonoImage *image, int section)
 
        if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
                return FALSE;
+#ifdef PLATFORM_WIN32
+       if (image->is_module_handle)
+               iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
+       else
+#endif
        /* FIXME: we ignore the writable flag since we don't patch the binary */
        iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
        return TRUE;
@@ -230,7 +250,7 @@ load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
 {
        guint32 offset;
        
-       offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
+       offset = mono_cli_rva_image_map (image, iinfo->cli_header.datadir.pe_cli_header.rva);
        if (offset == INVALID_ADDRESS)
                return FALSE;
 
@@ -298,7 +318,7 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
        guint32 pad;
        char *ptr;
        
-       offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
+       offset = mono_cli_rva_image_map (image, iinfo->cli_cli_header.ch_metadata.rva);
        if (offset == INVALID_ADDRESS)
                return FALSE;
 
@@ -315,9 +335,9 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
 
                ptr += 4;
                image->md_version_major = read16 (ptr);
-               ptr += 4;
+               ptr += 2;
                image->md_version_minor = read16 (ptr);
-               ptr += 4;
+               ptr += 6;
 
                version_string_len = read32 (ptr);
                ptr += 4;
@@ -443,7 +463,7 @@ mono_image_check_for_module_cctor (MonoImage *image)
        MonoTableInfo *t, *mt;
        t = &image->tables [MONO_TABLE_TYPEDEF];
        mt = &image->tables [MONO_TABLE_METHOD];
-       if (mono_get_runtime_info ()->framework_version [0] == '1') {
+       if (mono_framework_version () == 1) {
                image->checked_module_cctor = TRUE;
                return;
        }
@@ -546,6 +566,10 @@ mono_image_load_module (MonoImage *image, int idx)
                        if (image->modules [idx - 1]) {
                                mono_image_addref (image->modules [idx - 1]);
                                image->modules [idx - 1]->assembly = image->assembly;
+#ifdef PLATFORM_WIN32
+                               if (image->modules [idx - 1]->is_module_handle)
+                                       mono_image_fixup_vtable (image->modules [idx - 1]);
+#endif
                                /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
                        }
                        g_free (module_ref);
@@ -580,46 +604,20 @@ void
 mono_image_init (MonoImage *image)
 {
        image->mempool = mono_mempool_new_size (512);
-       image->method_cache = g_hash_table_new (NULL, NULL);
        mono_internal_hash_table_init (&image->class_cache,
                                       g_direct_hash,
                                       class_key_extract,
                                       class_next_value);
        image->field_cache = g_hash_table_new (NULL, NULL);
 
-       image->delegate_begin_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->delegate_end_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->delegate_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->runtime_invoke_cache  = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       
-       image->runtime_invoke_direct_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->managed_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->remoting_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->cominterop_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->cominterop_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->synchronized_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->unbox_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
-       image->ldfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->ldflda_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->stfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->castclass_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->proxy_isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
        image->typespec_cache = g_hash_table_new (NULL, NULL);
        image->memberref_signatures = g_hash_table_new (NULL, NULL);
        image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
        image->method_signatures = g_hash_table_new (NULL, NULL);
+
+       image->property_hash = mono_property_hash_new ();
+       InitializeCriticalSection (&image->lock);
+       InitializeCriticalSection (&image->szarray_cache_lock);
 }
 
 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
@@ -642,6 +640,9 @@ do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
 {
        MonoDotNetHeader64 header64;
 
+#ifdef PLATFORM_WIN32
+       if (!image->is_module_handle)
+#endif
        if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
                return -1;
 
@@ -760,28 +761,28 @@ do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
        SWAPPDE (header->datadir.pe_cli_header);
        SWAPPDE (header->datadir.pe_reserved);
 
+#ifdef PLATFORM_WIN32
+       if (image->is_module_handle)
+               image->raw_data_len = header->nt.pe_image_size;
+#endif
+
        return offset;
 }
 
-static MonoImage *
-do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
-                   gboolean care_about_cli)
+gboolean
+mono_image_load_pe_data (MonoImage *image)
 {
        MonoCLIImageInfo *iinfo;
        MonoDotNetHeader *header;
        MonoMSDOSHeader msdos;
        gint32 offset = 0;
 
-       mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
-
-       mono_image_init (image);
-
        iinfo = image->image_info;
        header = &iinfo->cli_header;
-               
-       if (status)
-               *status = MONO_IMAGE_IMAGE_INVALID;
 
+#ifdef PLATFORM_WIN32
+       if (!image->is_module_handle)
+#endif
        if (offset + sizeof (msdos) > image->raw_data_len)
                goto invalid_image;
        memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
@@ -826,16 +827,66 @@ do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
        
        if (!load_section_tables (image, iinfo, offset))
                goto invalid_image;
+
+       return TRUE;
+
+invalid_image:
+       return FALSE;
+}
+
+gboolean
+mono_image_load_cli_data (MonoImage *image)
+{
+       MonoCLIImageInfo *iinfo;
+       MonoDotNetHeader *header;
+
+       iinfo = image->image_info;
+       header = &iinfo->cli_header;
+
+       /* Load the CLI header */
+       if (!load_cli_header (image, iinfo))
+               return FALSE;
+
+       if (!load_metadata (image, iinfo))
+               return FALSE;
+
+       return TRUE;
+}
+
+static MonoImage *
+do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
+                   gboolean care_about_cli, gboolean care_about_pecoff)
+{
+       MonoCLIImageInfo *iinfo;
+       MonoDotNetHeader *header;
+
+       mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
+
+       mono_image_init (image);
+
+       iinfo = image->image_info;
+       header = &iinfo->cli_header;
+               
+       if (status)
+               *status = MONO_IMAGE_IMAGE_INVALID;
+
+       if (care_about_pecoff == FALSE)
+               goto done;
+
+       if (!mono_verifier_verify_pe_data (image, NULL))
+               goto invalid_image;
+
+       if (!mono_image_load_pe_data (image))
+               goto invalid_image;
        
        if (care_about_cli == FALSE) {
                goto done;
        }
-       
-       /* Load the CLI header */
-       if (!load_cli_header (image, iinfo))
+
+       if (!mono_verifier_verify_cli_data (image, NULL))
                goto invalid_image;
 
-       if (!load_metadata (image, iinfo))
+       if (!mono_image_load_cli_data (image))
                goto invalid_image;
 
        /* modules don't have an assembly table row */
@@ -866,18 +917,17 @@ invalid_image:
 
 static MonoImage *
 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
-                   gboolean care_about_cli, gboolean refonly)
+                   gboolean care_about_cli, gboolean care_about_pecoff, gboolean refonly)
 {
        MonoCLIImageInfo *iinfo;
        MonoImage *image;
-       FILE *filed;
-       struct stat stat_buf;
+       MonoFileMap *filed;
 
-       if ((filed = fopen (fname, "rb")) == NULL){
+       if ((filed = mono_file_map_open (fname)) == NULL){
                if (IS_PORTABILITY_SET) {
                        gchar *ffname = mono_portability_find_file (fname, TRUE);
                        if (ffname) {
-                               filed = fopen (ffname, "rb");
+                               filed = mono_file_map_open (ffname);
                                g_free (ffname);
                        }
                }
@@ -889,25 +939,27 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
                }
        }
 
-       if (fstat (fileno (filed), &stat_buf)) {
-               fclose (filed);
+       image = g_new0 (MonoImage, 1);
+       image->raw_buffer_used = TRUE;
+       image->raw_data_len = mono_file_map_size (filed);
+       image->raw_data = mono_file_map (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
+       if (!image->raw_data) {
+               mono_file_map_close (filed);
+               g_free (image);
                if (status)
-                       *status = MONO_IMAGE_ERROR_ERRNO;
+                       *status = MONO_IMAGE_IMAGE_INVALID;
                return NULL;
        }
-       image = g_new0 (MonoImage, 1);
-       image->raw_buffer_used = TRUE;
-       image->raw_data_len = stat_buf.st_size;
-       image->raw_data = mono_raw_buffer_load (fileno (filed), FALSE, 0, stat_buf.st_size);
        iinfo = g_new0 (MonoCLIImageInfo, 1);
        image->image_info = iinfo;
        image->name = mono_path_resolve_symlinks (fname);
        image->ref_only = refonly;
        image->ref_count = 1;
+       /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
+       image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
 
-       fclose (filed);
-
-       return do_mono_image_load (image, status, care_about_cli);
+       mono_file_map_close (filed);
+       return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
 }
 
 MonoImage *
@@ -1030,7 +1082,7 @@ mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy
        image->image_info = iinfo;
        image->ref_only = refonly;
 
-       image = do_mono_image_load (image, status, TRUE);
+       image = do_mono_image_load (image, status, TRUE, TRUE);
        if (image == NULL)
                return NULL;
 
@@ -1043,6 +1095,31 @@ mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, Mon
        return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
 }
 
+#ifdef PLATFORM_WIN32
+/* fname is not duplicated. */
+MonoImage*
+mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean has_entry_point, MonoImageOpenStatus* status)
+{
+       MonoImage* image;
+       MonoCLIImageInfo* iinfo;
+
+       image = g_new0 (MonoImage, 1);
+       image->raw_data = (char*) module_handle;
+       image->is_module_handle = TRUE;
+       iinfo = g_new0 (MonoCLIImageInfo, 1);
+       image->image_info = iinfo;
+       image->name = fname;
+       image->ref_count = has_entry_point ? 0 : 1;
+       image->has_entry_point = has_entry_point;
+
+       image = do_mono_image_load (image, status, TRUE, TRUE);
+       if (image == NULL)
+               return NULL;
+
+       return register_image (image);
+}
+#endif
+
 MonoImage *
 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
 {
@@ -1052,6 +1129,72 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
        
        g_return_val_if_fail (fname != NULL, NULL);
        
+#ifdef PLATFORM_WIN32
+       /* Load modules using LoadLibrary. */
+       if (!refonly && coree_module_handle) {
+               HMODULE module_handle;
+               guint16 *fname_utf16;
+               DWORD last_error;
+
+               absfname = mono_path_resolve_symlinks (fname);
+               fname_utf16 = NULL;
+
+               /* There is little overhead because the OS loader lock is held by LoadLibrary. */
+               mono_images_lock ();
+               image = g_hash_table_lookup (loaded_images_hash, absfname);
+               if (image) {
+                       g_assert (image->is_module_handle);
+                       if (image->has_entry_point && image->ref_count == 0) {
+                               /* Increment reference count on images loaded outside of the runtime. */
+                               fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
+                               /* The image is already loaded because _CorDllMain removes images from the hash. */
+                               module_handle = LoadLibrary (fname_utf16);
+                               g_assert (module_handle == (HMODULE) image->raw_data);
+                       }
+                       mono_image_addref (image);
+                       mono_images_unlock ();
+                       if (fname_utf16)
+                               g_free (fname_utf16);
+                       g_free (absfname);
+                       return image;
+               }
+
+               fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
+               module_handle = MonoLoadImage (fname_utf16);
+               if (status && module_handle == NULL)
+                       last_error = GetLastError ();
+
+               /* mono_image_open_from_module_handle is called by _CorDllMain. */
+               image = g_hash_table_lookup (loaded_images_hash, absfname);
+               if (image)
+                       mono_image_addref (image);
+               mono_images_unlock ();
+
+               g_free (fname_utf16);
+
+               if (module_handle == NULL) {
+                       g_assert (!image);
+                       g_free (absfname);
+                       if (status) {
+                               if (last_error == ERROR_BAD_EXE_FORMAT || last_error == STATUS_INVALID_IMAGE_FORMAT)
+                                       *status = MONO_IMAGE_IMAGE_INVALID;
+                               else
+                                       *status = MONO_IMAGE_ERROR_ERRNO;
+                       }
+                       return NULL;
+               }
+
+               if (image) {
+                       g_assert (image->is_module_handle);
+                       g_assert (image->has_entry_point);
+                       g_free (absfname);
+                       return image;
+               }
+
+               return mono_image_open_from_module_handle (module_handle, absfname, FALSE, status);
+       }
+#endif
+
        absfname = mono_path_canonicalize (fname);
 
        /*
@@ -1072,7 +1215,7 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
        }
        mono_images_unlock ();
 
-       image = do_mono_image_open (fname, status, TRUE, refonly);
+       image = do_mono_image_open (fname, status, TRUE, TRUE, refonly);
        if (image == NULL)
                return NULL;
 
@@ -1111,7 +1254,75 @@ mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
 {
        g_return_val_if_fail (fname != NULL, NULL);
        
-       return(do_mono_image_open (fname, status, FALSE, FALSE));
+       return(do_mono_image_open (fname, status, FALSE, TRUE, FALSE));
+}
+
+/**
+ * mono_image_open_raw
+ * @fname: filename that points to the module we want to open
+ * @status: An error condition is returned in this field
+ * 
+ * Returns an image without loading neither pe or cli data.
+ * 
+ * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.  
+ */
+MonoImage *
+mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
+{
+       g_return_val_if_fail (fname != NULL, NULL);
+       
+       return(do_mono_image_open (fname, status, FALSE, FALSE, FALSE));
+}
+
+void
+mono_image_fixup_vtable (MonoImage *image)
+{
+#ifdef PLATFORM_WIN32
+       MonoCLIImageInfo *iinfo;
+       MonoPEDirEntry *de;
+       MonoVTableFixup *vtfixup;
+       int count;
+       gpointer slot;
+       guint16 slot_type;
+       int slot_count;
+
+       g_assert (image->is_module_handle);
+
+       iinfo = image->image_info;
+       de = &iinfo->cli_cli_header.ch_vtable_fixups;
+       if (!de->rva || !de->size)
+               return;
+       vtfixup = (MonoVTableFixup*) mono_image_rva_map (image, de->rva);
+       if (!vtfixup)
+               return;
+       
+       count = de->size / sizeof (MonoVTableFixup);
+       while (count--) {
+               if (!vtfixup->rva || !vtfixup->count)
+                       continue;
+
+               slot = mono_image_rva_map (image, vtfixup->rva);
+               g_assert (slot);
+               slot_type = vtfixup->type;
+               slot_count = vtfixup->count;
+               if (slot_type & VTFIXUP_TYPE_32BIT)
+                       while (slot_count--) {
+                               *((guint32*) slot) = (guint32) mono_marshal_get_vtfixup_ftnptr (image, *((guint32*) slot), slot_type);
+                               slot = ((guint32*) slot) + 1;
+                       }
+               else if (slot_type & VTFIXUP_TYPE_64BIT)
+                       while (slot_count--) {
+                               *((guint64*) slot) = (guint64) mono_marshal_get_vtfixup_ftnptr (image, *((guint64*) slot), slot_type);
+                               slot = ((guint32*) slot) + 1;
+                       }
+               else
+                       g_assert_not_reached();
+
+               vtfixup++;
+       }
+#else
+       g_assert_not_reached();
+#endif
 }
 
 static void
@@ -1164,6 +1375,13 @@ mono_dynamic_stream_reset (MonoDynamicStream* stream)
        }
 }
 
+static inline void
+free_hash (GHashTable *hash)
+{
+       if (hash)
+               g_hash_table_destroy (hash);
+}
+
 /**
  * mono_image_close:
  * @image: The image file we wish to close
@@ -1183,6 +1401,19 @@ mono_image_close (MonoImage *image)
        if (InterlockedDecrement (&image->ref_count) > 0)
                return;
 
+#ifdef PLATFORM_WIN32
+       if (image->is_module_handle && image->has_entry_point) {
+               mono_images_lock ();
+               if (image->ref_count == 0) {
+                       /* Image will be closed by _CorDllMain. */
+                       FreeLibrary ((HMODULE) image->raw_data);
+                       mono_images_unlock ();
+                       return;
+               }
+               mono_images_unlock ();
+       }
+#endif
+
        mono_profiler_module_event (image, MONO_PROFILE_START_UNLOAD);
 
        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
@@ -1194,10 +1425,11 @@ mono_image_close (MonoImage *image)
         * assemblies, so we can't release these references in mono_assembly_close () since the
         * MonoImage might outlive its associated MonoAssembly.
         */
-       if (image->references) {
+       if (image->references && !image->dynamic) {
+               MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
                int i;
 
-               for (i = 0; image->references [i]; i++) {
+               for (i = 0; i < t->rows; i++) {
                        if (image->references [i])
                                mono_assembly_close (image->references [i]);
                }
@@ -1216,14 +1448,20 @@ mono_image_close (MonoImage *image)
        if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
                g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
 
+#ifdef PLATFORM_WIN32
+       if (image->is_module_handle && !image->has_entry_point)
+               FreeLibrary ((HMODULE) image->raw_data);
+#endif
+
        mono_images_unlock ();
 
        if (image->raw_buffer_used) {
                if (image->raw_data != NULL)
-                       mono_raw_buffer_free (image->raw_data);
+                       mono_file_unmap (image->raw_data, image->raw_data_handle);
        }
        
        if (image->raw_data_allocated) {
+               /* FIXME: do we need this? (image is disposed anyway) */
                /* image->raw_metadata and cli_sections might lie inside image->raw_data */
                MonoCLIImageInfo *ii = image->image_info;
 
@@ -1248,43 +1486,50 @@ mono_image_close (MonoImage *image)
                g_free (image->files);
        }
 
-       g_hash_table_destroy (image->method_cache);
+       if (image->method_cache)
+               mono_value_hash_table_destroy (image->method_cache);
+       if (image->methodref_cache)
+               g_hash_table_destroy (image->methodref_cache);
        mono_internal_hash_table_destroy (&image->class_cache);
        g_hash_table_destroy (image->field_cache);
        if (image->array_cache) {
                g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
                g_hash_table_destroy (image->array_cache);
        }
+       if (image->szarray_cache)
+               g_hash_table_destroy (image->szarray_cache);
        if (image->ptr_cache)
                g_hash_table_destroy (image->ptr_cache);
        if (image->name_cache) {
                g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
                g_hash_table_destroy (image->name_cache);
        }
-       g_hash_table_destroy (image->native_wrapper_cache);
-       g_hash_table_destroy (image->managed_wrapper_cache);
-       g_hash_table_destroy (image->delegate_begin_invoke_cache);
-       g_hash_table_destroy (image->delegate_end_invoke_cache);
-       g_hash_table_destroy (image->delegate_invoke_cache);
-       if (image->delegate_abstract_invoke_cache)
-               g_hash_table_destroy (image->delegate_abstract_invoke_cache);
-       g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
-       g_hash_table_destroy (image->remoting_invoke_cache);
-       g_hash_table_destroy (image->runtime_invoke_cache);
-       g_hash_table_destroy (image->runtime_invoke_direct_cache);
-       g_hash_table_destroy (image->synchronized_cache);
-       g_hash_table_destroy (image->unbox_wrapper_cache);
-       g_hash_table_destroy (image->cominterop_invoke_cache);
-       g_hash_table_destroy (image->cominterop_wrapper_cache);
-       g_hash_table_destroy (image->typespec_cache);
-       g_hash_table_destroy (image->ldfld_wrapper_cache);
-       g_hash_table_destroy (image->ldflda_wrapper_cache);
-       g_hash_table_destroy (image->stfld_wrapper_cache);
-       g_hash_table_destroy (image->isinst_cache);
-       g_hash_table_destroy (image->castclass_cache);
-       g_hash_table_destroy (image->proxy_isinst_cache);
-       if (image->static_rgctx_invoke_cache)
-               g_hash_table_destroy (image->static_rgctx_invoke_cache);
+
+       free_hash (image->native_wrapper_cache);
+       free_hash (image->managed_wrapper_cache);
+       free_hash (image->delegate_begin_invoke_cache);
+       free_hash (image->delegate_end_invoke_cache);
+       free_hash (image->delegate_invoke_cache);
+       free_hash (image->delegate_abstract_invoke_cache);
+       if (image->remoting_invoke_cache)
+               g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
+       free_hash (image->remoting_invoke_cache);
+       free_hash (image->runtime_invoke_cache);
+       free_hash (image->runtime_invoke_direct_cache);
+       free_hash (image->runtime_invoke_vcall_cache);
+       free_hash (image->synchronized_cache);
+       free_hash (image->unbox_wrapper_cache);
+       free_hash (image->cominterop_invoke_cache);
+       free_hash (image->cominterop_wrapper_cache);
+       free_hash (image->typespec_cache);
+       free_hash (image->ldfld_wrapper_cache);
+       free_hash (image->ldflda_wrapper_cache);
+       free_hash (image->stfld_wrapper_cache);
+       free_hash (image->isinst_cache);
+       free_hash (image->castclass_cache);
+       free_hash (image->proxy_isinst_cache);
+       free_hash (image->thunk_invoke_cache);
+       free_hash (image->static_rgctx_invoke_cache);
 
        /* The ownership of signatures is not well defined */
        //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
@@ -1299,8 +1544,8 @@ mono_image_close (MonoImage *image)
        if (image->rgctx_template_hash)
                g_hash_table_destroy (image->rgctx_template_hash);
 
-       if (image->generic_class_open_instances_hash)
-               g_hash_table_destroy (image->generic_class_open_instances_hash);
+       if (image->property_hash)
+               mono_property_hash_destroy (image->property_hash);
 
        if (image->interface_bitset) {
                mono_unload_interface_ids (image->interface_bitset);
@@ -1326,6 +1571,11 @@ mono_image_close (MonoImage *image)
                g_free (image->modules_loaded);
        if (image->references)
                g_free (image->references);
+       mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
+
+       DeleteCriticalSection (&image->szarray_cache_lock);
+       DeleteCriticalSection (&image->lock);
+
        /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
        if (!image->dynamic) {
                if (debug_assembly_unload)
@@ -1385,23 +1635,18 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
         * Level 2 holds a directory entry for each language pointing to
         * the actual data.
         */
-       name_offset = GUINT32_FROM_LE (entry->name_offset) & 0x7fffffff;
-       dir_offset = GUINT32_FROM_LE (entry->dir_offset) & 0x7fffffff;
+       is_string = MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING (*entry);
+       name_offset = MONO_PE_RES_DIR_ENTRY_NAME_OFFSET (*entry);
 
-#if G_BYTE_ORDER != G_LITTLE_ENDIAN
-       is_string = (GUINT32_FROM_LE (entry->name_offset) & 0x80000000) != 0;
-       is_dir = (GUINT32_FROM_LE (entry->dir_offset) & 0x80000000) != 0;
-#else
-       is_string = entry->name_is_string;
-       is_dir = entry->is_dir;
-#endif
+       is_dir = MONO_PE_RES_DIR_ENTRY_IS_DIR (*entry);
+       dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
 
        if(level==0) {
-               if((is_string==FALSE && name_offset!=res_id) ||
-                  (is_string==TRUE)) {
-                       return(NULL);
-               }
+               if (is_string)
+                       return NULL;
        } else if (level==1) {
+               if (res_id != name_offset)
+                       return NULL;
 #if 0
                if(name!=NULL &&
                   is_string==TRUE && name!=lookup (name_offset)) {
@@ -1409,12 +1654,8 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
                }
 #endif
        } else if (level==2) {
-               if ((is_string == FALSE &&
-                   name_offset != lang_id &&
-                   lang_id != 0) ||
-                  (is_string == TRUE)) {
-                       return(NULL);
-               }
+               if (is_string == TRUE || (is_string == FALSE && lang_id != 0 && name_offset != lang_id))
+                       return NULL;
        } else {
                g_assert_not_reached ();
        }
@@ -1481,6 +1722,8 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
                return(NULL);
        }
 
+       mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
+
        info=image->image_info;
        if(info==NULL) {
                return(NULL);
@@ -1607,6 +1850,10 @@ mono_image_load_file_for_image (MonoImage *image, int fileidx)
                }
 
                image->files [fileidx - 1] = res;
+#ifdef PLATFORM_WIN32
+               if (res->is_module_handle)
+                       mono_image_fixup_vtable (res);
+#endif
        }
        mono_loader_unlock ();
        g_free (name);
@@ -1658,25 +1905,14 @@ mono_image_strong_name_position (MonoImage *image, guint32 *size)
 {
        MonoCLIImageInfo *iinfo = image->image_info;
        MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
-       const int top = iinfo->cli_section_count;
-       MonoSectionTable *tables = iinfo->cli_section_tables;
-       int i;
-       guint32 addr = de->rva;
-       
+       guint32 pos;
+
        if (size)
                *size = de->size;
        if (!de->size || !de->rva)
                return 0;
-       for (i = 0; i < top; i++){
-               if ((addr >= tables->st_virtual_address) &&
-                   (addr < tables->st_virtual_address + tables->st_raw_data_size)){
-                       return tables->st_raw_data_ptr +
-                               (addr - tables->st_virtual_address);
-               }
-               tables++;
-       }
-
-       return 0;
+       pos = mono_cli_rva_image_map (image, de->rva);
+       return pos == INVALID_ADDRESS ? 0 : pos;
 }
 
 /**
@@ -1817,3 +2053,134 @@ mono_image_has_authenticode_entry (MonoImage *image)
        // the Authenticode "pre" (non ASN.1) header is 8 bytes long
        return ((de->rva != 0) && (de->size > 8));
 }
+
+gpointer
+mono_image_alloc (MonoImage *image, guint size)
+{
+       gpointer res;
+
+       mono_perfcounters->loader_bytes += size;
+       mono_image_lock (image);
+       res = mono_mempool_alloc (image->mempool, size);
+       mono_image_unlock (image);
+
+       return res;
+}
+
+gpointer
+mono_image_alloc0 (MonoImage *image, guint size)
+{
+       gpointer res;
+
+       mono_perfcounters->loader_bytes += size;
+       mono_image_lock (image);
+       res = mono_mempool_alloc0 (image->mempool, size);
+       mono_image_unlock (image);
+
+       return res;
+}
+
+char*
+mono_image_strdup (MonoImage *image, const char *s)
+{
+       char *res;
+
+       mono_perfcounters->loader_bytes += strlen (s);
+       mono_image_lock (image);
+       res = mono_mempool_strdup (image->mempool, s);
+       mono_image_unlock (image);
+
+       return res;
+}
+
+GList*
+g_list_prepend_image (MonoImage *image, GList *list, gpointer data)
+{
+       GList *new_list;
+       
+       new_list = mono_image_alloc (image, sizeof (GList));
+       new_list->data = data;
+       new_list->prev = list ? list->prev : NULL;
+    new_list->next = list;
+
+    if (new_list->prev)
+            new_list->prev->next = new_list;
+    if (list)
+            list->prev = new_list;
+
+       return new_list;
+}
+
+GSList*
+g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
+{
+       GSList *new_list;
+
+       new_list = mono_image_alloc (image, sizeof (GSList));
+       new_list->data = data;
+       new_list->next = NULL;
+
+       return g_slist_concat (list, new_list);
+}
+
+void
+mono_image_lock (MonoImage *image)
+{
+       mono_locks_acquire (&image->lock, ImageDataLock);
+}
+
+void
+mono_image_unlock (MonoImage *image)
+{
+       mono_locks_release (&image->lock, ImageDataLock);
+}
+
+
+/**
+ * mono_image_property_lookup:
+ *
+ * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+gpointer 
+mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
+{
+       gpointer res;
+
+       mono_image_lock (image);
+       res = mono_property_hash_lookup (image->property_hash, subject, property);
+       mono_image_unlock (image);
+
+       return res;
+}
+
+/**
+ * mono_image_property_insert:
+ *
+ * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
+{
+       mono_image_lock (image);
+       mono_property_hash_insert (image->property_hash, subject, property, value);
+       mono_image_unlock (image);
+}
+
+/**
+ * mono_image_property_remove:
+ *
+ * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_remove (MonoImage *image, gpointer subject)
+{
+       mono_image_lock (image);
+       mono_property_hash_remove_object (image->property_hash, subject);
+       mono_image_unlock (image);
+}