*
* (C) 2001 Ximian, Inc. http://www.ximian.com
*
- * TODO:
- * Implement big-endian versions of the reading routines.
*/
#include <config.h>
#include <stdio.h>
#endif
#include <mono/metadata/loader.h>
#include <mono/metadata/tabledefs.h>
+#include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/domain-internals.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>
+#include <mono/utils/mono-logger.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;
-static gboolean allow_user_gac = FALSE;
-
-#ifdef PLATFORM_WIN32
-
-static void
-init_default_path (void)
-{
- 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] = '\\';
- }
-}
-#endif
-
+static char **extra_gac_paths = NULL;
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' };
+ const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
gchar *res;
int i;
}
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_PREFIX");
+ 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
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);
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])
+ if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
+ gchar* token = g_malloc (8);
+ gchar* encoded;
+ int len;
+
aname->public_key = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
+ len = mono_metadata_decode_blob_size (aname->public_key, (const char**)&aname->public_key);
+
+ mono_digest_get_public_token (token, aname->public_key, len);
+ encoded = encode_public_tok (token, 8);
+ g_strlcpy (aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
+
+ g_free (encoded);
+ g_free (token);
+ }
+ else {
+ aname->public_key = NULL;
+ memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
+ }
+
+ if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
+ aname->public_key = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
+ }
else
aname->public_key = 0;
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);
+ gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname.flags);
+ g_strlcpy (aname.public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
+ g_free (token);
} else {
- aname.public_tok_value = NULL;
+ memset (aname.public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
}
references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
}
if (references [i] != NULL){
- if (getenv ("MONO_SILENT_WARNING") == 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 {
+ char *extra_msg = g_strdup ("");
+
+ if (*status == MONO_IMAGE_ERROR_ERRNO) {
+ extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
+ } else if (*status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
+ extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
+ } else if (*status == MONO_IMAGE_IMAGE_INVALID) {
+ extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
+ }
for (j = 0; j < i; j++)
mono_assembly_close (references [j]);
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",
+ " Token: %s\n%s",
aname.name, image->name, i,
aname.major, aname.minor, aname.build, aname.revision,
- aname.public_tok_value);
+ aname.public_key_token, extra_msg);
*status = MONO_IMAGE_MISSING_ASSEMBLYREF;
+ g_free (extra_msg);
return;
}
}
EnterCriticalSection (&assemblies_mutex);
image = mono_image_open (filename, status);
LeaveCriticalSection (&assemblies_mutex);
-
+
return image;
}
fname = g_strdup (filename);
}
- /* g_print ("file loading %s\n", fname); */
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
+ "Assembly Loader probing location: '%s'.", filename);
image = do_mono_assembly_open (fname, status);
if (!image){
ass = mono_assembly_load_from (image, fname, status);
- mono_config_for_assembly (ass->image);
+ if (ass) {
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
+ "Assembly Loader loaded assembly from location: '%s'.", filename);
+ mono_config_for_assembly (ass->image);
+ }
g_free (fname);
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 *libpath, *fullpath, *fullname;
- const char* direntry;
- GDir *dirhandle;
+ gchar *fullname, *gacpath;
+ gchar **paths;
memset (&aname, 0, sizeof (MonoAssemblyName));
aname.name = name;
- res = mono_assembly_loaded (&aname);
+ res = invoke_assembly_preload_hook (&aname, assemblies_path);
+ if (res) {
+ res->in_gac = FALSE;
+ return res;
+ }
+ res = mono_assembly_loaded (&aname);
if (res)
return res;
- libpath = g_build_path (G_DIR_SEPARATOR_S, MONO_ASSEMBLIES, "mono", "gac", name, NULL);
+ fullname = g_strdup_printf ("%s.dll", name);
- dirhandle = g_dir_open (libpath, 0, NULL);
- if (!dirhandle)
- return NULL;
+ if (extra_gac_paths) {
+ paths = extra_gac_paths;
+ while (!res && *paths) {
+ gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", name, NULL);
+ res = probe_for_partial_name (gacpath, fullname, status);
+ g_free (gacpath);
+ paths++;
+ }
+ }
- fullname = g_strdup_printf ("%s.dll", name);
- while ((direntry = g_dir_read_name (dirhandle))) {
- fullpath = g_build_path (G_DIR_SEPARATOR_S, libpath, direntry, fullname, NULL);
- res = mono_assembly_open (fullpath, status);
- g_free (fullpath);
- if (res)
- break;
+ if (res) {
+ res->in_gac = TRUE;
+ g_free (fullname);
+ return res;
+
}
- g_dir_close (dirhandle);
+
+ 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);
mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status)
{
MonoAssembly *result = NULL;
- gchar *name, *version, *fullpath;
+ gchar *name, *version, *culture, *fullpath, *subpath;
gint32 len;
+ gchar **paths;
- if (!aname->public_tok_value) {
+ if (aname->public_key_token [0] == 0) {
return NULL;
}
name = g_strdup (aname->name);
}
+ if (aname->culture) {
+ culture = g_strdup (aname->culture);
+ g_strdown (culture);
+ } else {
+ culture = g_strdup ("");
+ }
+
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);
+ culture, aname->public_key_token);
- fullpath = g_build_path (G_DIR_SEPARATOR_S, MONO_ASSEMBLIES, "mono", "gac",
- name, version, filename, NULL);
- result = mono_assembly_open (fullpath, status);
-
- g_free (fullpath);
+ subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
+ g_free (name);
+ g_free (version);
+ g_free (culture);
+
+ if (extra_gac_paths) {
+ paths = extra_gac_paths;
+ while (!result && *paths) {
+ fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", subpath, NULL);
+ result = mono_assembly_open (fullpath, status);
+ g_free (fullpath);
+ paths++;
+ }
+ }
- if (!result && allow_user_gac) {
- fullpath = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), ".mono", "gac",
- name, version, filename, NULL);
- result = mono_assembly_open (fullpath, status);
- g_free (fullpath);
+ 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 (name);
- g_free (version);
+ g_free (subpath);
return result;
}
char *fullpath, *filename;
result = invoke_assembly_preload_hook (aname, assemblies_path);
+ if (result) {
+ result->in_gac = FALSE;
+ return result;
+ }
+
+ result = mono_assembly_loaded (aname);
if (result)
return result;
+
/* g_print ("loading %s\n", aname->name); */
/* special case corlib */
if (strcmp (aname->name, "mscorlib") == 0) {
+ char *corlib_file;
if (corlib) {
/* g_print ("corlib already loaded\n"); */
return corlib;
return corlib;
}
corlib = load_in_path ("mscorlib.dll", default_path, status);
+
+ if (corlib)
+ return corlib;
+
+ /* Load corlib from mono/<version> */
+
+ corlib_file = g_build_filename ("mono", mono_get_framework_version (), "mscorlib.dll", NULL);
+ if (assemblies_path) {
+ corlib = load_in_path (corlib_file, (const char**)assemblies_path, status);
+ if (corlib) {
+ g_free (corlib_file);
+ return corlib;
+ }
+ }
+ corlib = load_in_path (corlib_file, default_path, status);
+ g_free (corlib_file);
+
return corlib;
}
- result = search_loaded (aname);
- 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
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(main_assembly);
}
-void
-mono_assembly_allow_user_gac (gboolean allow)
+MonoImage*
+mono_assembly_get_image (MonoAssembly *assembly)
{
- allow_user_gac = allow;
+ return assembly->image;
}
-