* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
- * (C) 2001-2003 Ximian, Inc. http://www.ximian.com
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*
*/
#include <config.h>
#include <string.h>
#include "image.h"
#include "cil-coff.h"
-#include "rawbuffer.h"
#include "mono-endian.h"
#include "tabledefs.h"
#include "tokentype.h"
#include <mono/io-layer/io-layer.h>
#include <mono/utils/mono-logger.h>
#include <mono/utils/mono-path.h>
+#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-io-portability.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/assembly.h>
MonoTableInfo *t, *mt;
t = &image->tables [MONO_TABLE_TYPEDEF];
mt = &image->tables [MONO_TABLE_METHOD];
- if (mono_get_runtime_info ()->framework_version [0] == '1') {
+ if (mono_framework_version () == 1) {
image->checked_module_cctor = TRUE;
return;
}
class_next_value);
image->field_cache = g_hash_table_new (NULL, NULL);
- image->delegate_begin_invoke_cache =
- g_hash_table_new ((GHashFunc)mono_signature_hash,
- (GCompareFunc)mono_metadata_signature_equal);
- image->delegate_end_invoke_cache =
- g_hash_table_new ((GHashFunc)mono_signature_hash,
- (GCompareFunc)mono_metadata_signature_equal);
- image->delegate_invoke_cache =
- g_hash_table_new ((GHashFunc)mono_signature_hash,
- (GCompareFunc)mono_metadata_signature_equal);
- image->runtime_invoke_cache =
- g_hash_table_new ((GHashFunc)mono_signature_hash,
- (GCompareFunc)mono_metadata_signature_equal);
-
- image->runtime_invoke_direct_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->managed_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->remoting_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->cominterop_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->cominterop_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->synchronized_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->unbox_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
- image->ldfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->ldflda_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->stfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->castclass_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->proxy_isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
- image->thunk_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
image->typespec_cache = g_hash_table_new (NULL, NULL);
image->memberref_signatures = g_hash_table_new (NULL, NULL);
image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
image->method_signatures = g_hash_table_new (NULL, NULL);
image->property_hash = mono_property_hash_new ();
+ InitializeCriticalSection (&image->lock);
+ InitializeCriticalSection (&image->szarray_cache_lock);
}
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
{
MonoCLIImageInfo *iinfo;
MonoImage *image;
- FILE *filed;
- struct stat stat_buf;
+ MonoFileMap *filed;
- if ((filed = fopen (fname, "rb")) == NULL){
+ if ((filed = mono_file_map_open (fname)) == NULL){
if (IS_PORTABILITY_SET) {
gchar *ffname = mono_portability_find_file (fname, TRUE);
if (ffname) {
- filed = fopen (ffname, "rb");
+ filed = mono_file_map_open (ffname);
g_free (ffname);
}
}
}
}
- if (fstat (fileno (filed), &stat_buf)) {
- fclose (filed);
+ image = g_new0 (MonoImage, 1);
+ image->raw_buffer_used = TRUE;
+ image->raw_data_len = mono_file_map_size (filed);
+ image->raw_data = mono_file_map (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
+ if (!image->raw_data) {
+ mono_file_map_close (filed);
+ g_free (image);
if (status)
- *status = MONO_IMAGE_ERROR_ERRNO;
+ *status = MONO_IMAGE_IMAGE_INVALID;
return NULL;
}
- image = g_new0 (MonoImage, 1);
- image->raw_buffer_used = TRUE;
- image->raw_data_len = stat_buf.st_size;
- image->raw_data = mono_raw_buffer_load (fileno (filed), FALSE, 0, stat_buf.st_size);
iinfo = g_new0 (MonoCLIImageInfo, 1);
image->image_info = iinfo;
image->name = mono_path_resolve_symlinks (fname);
image->ref_only = refonly;
image->ref_count = 1;
- fclose (filed);
-
+ mono_file_map_close (filed);
return do_mono_image_load (image, status, care_about_cli);
}
}
}
+static inline void
+free_hash (GHashTable *hash)
+{
+ if (hash)
+ g_hash_table_destroy (hash);
+}
+
/**
* mono_image_close:
* @image: The image file we wish to close
* assemblies, so we can't release these references in mono_assembly_close () since the
* MonoImage might outlive its associated MonoAssembly.
*/
- if (image->references) {
+ if (image->references && !image->dynamic) {
+ MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
int i;
- for (i = 0; image->references [i]; i++) {
+ for (i = 0; i < t->rows; i++) {
if (image->references [i])
mono_assembly_close (image->references [i]);
}
if (image->raw_buffer_used) {
if (image->raw_data != NULL)
- mono_raw_buffer_free (image->raw_data);
+ mono_file_unmap (image->raw_data, image->raw_data_handle);
}
if (image->raw_data_allocated) {
g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
g_hash_table_destroy (image->array_cache);
}
+ if (image->szarray_cache)
+ g_hash_table_destroy (image->szarray_cache);
if (image->ptr_cache)
g_hash_table_destroy (image->ptr_cache);
if (image->name_cache) {
g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
g_hash_table_destroy (image->name_cache);
}
- g_hash_table_destroy (image->native_wrapper_cache);
- g_hash_table_destroy (image->managed_wrapper_cache);
- g_hash_table_destroy (image->delegate_begin_invoke_cache);
- g_hash_table_destroy (image->delegate_end_invoke_cache);
- g_hash_table_destroy (image->delegate_invoke_cache);
- if (image->delegate_abstract_invoke_cache)
- g_hash_table_destroy (image->delegate_abstract_invoke_cache);
- g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
- g_hash_table_destroy (image->remoting_invoke_cache);
- g_hash_table_destroy (image->runtime_invoke_cache);
- g_hash_table_destroy (image->runtime_invoke_direct_cache);
- g_hash_table_destroy (image->synchronized_cache);
- g_hash_table_destroy (image->unbox_wrapper_cache);
- g_hash_table_destroy (image->cominterop_invoke_cache);
- g_hash_table_destroy (image->cominterop_wrapper_cache);
- g_hash_table_destroy (image->typespec_cache);
- g_hash_table_destroy (image->ldfld_wrapper_cache);
- g_hash_table_destroy (image->ldflda_wrapper_cache);
- g_hash_table_destroy (image->stfld_wrapper_cache);
- g_hash_table_destroy (image->isinst_cache);
- g_hash_table_destroy (image->castclass_cache);
- g_hash_table_destroy (image->proxy_isinst_cache);
- g_hash_table_destroy (image->thunk_invoke_cache);
- if (image->static_rgctx_invoke_cache)
- g_hash_table_destroy (image->static_rgctx_invoke_cache);
+
+ free_hash (image->native_wrapper_cache);
+ free_hash (image->managed_wrapper_cache);
+ free_hash (image->delegate_begin_invoke_cache);
+ free_hash (image->delegate_end_invoke_cache);
+ free_hash (image->delegate_invoke_cache);
+ free_hash (image->delegate_abstract_invoke_cache);
+ if (image->remoting_invoke_cache)
+ g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
+ free_hash (image->remoting_invoke_cache);
+ free_hash (image->runtime_invoke_cache);
+ free_hash (image->runtime_invoke_direct_cache);
+ free_hash (image->synchronized_cache);
+ free_hash (image->unbox_wrapper_cache);
+ free_hash (image->cominterop_invoke_cache);
+ free_hash (image->cominterop_wrapper_cache);
+ free_hash (image->typespec_cache);
+ free_hash (image->ldfld_wrapper_cache);
+ free_hash (image->ldflda_wrapper_cache);
+ free_hash (image->stfld_wrapper_cache);
+ free_hash (image->isinst_cache);
+ free_hash (image->castclass_cache);
+ free_hash (image->proxy_isinst_cache);
+ free_hash (image->thunk_invoke_cache);
+ free_hash (image->static_rgctx_invoke_cache);
/* The ownership of signatures is not well defined */
//g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
g_free (image->modules_loaded);
if (image->references)
g_free (image->references);
+ mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
+
+ DeleteCriticalSection (&image->szarray_cache_lock);
+ DeleteCriticalSection (&image->lock);
+
/*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
if (!image->dynamic) {
if (debug_assembly_unload)
dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
if(level==0) {
- if((is_string==FALSE && name_offset!=res_id) ||
- (is_string==TRUE)) {
- return(NULL);
- }
+ if (is_string)
+ return NULL;
} else if (level==1) {
+ if (res_id != name_offset)
+ return NULL;
#if 0
if(name!=NULL &&
is_string==TRUE && name!=lookup (name_offset)) {
}
#endif
} else if (level==2) {
- if ((is_string == FALSE &&
- name_offset != lang_id &&
- lang_id != 0) ||
- (is_string == TRUE)) {
- return(NULL);
- }
+ if (is_string == TRUE || (is_string == FALSE && lang_id != 0 && name_offset != lang_id))
+ return NULL;
} else {
g_assert_not_reached ();
}
return(NULL);
}
+ mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
+
info=image->image_info;
if(info==NULL) {
return(NULL);
// the Authenticode "pre" (non ASN.1) header is 8 bytes long
return ((de->rva != 0) && (de->size > 8));
}
+
+gpointer
+mono_image_alloc (MonoImage *image, guint size)
+{
+ gpointer res;
+
+ mono_perfcounters->loader_bytes += size;
+ mono_image_lock (image);
+ res = mono_mempool_alloc (image->mempool, size);
+ mono_image_unlock (image);
+
+ return res;
+}
+
+gpointer
+mono_image_alloc0 (MonoImage *image, guint size)
+{
+ gpointer res;
+
+ mono_perfcounters->loader_bytes += size;
+ mono_image_lock (image);
+ res = mono_mempool_alloc0 (image->mempool, size);
+ mono_image_unlock (image);
+
+ return res;
+}
+
+char*
+mono_image_strdup (MonoImage *image, const char *s)
+{
+ char *res;
+
+ mono_perfcounters->loader_bytes += strlen (s);
+ mono_image_lock (image);
+ res = mono_mempool_strdup (image->mempool, s);
+ mono_image_unlock (image);
+
+ return res;
+}
+
+GList*
+g_list_prepend_image (MonoImage *image, GList *list, gpointer data)
+{
+ GList *new_list;
+
+ new_list = mono_image_alloc (image, sizeof (GList));
+ new_list->data = data;
+ new_list->prev = list ? list->prev : NULL;
+ new_list->next = list;
+
+ if (new_list->prev)
+ new_list->prev->next = new_list;
+ if (list)
+ list->prev = new_list;
+
+ return new_list;
+}
+
+GSList*
+g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
+{
+ GSList *new_list;
+
+ new_list = mono_image_alloc (image, sizeof (GSList));
+ new_list->data = data;
+ new_list->next = NULL;
+
+ return g_slist_concat (list, new_list);
+}
+
+void
+mono_image_lock (MonoImage *image)
+{
+ mono_locks_acquire (&image->lock, ImageDataLock);
+}
+
+void
+mono_image_unlock (MonoImage *image)
+{
+ mono_locks_release (&image->lock, ImageDataLock);
+}
+
+
+/**
+ * mono_image_property_lookup:
+ *
+ * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+gpointer
+mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
+{
+ gpointer res;
+
+ mono_image_lock (image);
+ res = mono_property_hash_lookup (image->property_hash, subject, property);
+ mono_image_unlock (image);
+
+ return res;
+}
+
+/**
+ * mono_image_property_insert:
+ *
+ * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
+{
+ mono_image_lock (image);
+ mono_property_hash_insert (image->property_hash, subject, property, value);
+ mono_image_unlock (image);
+}
+
+/**
+ * mono_image_property_remove:
+ *
+ * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_remove (MonoImage *image, gpointer subject)
+{
+ mono_image_lock (image);
+ mono_property_hash_remove_object (image->property_hash, subject);
+ mono_image_unlock (image);
+}