/*
- * assembly.c: Routines for manipulating and assembly stored in an
- * extended PE/COFF file.
+ * assembly.c: Routines for loading assemblies.
*
* Author:
* Miguel de Icaza (miguel@ximian.com)
* (C) 2001 Ximian, Inc. http://www.ximian.com
*
* TODO:
- * Do byteswaps for big-endian systems on the various headers.
+ * Implement big-endian versions of the reading routines.
*/
#include <config.h>
#include <stdio.h>
#include <glib.h>
#include <errno.h>
#include <string.h>
+#include <stdlib.h>
#include "assembly.h"
+#include "image.h"
#include "cil-coff.h"
#include "rawbuffer.h"
+#ifdef WITH_BUNDLE
+#include "mono-bundle.h"
+#endif
+#include <mono/metadata/loader.h>
+#include <mono/metadata/tabledefs.h>
+#include <mono/io-layer/io-layer.h>
+#include <mono/utils/mono-uri.h>
+#include <mono/metadata/mono-config.h>
+#include <mono/utils/mono-digest.h>
+#ifdef PLATFORM_WIN32
+#include <mono/os/util.h>
+#endif
-#define INVALID_ADDRESS 0xffffffff
+/* the default search path is just MONO_ASSEMBLIES */
+static const char*
+default_path [] = {
+ MONO_ASSEMBLIES,
+ NULL
+};
-/* FIXME: implement big endian versions */
-#define le64_to_cpu(x) (x)
-#define le32_to_cpu(x) (x)
-#define le16_to_cpu(x) (x)
-#define read32(x) le32_to_cpu (*((guint32 *) (x)))
-#define read16(x) le16_to_cpu (*((guint16 *) (x)))
-#define read64(x) le64_to_cpu (*((guint64 *) (x)))
+static char **assemblies_path = NULL;
-static guint32
-coff_map (dotnet_image_info_t *iinfo, guint32 addr)
+/*
+ * keeps track of loaded assemblies
+ */
+static GList *loaded_assemblies = NULL;
+static MonoAssembly *corlib;
+
+/* This protects loaded_assemblies and image->references */
+static CRITICAL_SECTION assemblies_mutex;
+
+/* A hastable of thread->assembly list mappings */
+static GHashTable *assemblies_loading;
+
+static char **extra_gac_paths = NULL;
+
+static gchar*
+encode_public_tok (const guchar *token, gint32 len)
{
- const int top = iinfo->dn_section_count;
- section_table_t *tables = iinfo->dn_section_tables;
+ static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+ gchar *res;
int i;
-
- for (i = 0; i < top; i++){
- if ((addr >= tables->st_virtual_address) &&
- (addr < tables->st_virtual_address + tables->st_raw_data_size)){
- return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
- }
- tables++;
+
+ res = g_malloc (len * 2 + 1);
+ for (i = 0; i < len; i++) {
+ res [i * 2] = allowed [token [i] >> 4];
+ res [i * 2 + 1] = allowed [token [i] & 0xF];
}
- return INVALID_ADDRESS;
+ res [len * 2] = 0;
+ return res;
}
-static int
-load_section_tables (MonoAssembly *assembly, dotnet_image_info_t *iinfo)
-{
- const int top = iinfo->dn_header.coff.coff_sections;
- int i;
+static void
+check_path_env (void) {
+ const char *path;
+ char **splitted;
+
+ path = g_getenv ("MONO_PATH");
+ if (!path)
+ return;
- iinfo->dn_section_count = top;
- iinfo->dn_section_tables = g_new (section_table_t, top);
+ splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
+ if (assemblies_path)
+ g_strfreev (assemblies_path);
+ assemblies_path = splitted;
+ if (g_getenv ("MONO_DEBUG") == NULL)
+ return;
- for (i = 0; i < top; i++){
- section_table_t *t = &iinfo->dn_section_tables [i];
-
- if (fread (t, sizeof (section_table_t), 1, assembly->f) != 1)
- return FALSE;
+ while (*splitted) {
+ if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
+ g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
- t->st_virtual_size = le32_to_cpu (t->st_virtual_size);
- t->st_virtual_address = le32_to_cpu (t->st_virtual_address);
- t->st_raw_data_size = le32_to_cpu (t->st_raw_data_size);
- t->st_raw_data_ptr = le32_to_cpu (t->st_raw_data_ptr);
- t->st_reloc_ptr = le32_to_cpu (t->st_reloc_ptr);
- t->st_lineno_ptr = le32_to_cpu (t->st_lineno_ptr);
- t->st_reloc_count = le16_to_cpu (t->st_reloc_count);
- t->st_line_count = le16_to_cpu (t->st_line_count);
+ splitted++;
}
+}
- return TRUE;
+static void
+check_extra_gac_path_env (void) {
+ const char *path;
+ char **splitted;
+
+ path = g_getenv ("MONO_GAC_PATH");
+ if (!path)
+ return;
+
+ splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
+ if (extra_gac_paths)
+ g_strfreev (extra_gac_paths);
+ extra_gac_paths = splitted;
+ if (g_getenv ("MONO_DEBUG") == NULL)
+ return;
+
+ while (*splitted) {
+ if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
+ g_warning ("'%s' in MONO_GAC_PATH doesn't exist or has wrong permissions.", *splitted);
+
+ splitted++;
+ }
}
static gboolean
-load_cli_header (MonoAssembly *assembly, dotnet_image_info_t *iinfo)
+mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
{
- guint32 offset;
- int n;
-
- offset = coff_map (iinfo, iinfo->dn_header.datadir.pe_cli_header.rva);
- if (offset == INVALID_ADDRESS)
+ if (!l->name || !r->name)
return FALSE;
- if (fseek (assembly->f, offset, 0) != 0)
+ if (strcmp (l->name, r->name))
return FALSE;
-
- if ((n = fread (&iinfo->dn_cli_header, sizeof (cli_header_t), 1, assembly->f)) != 1)
+
+ /*
+ * simply compare names until some other issues are resolved
+ * (version info not getting set correctly for custom
+ * attributes).
+ */
+ /*
+ if (l->major != r->major || l->minor != r->minor ||
+ l->build != r->build || l->revision != r->revision)
+ return FALSE;
+
+ if (!l->public_tok_value && !r->public_tok_value)
+ return TRUE;
+
+ if ((l->public_tok_value && !r->public_tok_value) || (!l->public_tok_value && r->public_tok_value))
+ return FALSE;
+
+ if (strcmp (l->public_tok_value, r->public_tok_value))
return FALSE;
+ */
+ return TRUE;
+}
+
+/* assemblies_mutex must be held by the caller */
+static MonoAssembly*
+search_loaded (MonoAssemblyName* aname)
+{
+ GList *tmp;
+ MonoAssembly *ass;
+ GList *loading;
+
+ for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
+ ass = tmp->data;
+ /* g_print ("compare %s %s\n", aname->name, ass->aname.name); */
+ if (!mono_assembly_names_equal (aname, &ass->aname))
+ continue;
+ /* g_print ("success\n"); */
+
+ return ass;
+ }
+
+ /*
+ * The assembly might be under load by this thread. In this case, it is
+ * safe to return an incomplete instance to prevent loops.
+ */
+ loading = g_hash_table_lookup (assemblies_loading, GetCurrentThread ());
+ for (tmp = loading; tmp; tmp = tmp->next) {
+ ass = tmp->data;
+ if (!mono_assembly_names_equal (aname, &ass->aname))
+ continue;
+
+ return ass;
+ }
- /* Catch new uses of the fields that are supposed to be zero */
+ return NULL;
+}
- if ((iinfo->dn_cli_header.ch_eeinfo_table.rva != 0) ||
- (iinfo->dn_cli_header.ch_helper_table.rva != 0) ||
- (iinfo->dn_cli_header.ch_dynamic_info.rva != 0) ||
- (iinfo->dn_cli_header.ch_delay_load_info.rva != 0) ||
- (iinfo->dn_cli_header.ch_module_image.rva != 0) ||
- (iinfo->dn_cli_header.ch_external_fixups.rva != 0) ||
- (iinfo->dn_cli_header.ch_ridmap.rva != 0) ||
- (iinfo->dn_cli_header.ch_debug_map.rva != 0) ||
- (iinfo->dn_cli_header.ch_ip_map.rva != 0)){
- g_message ("Some fields in the CLI header which should have been zero are not zero");
+static MonoAssembly *
+load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status)
+{
+ int i;
+ char *fullpath;
+ MonoAssembly *result;
+
+ for (i = 0; search_path [i]; ++i) {
+ fullpath = g_build_filename (search_path [i], basename, NULL);
+ result = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+ if (result)
+ return result;
}
-
+ return NULL;
+}
+
+/**
+ * mono_assembly_setrootdir:
+ * @root_dir: The pathname of the root directory where we will locate assemblies
+ *
+ * This routine sets the internal default root directory for looking up
+ * assemblies. This is used by Windows installations to compute dynamically
+ * the place where the Mono assemblies are located.
+ *
+ */
+void
+mono_assembly_setrootdir (const char *root_dir)
+{
+ /*
+ * Override the MONO_ASSEMBLIES directory configured at compile time.
+ */
+ /* Leak if called more than once */
+ default_path [0] = g_strdup (root_dir);
+}
+
+G_CONST_RETURN gchar *
+mono_assembly_getrootdir (void)
+{
+ return default_path [0];
+}
+
+/**
+ * mono_assemblies_init:
+ *
+ * Initialize global variables used by this module.
+ */
+void
+mono_assemblies_init (void)
+{
+#ifdef PLATFORM_WIN32
+ mono_set_rootdir ();
+#endif
+
+ check_path_env ();
+ check_extra_gac_path_env ();
+
+ InitializeCriticalSection (&assemblies_mutex);
+
+ assemblies_loading = g_hash_table_new (NULL, NULL);
+}
+
+gboolean
+mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
+{
+ MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
+ guint32 cols [MONO_ASSEMBLY_SIZE];
+
+ if (!t->rows)
+ return FALSE;
+
+ mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
+
+ aname->hash_len = 0;
+ aname->hash_value = NULL;
+ aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
+ aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
+ aname->flags = cols [MONO_ASSEMBLY_FLAGS];
+ aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
+ aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
+ aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
+ aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
+ aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
+ if (cols [MONO_ASSEMBLY_PUBLIC_KEY])
+ aname->public_key = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
+ else
+ aname->public_key = 0;
+
return TRUE;
}
-static gboolean
-load_metadata_ptrs (MonoAssembly *assembly, dotnet_image_info_t *iinfo)
+static gchar*
+assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
+{
+ const gchar *public_tok;
+ int len;
+
+ public_tok = mono_metadata_blob_heap (image, key_index);
+ len = mono_metadata_decode_blob_size (public_tok, &public_tok);
+
+ if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
+ gchar token [8];
+ mono_digest_get_public_token (token, public_tok, len);
+ return encode_public_tok (token, 8);
+ }
+
+ return encode_public_tok (public_tok, len);
+}
+
+void
+mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
{
- metadata_t *metadata = &iinfo->dn_metadata;
- guint32 offset, size;
- guint16 records;
+ MonoTableInfo *t;
+ guint32 cols [MONO_ASSEMBLYREF_SIZE];
+ const char *hash;
int i;
- char *ptr;
-
- offset = coff_map (iinfo, iinfo->dn_cli_header.ch_metadata.rva);
- size = iinfo->dn_cli_header.ch_metadata.size;
+ MonoAssembly **references = NULL;
+
+ *status = MONO_IMAGE_OK;
- metadata->raw_metadata = raw_buffer_load (fileno (assembly->f), FALSE, offset, size);
- if (metadata->raw_metadata == NULL)
- return FALSE;
+ /*
+ * image->references is shared between threads, so we need to access
+ * it inside a critical section.
+ */
+ EnterCriticalSection (&assemblies_mutex);
+ references = image->references;
+ LeaveCriticalSection (&assemblies_mutex);
+ if (references)
+ return;
- ptr = metadata->raw_metadata;
+ t = &image->tables [MONO_TABLE_ASSEMBLYREF];
+
+ references = g_new0 (MonoAssembly *, t->rows + 1);
/*
- * Handle Beta 1 and ECMA versions of the metadata root
+ * Load any assemblies this image references
*/
- if (strncmp (ptr, "COM+", 4) == 0){
- ptr += 10;
- } else if (strncmp (ptr, "BSJB", 4) == 0){
- guint32 version_string_len;
+ for (i = 0; i < t->rows; i++) {
+ MonoAssemblyName aname;
+
+ mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
+
+ hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
+ aname.hash_len = mono_metadata_decode_blob_size (hash, &hash);
+ aname.hash_value = hash;
+ aname.name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
+ aname.culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
+ aname.flags = cols [MONO_ASSEMBLYREF_FLAGS];
+ aname.major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
+ aname.minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
+ aname.build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
+ aname.revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
+
+ if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
+ aname.public_tok_value = assemblyref_public_tok (image,
+ cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname.flags);
+ } else {
+ aname.public_tok_value = NULL;
+ }
- ptr += 12;
- version_string_len = read32 (ptr);
- ptr += 4;
- ptr += version_string_len;
- if (((guint32) ptr) % 4)
- ptr += 4 - (((guint32) ptr) %4);
+ references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
+
+ if (references [i] == NULL){
+ int j;
+
+ /*
+ ** Temporary work around: any System.* which are 3300 build, will get
+ ** remapped, this is to keep old applications running that might have
+ ** been linked against our 5000 API, before we were strongnamed, and
+ ** hence were labeled as 3300 builds by reflection.c
+ */
+ if (aname.build == 3300 && strncmp (aname.name, "System", 6) == 0){
+ aname.build = 5000;
+
+ references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
+ }
+ if (references [i] != NULL){
+ if (g_getenv ("MONO_SILENT_WARNING") == NULL)
+ g_printerr ("Compat mode: the request from %s to load %s was remapped (http://www.go-mono.com/remap.html)\n",
+ image->name, aname.name);
+
+ } else {
+
+ for (j = 0; j < i; j++)
+ mono_assembly_close (references [j]);
+ g_free (references);
+
+ g_warning ("Could not find assembly %s, references from %s (assemblyref_index=%d)\n"
+ " Major/Minor: %d,%d\n"
+ " Build: %d,%d\n"
+ " Token: %s\n",
+ aname.name, image->name, i,
+ aname.major, aname.minor, aname.build, aname.revision,
+ aname.public_tok_value);
+ *status = MONO_IMAGE_MISSING_ASSEMBLYREF;
+ return;
+ }
+ }
+
+ /*
+ * This check is disabled since lots of people seem to have an older
+ * corlib which triggers this.
+ */
+ /*
+ if (image->references [i]->image == image)
+ g_error ("Error: Assembly %s references itself", image->name);
+ */
}
+ references [i] = NULL;
- /* skip over flags */
- ptr += 2;
-
- records = read16 (ptr);
- ptr += 2;
-
- for (i = 0; i < records; i++){
- if (strncmp (ptr + 8, "#~", 3) == 0){
- metadata->heap_tables.sh_offset = read32 (ptr);
- metadata->heap_tables.sh_size = read32 (ptr + 4);
- ptr += 8 + 3;
- } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
- metadata->heap_strings.sh_offset = read32 (ptr);
- metadata->heap_strings.sh_size = read32 (ptr + 4);
- ptr += 8 + 9;
- } else if (strncmp (ptr + 8, "#US", 4) == 0){
- metadata->heap_us.sh_offset = read32 (ptr);
- metadata->heap_us.sh_size = read32 (ptr + 4);
- ptr += 8 + 4;
- } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
- metadata->heap_blob.sh_offset = read32 (ptr);
- metadata->heap_blob.sh_size = read32 (ptr + 4);
- ptr += 8 + 6;
- } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
- metadata->heap_guid.sh_offset = read32 (ptr);
- metadata->heap_guid.sh_size = read32 (ptr + 4);
- ptr += 8 + 6;
- } else
- g_message ("Unknown heap type: %s\n", ptr + 8);
- if (((guint32)ptr) % 4){
- ptr += 4 - (((guint32)ptr) % 4);
+ /* resolve assembly references for modules */
+ t = &image->tables [MONO_TABLE_MODULEREF];
+ for (i = 0; i < t->rows; i++){
+ if (image->modules [i]) {
+ image->modules [i]->assembly = image->assembly;
+ mono_assembly_load_references (image->modules [i], status);
}
}
- return TRUE;
+
+ EnterCriticalSection (&assemblies_mutex);
+ if (!image->references)
+ image->references = references;
+ LeaveCriticalSection (&assemblies_mutex);
+
+ if (image->references != references) {
+ /* Somebody loaded it before us */
+ for (i = 0; i < t->rows; i++)
+ mono_assembly_close (references [i]);
+ g_free (references);
+ }
}
-static gboolean
-load_tables (MonoAssembly *assembly, metadata_t *meta)
+typedef struct AssemblyLoadHook AssemblyLoadHook;
+struct AssemblyLoadHook {
+ AssemblyLoadHook *next;
+ MonoAssemblyLoadFunc func;
+ gpointer user_data;
+};
+
+AssemblyLoadHook *assembly_load_hook = NULL;
+
+void
+mono_assembly_invoke_load_hook (MonoAssembly *ass)
+{
+ AssemblyLoadHook *hook;
+
+ for (hook = assembly_load_hook; hook; hook = hook->next) {
+ hook->func (ass, hook->user_data);
+ }
+}
+
+void
+mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
{
- char *heap_tables = meta->raw_metadata + meta->heap_tables.sh_offset;
- guint32 *rows;
- guint64 valid_mask;
- int valid = 0, table;
+ AssemblyLoadHook *hook;
- valid_mask = read64 (heap_tables + 8);
- rows = (guint32 *) (heap_tables + 24);
+ g_return_if_fail (func != NULL);
+
+ hook = g_new0 (AssemblyLoadHook, 1);
+ hook->func = func;
+ hook->user_data = user_data;
+ hook->next = assembly_load_hook;
+ assembly_load_hook = hook;
+}
+
+typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
+struct AssemblyPreLoadHook {
+ AssemblyPreLoadHook *next;
+ MonoAssemblyPreLoadFunc func;
+ gpointer user_data;
+};
+
+AssemblyPreLoadHook *assembly_preload_hook = NULL;
+
+static MonoAssembly *
+invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
+{
+ AssemblyPreLoadHook *hook;
+ MonoAssembly *assembly;
+
+ for (hook = assembly_preload_hook; hook; hook = hook->next) {
+ assembly = hook->func (aname, assemblies_path, hook->user_data);
+ if (assembly != NULL)
+ return assembly;
+ }
+
+ return NULL;
+}
+
+void
+mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
+{
+ AssemblyPreLoadHook *hook;
- for (table = 0; table < 64; table++){
- if ((valid_mask & ((guint64) 1 << table)) == 0){
- meta->rows [table] = 0;
+ g_return_if_fail (func != NULL);
+
+ hook = g_new0 (AssemblyPreLoadHook, 1);
+ hook->func = func;
+ hook->user_data = user_data;
+ hook->next = assembly_preload_hook;
+ assembly_preload_hook = hook;
+}
+
+static gchar *
+absolute_dir (const gchar *filename)
+{
+ gchar *cwd;
+ gchar *mixed;
+ gchar **parts;
+ gchar *part;
+ GList *list, *tmp;
+ GString *result;
+ gchar *res;
+ gint i;
+
+ if (g_path_is_absolute (filename))
+ return g_path_get_dirname (filename);
+
+ cwd = g_get_current_dir ();
+ mixed = g_build_filename (cwd, filename, NULL);
+ parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
+ g_free (mixed);
+ g_free (cwd);
+
+ list = NULL;
+ for (i = 0; (part = parts [i]) != NULL; i++) {
+ if (!strcmp (part, "."))
continue;
+
+ if (!strcmp (part, "..")) {
+ if (list && list->next) /* Don't remove root */
+ list = g_list_delete_link (list, list);
+ } else {
+ list = g_list_prepend (list, part);
}
- meta->rows [table] = *rows;
- rows++;
- valid++;
}
- return TRUE;
+ result = g_string_new ("");
+ list = g_list_reverse (list);
+
+ /* Ignores last data pointer, which should be the filename */
+ for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next)
+ if (tmp->data)
+ g_string_append_printf (result, "%s%c", (char *) tmp->data,
+ G_DIR_SEPARATOR);
+
+ res = result->str;
+ g_string_free (result, FALSE);
+ g_list_free (list);
+ g_strfreev (parts);
+ if (*res == '\0') {
+ g_free (res);
+ return g_strdup (".");
+ }
+
+ return res;
}
-static gboolean
-load_metadata (MonoAssembly *assembly, dotnet_image_info_t *iinfo)
+static MonoImage*
+do_mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
{
- if (!load_metadata_ptrs (assembly, iinfo))
- return FALSE;
+ MonoImage *image = NULL;
+#ifdef WITH_BUNDLE
+ int i;
+ char *name = g_path_get_basename (filename);
+ char *dot = strrchr (name, '.');
+ GList *tmp;
+ MonoAssembly *ass;
+
+ if (dot)
+ *dot = 0;
+ /* we do a very simple search for bundled assemblies: it's not a general
+ * purpose assembly loading mechanism.
+ */
+ EnterCriticalSection (&assemblies_mutex);
+ for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
+ ass = tmp->data;
+ if (!ass->aname.name)
+ continue;
+ if (strcmp (name, ass->aname.name))
+ continue;
+ image = ass->image;
+ break;
+ }
- return load_tables (assembly, &iinfo->dn_metadata);
+ for (i = 0; !image && bundled_assemblies [i]; ++i) {
+ if (strcmp (bundled_assemblies [i]->name, name) == 0) {
+ image = mono_image_open_from_data ((char*)bundled_assemblies [i]->data, bundled_assemblies [i]->size, FALSE, status);
+ break;
+ }
+ }
+ LeaveCriticalSection (&assemblies_mutex);
+ g_free (name);
+ if (image) {
+ mono_image_addref (image);
+ return image;
+ }
+#endif
+ EnterCriticalSection (&assemblies_mutex);
+ image = mono_image_open (filename, status);
+ LeaveCriticalSection (&assemblies_mutex);
+
+ return image;
}
+/**
+ * mono_assembly_open:
+ * @filename: Opens the assembly pointed out by this name
+ * @status: where a status code can be returned
+ *
+ * mono_assembly_open opens the PE-image pointed by @filename, and
+ * loads any external assemblies referenced by it.
+ *
+ * NOTE: we could do lazy loading of the assemblies. Or maybe not worth
+ * it.
+ */
MonoAssembly *
-mono_assembly_open (const char *fname, enum MonoAssemblyOpenStatus *status)
+mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
{
- dotnet_image_info_t *iinfo;
- dotnet_header_t *header;
- msdos_header_t msdos;
- MonoAssembly *assembly;
- int n;
+ MonoImage *image;
+ MonoAssembly *ass;
+ MonoImageOpenStatus def_status;
+ gchar *fname;
+
+ g_return_val_if_fail (filename != NULL, NULL);
- assembly = g_new (MonoAssembly, 1);
- assembly->f = fopen (fname, "r");
- iinfo = g_new (dotnet_image_info_t, 1);
- assembly->image_info = iinfo;
+ if (!status)
+ status = &def_status;
+ *status = MONO_IMAGE_OK;
- header = &iinfo->dn_header;
-
- if (assembly->f == NULL){
- if (status)
- *status = MONO_ASSEMBLY_ERROR_ERRNO;
- mono_assembly_close (assembly);
+ if (strncmp (filename, "file://", 7) == 0) {
+ GError *error = NULL;
+ gchar *uri = (gchar *) filename;
+ gchar *tmpuri;
+
+ /*
+ * MS allows file://c:/... and fails on file://localhost/c:/...
+ * They also throw an IndexOutOfRangeException if "file://"
+ */
+ if (uri [7] != '/')
+ uri = g_strdup_printf ("file:///%s", uri + 7);
+
+ tmpuri = uri;
+ uri = mono_escape_uri_string (tmpuri);
+ fname = g_filename_from_uri (uri, NULL, &error);
+ g_free (uri);
+
+ if (tmpuri != filename)
+ g_free (tmpuri);
+
+ if (error != NULL) {
+ g_warning ("%s\n", error->message);
+ g_error_free (error);
+ fname = g_strdup (filename);
+ }
+ } else {
+ fname = g_strdup (filename);
+ }
+
+ /* g_print ("file loading %s\n", fname); */
+ image = do_mono_assembly_open (fname, status);
+
+ if (!image){
+ *status = MONO_IMAGE_ERROR_ERRNO;
+ g_free (fname);
return NULL;
}
- if (status)
- *status = MONO_ASSEMBLY_IMAGE_INVALID;
-
- if (fread (&msdos, sizeof (msdos), 1, assembly->f) != 1)
- goto invalid_image;
-
- if (!(msdos.msdos_header [0] == 0x4d && msdos.msdos_header [1] == 0x5a))
- goto invalid_image;
-
- if ((n = fread (header, sizeof (dotnet_header_t), 1, assembly->f)) != 1)
- goto invalid_image;
+ ass = mono_assembly_load_from (image, fname, status);
+
+ mono_config_for_assembly (ass->image);
+
+ g_free (fname);
+
+ return ass;
+}
+
+MonoAssembly *
+mono_assembly_load_from (MonoImage *image, const char*fname,
+ MonoImageOpenStatus *status)
+{
+ MonoAssembly *ass, *ass2;
+ char *base_dir;
+ GList *loading;
+
+#if defined (PLATFORM_WIN32)
+ {
+ gchar *tmp_fn;
+ int i;
+
+ tmp_fn = g_strdup (fname);
+ for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
+ if (tmp_fn [i] == '/')
+ tmp_fn [i] = '\\';
+ }
+
+ base_dir = absolute_dir (tmp_fn);
+ g_free (tmp_fn);
+ }
+#else
+ base_dir = absolute_dir (fname);
+#endif
+
+ /*
+ * To avoid deadlocks and scalability problems, we load assemblies outside
+ * the assembly lock. This means that multiple threads might try to load
+ * the same assembly at the same time. The first one to load it completely
+ * "wins", the other threads free their copy and use the one loaded by
+ * the winning thread.
+ */
+
+ /*
+ * Create assembly struct, and enter it into the assembly cache
+ */
+ ass = g_new0 (MonoAssembly, 1);
+ ass->basedir = base_dir;
+ ass->image = image;
+
+ mono_assembly_fill_assembly_name (image, &ass->aname);
+
+ /*
+ * Atomically search the loaded list and add ourselves to it if necessary.
+ */
+ EnterCriticalSection (&assemblies_mutex);
+ if (ass->aname.name)
+ /* avoid loading the same assembly twice for now... */
+ if ((ass2 = search_loaded (&ass->aname))) {
+ g_free (ass);
+ g_free (base_dir);
+ mono_image_close (image);
+ *status = MONO_IMAGE_OK;
+ LeaveCriticalSection (&assemblies_mutex);
+ return ass2;
+ }
+ loading = g_hash_table_lookup (assemblies_loading, GetCurrentThread ());
+ loading = g_list_prepend (loading, ass);
+ g_hash_table_insert (assemblies_loading, GetCurrentThread (), loading);
+ LeaveCriticalSection (&assemblies_mutex);
+
+ image->assembly = ass;
/*
- * FIXME: byte swap all addresses here for header.
+ * We load referenced assemblies outside the lock to prevent deadlocks
+ * with regards to preload hooks.
*/
+ mono_assembly_load_references (image, status);
+
+ EnterCriticalSection (&assemblies_mutex);
+
+ loading = g_hash_table_lookup (assemblies_loading, GetCurrentThread ());
+ loading = g_list_remove (loading, ass);
+ if (loading == NULL)
+ /* Prevent memory leaks */
+ g_hash_table_remove (assemblies_loading, GetCurrentThread ());
+ else
+ g_hash_table_insert (assemblies_loading, GetCurrentThread (), loading);
+ if (*status != MONO_IMAGE_OK) {
+ LeaveCriticalSection (&assemblies_mutex);
+ mono_assembly_close (ass);
+ return NULL;
+ }
+
+ if (ass->aname.name) {
+ ass2 = search_loaded (&ass->aname);
+ if (ass2) {
+ /* Somebody else has loaded the assembly before us */
+ LeaveCriticalSection (&assemblies_mutex);
+ mono_assembly_close (ass);
+ return ass2;
+ }
+ }
+
+ loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
+ LeaveCriticalSection (&assemblies_mutex);
+
+ mono_assembly_invoke_load_hook (ass);
+
+ return ass;
+}
+
+static MonoAssembly*
+probe_for_partial_name (const char *basepath, const char *fullname, MonoImageOpenStatus *status)
+{
+ MonoAssembly *res = NULL;
+ gchar *fullpath;
+ GDir *dirhandle;
+ const char* direntry;
+
+ dirhandle = g_dir_open (basepath, 0, NULL);
+ if (!dirhandle)
+ return NULL;
+
+ while ((direntry = g_dir_read_name (dirhandle))) {
+ fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
+ res = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+ if (res)
+ break;
+ }
+ g_dir_close (dirhandle);
+
+ return res;
+}
+
+MonoAssembly*
+mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
+{
+ MonoAssembly *res;
+ MonoAssemblyName aname;
+ gchar *fullname, *gacpath;
+ gchar **paths;
+
+ memset (&aname, 0, sizeof (MonoAssemblyName));
+ aname.name = name;
+
+ res = mono_assembly_loaded (&aname);
+
+ if (res)
+ return res;
+
+ fullname = g_strdup_printf ("%s.dll", name);
+
+ if (extra_gac_paths) {
+ paths = extra_gac_paths;
+ while (!res && *paths) {
+ gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "gac", name, NULL);
+ res = probe_for_partial_name (gacpath, fullname, status);
+ g_free (gacpath);
+ paths++;
+ }
+ }
+
+ if (res) {
+ res->in_gac = TRUE;
+ g_free (fullname);
+ return res;
+
+ }
+
+ gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", name, NULL);
+ res = probe_for_partial_name (gacpath, fullname, status);
+ g_free (gacpath);
+
+ if (res)
+ res->in_gac = TRUE;
+
+ g_free (fullname);
+
+ return res;
+}
+
+/**
+ * mono_assembly_load_from_gac
+ *
+ * @aname: The assembly name object
+ */
+static MonoAssembly*
+mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status)
+{
+ MonoAssembly *result = NULL;
+ gchar *name, *version, *fullpath, *subpath;
+ gint32 len;
+ gchar **paths;
+
+ if (!aname->public_tok_value) {
+ return NULL;
+ }
+
+ if (strstr (aname->name, ".dll")) {
+ len = strlen (filename) - 4;
+ name = g_malloc (len);
+ strncpy (name, aname->name, len);
+ } else {
+ name = g_strdup (aname->name);
+ }
+
+ version = g_strdup_printf ("%d.%d.%d.%d_%s_%s", aname->major,
+ aname->minor, aname->build, aname->revision,
+ aname->culture == NULL ? "" : aname->culture,
+ aname->public_tok_value);
- if (!load_section_tables (assembly, iinfo))
- goto invalid_image;
+ subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
+ g_free (name);
+ g_free (version);
+
+ if (extra_gac_paths) {
+ paths = extra_gac_paths;
+ while (!result && *paths) {
+ fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "gac", subpath, NULL);
+ result = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+ paths++;
+ }
+ }
+
+ if (result) {
+ result->in_gac = TRUE;
+ g_free (subpath);
+ return result;
+ }
+
+ fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
+ "mono", "gac", subpath, NULL);
+ result = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+
+ if (result)
+ result->in_gac = TRUE;
- /* Load the CLI header */
- if (!load_cli_header (assembly, iinfo))
- goto invalid_image;
+ g_free (subpath);
+
+ return result;
+}
- if (!load_metadata (assembly, iinfo))
- goto invalid_image;
- if (status)
- *status = MONO_ASSEMBLY_OK;
+MonoAssembly*
+mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
+{
+ MonoAssembly *result;
+ char *fullpath, *filename;
+
+ result = invoke_assembly_preload_hook (aname, assemblies_path);
+ if (result)
+ return result;
+ /* g_print ("loading %s\n", aname->name); */
+ /* special case corlib */
+ if (strcmp (aname->name, "mscorlib") == 0) {
+ if (corlib) {
+ /* g_print ("corlib already loaded\n"); */
+ return corlib;
+ }
+ /* g_print ("corlib load\n"); */
+ if (assemblies_path) {
+ corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status);
+ if (corlib)
+ return corlib;
+ }
+ corlib = load_in_path ("mscorlib.dll", default_path, status);
+ return corlib;
+ }
+ result = search_loaded (aname);
- return assembly;
+ if (result)
+ return result;
+ /* g_print ("%s not found in cache\n", aname->name); */
+ if (strstr (aname->name, ".dll"))
+ filename = g_strdup (aname->name);
+ else
+ filename = g_strconcat (aname->name, ".dll", NULL);
-invalid_image:
- mono_assembly_close (assembly);
- return NULL;
+ result = mono_assembly_load_from_gac (aname, filename, status);
+ if (result) {
+ g_free (filename);
+ return result;
+ }
+
+ if (basedir) {
+ fullpath = g_build_filename (basedir, filename, NULL);
+ result = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+ if (result) {
+ result->in_gac = FALSE;
+ g_free (filename);
+ return result;
+ }
+ }
+ if (assemblies_path) {
+ result = load_in_path (filename, (const char**)assemblies_path, status);
+ if (result) {
+ result->in_gac = FALSE;
+ g_free (filename);
+ return result;
+ }
+ }
+ result = load_in_path (filename, default_path, status);
+
+ if (result)
+ result->in_gac = FALSE;
+ g_free (filename);
+ return result;
}
-void
-mono_assembly_close (MonoAssembly *assembly)
+MonoAssembly*
+mono_assembly_loaded (MonoAssemblyName *aname)
{
- g_return_if_fail (assembly != NULL);
+ MonoAssembly *res;
- if (assembly->f)
- fclose (assembly->f);
+ EnterCriticalSection (&assemblies_mutex);
+ res = search_loaded (aname);
+ LeaveCriticalSection (&assemblies_mutex);
- if (assembly->image_info){
- dotnet_image_info_t *ii = assembly->image_info;
+ return res;
+}
- if (ii->dn_metadata.raw_metadata != NULL)
- raw_buffer_free (ii->dn_metadata.raw_metadata);
+void
+mono_assembly_close (MonoAssembly *assembly)
+{
+ MonoImage *image;
+ int i;
- if (ii->dn_section_tables)
- g_free (ii->dn_section_tables);
+ g_return_if_fail (assembly != NULL);
- g_free (assembly->image_info);
+ EnterCriticalSection (&assemblies_mutex);
+ if (--assembly->ref_count != 0) {
+ LeaveCriticalSection (&assemblies_mutex);
+ return;
}
-
+ loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
+ LeaveCriticalSection (&assemblies_mutex);
+ image = assembly->image;
+ if (image->references) {
+ for (i = 0; image->references [i] != NULL; i++)
+ mono_image_close (image->references [i]->image);
+ g_free (image->references);
+ }
+
+ mono_image_close (assembly->image);
+
+ g_free (assembly->basedir);
g_free (assembly);
}
-const char *
-mono_assembly_strerror (enum MonoAssemblyOpenStatus status)
+MonoImage*
+mono_assembly_load_module (MonoAssembly *assembly, guint32 idx)
+{
+ MonoImageOpenStatus status;
+ MonoImage *module;
+
+ module = mono_image_load_file_for_image (assembly->image, idx);
+ if (module)
+ mono_assembly_load_references (module, &status);
+
+ return module;
+}
+
+void
+mono_assembly_foreach (GFunc func, gpointer user_data)
+{
+ GList *copy;
+
+ /*
+ * We make a copy of the list to avoid calling the callback inside the
+ * lock, which could lead to deadlocks.
+ */
+ EnterCriticalSection (&assemblies_mutex);
+ copy = g_list_copy (loaded_assemblies);
+ LeaveCriticalSection (&assemblies_mutex);
+
+ g_list_foreach (loaded_assemblies, func, user_data);
+
+ g_list_free (copy);
+}
+
+/* Holds the assembly of the application, for
+ * System.Diagnostics.Process::MainModule
+ */
+static MonoAssembly *main_assembly=NULL;
+
+void
+mono_assembly_set_main (MonoAssembly *assembly)
{
- switch (status){
- case MONO_ASSEMBLY_OK:
- return "succes";
- case MONO_ASSEMBLY_ERROR_ERRNO:
- return strerror (errno);
- case MONO_ASSEMBLY_IMAGE_INVALID:
- return "File does not contain a valid CIL image";
- }
- return "Internal error";
+ main_assembly=assembly;
}
+
+MonoAssembly *
+mono_assembly_get_main (void)
+{
+ return(main_assembly);
+}
+
+