2005-12-23 Dick Porter <dick@ximian.com>
[mono.git] / mono / metadata / image.c
index 42038eabbb85b6eaff118eb30158817503b0072d..6d412ea565c21d1835c1fe36b004b7fc320a1bc8 100644 (file)
  */
 static GHashTable *loaded_images_hash;
 static GHashTable *loaded_images_guid_hash;
-
+static GHashTable *loaded_images_refonly_hash;
+static GHashTable *loaded_images_refonly_guid_hash;
+#define mono_images_lock() EnterCriticalSection (&images_mutex)
+#define mono_images_unlock() LeaveCriticalSection (&images_mutex)
 static CRITICAL_SECTION images_mutex;
 
 guint32
@@ -131,6 +134,8 @@ mono_images_init (void)
 
        loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
        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);
 }
 
 /**
@@ -314,7 +319,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);
@@ -450,6 +460,7 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
        MonoTableInfo *t;
        int i;
        char *base_dir;
+       gboolean refonly = image->ref_only;
 
        if (image->modules)
                return;
@@ -466,7 +477,7 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
                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 (module_ref, status);
+               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); */
                }
@@ -551,9 +562,10 @@ register_guid (gpointer key, gpointer value, gpointer user_data)
 }
 
 static void
-build_guid_table (void)
+build_guid_table (gboolean refonly)
 {
-       g_hash_table_foreach (loaded_images_hash, register_guid, NULL);
+       GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
+       g_hash_table_foreach (loaded_images, register_guid, NULL);
 }
 
 void
@@ -761,7 +773,7 @@ invalid_image:
 
 static MonoImage *
 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
-                   gboolean care_about_cli)
+                   gboolean care_about_cli, gboolean refonly)
 {
        MonoCLIImageInfo *iinfo;
        MonoImage *image;
@@ -788,58 +800,74 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
        iinfo = g_new0 (MonoCLIImageInfo, 1);
        image->image_info = iinfo;
        image->name = canonicalize_path (fname);
+       image->ref_only = refonly;
 
        return do_mono_image_load (image, status, care_about_cli);
 }
 
 MonoImage *
-mono_image_loaded (const char *name)
+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);
-       res = g_hash_table_lookup (loaded_images_hash, name);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_lock ();
+       res = g_hash_table_lookup (loaded_images, name);
+       mono_images_unlock ();
        return res;
 }
 
 MonoImage *
-mono_image_loaded_by_guid (const char *guid)
+mono_image_loaded (const char *name)
+{
+       return mono_image_loaded_full (name, FALSE);
+}
+
+MonoImage *
+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);
-       res = g_hash_table_lookup (loaded_images_guid_hash, guid);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_lock ();
+       res = g_hash_table_lookup (loaded_images, guid);
+       mono_images_unlock ();
        return res;
 }
 
+MonoImage *
+mono_image_loaded_by_guid (const char *guid)
+{
+       return mono_image_loaded_by_guid_full (guid, FALSE);
+}
+
 static MonoImage *
 register_image (MonoImage *image)
 {
        MonoImage *image2;
+       GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
 
-       EnterCriticalSection (&images_mutex);
-       image2 = g_hash_table_lookup (loaded_images_hash, image->name);
+       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;
        }
-       g_hash_table_insert (loaded_images_hash, image->name, image);
-       if (image->assembly_name && (g_hash_table_lookup (loaded_images_hash, image->assembly_name) == NULL))
-               g_hash_table_insert (loaded_images_hash, (char *) image->assembly_name, image); 
-       g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
-       LeaveCriticalSection (&images_mutex);
+       g_hash_table_insert (loaded_images, image->name, 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);
+       mono_images_unlock ();
 
        return image;
 }
 
 MonoImage *
-mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
+mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
 {
        MonoCLIImageInfo *iinfo;
        MonoImage *image;
@@ -869,6 +897,7 @@ mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, Mon
        image->name = g_strdup_printf ("data-%p", datac);
        iinfo = g_new0 (MonoCLIImageInfo, 1);
        image->image_info = iinfo;
+       image->ref_only = refonly;
 
        image = do_mono_image_load (image, status, TRUE);
        if (image == NULL)
@@ -877,18 +906,17 @@ mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, Mon
        return register_image (image);
 }
 
-/**
- * mono_image_open:
- * @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.
- * if NULL, then check the value of @status for details on the error
- */
 MonoImage *
-mono_image_open (const char *fname, MonoImageOpenStatus *status)
+mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
+{
+       return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
+}
+
+MonoImage *
+mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
 {
        MonoImage *image;
+       GHashTable *loaded_images;
        char *absfname;
        
        g_return_val_if_fail (fname != NULL, NULL);
@@ -901,24 +929,39 @@ mono_image_open (const char *fname, MonoImageOpenStatus *status)
         * happen outside the mutex, and if multiple threads happen to load
         * the same image, we discard all but the first copy.
         */
-       EnterCriticalSection (&images_mutex);
-       image = g_hash_table_lookup (loaded_images_hash, absfname);
+       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);
+       image = do_mono_image_open (fname, status, TRUE, refonly);
        if (image == NULL)
                return NULL;
 
        return register_image (image);
 }
 
+/**
+ * mono_image_open:
+ * @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.
+ * if NULL, then check the value of @status for details on the error
+ */
+MonoImage *
+mono_image_open (const char *fname, MonoImageOpenStatus *status)
+{
+       return mono_image_open_full (fname, status, FALSE);
+}
+
 /**
  * mono_pe_file_open:
  * @fname: filename that points to the module we want to open
@@ -935,7 +978,7 @@ 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));
+       return(do_mono_image_open (fname, status, FALSE, FALSE));
 }
 
 static void
@@ -991,6 +1034,7 @@ void
 mono_image_close (MonoImage *image)
 {
        MonoImage *image2;
+       GHashTable *loaded_images, *loaded_images_guid;
        int i;
 
        g_return_if_fail (image != NULL);
@@ -998,18 +1042,20 @@ mono_image_close (MonoImage *image)
        if (InterlockedDecrement (&image->ref_count))
                return;
        
-       EnterCriticalSection (&images_mutex);
-       image2 = g_hash_table_lookup (loaded_images_hash, image->name);
+       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);
        if (image == image2) {
                /* This is not true if we are called from mono_image_open () */
-               g_hash_table_remove (loaded_images_hash, image->name);
-               g_hash_table_remove (loaded_images_guid_hash, image->guid);
+               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 ();
+               build_guid_table (image->ref_only);
        }
-       if (image->assembly_name && (g_hash_table_lookup (loaded_images_hash, image->assembly_name) == image))
-               g_hash_table_remove (loaded_images_hash, (char *) image->assembly_name);        
-       LeaveCriticalSection (&images_mutex);
+       if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
+               g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
+       mono_images_unlock ();
 
        if (image->file_descr) {
                fclose (image->file_descr);
@@ -1212,6 +1258,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)
 {
@@ -1270,12 +1326,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)
 {