2007-05-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mono / metadata / image.c
index 6d412ea565c21d1835c1fe36b004b7fc320a1bc8..72b1bb40ce1178f9fb3ab6e88d9e7072840a89cd 100644 (file)
 #include "tabledefs.h"
 #include "tokentype.h"
 #include "metadata-internals.h"
+#include "loader.h"
 #include <mono/io-layer/io-layer.h>
+#include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-path.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -36,6 +39,9 @@ static GHashTable *loaded_images_hash;
 static GHashTable *loaded_images_guid_hash;
 static GHashTable *loaded_images_refonly_hash;
 static GHashTable *loaded_images_refonly_guid_hash;
+
+static gboolean debug_assembly_unload = FALSE;
+
 #define mono_images_lock() EnterCriticalSection (&images_mutex)
 #define mono_images_unlock() LeaveCriticalSection (&images_mutex)
 static CRITICAL_SECTION images_mutex;
@@ -57,6 +63,17 @@ mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
        return INVALID_ADDRESS;
 }
 
+/**
+ * mono_images_rva_map:
+ * @image: a MonoImage
+ * @addr: relative virtual address (RVA)
+ *
+ * This is a low-level routine used by the runtime to map relative
+ * virtual address (RVA) into their location in memory. 
+ *
+ * Returns: the address in memory for the given RVA, or NULL if the
+ * RVA is not valid for this image. 
+ */
 char *
 mono_image_rva_map (MonoImage *image, guint32 addr)
 {
@@ -80,48 +97,6 @@ mono_image_rva_map (MonoImage *image, guint32 addr)
        return NULL;
 }
 
-static gchar *
-canonicalize_path (const char *path)
-{
-       gchar *abspath, *pos, *lastpos, *dest;
-       int backc;
-
-       if (g_path_is_absolute (path)) {
-               abspath = g_strdup (path);
-       } else {
-               gchar *tmpdir = g_get_current_dir ();
-               abspath = g_build_filename (tmpdir, path, NULL);
-               g_free (tmpdir);
-       }
-
-       abspath = g_strreverse (abspath);
-
-       backc = 0;
-       dest = lastpos = abspath;
-       pos = strchr (lastpos, G_DIR_SEPARATOR);
-
-       while (pos != NULL) {
-               int len = pos - lastpos;
-               if (len == 1 && lastpos [0] == '.') {
-                       // nop
-               } else if (len == 2 && lastpos [0] == '.' && lastpos [1] == '.') {
-                       backc++;
-               } else if (len > 0) {
-                       if (backc > 0) {
-                               backc--;
-                       } else {
-                               if (dest != lastpos) strncpy (dest, lastpos, len + 1);
-                               dest += len + 1;
-                       }
-               }
-               lastpos = pos + 1;
-               pos = strchr (lastpos, G_DIR_SEPARATOR);
-       }
-       
-       if (dest != lastpos) strcpy (dest, lastpos);
-       return g_strreverse (abspath);
-}
-
 /**
  * mono_images_init:
  *
@@ -136,6 +111,24 @@ mono_images_init (void)
        loaded_images_guid_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);
        loaded_images_refonly_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
+
+       debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
+}
+
+/**
+ * mono_images_cleanup:
+ *
+ *  Free all resources used by this module.
+ */
+void
+mono_images_cleanup (void)
+{
+       DeleteCriticalSection (&images_mutex);
+
+       g_hash_table_destroy (loaded_images_hash);
+       g_hash_table_destroy (loaded_images_guid_hash);
+       g_hash_table_destroy (loaded_images_refonly_hash);
+       g_hash_table_destroy (loaded_images_refonly_guid_hash);
 }
 
 /**
@@ -363,8 +356,11 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
                        image->heap_guid.size = read32 (ptr + 4);
                        ptr += 8 + 6;
                } else if (strncmp (ptr + 8, "#-", 3) == 0) {
-                       g_print ("Assembly '%s' has the non-standard metadata heap #-.\nRecompile it correctly (without the /incremental switch or in Release mode).", image->name);
-                       return FALSE;
+                       image->heap_tables.data = image->raw_metadata + read32 (ptr);
+                       image->heap_tables.size = read32 (ptr + 4);
+                       ptr += 8 + 3;
+                       image->uncompressed_metadata = TRUE;
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly '%s' has the non-standard metadata heap #-.\nRecompile it correctly (without the /incremental switch or in Release mode).\n", image->name);
                } else {
                        g_message ("Unknown heap type: %s\n", ptr + 8);
                        ptr += 8 + strlen (ptr + 8) + 1;
@@ -377,7 +373,7 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
        g_assert (image->heap_guid.data);
        g_assert (image->heap_guid.size >= 16);
 
-       image->guid = mono_guid_to_string (image->heap_guid.data);
+       image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
 
        return TRUE;
 }
@@ -441,115 +437,121 @@ load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
 }
 
 void
-mono_image_add_to_name_cache (MonoImage *image, const char *nspace, 
-                                                         const char *name, guint32 index)
+mono_image_check_for_module_cctor (MonoImage *image)
 {
-       GHashTable *nspace_table;
-       GHashTable *name_cache = image->name_cache;
-
-       if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
-               nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
-               g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
+       MonoTableInfo *t, *mt;
+       t = &image->tables [MONO_TABLE_TYPEDEF];
+       mt = &image->tables [MONO_TABLE_METHOD];
+       if (mono_get_runtime_info ()->framework_version [0] == '1') {
+               image->checked_module_cctor = TRUE;
+               return;
        }
-       g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
+       if (t->rows >= 1) {
+               guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
+               const char *name = mono_metadata_string_heap (image, nameidx);
+               if (strcmp (name, "<Module>") == 0) {
+                       guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
+                       guint32 last_method;
+                       if (t->rows > 1)
+                               last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
+                       else 
+                               last_method = mt->rows;
+                       for (; first_method < last_method; first_method++) {
+                               nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
+                               name = mono_metadata_string_heap (image, nameidx);
+                               if (strcmp (name, ".cctor") == 0) {
+                                       image->has_module_cctor = TRUE;
+                                       image->checked_module_cctor = TRUE;
+                                       return;
+                               }
+                       }
+               }
+       }
+       image->has_module_cctor = FALSE;
+       image->checked_module_cctor = TRUE;
 }
 
 static void
-load_modules (MonoImage *image, MonoImageOpenStatus *status)
+load_modules (MonoImage *image)
 {
        MonoTableInfo *t;
-       int i;
-       char *base_dir;
-       gboolean refonly = image->ref_only;
 
        if (image->modules)
                return;
 
        t = &image->tables [MONO_TABLE_MODULEREF];
        image->modules = g_new0 (MonoImage *, t->rows);
+       image->modules_loaded = g_new0 (gboolean, t->rows);
        image->module_count = t->rows;
-       base_dir = g_path_get_dirname (image->name);
-       for (i = 0; i < t->rows; i++){
-               char *module_ref;
-               const char *name;
-               guint32 cols [MONO_MODULEREF_SIZE];
-
-               mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
-               name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
-               module_ref = g_build_filename (base_dir, name, NULL);
-               image->modules [i] = mono_image_open_full (module_ref, status, refonly);
-               if (image->modules [i]) {
-                       /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
-               }
-               /* 
-                * FIXME: what do we do here? it could be a native dll...
-                * We should probably do lazy-loading of modules.
-                */
-               if (status)
-                       *status = MONO_IMAGE_OK;
-               g_free (module_ref);
-       }
-
-       g_free (base_dir);
 }
 
-static void
-load_class_names (MonoImage *image)
+/**
+ * mono_image_load_module:
+ *
+ *   Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
+ * it cannot be loaded.
+ */
+MonoImage*
+mono_image_load_module (MonoImage *image, int idx)
 {
-       MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
-       guint32 cols [MONO_TYPEDEF_SIZE];
-       const char *name;
-       const char *nspace;
-       guint32 i, visib, nspace_index;
-       GHashTable *name_cache2, *nspace_table;
-
-       /* Temporary hash table to avoid lookups in the nspace_table */
-       name_cache2 = g_hash_table_new (NULL, NULL);
-
-       for (i = 1; i <= t->rows; ++i) {
-               mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
-               /* nested types are accessed from the nesting name */
-               visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
-               if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
+       MonoTableInfo *t;
+       MonoTableInfo *file_table;
+       int i;
+       char *base_dir;
+       gboolean refonly = image->ref_only;
+       GList *list_iter, *valid_modules = NULL;
+       MonoImageOpenStatus status;
+
+       g_assert (idx <= image->module_count);
+       if (image->modules_loaded [idx - 1])
+               return image->modules [idx - 1];
+
+       file_table = &image->tables [MONO_TABLE_FILE];
+       for (i = 0; i < file_table->rows; i++) {
+               guint32 cols [MONO_FILE_SIZE];
+               mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
+               if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
                        continue;
-               name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
-               nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
-
-               nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
-               nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
-               if (!nspace_table) {
-                       nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
-                       g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
-                       g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
-                                                                nspace_table);
-               }
-               g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (i));
+               valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
        }
 
-       /* Load type names from EXPORTEDTYPES table */
+       t = &image->tables [MONO_TABLE_MODULEREF];
+       base_dir = g_path_get_dirname (image->name);
+
        {
-               MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
-               guint32 cols [MONO_EXP_TYPE_SIZE];
-               int i;
+               char *module_ref;
+               const char *name;
+               guint32 cols [MONO_MODULEREF_SIZE];
+               /* if there is no file table, we try to load the module... */
+               int valid = file_table->rows == 0;
 
-               for (i = 0; i < t->rows; ++i) {
-                       mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
-                       name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
-                       nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
-
-                       nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
-                       nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
-                       if (!nspace_table) {
-                               nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
-                               g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
-                               g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
-                                                                        nspace_table);
+               mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
+               name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
+               for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
+                       /* be safe with string dups, but we could just compare string indexes  */
+                       if (strcmp (list_iter->data, name) == 0) {
+                               valid = TRUE;
+                               break;
                        }
-                       g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
+               }
+               if (valid) {
+                       module_ref = g_build_filename (base_dir, name, NULL);
+                       image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
+                       if (image->modules [idx - 1]) {
+                               mono_image_addref (image->modules [idx - 1]);
+                               image->modules [idx - 1]->assembly = image->assembly;
+                               /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
+                       }
+                       g_free (module_ref);
                }
        }
 
-       g_hash_table_destroy (name_cache2);
+       image->modules_loaded [idx - 1] = TRUE;
+
+       g_free (base_dir);
+       g_list_free (valid_modules);
+
+       return image->modules [idx - 1];
 }
 
 static void
@@ -575,8 +577,6 @@ mono_image_init (MonoImage *image)
        image->method_cache = g_hash_table_new (NULL, NULL);
        image->class_cache = g_hash_table_new (NULL, NULL);
        image->field_cache = g_hash_table_new (NULL, NULL);
-       image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
-       image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        image->delegate_begin_invoke_cache = 
                g_hash_table_new ((GHashFunc)mono_signature_hash, 
@@ -594,12 +594,24 @@ mono_image_init (MonoImage *image)
        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->ldfld_remote_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->stfld_remote_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);
 }
 
 static MonoImage *
@@ -744,8 +756,6 @@ do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
        if (!load_metadata (image, iinfo))
                goto invalid_image;
 
-       load_class_names (image);
-
        /* modules don't have an assembly table row */
        if (image->tables [MONO_TABLE_ASSEMBLY].rows)
                image->assembly_name = mono_metadata_string_heap (image, 
@@ -756,14 +766,12 @@ do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
                        mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
                                        0, MONO_MODULE_NAME));
 
-       load_modules (image, status);
+       load_modules (image);
 
 done:
        if (status)
                *status = MONO_IMAGE_OK;
 
-       image->ref_count=1;
-
        return image;
 
 invalid_image:
@@ -793,14 +801,14 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
                return NULL;
        }
        image = g_new0 (MonoImage, 1);
-       image->ref_count = 1;
        image->file_descr = filed;
        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 = canonicalize_path (fname);
+       image->name = mono_path_resolve_symlinks (fname);
        image->ref_only = refonly;
+       image->ref_count = 1;
 
        return do_mono_image_load (image, status, care_about_cli);
 }
@@ -817,6 +825,14 @@ mono_image_loaded_full (const char *name, gboolean refonly)
        return res;
 }
 
+/**
+ * mono_image_loaded:
+ * @name: name of the image to load
+ *
+ * This routine ensures that the given image is loaded.
+ *
+ * Returns: the loaded MonoImage, or NULL on failure.
+ */
 MonoImage *
 mono_image_loaded (const char *name)
 {
@@ -890,7 +906,6 @@ mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy
        }
 
        image = g_new0 (MonoImage, 1);
-       image->ref_count = 1;
        image->raw_data = datac;
        image->raw_data_len = data_len;
        image->raw_data_allocated = need_copy;
@@ -921,7 +936,7 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
        
        g_return_val_if_fail (fname != NULL, NULL);
        
-       absfname = canonicalize_path (fname);
+       absfname = mono_path_canonicalize (fname);
 
        /*
         * The easiest solution would be to do all the loading inside the mutex,
@@ -953,7 +968,9 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
  * @fname: filename that points to the module we want to open
  * @status: An error condition is returned in this field
  *
- * Returns: An open image of type %MonoImage or NULL on error.
+ * Returns: An open image of type %MonoImage or NULL on error. 
+ * The caller holds a temporary reference to the returned image which should be cleared 
+ * when no longer needed by calling mono_image_close ().
  * if NULL, then check the value of @status for details on the error
  */
 MonoImage *
@@ -987,11 +1004,19 @@ free_hash_table (gpointer key, gpointer val, gpointer user_data)
        g_hash_table_destroy ((GHashTable*)val);
 }
 
+/*
 static void
 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
 {
        mono_metadata_free_method_signature ((MonoMethodSignature*)val);
 }
+*/
+
+static void
+free_blob_cache_entry (gpointer key, gpointer val, gpointer user_data)
+{
+       g_free (key);
+}
 
 static void
 free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
@@ -999,6 +1024,12 @@ free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
        g_free (val);
 }
 
+static void
+free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
+{
+       g_slist_free ((GSList*)val);
+}
+
 /**
  * mono_image_addref:
  * @image: The image file we wish to add a reference to
@@ -1039,8 +1070,10 @@ mono_image_close (MonoImage *image)
 
        g_return_if_fail (image != NULL);
 
-       if (InterlockedDecrement (&image->ref_count))
+       if (InterlockedDecrement (&image->ref_count) > 0)
                return;
+
+       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
        
        mono_images_lock ();
        loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
@@ -1050,11 +1083,13 @@ mono_image_close (MonoImage *image)
                /* This is not true if we are called from mono_image_open () */
                g_hash_table_remove (loaded_images, image->name);
                g_hash_table_remove (loaded_images_guid, image->guid);
-               /* Multiple images might have the same guid */
-               build_guid_table (image->ref_only);
        }
        if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
                g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
+
+       /* Multiple images might have the same guid */
+       build_guid_table (image->ref_only);
+
        mono_images_unlock ();
 
        if (image->file_descr) {
@@ -1079,16 +1114,29 @@ mono_image_close (MonoImage *image)
 
                g_free (image->raw_data);
        }
-       g_free (image->name);
-       g_free (image->guid);
-       g_free (image->files);
+
+       if (debug_assembly_unload) {
+               image->name = g_strdup_printf ("%s - UNLOADED", image->name);
+       } else {
+               g_free (image->name);
+               g_free (image->guid);
+               g_free (image->version);
+               g_free (image->files);
+       }
 
        g_hash_table_destroy (image->method_cache);
        g_hash_table_destroy (image->class_cache);
        g_hash_table_destroy (image->field_cache);
-       g_hash_table_destroy (image->array_cache);
-       g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
-       g_hash_table_destroy (image->name_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->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);
@@ -1099,12 +1147,29 @@ mono_image_close (MonoImage *image)
        g_hash_table_destroy (image->runtime_invoke_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_foreach (image->memberref_signatures, free_mr_signatures, NULL);
+       g_hash_table_destroy (image->ldfld_wrapper_cache);
+       g_hash_table_destroy (image->ldflda_wrapper_cache);
+       g_hash_table_destroy (image->ldfld_remote_wrapper_cache);
+       g_hash_table_destroy (image->stfld_wrapper_cache);
+       g_hash_table_destroy (image->stfld_remote_wrapper_cache);
+       g_hash_table_destroy (image->isinst_cache);
+       g_hash_table_destroy (image->castclass_cache);
+       g_hash_table_destroy (image->proxy_isinst_cache);
+
+       /* The ownership of signatures is not well defined */
+       //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
        g_hash_table_destroy (image->memberref_signatures);
-       g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
+       //g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
        g_hash_table_destroy (image->helper_signatures);
-       
+       g_hash_table_destroy (image->method_signatures);
+
+       if (image->interface_bitset) {
+               mono_unload_interface_ids (image->interface_bitset);
+               mono_bitset_free (image->interface_bitset);
+       }
        if (image->image_info){
                MonoCLIImageInfo *ii = image->image_info;
 
@@ -1119,10 +1184,18 @@ mono_image_close (MonoImage *image)
                if (image->modules [i])
                        mono_image_close (image->modules [i]);
        }
+       if (image->modules)
+               g_free (image->modules);
+       if (image->references)
+               g_free (image->references);
        /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
        if (!image->dynamic) {
-               mono_mempool_destroy (image->mempool);
-               g_free (image);
+               if (debug_assembly_unload)
+                       mono_mempool_invalidate (image->mempool);
+               else {
+                       mono_mempool_destroy (image->mempool);
+                       g_free (image);
+               }
        } else {
                /* Dynamic images are GC_MALLOCed */
                struct _MonoDynamicImage *di = (struct _MonoDynamicImage*)image;
@@ -1136,8 +1209,10 @@ mono_image_close (MonoImage *image)
                        g_hash_table_destroy (di->handleref);
                if (di->tokens)
                        mono_g_hash_table_destroy (di->tokens);
-               if (di->blob_cache)
+               if (di->blob_cache) {
+                       g_hash_table_foreach (di->blob_cache, free_blob_cache_entry, NULL);
                        g_hash_table_destroy (di->blob_cache);
+               }
                g_list_free (di->array_methods);
                if (di->gen_params)
                        g_ptr_array_free (di->gen_params, TRUE);
@@ -1194,7 +1269,8 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
                               MonoPEResourceDirEntry *entry,
                               MonoPEResourceDir *root, guint32 level)
 {
-       gboolean is_string=entry->name_is_string;
+       gboolean is_string, is_dir;
+       guint32 name_offset, dir_offset;
 
        /* Level 0 holds a directory entry for each type of resource
         * (identified by ID or name).
@@ -1206,22 +1282,32 @@ 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;
+
+#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
 
        if(level==0) {
-               if((is_string==FALSE && entry->name_offset!=res_id) ||
+               if((is_string==FALSE && name_offset!=res_id) ||
                   (is_string==TRUE)) {
                        return(NULL);
                }
        } else if (level==1) {
 #if 0
                if(name!=NULL &&
-                  is_string==TRUE && name!=lookup (entry->name_offset)) {
+                  is_string==TRUE && name!=lookup (name_offset)) {
                        return(NULL);
                }
 #endif
        } else if (level==2) {
                if ((is_string == FALSE &&
-                   entry->name_offset != lang_id &&
+                   name_offset != lang_id &&
                    lang_id != 0) ||
                   (is_string == TRUE)) {
                        return(NULL);
@@ -1230,12 +1316,12 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
                g_assert_not_reached ();
        }
 
-       if(entry->is_dir==TRUE) {
-               MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+entry->dir_offset);
+       if(is_dir==TRUE) {
+               MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
                MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
                guint32 entries, i;
-               
-               entries=res_dir->res_named_entries + res_dir->res_id_entries;
+
+               entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
 
                for(i=0; i<entries; i++) {
                        MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
@@ -1252,9 +1338,17 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
 
                return(NULL);
        } else {
-               MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+entry->dir_offset);
-               
-               return(data_entry);
+               MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
+               MonoPEResourceDataEntry *res;
+
+               res = g_new0 (MonoPEResourceDataEntry, 1);
+
+               res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
+               res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
+               res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
+               res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
+
+               return (res);
        }
 }
 
@@ -1266,7 +1360,8 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
  * @name: the resource name to lookup.
  *
  * Returns: NULL if not found, otherwise a pointer to the in-memory representation
- * of the given resource.
+ * of the given resource. The caller should free it using g_free () when no longer
+ * needed.
  */
 gpointer
 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
@@ -1307,8 +1402,8 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
        if(resource_dir==NULL) {
                return(NULL);
        }
-       
-       entries=resource_dir->res_named_entries + resource_dir->res_id_entries;
+
+       entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
        res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
        
        for(i=0; i<entries; i++) {
@@ -1412,6 +1507,17 @@ mono_image_load_file_for_image (MonoImage *image, int fileidx)
        return res;
 }
 
+/**
+ * mono_image_get_strong_name:
+ * @image: a MonoImage
+ * @size: a guint32 pointer, or NULL.
+ *
+ * If the image has a strong name, and @size is not NULL, the value
+ * pointed to by size will have the size of the strong name.
+ *
+ * Returns: NULL if the image does not have a strong name, or a
+ * pointer to the public key.
+ */
 const char*
 mono_image_get_strong_name (MonoImage *image, guint32 *size)
 {
@@ -1429,6 +1535,17 @@ mono_image_get_strong_name (MonoImage *image, guint32 *size)
        return data;
 }
 
+/**
+ * mono_image_strong_name_position:
+ * @image: a MonoImage
+ * @size: a guint32 pointer, or NULL.
+ *
+ * If the image has a strong name, and @size is not NULL, the value
+ * pointed to by size will have the size of the strong name.
+ *
+ * Returns: the position within the image file where the strong name
+ * is stored.
+ */
 guint32
 mono_image_strong_name_position (MonoImage *image, guint32 *size)
 {
@@ -1455,6 +1572,19 @@ mono_image_strong_name_position (MonoImage *image, guint32 *size)
        return 0;
 }
 
+/**
+ * mono_image_get_public_key:
+ * @image: a MonoImage
+ * @size: a guint32 pointer, or NULL.
+ *
+ * This is used to obtain the public key in the @image.
+ * 
+ * If the image has a public key, and @size is not NULL, the value
+ * pointed to by size will have the size of the public key.
+ * 
+ * Returns: NULL if the image does not have a public key, or a pointer
+ * to the public key.
+ */
 const char*
 mono_image_get_public_key (MonoImage *image, guint32 *size)
 {
@@ -1472,12 +1602,26 @@ mono_image_get_public_key (MonoImage *image, guint32 *size)
        return pubkey;
 }
 
+/**
+ * mono_image_get_name:
+ * @name: a MonoImage
+ *
+ * Returns: the name of the assembly.
+ */
 const char*
 mono_image_get_name (MonoImage *image)
 {
        return image->assembly_name;
 }
 
+/**
+ * mono_image_get_filename:
+ * @image: a MonoImage
+ *
+ * Used to get the filename that hold the actual MonoImage
+ *
+ * Returns: the filename.
+ */
 const char*
 mono_image_get_filename (MonoImage *image)
 {
@@ -1512,18 +1656,45 @@ mono_table_info_get_rows (const MonoTableInfo *table)
        return table->rows;
 }
 
+/**
+ * mono_image_get_assembly:
+ * @image: the MonoImage.
+ *
+ * Use this routine to get the assembly that owns this image.
+ *
+ * Returns: the assembly that holds this image.
+ */
 MonoAssembly* 
 mono_image_get_assembly (MonoImage *image)
 {
        return image->assembly;
 }
 
+/**
+ * mono_image_is_dynamic:
+ * @image: the MonoImage
+ *
+ * Determines if the given image was created dynamically through the
+ * System.Reflection.Emit API
+ *
+ * Returns: TRUE if the image was created dynamically, FALSE if not.
+ */
 gboolean
 mono_image_is_dynamic (MonoImage *image)
 {
        return image->dynamic;
 }
 
+/**
+ * mono_image_has_authenticode_entry:
+ * @image: the MonoImage
+ *
+ * Use this routine to determine if the image has a Authenticode
+ * Certificate Table.
+ *
+ * Returns: TRUE if the image contains an authenticode entry in the PE
+ * directory.
+ */
 gboolean
 mono_image_has_authenticode_entry (MonoImage *image)
 {