Sat Jan 8 19:03:26 CET 2005 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / image.c
index 852e1c2991cb2656fc405353bc995893a3bb112b..19aee1acc578ca983a9d281a294282c591a40404 100644 (file)
@@ -22,6 +22,7 @@
 #include "private.h"
 #include "tabledefs.h"
 #include "tokentype.h"
+#include "metadata-internals.h"
 #include <mono/io-layer/io-layer.h>
 
 #define INVALID_ADDRESS 0xffffffff
@@ -52,8 +53,9 @@ mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
 }
 
 char *
-mono_cli_rva_map (MonoCLIImageInfo *iinfo, guint32 addr)
+mono_image_rva_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 +63,10 @@ mono_cli_rva_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)){
+                       if (!iinfo->cli_sections [i]) {
+                               if (!mono_image_ensure_section_idx (image, i))
+                                       return NULL;
+                       }
                        return (char*)iinfo->cli_sections [i] +
                                (addr - tables->st_virtual_address);
                }
@@ -69,6 +75,48 @@ mono_cli_rva_map (MonoCLIImageInfo *iinfo, 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:
  *
@@ -188,10 +236,6 @@ load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
                /* consistency checks here */
        }
 
-       for (i = 0; i < top; i++)
-               if (!mono_image_ensure_section_idx (image, i))
-                       return FALSE;
-       
        return TRUE;
 }
 
@@ -278,6 +322,9 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
        char *ptr;
        
        offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
+       if (offset == INVALID_ADDRESS)
+               return FALSE;
+
        size = iinfo->cli_cli_header.ch_metadata.size;
 
        if (!image->f) {
@@ -338,7 +385,7 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
                        return FALSE;
                } else {
                        g_message ("Unknown heap type: %s\n", ptr + 8);
-                       ptr += 8 + strlen (ptr) + 1;
+                       ptr += 8 + strlen (ptr + 8) + 1;
                }
                pad = ptr - image->raw_metadata;
                if (pad % 4)
@@ -434,6 +481,7 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
 
        t = &image->tables [MONO_TABLE_MODULEREF];
        image->modules = g_new0 (MonoImage *, 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;
@@ -445,7 +493,7 @@ load_modules (MonoImage *image, MonoImageOpenStatus *status)
                module_ref = g_build_filename (base_dir, name, NULL);
                image->modules [i] = mono_image_open (module_ref, status);
                if (image->modules [i]) {
-                       //g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly);
+                       /* 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...
@@ -492,6 +540,29 @@ load_class_names (MonoImage *image)
                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);
 }
 
@@ -515,6 +586,7 @@ 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 (NULL, NULL);
 
@@ -527,15 +599,19 @@ mono_image_init (MonoImage *image)
        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 (NULL, NULL);
+       image->runtime_invoke_cache  = 
+               g_hash_table_new ((GHashFunc)mono_signature_hash, 
+                                 (GCompareFunc)mono_metadata_signature_equal);
+       
        image->managed_wrapper_cache = g_hash_table_new (NULL, NULL);
        image->native_wrapper_cache = g_hash_table_new (NULL, NULL);
        image->remoting_invoke_cache = g_hash_table_new (NULL, NULL);
        image->synchronized_cache = g_hash_table_new (NULL, NULL);
+       image->unbox_wrapper_cache = g_hash_table_new (NULL, NULL);
 
-       image->generics_cache = g_hash_table_new ((GHashFunc)mono_metadata_type_hash, (GEqualFunc)mono_metadata_type_equal);
        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);
 }
 
 static MonoImage *
@@ -732,9 +808,9 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status)
        image = g_new0 (MonoImage, 1);
        image->ref_count = 1;
        image->f = filed;
-       image->name = g_strdup (fname);
        iinfo = g_new0 (MonoCLIImageInfo, 1);
        image->image_info = iinfo;
+       image->name = canonicalize_path (fname);
 
        return do_mono_image_load (image, status);
 }
@@ -743,9 +819,7 @@ MonoImage *
 mono_image_loaded (const char *name)
 {
        MonoImage *res;
-       
-       if (strcmp (name, "mscorlib") == 0)
-               name = "corlib";
+        
        EnterCriticalSection (&images_mutex);
        res = g_hash_table_lookup (loaded_images_hash, name);
        LeaveCriticalSection (&images_mutex);
@@ -810,8 +884,11 @@ MonoImage *
 mono_image_open (const char *fname, MonoImageOpenStatus *status)
 {
        MonoImage *image, *image2;
+       char *absfname;
        
        g_return_val_if_fail (fname != NULL, NULL);
+       
+       absfname = canonicalize_path (fname);
 
        /*
         * The easiest solution would be to do all the loading inside the mutex,
@@ -820,7 +897,9 @@ mono_image_open (const char *fname, MonoImageOpenStatus *status)
         * the same image, we discard all but the first copy.
         */
        EnterCriticalSection (&images_mutex);
-       image = g_hash_table_lookup (loaded_images_hash, fname);
+       image = g_hash_table_lookup (loaded_images_hash, absfname);
+       g_free (absfname);
+       
        if (image){
                image->ref_count++;
                LeaveCriticalSection (&images_mutex);
@@ -833,17 +912,17 @@ mono_image_open (const char *fname, MonoImageOpenStatus *status)
                return NULL;
 
        EnterCriticalSection (&images_mutex);
-       image2 = g_hash_table_lookup (loaded_images_hash, fname);
+       image2 = g_hash_table_lookup (loaded_images_hash, image->name);
+
        if (image2) {
                /* Somebody else beat us to it */
                image2->ref_count ++;
                LeaveCriticalSection (&images_mutex);
                mono_image_close (image);
-
                return image2;
        }
        g_hash_table_insert (loaded_images_hash, image->name, image);
-       if (image->assembly_name)
+       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);
@@ -857,6 +936,30 @@ 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_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
+{
+       g_free (val);
+}
+
+/**
+ * mono_image_addref:
+ * @image: The image file we wish to add a reference to
+ *
+ *  Increases the reference count of an image.
+ */
+void
+mono_image_addref (MonoImage *image)
+{
+       InterlockedIncrement (&image->ref_count);
+}      
+
 /**
  * mono_image_close:
  * @image: The image file we wish to close
@@ -880,23 +983,39 @@ mono_image_close (MonoImage *image)
        if (image == image2) {
                /* This is not true if we are called from mono_image_open () */
                g_hash_table_remove (loaded_images_hash, image->name);
-               if (image->assembly_name)
-                       g_hash_table_remove (loaded_images_hash, (char *) image->assembly_name);        
                g_hash_table_remove (loaded_images_guid_hash, image->guid);
                /* Multiple images might have the same guid */
                build_guid_table ();
-       }       
+       }
+       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->f)
                fclose (image->f);
-       if (image->raw_data_allocated)
-               g_free (image->raw_data);
+       if (image->raw_data_allocated) {
+               /* image->raw_metadata and cli_sections might lie inside image->raw_data */
+               MonoCLIImageInfo *ii = image->image_info;
+               int i;
 
+               if ((image->raw_metadata > image->raw_data) &&
+                       (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
+                       image->raw_metadata = NULL;
+
+               for (i = 0; i < ii->cli_section_count; i++)
+                       if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
+                               ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
+                               ii->cli_sections [i] = NULL;
+
+               g_free (image->raw_data);
+       }
        g_free (image->name);
+       g_free (image->guid);
+       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);
@@ -905,8 +1024,16 @@ mono_image_close (MonoImage *image)
        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);
+       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->synchronized_cache);
+       g_hash_table_destroy (image->unbox_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->memberref_signatures);
+       g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
+       g_hash_table_destroy (image->helper_signatures);
        
        if (image->raw_metadata != NULL)
                mono_raw_buffer_free (image->raw_metadata);
@@ -926,8 +1053,10 @@ mono_image_close (MonoImage *image)
                        g_free (ii->cli_sections);
                g_free (image->image_info);
        }
-       
-       g_free (image);
+
+       if (!image->dynamic)
+               /* Dynamic images are GC_MALLOCed */
+               g_free (image);
 }
 
 /** 
@@ -1055,7 +1184,7 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
                return(NULL);
        }
 
-       resource_dir=(MonoPEResourceDir *)mono_cli_rva_map (info, rsrc->rva);
+       resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
        if(resource_dir==NULL) {
                return(NULL);
        }
@@ -1094,7 +1223,7 @@ mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
        if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
                return NULL;
        
-       data = mono_cli_rva_map (iinfo, ch->ch_resources.rva);
+       data = mono_image_rva_map (image, ch->ch_resources.rva);
        if (!data)
                return NULL;
        data += offset;
@@ -1104,6 +1233,45 @@ mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
        return data;
 }
 
+MonoImage*
+mono_image_load_file_for_image (MonoImage *image, int fileidx)
+{
+       char *base_dir, *name;
+       MonoImage *res;
+       MonoTableInfo  *t = &image->tables [MONO_TABLE_FILE];
+       const char *fname;
+       guint32 fname_id;
+
+       if (fileidx < 1 || fileidx > t->rows)
+               return NULL;
+
+       if (image->files && image->files [fileidx - 1])
+               return image->files [fileidx - 1];
+
+       if (!image->files)
+               image->files = g_new0 (MonoImage*, t->rows);
+
+       fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
+       fname = mono_metadata_string_heap (image, fname_id);
+       base_dir = g_path_get_dirname (image->name);
+       name = g_build_filename (base_dir, fname, NULL);
+       res = mono_image_open (name, NULL);
+       if (res) {
+               int i;
+               /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
+               res->assembly = image->assembly;
+               for (i = 0; i < res->module_count; ++i) {
+                       if (res->modules [i] && !res->modules [i]->assembly)
+                               res->modules [i]->assembly = image->assembly;
+               }
+
+               image->files [fileidx - 1] = res;
+       }
+       g_free (name);
+       g_free (base_dir);
+       return res;
+}
+
 const char*
 mono_image_get_strong_name (MonoImage *image, guint32 *size)
 {
@@ -1113,7 +1281,7 @@ mono_image_get_strong_name (MonoImage *image, guint32 *size)
 
        if (!de->size || !de->rva)
                return NULL;
-       data = mono_cli_rva_map (iinfo, de->rva);
+       data = mono_image_rva_map (image, de->rva);
        if (!data)
                return NULL;
        if (size)
@@ -1164,3 +1332,58 @@ mono_image_get_public_key (MonoImage *image, guint32 *size)
        return pubkey;
 }
 
+const char*
+mono_image_get_name (MonoImage *image)
+{
+       return image->assembly_name;
+}
+
+const char*
+mono_image_get_filename (MonoImage *image)
+{
+       return image->name;
+}
+
+const MonoTableInfo*
+mono_image_get_table_info (MonoImage *image, int table_id)
+{
+       if (table_id < 0 || table_id >= 64)
+               return NULL;
+       return &image->tables [table_id];
+}
+
+int
+mono_image_get_table_rows (MonoImage *image, int table_id)
+{
+       if (table_id < 0 || table_id >= 64)
+               return 0;
+       return image->tables [table_id].rows;
+}
+
+int
+mono_table_info_get_rows (const MonoTableInfo *table)
+{
+       return table->rows;
+}
+
+MonoAssembly* 
+mono_image_get_assembly (MonoImage *image)
+{
+       return image->assembly;
+}
+
+gboolean
+mono_image_is_dynamic (MonoImage *image)
+{
+       return image->dynamic;
+}
+
+gboolean
+mono_image_has_authenticode_entry (MonoImage *image)
+{
+       MonoCLIImageInfo *iinfo = image->image_info;
+       MonoDotNetHeader *header = &iinfo->cli_header;
+       MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
+       // the Authenticode "pre" (non ASN.1) header is 8 bytes long
+       return ((de->rva != 0) && (de->size > 8));
+}