2006-09-14 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / image.c
index 1db273008c8d2d1ae7889118b664a970c122717f..0a1220b0300dfcc7e6da27c20e2e7f43285d621a 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>
@@ -37,6 +40,10 @@ 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;
 
 guint32
@@ -79,48 +86,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:
  *
@@ -135,6 +100,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);
 }
 
 /**
@@ -318,7 +301,12 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
        if (strncmp (ptr, "BSJB", 4) == 0){
                guint32 version_string_len;
 
-               ptr += 12;
+               ptr += 4;
+               image->md_version_major = read16 (ptr);
+               ptr += 4;
+               image->md_version_minor = read16 (ptr);
+               ptr += 4;
+
                version_string_len = read32 (ptr);
                ptr += 4;
                image->version = g_strndup (ptr, version_string_len);
@@ -371,7 +359,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;
 }
@@ -434,31 +422,28 @@ load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
        return load_tables (image);
 }
 
-void
-mono_image_add_to_name_cache (MonoImage *image, const char *nspace, 
-                                                         const char *name, guint32 index)
-{
-       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);
-       }
-       g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
-}
-
 static void
 load_modules (MonoImage *image, MonoImageOpenStatus *status)
 {
        MonoTableInfo *t;
+       MonoTableInfo *file_table;
        int i;
        char *base_dir;
        gboolean refonly = image->ref_only;
+       GList *list_iter, *valid_modules = NULL;
 
        if (image->modules)
                return;
 
+       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;
+               valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
+       }
+
        t = &image->tables [MONO_TABLE_MODULEREF];
        image->modules = g_new0 (MonoImage *, t->rows);
        image->module_count = t->rows;
@@ -467,17 +452,27 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
                char *module_ref;
                const char *name;
                guint32 cols [MONO_MODULEREF_SIZE];
+               int valid = 0;
 
                mono_metadata_decode_row (t, i, 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;
+                       }
+               }
+               if (!valid)
+                       continue;
                module_ref = g_build_filename (base_dir, name, NULL);
                image->modules [i] = mono_image_open_full (module_ref, status, refonly);
                if (image->modules [i]) {
+                       mono_image_addref (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.
+                * FIXME: We should probably do lazy-loading of modules.
                 */
                if (status)
                        *status = MONO_IMAGE_OK;
@@ -485,65 +480,7 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
        }
 
        g_free (base_dir);
-}
-
-static void
-load_class_names (MonoImage *image)
-{
-       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)
-                       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));
-       }
-
-       /* Load type names from EXPORTEDTYPES table */
-       {
-               MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
-               guint32 cols [MONO_EXP_TYPE_SIZE];
-               int i;
-
-               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);
-                       }
-                       g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
-               }
-       }
-
-       g_hash_table_destroy (name_cache2);
+       g_list_free (valid_modules);
 }
 
 static void
@@ -569,8 +506,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, 
@@ -588,12 +523,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 *
@@ -738,8 +685,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,8 +701,6 @@ done:
        if (status)
                *status = MONO_IMAGE_OK;
 
-       image->ref_count=1;
-
        return image;
 
 invalid_image:
@@ -787,14 +730,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);
 }
@@ -805,9 +748,9 @@ mono_image_loaded_full (const char *name, gboolean refonly)
        MonoImage *res;
        GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
         
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        res = g_hash_table_lookup (loaded_images, name);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
        return res;
 }
 
@@ -823,9 +766,9 @@ mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
        MonoImage *res;
        GHashTable *loaded_images = refonly ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
 
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        res = g_hash_table_lookup (loaded_images, guid);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
        return res;
 }
 
@@ -841,13 +784,13 @@ register_image (MonoImage *image)
        MonoImage *image2;
        GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
 
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        image2 = g_hash_table_lookup (loaded_images, image->name);
 
        if (image2) {
                /* Somebody else beat us to it */
                mono_image_addref (image2);
-               LeaveCriticalSection (&images_mutex);
+               mono_images_unlock ();
                mono_image_close (image);
                return image2;
        }
@@ -855,7 +798,7 @@ register_image (MonoImage *image)
        if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == NULL))
                g_hash_table_insert (loaded_images, (char *) image->assembly_name, image);      
        g_hash_table_insert (image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash, image->guid, image);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
 
        return image;
 }
@@ -884,7 +827,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;
@@ -915,7 +857,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,
@@ -923,17 +865,17 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
         * happen outside the mutex, and if multiple threads happen to load
         * the same image, we discard all but the first copy.
         */
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
        image = g_hash_table_lookup (loaded_images, absfname);
        g_free (absfname);
        
        if (image){
                mono_image_addref (image);
-               LeaveCriticalSection (&images_mutex);
+               mono_images_unlock ();
                return image;
        }
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
 
        image = do_mono_image_open (fname, status, TRUE, refonly);
        if (image == NULL)
@@ -947,7 +889,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 *
@@ -981,11 +925,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)
@@ -993,6 +945,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
@@ -1033,10 +991,12 @@ 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);
        
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
        loaded_images_guid = image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
        image2 = g_hash_table_lookup (loaded_images, image->name);
@@ -1044,12 +1004,14 @@ 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);     
-       LeaveCriticalSection (&images_mutex);
+
+       /* Multiple images might have the same guid */
+       build_guid_table (image->ref_only);
+
+       mono_images_unlock ();
 
        if (image->file_descr) {
                fclose (image->file_descr);
@@ -1073,16 +1035,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);
@@ -1093,12 +1068,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;
 
@@ -1113,10 +1105,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;
@@ -1130,8 +1130,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);
@@ -1252,6 +1254,16 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
        }
 }
 
+/**
+ * mono_image_lookup_resource:
+ * @image: the image to look up the resource in
+ * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
+ * @lang_id: The language id.
+ * @name: the resource name to lookup.
+ *
+ * Returns: NULL if not found, otherwise a pointer to the in-memory representation
+ * of the given resource.
+ */
 gpointer
 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
 {
@@ -1310,12 +1322,33 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
        return(NULL);
 }
 
+/** 
+ * mono_image_get_entry_point:
+ * @image: the image where the entry point will be looked up.
+ *
+ * Use this routine to determine the metadata token for method that
+ * has been flagged as the entry point.
+ *
+ * Returns: the token for the entry point method in the image
+ */
 guint32
 mono_image_get_entry_point (MonoImage *image)
 {
        return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
 }
 
+/**
+ * mono_image_get_resource:
+ * @image: the image where the resource will be looked up.
+ * @offset: The offset to add to the resource
+ * @size: a pointer to an int where the size of the resource will be stored
+ *
+ * This is a low-level routine that fetches a resource from the
+ * metadata that starts at a given @offset.  The @size parameter is
+ * filled with the data field as encoded in the metadata.
+ *
+ * Returns: the pointer to the resource whose offset is @offset.
+ */
 const char*
 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
 {