#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
/* the default search path is just MONO_ASSEMBLIES */
static const char*
/* A hastable of thread->assembly list mappings */
static GHashTable *assemblies_loading;
-#ifdef PLATFORM_WIN32
+static char **extra_gac_paths = NULL;
-static void
-init_default_path (void)
+static gchar*
+encode_public_tok (const guchar *token, gint32 len)
{
+ static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+ gchar *res;
int i;
- default_path [0] = g_strdup (MONO_ASSEMBLIES);
- for (i = strlen (MONO_ASSEMBLIES) - 1; i >= 0; i--) {
- if (default_path [0][i] == '/')
- ((char*) default_path [0])[i] = '\\';
+ 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];
}
+ res [len * 2] = 0;
+ return res;
}
-#endif
static void
-check_env (void) {
+check_path_env (void) {
const char *path;
char **splitted;
- path = getenv ("MONO_PATH");
+ path = g_getenv ("MONO_PATH");
if (!path)
return;
+
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;
+
+ 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);
+
+ splitted++;
+ }
+}
+
+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
+mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
+{
+ if (!l->name || !r->name)
+ return FALSE;
+
+ if (strcmp (l->name, r->name))
+ return FALSE;
+
+ /*
+ * 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 */
for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
ass = tmp->data;
- if (!ass->aname.name)
- continue;
- /* we just compare the name, but later we'll do all the checks */
/* g_print ("compare %s %s\n", aname->name, ass->aname.name); */
- if (strcmp (aname->name, ass->aname.name))
+ if (!mono_assembly_names_equal (aname, &ass->aname))
continue;
/* g_print ("success\n"); */
loading = g_hash_table_lookup (assemblies_loading, GetCurrentThread ());
for (tmp = loading; tmp; tmp = tmp->next) {
ass = tmp->data;
- if (!ass->aname.name)
- continue;
- if (strcmp (aname->name, ass->aname.name))
+ if (!mono_assembly_names_equal (aname, &ass->aname))
continue;
return ass;
default_path [0] = g_strdup (root_dir);
}
+G_CONST_RETURN gchar *
+mono_assembly_getrootdir (void)
+{
+ return default_path [0];
+}
+
/**
* mono_assemblies_init:
*
mono_assemblies_init (void)
{
#ifdef PLATFORM_WIN32
- init_default_path ();
+ mono_set_rootdir ();
#endif
- check_env ();
+ check_path_env ();
+ check_extra_gac_path_env ();
InitializeCriticalSection (&assemblies_mutex);
return TRUE;
}
-static void
+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)
{
MonoTableInfo *t;
guint32 cols [MONO_ASSEMBLYREF_SIZE];
const char *hash;
int i;
+ MonoAssembly **references = NULL;
*status = MONO_IMAGE_OK;
+
+ /*
+ * 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;
t = &image->tables [MONO_TABLE_ASSEMBLYREF];
- image->references = g_new0 (MonoAssembly *, t->rows + 1);
+ references = g_new0 (MonoAssembly *, t->rows + 1);
/*
* Load any assemblies this image references
aname.build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
aname.revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
- image->references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
+ 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;
+ }
+
+ references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
- if (image->references [i] == NULL){
+ if (references [i] == NULL){
int j;
-
- for (j = 0; j < i; j++)
- mono_assembly_close (image->references [j]);
- g_free (image->references);
- g_warning ("Could not find assembly %s", aname.name);
- *status = MONO_IMAGE_MISSING_ASSEMBLYREF;
- return;
+ /*
+ ** 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;
+ }
}
/*
g_error ("Error: Assembly %s references itself", image->name);
*/
}
- image->references [i] = NULL;
+ references [i] = NULL;
/* resolve assembly references for modules */
t = &image->tables [MONO_TABLE_MODULEREF];
mono_assembly_load_references (image->modules [i], status);
}
}
+
+ 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);
+ }
}
typedef struct AssemblyLoadHook AssemblyLoadHook;
if (dot)
*dot = 0;
- if (strcmp (name, "corlib") == 0) {
- g_free (name);
- name = g_strdup ("mscorlib");
- }
/* we do a very simple search for bundled assemblies: it's not a general
* purpose assembly loading mechanism.
*/
image = ass->image;
break;
}
- LeaveCriticalSection (&assemblies_mutex);
+
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) {
- InterlockedIncrement (&image->ref_count);
+ mono_image_addref (image);
return image;
}
#endif
+ EnterCriticalSection (&assemblies_mutex);
image = mono_image_open (filename, status);
+ LeaveCriticalSection (&assemblies_mutex);
+
return image;
}
MonoAssembly *
mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
{
- MonoAssembly *ass, *ass2;
MonoImage *image;
- char *base_dir;
+ MonoAssembly *ass;
MonoImageOpenStatus def_status;
gchar *fname;
- GList *loading;
g_return_val_if_fail (filename != NULL, NULL);
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:/...
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);
- if (uri != filename)
- g_free (uri);
+ g_free (uri);
+
+ if (tmpuri != filename)
+ g_free (tmpuri);
if (error != NULL) {
g_warning ("%s\n", error->message);
return NULL;
}
+ 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;
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->basedir = base_dir;
ass->image = image;
-
- g_free (fname);
-
mono_assembly_fill_assembly_name (image, &ass->aname);
/*
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);
+
+ 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;
+
+ g_free (subpath);
+
+ return result;
+}
+
+
MonoAssembly*
mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
{
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) || (strcmp (aname->name, "corlib") == 0)) {
+ if (strcmp (aname->name, "mscorlib") == 0) {
if (corlib) {
/* g_print ("corlib already loaded\n"); */
return corlib;
corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status);
if (corlib)
return corlib;
- corlib = load_in_path ("corlib.dll", (const char**)assemblies_path, status);
- if (corlib)
- return corlib;
}
corlib = load_in_path ("mscorlib.dll", default_path, status);
- if (!corlib)
- corlib = load_in_path ("corlib.dll", default_path, status);
return corlib;
}
result = search_loaded (aname);
+
if (result)
return result;
/* g_print ("%s not found in cache\n", aname->name); */
filename = g_strdup (aname->name);
else
filename = g_strconcat (aname->name, ".dll", 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;
}
+MonoAssembly*
+mono_assembly_loaded (MonoAssemblyName *aname)
+{
+ MonoAssembly *res;
+
+ EnterCriticalSection (&assemblies_mutex);
+ res = search_loaded (aname);
+ LeaveCriticalSection (&assemblies_mutex);
+
+ return res;
+}
+
void
mono_assembly_close (MonoAssembly *assembly)
{
{
return(main_assembly);
}
+
+