2010-05-05 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / assembly.c
bloba33d726d3a6d7c1b1f6c96c03dde229f002c477a
1 /*
2 * assembly.c: Routines for loading assemblies.
3 *
4 * Author:
5 * Miguel de Icaza (miguel@ximian.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 */
10 #include <config.h>
11 #include <stdio.h>
12 #include <glib.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "assembly.h"
17 #include "image.h"
18 #include "object-internals.h"
19 #include <mono/metadata/loader.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/profiler-private.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/domain-internals.h>
25 #include <mono/metadata/mono-endian.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/io-layer/io-layer.h>
28 #include <mono/utils/mono-uri.h>
29 #include <mono/metadata/mono-config.h>
30 #include <mono/utils/mono-digest.h>
31 #include <mono/utils/mono-logger-internal.h>
32 #include <mono/metadata/reflection.h>
33 #include <mono/metadata/coree.h>
35 #ifndef HOST_WIN32
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #endif
41 #ifdef PLATFORM_MACOSX
42 #include <mach-o/dyld.h>
43 #endif
45 /* AssemblyVersionMap: an assembly name and the assembly version set on which it is based */
46 typedef struct {
47 const char* assembly_name;
48 guint8 version_set_index;
49 } AssemblyVersionMap;
51 /* the default search path is empty, the first slot is replaced with the computed value */
52 static const char*
53 default_path [] = {
54 NULL,
55 NULL
58 /* Contains the list of directories to be searched for assemblies (MONO_PATH) */
59 static char **assemblies_path = NULL;
61 /* Contains the list of directories that point to auxiliary GACs */
62 static char **extra_gac_paths = NULL;
64 /* The list of system assemblies what will be remapped to the running
65 * runtime version. WARNING: this list must be sorted.
67 static const AssemblyVersionMap framework_assemblies [] = {
68 {"Accessibility", 0},
69 {"Commons.Xml.Relaxng", 0},
70 {"I18N", 0},
71 {"I18N.CJK", 0},
72 {"I18N.MidEast", 0},
73 {"I18N.Other", 0},
74 {"I18N.Rare", 0},
75 {"I18N.West", 0},
76 {"Microsoft.VisualBasic", 1},
77 {"Microsoft.VisualC", 1},
78 {"Mono.Cairo", 0},
79 {"Mono.CompilerServices.SymbolWriter", 0},
80 {"Mono.Data", 0},
81 {"Mono.Data.SybaseClient", 0},
82 {"Mono.Data.Tds", 0},
83 {"Mono.Data.TdsClient", 0},
84 {"Mono.GetOptions", 0},
85 {"Mono.Http", 0},
86 {"Mono.Posix", 0},
87 {"Mono.Security", 0},
88 {"Mono.Security.Win32", 0},
89 {"Mono.Xml.Ext", 0},
90 {"Novell.Directory.Ldap", 0},
91 {"Npgsql", 0},
92 {"PEAPI", 0},
93 {"System", 0},
94 {"System.Configuration.Install", 0},
95 {"System.Data", 0},
96 {"System.Data.OracleClient", 0},
97 {"System.Data.SqlXml", 0},
98 {"System.Design", 0},
99 {"System.DirectoryServices", 0},
100 {"System.Drawing", 0},
101 {"System.Drawing.Design", 0},
102 {"System.EnterpriseServices", 0},
103 {"System.Management", 0},
104 {"System.Messaging", 0},
105 {"System.Runtime.Remoting", 0},
106 {"System.Runtime.Serialization.Formatters.Soap", 0},
107 {"System.Security", 0},
108 {"System.ServiceProcess", 0},
109 {"System.Web", 0},
110 {"System.Web.Mobile", 0},
111 {"System.Web.Services", 0},
112 {"System.Windows.Forms", 0},
113 {"System.Xml", 0},
114 {"mscorlib", 0}
118 * keeps track of loaded assemblies
120 static GList *loaded_assemblies = NULL;
121 static MonoAssembly *corlib;
123 /* This protects loaded_assemblies and image->references */
124 #define mono_assemblies_lock() EnterCriticalSection (&assemblies_mutex)
125 #define mono_assemblies_unlock() LeaveCriticalSection (&assemblies_mutex)
126 static CRITICAL_SECTION assemblies_mutex;
128 /* If defined, points to the bundled assembly information */
129 const MonoBundledAssembly **bundles;
131 /* Loaded assembly binding info */
132 static GSList *loaded_assembly_bindings = NULL;
134 static MonoAssembly*
135 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload);
136 static MonoBoolean
137 mono_assembly_is_in_gac (const gchar *filanem);
139 static gchar*
140 encode_public_tok (const guchar *token, gint32 len)
142 const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
143 gchar *res;
144 int i;
146 res = g_malloc (len * 2 + 1);
147 for (i = 0; i < len; i++) {
148 res [i * 2] = allowed [token [i] >> 4];
149 res [i * 2 + 1] = allowed [token [i] & 0xF];
151 res [len * 2] = 0;
152 return res;
156 * mono_public_tokens_are_equal:
157 * @pubt1: first public key token
158 * @pubt2: second public key token
160 * Compare two public key tokens and return #TRUE is they are equal and #FALSE
161 * otherwise.
163 gboolean
164 mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2)
166 return memcmp (pubt1, pubt2, 16) == 0;
169 static void
170 check_path_env (void)
172 const char *path;
173 char **splitted, **dest;
175 path = g_getenv ("MONO_PATH");
176 if (!path)
177 return;
179 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
180 if (assemblies_path)
181 g_strfreev (assemblies_path);
182 assemblies_path = dest = splitted;
183 while (*splitted){
184 if (**splitted)
185 *dest++ = *splitted;
186 splitted++;
188 *dest = *splitted;
190 if (g_getenv ("MONO_DEBUG") == NULL)
191 return;
193 splitted = assemblies_path;
194 while (*splitted) {
195 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
196 g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
198 splitted++;
202 static void
203 check_extra_gac_path_env (void) {
204 const char *path;
205 char **splitted, **dest;
207 path = g_getenv ("MONO_GAC_PREFIX");
208 if (!path)
209 return;
211 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
212 if (extra_gac_paths)
213 g_strfreev (extra_gac_paths);
214 extra_gac_paths = dest = splitted;
215 while (*splitted){
216 if (**splitted)
217 *dest++ = *splitted;
218 splitted++;
220 *dest = *splitted;
222 if (g_getenv ("MONO_DEBUG") == NULL)
223 return;
225 while (*splitted) {
226 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
227 g_warning ("'%s' in MONO_GAC_PREFIX doesn't exist or has wrong permissions.", *splitted);
229 splitted++;
233 static gboolean
234 assembly_binding_maps_name (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
236 if (!info || !info->name)
237 return FALSE;
239 if (strcmp (info->name, aname->name))
240 return FALSE;
242 if (info->major != aname->major || info->minor != aname->minor)
243 return FALSE;
245 if ((info->culture != NULL && info->culture [0]) != (aname->culture != NULL && aname->culture [0]))
246 return FALSE;
248 if (info->culture && aname->culture && strcmp (info->culture, aname->culture))
249 return FALSE;
251 if (!mono_public_tokens_are_equal (info->public_key_token, aname->public_key_token))
252 return FALSE;
254 return TRUE;
257 static void
258 mono_assembly_binding_info_free (MonoAssemblyBindingInfo *info)
260 if (!info)
261 return;
263 g_free (info->name);
264 g_free (info->culture);
267 static void
268 get_publisher_policy_info (MonoImage *image, MonoAssemblyName *aname, MonoAssemblyBindingInfo *binding_info)
270 MonoTableInfo *t;
271 guint32 cols [MONO_MANIFEST_SIZE];
272 const gchar *filename;
273 gchar *subpath, *fullpath;
275 t = &image->tables [MONO_TABLE_MANIFESTRESOURCE];
276 /* MS Impl. accepts policy assemblies with more than
277 * one manifest resource, and only takes the first one */
278 if (t->rows < 1) {
279 binding_info->is_valid = FALSE;
280 return;
283 mono_metadata_decode_row (t, 0, cols, MONO_MANIFEST_SIZE);
284 if ((cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) != MONO_IMPLEMENTATION_FILE) {
285 binding_info->is_valid = FALSE;
286 return;
289 filename = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
290 g_assert (filename != NULL);
292 subpath = g_path_get_dirname (image->name);
293 fullpath = g_build_path (G_DIR_SEPARATOR_S, subpath, filename, NULL);
294 mono_config_parse_publisher_policy (fullpath, binding_info);
295 g_free (subpath);
296 g_free (fullpath);
298 /* Define the optional elements/attributes before checking */
299 if (!binding_info->culture)
300 binding_info->culture = g_strdup ("");
302 /* Check that the most important elements/attributes exist */
303 if (!binding_info->name || !binding_info->public_key_token [0] || !binding_info->has_old_version_bottom ||
304 !binding_info->has_new_version || !assembly_binding_maps_name (binding_info, aname)) {
305 mono_assembly_binding_info_free (binding_info);
306 binding_info->is_valid = FALSE;
307 return;
310 binding_info->is_valid = TRUE;
313 static int
314 compare_versions (AssemblyVersionSet *v, MonoAssemblyName *aname)
316 if (v->major > aname->major)
317 return 1;
318 else if (v->major < aname->major)
319 return -1;
321 if (v->minor > aname->minor)
322 return 1;
323 else if (v->minor < aname->minor)
324 return -1;
326 if (v->build > aname->build)
327 return 1;
328 else if (v->build < aname->build)
329 return -1;
331 if (v->revision > aname->revision)
332 return 1;
333 else if (v->revision < aname->revision)
334 return -1;
336 return 0;
339 static gboolean
340 check_policy_versions (MonoAssemblyBindingInfo *info, MonoAssemblyName *name)
342 if (!info->is_valid)
343 return FALSE;
345 /* If has_old_version_top doesn't exist, we don't have an interval */
346 if (!info->has_old_version_top) {
347 if (compare_versions (&info->old_version_bottom, name) == 0)
348 return TRUE;
350 return FALSE;
353 /* Check that the version defined by name is valid for the interval */
354 if (compare_versions (&info->old_version_top, name) < 0)
355 return FALSE;
357 /* We should be greater or equal than the small version */
358 if (compare_versions (&info->old_version_bottom, name) > 0)
359 return FALSE;
361 return TRUE;
365 * mono_assembly_names_equal:
366 * @l: first assembly
367 * @r: second assembly.
369 * Compares two MonoAssemblyNames and returns whether they are equal.
371 * This compares the names, the cultures, the release version and their
372 * public tokens.
374 * Returns: TRUE if both assembly names are equal.
376 gboolean
377 mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
379 if (!l->name || !r->name)
380 return FALSE;
382 if (strcmp (l->name, r->name))
383 return FALSE;
385 if (l->culture && r->culture && strcmp (l->culture, r->culture))
386 return FALSE;
388 if (l->major != r->major || l->minor != r->minor ||
389 l->build != r->build || l->revision != r->revision)
390 if (! ((l->major == 0 && l->minor == 0 && l->build == 0 && l->revision == 0) || (r->major == 0 && r->minor == 0 && r->build == 0 && r->revision == 0)))
391 return FALSE;
393 if (!l->public_key_token [0] || !r->public_key_token [0])
394 return TRUE;
396 if (!mono_public_tokens_are_equal (l->public_key_token, r->public_key_token))
397 return FALSE;
399 return TRUE;
402 static MonoAssembly *
403 load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status, MonoBoolean refonly)
405 int i;
406 char *fullpath;
407 MonoAssembly *result;
409 for (i = 0; search_path [i]; ++i) {
410 fullpath = g_build_filename (search_path [i], basename, NULL);
411 result = mono_assembly_open_full (fullpath, status, refonly);
412 g_free (fullpath);
413 if (result)
414 return result;
416 return NULL;
420 * mono_assembly_setrootdir:
421 * @root_dir: The pathname of the root directory where we will locate assemblies
423 * This routine sets the internal default root directory for looking up
424 * assemblies.
426 * This is used by Windows installations to compute dynamically the
427 * place where the Mono assemblies are located.
430 void
431 mono_assembly_setrootdir (const char *root_dir)
434 * Override the MONO_ASSEMBLIES directory configured at compile time.
436 /* Leak if called more than once */
437 default_path [0] = g_strdup (root_dir);
441 * mono_assembly_getrootdir:
443 * Obtains the root directory used for looking up assemblies.
445 * Returns: a string with the directory, this string should not be freed.
447 G_CONST_RETURN gchar *
448 mono_assembly_getrootdir (void)
450 return default_path [0];
454 * mono_set_dirs:
455 * @assembly_dir: the base directory for assemblies
456 * @config_dir: the base directory for configuration files
458 * This routine is used internally and by developers embedding
459 * the runtime into their own applications.
461 * There are a number of cases to consider: Mono as a system-installed
462 * package that is available on the location preconfigured or Mono in
463 * a relocated location.
465 * If you are using a system-installed Mono, you can pass NULL
466 * to both parameters. If you are not, you should compute both
467 * directory values and call this routine.
469 * The values for a given PREFIX are:
471 * assembly_dir: PREFIX/lib
472 * config_dir: PREFIX/etc
474 * Notice that embedders that use Mono in a relocated way must
475 * compute the location at runtime, as they will be in control
476 * of where Mono is installed.
478 void
479 mono_set_dirs (const char *assembly_dir, const char *config_dir)
481 #if defined (MONO_ASSEMBLIES)
482 if (assembly_dir == NULL)
483 assembly_dir = MONO_ASSEMBLIES;
484 #endif
485 #if defined (MONO_CFG_DIR)
486 if (config_dir == NULL)
487 config_dir = MONO_CFG_DIR;
488 #endif
489 mono_assembly_setrootdir (assembly_dir);
490 mono_set_config_dir (config_dir);
493 #ifndef HOST_WIN32
495 static char *
496 compute_base (char *path)
498 char *p = strrchr (path, '/');
499 if (p == NULL)
500 return NULL;
502 /* Not a well known Mono executable, we are embedded, cant guess the base */
503 if (strcmp (p, "/mono") && strcmp (p, "/monodis") && strcmp (p, "/mint") && strcmp (p, "/monodiet"))
504 return NULL;
506 *p = 0;
507 p = strrchr (path, '/');
508 if (p == NULL)
509 return NULL;
511 if (strcmp (p, "/bin") != 0)
512 return NULL;
513 *p = 0;
514 return path;
517 static void
518 fallback (void)
520 mono_set_dirs (MONO_ASSEMBLIES, MONO_CFG_DIR);
523 static void
524 set_dirs (char *exe)
526 char *base;
527 char *config, *lib, *mono;
528 struct stat buf;
531 * Only /usr prefix is treated specially
533 if (strncmp (exe, MONO_BINDIR, strlen (MONO_BINDIR)) == 0 || (base = compute_base (exe)) == NULL){
534 fallback ();
535 return;
538 config = g_build_filename (base, "etc", NULL);
539 lib = g_build_filename (base, "lib", NULL);
540 mono = g_build_filename (lib, "mono/1.0", NULL);
541 if (stat (mono, &buf) == -1)
542 fallback ();
543 else {
544 mono_set_dirs (lib, config);
547 g_free (config);
548 g_free (lib);
549 g_free (mono);
552 #endif /* HOST_WIN32 */
555 * mono_set_rootdir:
557 * Registers the root directory for the Mono runtime, for Linux and Solaris 10,
558 * this auto-detects the prefix where Mono was installed.
560 void
561 mono_set_rootdir (void)
563 #if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
564 gchar *bindir, *installdir, *root, *name, *config;
566 #ifdef HOST_WIN32
567 name = mono_get_module_file_name ((HMODULE) &__ImageBase);
568 #else
571 * _NSGetExecutablePath may return -1 to indicate buf is not large
572 * enough, but we ignore that case to avoid having to do extra dynamic
573 * allocation for the path and hope that 4096 is enough - this is
574 * ok in the Linux/Solaris case below at least...
577 gchar buf[4096];
578 guint buf_size = sizeof (buf);
580 if (_NSGetExecutablePath (buf, &buf_size) == 0)
581 name = g_strdup (buf);
583 if (name == NULL) {
584 fallback ();
585 return;
588 #endif
590 bindir = g_path_get_dirname (name);
591 installdir = g_path_get_dirname (bindir);
592 root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", NULL);
594 config = g_build_filename (root, "..", "etc", NULL);
595 #ifdef HOST_WIN32
596 mono_set_dirs (root, config);
597 #else
598 if (g_file_test (root, G_FILE_TEST_EXISTS) && g_file_test (config, G_FILE_TEST_EXISTS))
599 mono_set_dirs (root, config);
600 else
601 fallback ();
602 #endif
604 g_free (config);
605 g_free (root);
606 g_free (installdir);
607 g_free (bindir);
608 g_free (name);
609 #elif defined(DISABLE_MONO_AUTODETECTION)
610 fallback ();
611 #else
612 char buf [4096];
613 int s;
614 char *str;
616 /* Linux style */
617 s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
619 if (s != -1){
620 buf [s] = 0;
621 set_dirs (buf);
622 return;
625 /* Solaris 10 style */
626 str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
627 s = readlink (str, buf, sizeof (buf)-1);
628 g_free (str);
629 if (s != -1){
630 buf [s] = 0;
631 set_dirs (buf);
632 return;
634 fallback ();
635 #endif
639 * mono_assemblies_init:
641 * Initialize global variables used by this module.
643 void
644 mono_assemblies_init (void)
647 * Initialize our internal paths if we have not been initialized yet.
648 * This happens when embedders use Mono.
650 if (mono_assembly_getrootdir () == NULL)
651 mono_set_rootdir ();
653 check_path_env ();
654 check_extra_gac_path_env ();
656 InitializeCriticalSection (&assemblies_mutex);
659 gboolean
660 mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
662 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
663 guint32 cols [MONO_ASSEMBLY_SIZE];
665 if (!t->rows)
666 return FALSE;
668 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
670 aname->hash_len = 0;
671 aname->hash_value = NULL;
672 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
673 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
674 aname->flags = cols [MONO_ASSEMBLY_FLAGS];
675 aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
676 aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
677 aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
678 aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
679 aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
680 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
681 guchar* token = g_malloc (8);
682 gchar* encoded;
683 const gchar* pkey;
684 int len;
686 pkey = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
687 len = mono_metadata_decode_blob_size (pkey, &pkey);
688 aname->public_key = (guchar*)pkey;
690 mono_digest_get_public_token (token, aname->public_key, len);
691 encoded = encode_public_tok (token, 8);
692 g_strlcpy ((char*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
694 g_free (encoded);
695 g_free (token);
697 else {
698 aname->public_key = NULL;
699 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
702 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
703 aname->public_key = (guchar*)mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
705 else
706 aname->public_key = 0;
708 return TRUE;
712 * mono_stringify_assembly_name:
713 * @aname: the assembly name.
715 * Convert @aname into its string format. The returned string is dynamically
716 * allocated and should be freed by the caller.
718 * Returns: a newly allocated string with a string representation of
719 * the assembly name.
721 char*
722 mono_stringify_assembly_name (MonoAssemblyName *aname)
724 return g_strdup_printf (
725 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
726 aname->name,
727 aname->major, aname->minor, aname->build, aname->revision,
728 aname->culture && *aname->culture? aname->culture: "neutral",
729 aname->public_key_token [0] ? (char *)aname->public_key_token : "null",
730 (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
733 static gchar*
734 assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
736 const gchar *public_tok;
737 int len;
739 public_tok = mono_metadata_blob_heap (image, key_index);
740 len = mono_metadata_decode_blob_size (public_tok, &public_tok);
742 if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
743 guchar token [8];
744 mono_digest_get_public_token (token, (guchar*)public_tok, len);
745 return encode_public_tok (token, 8);
748 return encode_public_tok ((guchar*)public_tok, len);
752 * mono_assembly_addref:
753 * @assemnly: the assembly to reference
755 * This routine increments the reference count on a MonoAssembly.
756 * The reference count is reduced every time the method mono_assembly_close() is
757 * invoked.
759 void
760 mono_assembly_addref (MonoAssembly *assembly)
762 InterlockedIncrement (&assembly->ref_count);
765 static MonoAssemblyName *
766 mono_assembly_remap_version (MonoAssemblyName *aname, MonoAssemblyName *dest_aname)
768 const MonoRuntimeInfo *current_runtime;
769 int pos, first, last;
771 if (aname->name == NULL) return aname;
772 current_runtime = mono_get_runtime_info ();
774 first = 0;
775 last = G_N_ELEMENTS (framework_assemblies) - 1;
777 while (first <= last) {
778 int res;
779 pos = first + (last - first) / 2;
780 res = strcmp (aname->name, framework_assemblies[pos].assembly_name);
781 if (res == 0) {
782 const AssemblyVersionSet* vset;
783 int index = framework_assemblies[pos].version_set_index;
784 g_assert (index < G_N_ELEMENTS (current_runtime->version_sets));
785 vset = &current_runtime->version_sets [index];
787 if (aname->major == vset->major && aname->minor == vset->minor &&
788 aname->build == vset->build && aname->revision == vset->revision)
789 return aname;
791 if ((aname->major | aname->minor | aname->build | aname->revision) != 0)
792 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
793 "The request to load the assembly %s v%d.%d.%d.%d was remapped to v%d.%d.%d.%d",
794 aname->name,
795 aname->major, aname->minor, aname->build, aname->revision,
796 vset->major, vset->minor, vset->build, vset->revision
799 memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
800 dest_aname->major = vset->major;
801 dest_aname->minor = vset->minor;
802 dest_aname->build = vset->build;
803 dest_aname->revision = vset->revision;
804 return dest_aname;
805 } else if (res < 0) {
806 last = pos - 1;
807 } else {
808 first = pos + 1;
811 return aname;
815 * mono_assembly_get_assemblyref:
817 * Fill out ANAME with the assembly name of the INDEXth assembly reference in IMAGE.
819 void
820 mono_assembly_get_assemblyref (MonoImage *image, int index, MonoAssemblyName *aname)
822 MonoTableInfo *t;
823 guint32 cols [MONO_ASSEMBLYREF_SIZE];
824 const char *hash;
826 t = &image->tables [MONO_TABLE_ASSEMBLYREF];
828 mono_metadata_decode_row (t, index, cols, MONO_ASSEMBLYREF_SIZE);
830 hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
831 aname->hash_len = mono_metadata_decode_blob_size (hash, &hash);
832 aname->hash_value = hash;
833 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
834 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
835 aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
836 aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
837 aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
838 aname->build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
839 aname->revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
841 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
842 gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname->flags);
843 g_strlcpy ((char*)aname->public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
844 g_free (token);
845 } else {
846 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
850 void
851 mono_assembly_load_reference (MonoImage *image, int index)
853 MonoAssembly *reference;
854 MonoAssemblyName aname;
855 MonoImageOpenStatus status;
858 * image->references is shared between threads, so we need to access
859 * it inside a critical section.
861 mono_assemblies_lock ();
862 if (!image->references) {
863 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
865 image->references = g_new0 (MonoAssembly *, t->rows + 1);
867 reference = image->references [index];
868 mono_assemblies_unlock ();
869 if (reference)
870 return;
872 mono_assembly_get_assemblyref (image, index, &aname);
874 if (image->assembly && image->assembly->ref_only) {
875 /* We use the loaded corlib */
876 if (!strcmp (aname.name, "mscorlib"))
877 reference = mono_assembly_load_full (&aname, image->assembly->basedir, &status, FALSE);
878 else {
879 reference = mono_assembly_loaded_full (&aname, TRUE);
880 if (!reference)
881 /* Try a postload search hook */
882 reference = mono_assembly_invoke_search_hook_internal (&aname, TRUE, TRUE);
886 * Here we must advice that the error was due to
887 * a non loaded reference using the ReflectionOnly api
889 if (!reference)
890 reference = REFERENCE_MISSING;
891 } else
892 reference = mono_assembly_load (&aname, image->assembly? image->assembly->basedir: NULL, &status);
894 if (reference == NULL){
895 char *extra_msg;
897 if (status == MONO_IMAGE_ERROR_ERRNO && errno == ENOENT) {
898 extra_msg = g_strdup_printf ("The assembly was not found in the Global Assembly Cache, a path listed in the MONO_PATH environment variable, or in the location of the executing assembly (%s).\n", image->assembly != NULL ? image->assembly->basedir : "" );
899 } else if (status == MONO_IMAGE_ERROR_ERRNO) {
900 extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
901 } else if (status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
902 extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
903 } else if (status == MONO_IMAGE_IMAGE_INVALID) {
904 extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
905 } else {
906 extra_msg = g_strdup ("");
909 g_warning ("The following assembly referenced from %s could not be loaded:\n"
910 " Assembly: %s (assemblyref_index=%d)\n"
911 " Version: %d.%d.%d.%d\n"
912 " Public Key: %s\n%s",
913 image->name, aname.name, index,
914 aname.major, aname.minor, aname.build, aname.revision,
915 strlen ((char*)aname.public_key_token) == 0 ? "(none)" : (char*)aname.public_key_token, extra_msg);
916 g_free (extra_msg);
919 mono_assemblies_lock ();
920 if (reference == NULL) {
921 /* Flag as not found */
922 reference = REFERENCE_MISSING;
925 if (!image->references [index]) {
926 if (reference != REFERENCE_MISSING){
927 mono_assembly_addref (reference);
928 if (image->assembly)
929 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Ref addref %s %p -> %s %p: %d\n",
930 image->assembly->aname.name, image->assembly, reference->aname.name, reference, reference->ref_count);
931 } else {
932 if (image->assembly)
933 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Failed to load assembly %s %p\n",
934 image->assembly->aname.name, image->assembly);
937 image->references [index] = reference;
939 mono_assemblies_unlock ();
941 if (image->references [index] != reference) {
942 /* Somebody loaded it before us */
943 mono_assembly_close (reference);
947 void
948 mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
950 /* This is a no-op now but it is part of the embedding API so we can't remove it */
951 *status = MONO_IMAGE_OK;
954 typedef struct AssemblyLoadHook AssemblyLoadHook;
955 struct AssemblyLoadHook {
956 AssemblyLoadHook *next;
957 MonoAssemblyLoadFunc func;
958 gpointer user_data;
961 AssemblyLoadHook *assembly_load_hook = NULL;
963 void
964 mono_assembly_invoke_load_hook (MonoAssembly *ass)
966 AssemblyLoadHook *hook;
968 for (hook = assembly_load_hook; hook; hook = hook->next) {
969 hook->func (ass, hook->user_data);
973 void
974 mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
976 AssemblyLoadHook *hook;
978 g_return_if_fail (func != NULL);
980 hook = g_new0 (AssemblyLoadHook, 1);
981 hook->func = func;
982 hook->user_data = user_data;
983 hook->next = assembly_load_hook;
984 assembly_load_hook = hook;
987 static void
988 free_assembly_load_hooks (void)
990 AssemblyLoadHook *hook, *next;
992 for (hook = assembly_load_hook; hook; hook = next) {
993 next = hook->next;
994 g_free (hook);
998 typedef struct AssemblySearchHook AssemblySearchHook;
999 struct AssemblySearchHook {
1000 AssemblySearchHook *next;
1001 MonoAssemblySearchFunc func;
1002 gboolean refonly;
1003 gboolean postload;
1004 gpointer user_data;
1007 AssemblySearchHook *assembly_search_hook = NULL;
1009 static MonoAssembly*
1010 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload)
1012 AssemblySearchHook *hook;
1014 for (hook = assembly_search_hook; hook; hook = hook->next) {
1015 if ((hook->refonly == refonly) && (hook->postload == postload)) {
1016 MonoAssembly *ass = hook->func (aname, hook->user_data);
1017 if (ass)
1018 return ass;
1022 return NULL;
1025 MonoAssembly*
1026 mono_assembly_invoke_search_hook (MonoAssemblyName *aname)
1028 return mono_assembly_invoke_search_hook_internal (aname, FALSE, FALSE);
1031 static void
1032 mono_install_assembly_search_hook_internal (MonoAssemblySearchFunc func, gpointer user_data, gboolean refonly, gboolean postload)
1034 AssemblySearchHook *hook;
1036 g_return_if_fail (func != NULL);
1038 hook = g_new0 (AssemblySearchHook, 1);
1039 hook->func = func;
1040 hook->user_data = user_data;
1041 hook->refonly = refonly;
1042 hook->postload = postload;
1043 hook->next = assembly_search_hook;
1044 assembly_search_hook = hook;
1047 void
1048 mono_install_assembly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1050 mono_install_assembly_search_hook_internal (func, user_data, FALSE, FALSE);
1053 static void
1054 free_assembly_search_hooks (void)
1056 AssemblySearchHook *hook, *next;
1058 for (hook = assembly_search_hook; hook; hook = next) {
1059 next = hook->next;
1060 g_free (hook);
1064 void
1065 mono_install_assembly_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1067 mono_install_assembly_search_hook_internal (func, user_data, TRUE, FALSE);
1070 void
1071 mono_install_assembly_postload_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1073 mono_install_assembly_search_hook_internal (func, user_data, FALSE, TRUE);
1076 void
1077 mono_install_assembly_postload_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1079 mono_install_assembly_search_hook_internal (func, user_data, TRUE, TRUE);
1082 typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
1083 struct AssemblyPreLoadHook {
1084 AssemblyPreLoadHook *next;
1085 MonoAssemblyPreLoadFunc func;
1086 gpointer user_data;
1089 static AssemblyPreLoadHook *assembly_preload_hook = NULL;
1090 static AssemblyPreLoadHook *assembly_refonly_preload_hook = NULL;
1092 static MonoAssembly *
1093 invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1095 AssemblyPreLoadHook *hook;
1096 MonoAssembly *assembly;
1098 for (hook = assembly_preload_hook; hook; hook = hook->next) {
1099 assembly = hook->func (aname, assemblies_path, hook->user_data);
1100 if (assembly != NULL)
1101 return assembly;
1104 return NULL;
1107 static MonoAssembly *
1108 invoke_assembly_refonly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1110 AssemblyPreLoadHook *hook;
1111 MonoAssembly *assembly;
1113 for (hook = assembly_refonly_preload_hook; hook; hook = hook->next) {
1114 assembly = hook->func (aname, assemblies_path, hook->user_data);
1115 if (assembly != NULL)
1116 return assembly;
1119 return NULL;
1122 void
1123 mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1125 AssemblyPreLoadHook *hook;
1127 g_return_if_fail (func != NULL);
1129 hook = g_new0 (AssemblyPreLoadHook, 1);
1130 hook->func = func;
1131 hook->user_data = user_data;
1132 hook->next = assembly_preload_hook;
1133 assembly_preload_hook = hook;
1136 void
1137 mono_install_assembly_refonly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1139 AssemblyPreLoadHook *hook;
1141 g_return_if_fail (func != NULL);
1143 hook = g_new0 (AssemblyPreLoadHook, 1);
1144 hook->func = func;
1145 hook->user_data = user_data;
1146 hook->next = assembly_refonly_preload_hook;
1147 assembly_refonly_preload_hook = hook;
1150 static void
1151 free_assembly_preload_hooks (void)
1153 AssemblyPreLoadHook *hook, *next;
1155 for (hook = assembly_preload_hook; hook; hook = next) {
1156 next = hook->next;
1157 g_free (hook);
1160 for (hook = assembly_refonly_preload_hook; hook; hook = next) {
1161 next = hook->next;
1162 g_free (hook);
1166 static gchar *
1167 absolute_dir (const gchar *filename)
1169 gchar *cwd;
1170 gchar *mixed;
1171 gchar **parts;
1172 gchar *part;
1173 GList *list, *tmp;
1174 GString *result;
1175 gchar *res;
1176 gint i;
1178 if (g_path_is_absolute (filename)) {
1179 part = g_path_get_dirname (filename);
1180 res = g_strconcat (part, G_DIR_SEPARATOR_S, NULL);
1181 g_free (part);
1182 return res;
1185 cwd = g_get_current_dir ();
1186 mixed = g_build_filename (cwd, filename, NULL);
1187 parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
1188 g_free (mixed);
1189 g_free (cwd);
1191 list = NULL;
1192 for (i = 0; (part = parts [i]) != NULL; i++) {
1193 if (!strcmp (part, "."))
1194 continue;
1196 if (!strcmp (part, "..")) {
1197 if (list && list->next) /* Don't remove root */
1198 list = g_list_delete_link (list, list);
1199 } else {
1200 list = g_list_prepend (list, part);
1204 result = g_string_new ("");
1205 list = g_list_reverse (list);
1207 /* Ignores last data pointer, which should be the filename */
1208 for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next){
1209 if (tmp->data)
1210 g_string_append_printf (result, "%s%c", (char *) tmp->data,
1211 G_DIR_SEPARATOR);
1214 res = result->str;
1215 g_string_free (result, FALSE);
1216 g_list_free (list);
1217 g_strfreev (parts);
1218 if (*res == '\0') {
1219 g_free (res);
1220 return g_strdup (".");
1223 return res;
1226 /**
1227 * mono_assembly_open_from_bundle:
1228 * @filename: Filename requested
1229 * @status: return value
1231 * This routine tries to open the assembly specified by `filename' from the
1232 * defined bundles, if found, returns the MonoImage for it, if not found
1233 * returns NULL
1235 MonoImage *
1236 mono_assembly_open_from_bundle (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1238 int i;
1239 char *name;
1240 MonoImage *image = NULL;
1243 * we do a very simple search for bundled assemblies: it's not a general
1244 * purpose assembly loading mechanism.
1247 if (!bundles)
1248 return NULL;
1250 name = g_path_get_basename (filename);
1252 mono_assemblies_lock ();
1253 for (i = 0; !image && bundles [i]; ++i) {
1254 if (strcmp (bundles [i]->name, name) == 0) {
1255 image = mono_image_open_from_data_with_name ((char*)bundles [i]->data, bundles [i]->size, FALSE, status, refonly, name);
1256 break;
1259 mono_assemblies_unlock ();
1260 g_free (name);
1261 if (image) {
1262 mono_image_addref (image);
1263 return image;
1265 return NULL;
1268 MonoAssembly *
1269 mono_assembly_open_full (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1271 MonoImage *image;
1272 MonoAssembly *ass;
1273 MonoImageOpenStatus def_status;
1274 gchar *fname;
1275 gchar *new_fname;
1277 g_return_val_if_fail (filename != NULL, NULL);
1279 if (!status)
1280 status = &def_status;
1281 *status = MONO_IMAGE_OK;
1283 if (strncmp (filename, "file://", 7) == 0) {
1284 GError *error = NULL;
1285 gchar *uri = (gchar *) filename;
1286 gchar *tmpuri;
1289 * MS allows file://c:/... and fails on file://localhost/c:/...
1290 * They also throw an IndexOutOfRangeException if "file://"
1292 if (uri [7] != '/')
1293 uri = g_strdup_printf ("file:///%s", uri + 7);
1295 tmpuri = uri;
1296 uri = mono_escape_uri_string (tmpuri);
1297 fname = g_filename_from_uri (uri, NULL, &error);
1298 g_free (uri);
1300 if (tmpuri != filename)
1301 g_free (tmpuri);
1303 if (error != NULL) {
1304 g_warning ("%s\n", error->message);
1305 g_error_free (error);
1306 fname = g_strdup (filename);
1308 } else {
1309 fname = g_strdup (filename);
1312 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1313 "Assembly Loader probing location: '%s'.", fname);
1315 new_fname = NULL;
1316 if (!mono_assembly_is_in_gac (fname))
1317 new_fname = mono_make_shadow_copy (fname);
1318 if (new_fname && new_fname != fname) {
1319 g_free (fname);
1320 fname = new_fname;
1321 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1322 "Assembly Loader shadow-copied assembly to: '%s'.", fname);
1325 image = NULL;
1327 if (bundles != NULL)
1328 image = mono_assembly_open_from_bundle (fname, status, refonly);
1330 if (!image)
1331 image = mono_image_open_full (fname, status, refonly);
1333 if (!image){
1334 if (*status == MONO_IMAGE_OK)
1335 *status = MONO_IMAGE_ERROR_ERRNO;
1336 g_free (fname);
1337 return NULL;
1340 if (image->assembly) {
1341 /* Already loaded by another appdomain */
1342 mono_assembly_invoke_load_hook (image->assembly);
1343 mono_image_close (image);
1344 g_free (fname);
1345 return image->assembly;
1348 ass = mono_assembly_load_from_full (image, fname, status, refonly);
1350 if (ass) {
1351 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1352 "Assembly Loader loaded assembly from location: '%s'.", filename);
1353 if (!refonly)
1354 mono_config_for_assembly (ass->image);
1357 /* Clear the reference added by mono_image_open */
1358 mono_image_close (image);
1360 g_free (fname);
1362 return ass;
1365 static void
1366 free_item (gpointer val, gpointer user_data)
1368 g_free (val);
1372 * mono_assembly_load_friends:
1373 * @ass: an assembly
1375 * Load the list of friend assemblies that are allowed to access
1376 * the assembly's internal types and members. They are stored as assembly
1377 * names in custom attributes.
1379 * This is an internal method, we need this because when we load mscorlib
1380 * we do not have the mono_defaults.internals_visible_class loaded yet,
1381 * so we need to load these after we initialize the runtime.
1383 * LOCKING: Acquires the assemblies lock plus the loader lock.
1385 void
1386 mono_assembly_load_friends (MonoAssembly* ass)
1388 int i;
1389 MonoCustomAttrInfo* attrs;
1390 GSList *list;
1392 if (ass->friend_assembly_names_inited)
1393 return;
1395 attrs = mono_custom_attrs_from_assembly (ass);
1396 if (!attrs) {
1397 mono_assemblies_lock ();
1398 ass->friend_assembly_names_inited = TRUE;
1399 mono_assemblies_unlock ();
1400 return;
1403 mono_assemblies_lock ();
1404 if (ass->friend_assembly_names_inited) {
1405 mono_assemblies_unlock ();
1406 return;
1408 mono_assemblies_unlock ();
1410 list = NULL;
1412 * We build the list outside the assemblies lock, the worse that can happen
1413 * is that we'll need to free the allocated list.
1415 for (i = 0; i < attrs->num_attrs; ++i) {
1416 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1417 MonoAssemblyName *aname;
1418 const gchar *data;
1419 guint slen;
1420 /* Do some sanity checking */
1421 if (!attr->ctor || attr->ctor->klass != mono_defaults.internals_visible_class)
1422 continue;
1423 if (attr->data_size < 4)
1424 continue;
1425 data = (const char*)attr->data;
1426 /* 0xFF means null string, see custom attr format */
1427 if (data [0] != 1 || data [1] != 0 || (data [2] & 0xFF) == 0xFF)
1428 continue;
1429 slen = mono_metadata_decode_value (data + 2, &data);
1430 aname = g_new0 (MonoAssemblyName, 1);
1431 /*g_print ("friend ass: %s\n", data);*/
1432 if (mono_assembly_name_parse_full (data, aname, TRUE, NULL, NULL)) {
1433 list = g_slist_prepend (list, aname);
1434 } else {
1435 g_free (aname);
1438 mono_custom_attrs_free (attrs);
1440 mono_assemblies_lock ();
1441 if (ass->friend_assembly_names_inited) {
1442 mono_assemblies_unlock ();
1443 g_slist_foreach (list, free_item, NULL);
1444 g_slist_free (list);
1445 return;
1447 ass->friend_assembly_names = list;
1449 /* Because of the double checked locking pattern above */
1450 mono_memory_barrier ();
1451 ass->friend_assembly_names_inited = TRUE;
1452 mono_assemblies_unlock ();
1456 * mono_assembly_open:
1457 * @filename: Opens the assembly pointed out by this name
1458 * @status: where a status code can be returned
1460 * mono_assembly_open opens the PE-image pointed by @filename, and
1461 * loads any external assemblies referenced by it.
1463 * Return: a pointer to the MonoAssembly if @filename contains a valid
1464 * assembly or NULL on error. Details about the error are stored in the
1465 * @status variable.
1467 MonoAssembly *
1468 mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
1470 return mono_assembly_open_full (filename, status, FALSE);
1473 MonoAssembly *
1474 mono_assembly_load_from_full (MonoImage *image, const char*fname,
1475 MonoImageOpenStatus *status, gboolean refonly)
1477 MonoAssembly *ass, *ass2;
1478 char *base_dir;
1480 if (!image->tables [MONO_TABLE_ASSEMBLY].rows) {
1481 /* 'image' doesn't have a manifest -- maybe someone is trying to Assembly.Load a .netmodule */
1482 *status = MONO_IMAGE_IMAGE_INVALID;
1483 return NULL;
1486 #if defined (HOST_WIN32)
1488 gchar *tmp_fn;
1489 int i;
1491 tmp_fn = g_strdup (fname);
1492 for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
1493 if (tmp_fn [i] == '/')
1494 tmp_fn [i] = '\\';
1497 base_dir = absolute_dir (tmp_fn);
1498 g_free (tmp_fn);
1500 #else
1501 base_dir = absolute_dir (fname);
1502 #endif
1505 * Create assembly struct, and enter it into the assembly cache
1507 ass = g_new0 (MonoAssembly, 1);
1508 ass->basedir = base_dir;
1509 ass->ref_only = refonly;
1510 ass->image = image;
1512 mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
1514 mono_assembly_fill_assembly_name (image, &ass->aname);
1516 if (mono_defaults.corlib && strcmp (ass->aname.name, "mscorlib") == 0) {
1517 // MS.NET doesn't support loading other mscorlibs
1518 g_free (ass);
1519 g_free (base_dir);
1520 mono_image_addref (mono_defaults.corlib);
1521 *status = MONO_IMAGE_OK;
1522 return mono_defaults.corlib->assembly;
1525 /* Add a non-temporary reference because of ass->image */
1526 mono_image_addref (image);
1528 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Image addref %s %p -> %s %p: %d\n", ass->aname.name, ass, image->name, image, image->ref_count);
1531 * The load hooks might take locks so we can't call them while holding the
1532 * assemblies lock.
1534 if (ass->aname.name) {
1535 ass2 = mono_assembly_invoke_search_hook_internal (&ass->aname, refonly, FALSE);
1536 if (ass2) {
1537 g_free (ass);
1538 g_free (base_dir);
1539 mono_image_close (image);
1540 *status = MONO_IMAGE_OK;
1541 return ass2;
1545 mono_assemblies_lock ();
1547 if (image->assembly) {
1549 * This means another thread has already loaded the assembly, but not yet
1550 * called the load hooks so the search hook can't find the assembly.
1552 mono_assemblies_unlock ();
1553 ass2 = image->assembly;
1554 g_free (ass);
1555 g_free (base_dir);
1556 mono_image_close (image);
1557 *status = MONO_IMAGE_OK;
1558 return ass2;
1561 image->assembly = ass;
1563 loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
1564 mono_assemblies_unlock ();
1566 #ifdef HOST_WIN32
1567 if (image->is_module_handle)
1568 mono_image_fixup_vtable (image);
1569 #endif
1571 mono_assembly_invoke_load_hook (ass);
1573 mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
1575 return ass;
1578 MonoAssembly *
1579 mono_assembly_load_from (MonoImage *image, const char *fname,
1580 MonoImageOpenStatus *status)
1582 return mono_assembly_load_from_full (image, fname, status, FALSE);
1586 * mono_assembly_name_free:
1587 * @aname: assembly name to free
1589 * Frees the provided assembly name object.
1590 * (it does not frees the object itself, only the name members).
1592 void
1593 mono_assembly_name_free (MonoAssemblyName *aname)
1595 if (aname == NULL)
1596 return;
1598 g_free ((void *) aname->name);
1599 g_free ((void *) aname->culture);
1600 g_free ((void *) aname->hash_value);
1603 static gboolean
1604 parse_public_key (const gchar *key, gchar** pubkey)
1606 const gchar *pkey;
1607 gchar header [16], val, *arr;
1608 gint i, j, offset, bitlen, keylen, pkeylen;
1610 keylen = strlen (key) >> 1;
1611 if (keylen < 1)
1612 return FALSE;
1614 val = g_ascii_xdigit_value (key [0]) << 4;
1615 val |= g_ascii_xdigit_value (key [1]);
1616 switch (val) {
1617 case 0x00:
1618 if (keylen < 13)
1619 return FALSE;
1620 val = g_ascii_xdigit_value (key [24]);
1621 val |= g_ascii_xdigit_value (key [25]);
1622 if (val != 0x06)
1623 return FALSE;
1624 pkey = key + 24;
1625 break;
1626 case 0x06:
1627 pkey = key;
1628 break;
1629 default:
1630 return FALSE;
1633 /* We need the first 16 bytes
1634 * to check whether this key is valid or not */
1635 pkeylen = strlen (pkey) >> 1;
1636 if (pkeylen < 16)
1637 return FALSE;
1639 for (i = 0, j = 0; i < 16; i++) {
1640 header [i] = g_ascii_xdigit_value (pkey [j++]) << 4;
1641 header [i] |= g_ascii_xdigit_value (pkey [j++]);
1644 if (header [0] != 0x06 || /* PUBLICKEYBLOB (0x06) */
1645 header [1] != 0x02 || /* Version (0x02) */
1646 header [2] != 0x00 || /* Reserved (word) */
1647 header [3] != 0x00 ||
1648 (guint)(read32 (header + 8)) != 0x31415352) /* DWORD magic = RSA1 */
1649 return FALSE;
1651 /* Based on this length, we _should_ be able to know if the length is right */
1652 bitlen = read32 (header + 12) >> 3;
1653 if ((bitlen + 16 + 4) != pkeylen)
1654 return FALSE;
1656 /* parsing is OK and the public key itself is not requested back */
1657 if (!pubkey)
1658 return TRUE;
1660 /* Encode the size of the blob */
1661 offset = 0;
1662 if (keylen <= 127) {
1663 arr = g_malloc (keylen + 1);
1664 arr [offset++] = keylen;
1665 } else {
1666 arr = g_malloc (keylen + 2);
1667 arr [offset++] = 0x80; /* 10bs */
1668 arr [offset++] = keylen;
1671 for (i = offset, j = 0; i < keylen + offset; i++) {
1672 arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
1673 arr [i] |= g_ascii_xdigit_value (key [j++]);
1676 *pubkey = arr;
1678 return TRUE;
1681 static gboolean
1682 build_assembly_name (const char *name, const char *version, const char *culture, const char *token, const char *key, guint32 flags, MonoAssemblyName *aname, gboolean save_public_key)
1684 gint major, minor, build, revision;
1685 gint len;
1686 gint version_parts;
1687 gchar *pkey, *pkeyptr, *encoded, tok [8];
1689 memset (aname, 0, sizeof (MonoAssemblyName));
1691 if (version) {
1692 version_parts = sscanf (version, "%u.%u.%u.%u", &major, &minor, &build, &revision);
1693 if (version_parts < 2 || version_parts > 4)
1694 return FALSE;
1696 /* FIXME: we should set build & revision to -1 (instead of 0)
1697 if these are not set in the version string. That way, later on,
1698 we can still determine if these were specified. */
1699 aname->major = major;
1700 aname->minor = minor;
1701 if (version_parts >= 3)
1702 aname->build = build;
1703 else
1704 aname->build = 0;
1705 if (version_parts == 4)
1706 aname->revision = revision;
1707 else
1708 aname->revision = 0;
1711 aname->flags = flags;
1712 aname->name = g_strdup (name);
1714 if (culture) {
1715 if (g_ascii_strcasecmp (culture, "neutral") == 0)
1716 aname->culture = g_strdup ("");
1717 else
1718 aname->culture = g_strdup (culture);
1721 if (token && strncmp (token, "null", 4) != 0) {
1722 char *lower;
1724 /* the constant includes the ending NULL, hence the -1 */
1725 if (strlen (token) != (MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)) {
1726 mono_assembly_name_free (aname);
1727 return FALSE;
1729 lower = g_ascii_strdown (token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1730 g_strlcpy ((char*)aname->public_key_token, lower, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1731 g_free (lower);
1734 if (key) {
1735 if (strcmp (key, "null") == 0 || !parse_public_key (key, &pkey)) {
1736 mono_assembly_name_free (aname);
1737 return FALSE;
1740 len = mono_metadata_decode_blob_size ((const gchar *) pkey, (const gchar **) &pkeyptr);
1741 // We also need to generate the key token
1742 mono_digest_get_public_token ((guchar*) tok, (guint8*) pkeyptr, len);
1743 encoded = encode_public_tok ((guchar*) tok, 8);
1744 g_strlcpy ((gchar*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1745 g_free (encoded);
1747 if (save_public_key)
1748 aname->public_key = (guint8*) pkey;
1749 else
1750 g_free (pkey);
1753 return TRUE;
1756 static gboolean
1757 parse_assembly_directory_name (const char *name, const char *dirname, MonoAssemblyName *aname)
1759 gchar **parts;
1760 gboolean res;
1762 parts = g_strsplit (dirname, "_", 3);
1763 if (!parts || !parts[0] || !parts[1] || !parts[2]) {
1764 g_strfreev (parts);
1765 return FALSE;
1768 res = build_assembly_name (name, parts[0], parts[1], parts[2], NULL, 0, aname, FALSE);
1769 g_strfreev (parts);
1770 return res;
1773 gboolean
1774 mono_assembly_name_parse_full (const char *name, MonoAssemblyName *aname, gboolean save_public_key, gboolean *is_version_defined, gboolean *is_token_defined)
1776 gchar *dllname;
1777 gchar *version = NULL;
1778 gchar *culture = NULL;
1779 gchar *token = NULL;
1780 gchar *key = NULL;
1781 gchar *retargetable = NULL;
1782 gboolean res;
1783 gchar *value;
1784 gchar **parts;
1785 gchar **tmp;
1786 gboolean version_defined;
1787 gboolean token_defined;
1788 guint32 flags = 0;
1790 if (!is_version_defined)
1791 is_version_defined = &version_defined;
1792 *is_version_defined = FALSE;
1793 if (!is_token_defined)
1794 is_token_defined = &token_defined;
1795 *is_token_defined = FALSE;
1797 parts = tmp = g_strsplit (name, ",", 6);
1798 if (!tmp || !*tmp) {
1799 g_strfreev (tmp);
1800 return FALSE;
1803 dllname = g_strstrip (*tmp);
1805 tmp++;
1807 while (*tmp) {
1808 value = g_strstrip (*tmp);
1809 if (!g_ascii_strncasecmp (value, "Version=", 8)) {
1810 *is_version_defined = TRUE;
1811 version = g_strstrip (value + 8);
1812 if (strlen (version) == 0) {
1813 goto cleanup_and_fail;
1815 tmp++;
1816 continue;
1819 if (!g_ascii_strncasecmp (value, "Culture=", 8)) {
1820 culture = g_strstrip (value + 8);
1821 if (strlen (culture) == 0) {
1822 goto cleanup_and_fail;
1824 tmp++;
1825 continue;
1828 if (!g_ascii_strncasecmp (value, "PublicKeyToken=", 15)) {
1829 *is_token_defined = TRUE;
1830 token = g_strstrip (value + 15);
1831 if (strlen (token) == 0) {
1832 goto cleanup_and_fail;
1834 tmp++;
1835 continue;
1838 if (!g_ascii_strncasecmp (value, "PublicKey=", 10)) {
1839 key = g_strstrip (value + 10);
1840 if (strlen (key) == 0) {
1841 goto cleanup_and_fail;
1843 tmp++;
1844 continue;
1847 if (!g_ascii_strncasecmp (value, "Retargetable=", 13)) {
1848 retargetable = g_strstrip (value + 13);
1849 if (strlen (retargetable) == 0) {
1850 goto cleanup_and_fail;
1852 if (!g_ascii_strcasecmp (retargetable, "yes")) {
1853 flags |= ASSEMBLYREF_RETARGETABLE_FLAG;
1854 } else if (g_ascii_strcasecmp (retargetable, "no")) {
1855 goto cleanup_and_fail;
1857 tmp++;
1858 continue;
1861 if (!g_ascii_strncasecmp (value, "ProcessorArchitecture=", 22)) {
1862 /* this is ignored for now, until we can change MonoAssemblyName */
1863 tmp++;
1864 continue;
1867 g_strfreev (parts);
1868 return FALSE;
1871 /* if retargetable flag is set, then we must have a fully qualified name */
1872 if (retargetable != NULL && (version == NULL || culture == NULL || (key == NULL && token == NULL))) {
1873 goto cleanup_and_fail;
1876 res = build_assembly_name (dllname, version, culture, token, key, flags,
1877 aname, save_public_key);
1878 g_strfreev (parts);
1879 return res;
1881 cleanup_and_fail:
1882 g_strfreev (parts);
1883 return FALSE;
1887 * mono_assembly_name_parse:
1888 * @name: name to parse
1889 * @aname: the destination assembly name
1891 * Parses an assembly qualified type name and assigns the name,
1892 * version, culture and token to the provided assembly name object.
1894 * Returns: true if the name could be parsed.
1896 gboolean
1897 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname)
1899 return mono_assembly_name_parse_full (name, aname, FALSE, NULL, NULL);
1903 * mono_assembly_name_new:
1904 * @name: name to parse
1906 * Allocate a new MonoAssemblyName and fill its values from the
1907 * passed @name.
1909 * Returns: a newly allocated structure or NULL if there was any failure.
1911 MonoAssemblyName*
1912 mono_assembly_name_new (const char *name)
1914 MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
1915 if (mono_assembly_name_parse (name, aname))
1916 return aname;
1917 g_free (aname);
1918 return NULL;
1921 const char*
1922 mono_assembly_name_get_name (MonoAssemblyName *aname)
1924 return aname->name;
1927 const char*
1928 mono_assembly_name_get_culture (MonoAssemblyName *aname)
1930 return aname->culture;
1933 mono_byte*
1934 mono_assembly_name_get_pubkeytoken (MonoAssemblyName *aname)
1936 if (aname->public_key_token [0])
1937 return aname->public_key_token;
1938 return NULL;
1941 uint16_t
1942 mono_assembly_name_get_version (MonoAssemblyName *aname, uint16_t *minor, uint16_t *build, uint16_t *revision)
1944 if (minor)
1945 *minor = aname->minor;
1946 if (build)
1947 *build = aname->build;
1948 if (revision)
1949 *revision = aname->revision;
1950 return aname->major;
1953 static MonoAssembly*
1954 probe_for_partial_name (const char *basepath, const char *fullname, MonoAssemblyName *aname, MonoImageOpenStatus *status)
1956 gchar *fullpath = NULL;
1957 GDir *dirhandle;
1958 const char* direntry;
1959 MonoAssemblyName gac_aname;
1960 gint major=-1, minor=0, build=0, revision=0;
1961 gboolean exact_version;
1963 dirhandle = g_dir_open (basepath, 0, NULL);
1964 if (!dirhandle)
1965 return NULL;
1967 exact_version = (aname->major | aname->minor | aname->build | aname->revision) != 0;
1969 while ((direntry = g_dir_read_name (dirhandle))) {
1970 gboolean match = TRUE;
1972 if(!parse_assembly_directory_name (aname->name, direntry, &gac_aname))
1973 continue;
1975 if (aname->culture != NULL && strcmp (aname->culture, gac_aname.culture) != 0)
1976 match = FALSE;
1978 if (match && strlen ((char*)aname->public_key_token) > 0 &&
1979 !mono_public_tokens_are_equal (aname->public_key_token, gac_aname.public_key_token))
1980 match = FALSE;
1982 if (match) {
1983 if (exact_version) {
1984 match = (aname->major == gac_aname.major && aname->minor == gac_aname.minor &&
1985 aname->build == gac_aname.build && aname->revision == gac_aname.revision);
1987 else if (gac_aname.major < major)
1988 match = FALSE;
1989 else if (gac_aname.major == major) {
1990 if (gac_aname.minor < minor)
1991 match = FALSE;
1992 else if (gac_aname.minor == minor) {
1993 if (gac_aname.build < build)
1994 match = FALSE;
1995 else if (gac_aname.build == build && gac_aname.revision <= revision)
1996 match = FALSE;
2001 if (match) {
2002 major = gac_aname.major;
2003 minor = gac_aname.minor;
2004 build = gac_aname.build;
2005 revision = gac_aname.revision;
2006 g_free (fullpath);
2007 fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
2010 mono_assembly_name_free (&gac_aname);
2013 g_dir_close (dirhandle);
2015 if (fullpath == NULL)
2016 return NULL;
2017 else {
2018 MonoAssembly *res = mono_assembly_open (fullpath, status);
2019 g_free (fullpath);
2020 return res;
2024 MonoAssembly*
2025 mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
2027 MonoAssembly *res;
2028 MonoAssemblyName *aname, base_name, maped_aname;
2029 gchar *fullname, *gacpath;
2030 gchar **paths;
2032 memset (&base_name, 0, sizeof (MonoAssemblyName));
2033 aname = &base_name;
2035 if (!mono_assembly_name_parse (name, aname))
2036 return NULL;
2039 * If no specific version has been requested, make sure we load the
2040 * correct version for system assemblies.
2042 if ((aname->major | aname->minor | aname->build | aname->revision) == 0)
2043 aname = mono_assembly_remap_version (aname, &maped_aname);
2045 res = mono_assembly_loaded (aname);
2046 if (res) {
2047 mono_assembly_name_free (aname);
2048 return res;
2051 res = invoke_assembly_preload_hook (aname, assemblies_path);
2052 if (res) {
2053 res->in_gac = FALSE;
2054 mono_assembly_name_free (aname);
2055 return res;
2058 fullname = g_strdup_printf ("%s.dll", aname->name);
2060 if (extra_gac_paths) {
2061 paths = extra_gac_paths;
2062 while (!res && *paths) {
2063 gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", aname->name, NULL);
2064 res = probe_for_partial_name (gacpath, fullname, aname, status);
2065 g_free (gacpath);
2066 paths++;
2070 if (res) {
2071 res->in_gac = TRUE;
2072 g_free (fullname);
2073 mono_assembly_name_free (aname);
2074 return res;
2077 gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", aname->name, NULL);
2078 res = probe_for_partial_name (gacpath, fullname, aname, status);
2079 g_free (gacpath);
2081 if (res)
2082 res->in_gac = TRUE;
2083 else {
2084 MonoDomain *domain = mono_domain_get ();
2085 MonoReflectionAssembly *refasm = mono_try_assembly_resolve (domain, mono_string_new (domain, name), FALSE);
2086 if (refasm)
2087 res = refasm->assembly;
2090 g_free (fullname);
2091 mono_assembly_name_free (aname);
2093 return res;
2096 static MonoBoolean
2097 mono_assembly_is_in_gac (const gchar *filename)
2099 const gchar *rootdir;
2100 gchar *gp;
2101 gchar **paths;
2103 if (filename == NULL)
2104 return FALSE;
2106 for (paths = extra_gac_paths; paths && *paths; paths++) {
2107 if (strstr (*paths, filename) != *paths)
2108 continue;
2110 gp = (gchar *) (filename + strlen (*paths));
2111 if (*gp != G_DIR_SEPARATOR)
2112 continue;
2113 gp++;
2114 if (strncmp (gp, "lib", 3))
2115 continue;
2116 gp += 3;
2117 if (*gp != G_DIR_SEPARATOR)
2118 continue;
2119 gp++;
2120 if (strncmp (gp, "mono", 4))
2121 continue;
2122 gp += 4;
2123 if (*gp != G_DIR_SEPARATOR)
2124 continue;
2125 gp++;
2126 if (strncmp (gp, "gac", 3))
2127 continue;
2128 gp += 3;
2129 if (*gp != G_DIR_SEPARATOR)
2130 continue;
2132 return TRUE;
2135 rootdir = mono_assembly_getrootdir ();
2136 if (strstr (filename, rootdir) != filename)
2137 return FALSE;
2139 gp = (gchar *) (filename + strlen (rootdir));
2140 if (*gp != G_DIR_SEPARATOR)
2141 return FALSE;
2142 gp++;
2143 if (strncmp (gp, "mono", 4))
2144 return FALSE;
2145 gp += 4;
2146 if (*gp != G_DIR_SEPARATOR)
2147 return FALSE;
2148 gp++;
2149 if (strncmp (gp, "gac", 3))
2150 return FALSE;
2151 gp += 3;
2152 if (*gp != G_DIR_SEPARATOR)
2153 return FALSE;
2154 return TRUE;
2157 static MonoImage*
2158 mono_assembly_load_publisher_policy (MonoAssemblyName *aname)
2160 MonoImage *image;
2161 gchar *filename, *pname, *name, *culture, *version, *fullpath, *subpath;
2162 gchar **paths;
2163 gint32 len;
2165 if (strstr (aname->name, ".dll")) {
2166 len = strlen (aname->name) - 4;
2167 name = g_malloc (len);
2168 strncpy (name, aname->name, len);
2169 } else
2170 name = g_strdup (aname->name);
2172 if (aname->culture)
2173 culture = g_utf8_strdown (aname->culture, -1);
2174 else
2175 culture = g_strdup ("");
2177 pname = g_strdup_printf ("policy.%d.%d.%s", aname->major, aname->minor, name);
2178 version = g_strdup_printf ("0.0.0.0_%s_%s", culture, aname->public_key_token);
2179 g_free (name);
2180 g_free (culture);
2182 filename = g_strconcat (pname, ".dll", NULL);
2183 subpath = g_build_path (G_DIR_SEPARATOR_S, pname, version, filename, NULL);
2184 g_free (pname);
2185 g_free (version);
2186 g_free (filename);
2188 image = NULL;
2189 if (extra_gac_paths) {
2190 paths = extra_gac_paths;
2191 while (!image && *paths) {
2192 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths,
2193 "lib", "mono", "gac", subpath, NULL);
2194 image = mono_image_open (fullpath, NULL);
2195 g_free (fullpath);
2196 paths++;
2200 if (image) {
2201 g_free (subpath);
2202 return image;
2205 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
2206 "mono", "gac", subpath, NULL);
2207 image = mono_image_open (fullpath, NULL);
2208 g_free (subpath);
2209 g_free (fullpath);
2211 return image;
2214 static MonoAssemblyName*
2215 mono_assembly_bind_version (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2217 memcpy (dest_name, aname, sizeof (MonoAssemblyName));
2218 dest_name->major = info->new_version.major;
2219 dest_name->minor = info->new_version.minor;
2220 dest_name->build = info->new_version.build;
2221 dest_name->revision = info->new_version.revision;
2223 return dest_name;
2226 /* LOCKING: Assumes that we are already locked */
2227 static MonoAssemblyBindingInfo*
2228 search_binding_loaded (MonoAssemblyName *aname)
2230 GSList *tmp;
2232 for (tmp = loaded_assembly_bindings; tmp; tmp = tmp->next) {
2233 MonoAssemblyBindingInfo *info = tmp->data;
2234 if (assembly_binding_maps_name (info, aname))
2235 return info;
2238 return NULL;
2241 static inline gboolean
2242 info_compare_versions (AssemblyVersionSet *left, AssemblyVersionSet *right)
2244 if (left->major != right->major || left->minor != right->minor ||
2245 left->build != right->build || left->revision != right->revision)
2246 return FALSE;
2248 return TRUE;
2251 static inline gboolean
2252 info_versions_equal (MonoAssemblyBindingInfo *left, MonoAssemblyBindingInfo *right)
2254 if (left->has_old_version_bottom != right->has_old_version_bottom)
2255 return FALSE;
2257 if (left->has_old_version_top != right->has_old_version_top)
2258 return FALSE;
2260 if (left->has_new_version != right->has_new_version)
2261 return FALSE;
2263 if (left->has_old_version_bottom && !info_compare_versions (&left->old_version_bottom, &right->old_version_bottom))
2264 return FALSE;
2266 if (left->has_old_version_top && !info_compare_versions (&left->old_version_top, &right->old_version_top))
2267 return FALSE;
2269 if (left->has_new_version && !info_compare_versions (&left->new_version, &right->new_version))
2270 return FALSE;
2272 return TRUE;
2275 /* LOCKING: assumes all the necessary locks are held */
2276 static void
2277 assembly_binding_info_parsed (MonoAssemblyBindingInfo *info, void *user_data)
2279 MonoAssemblyBindingInfo *info_copy;
2280 GSList *tmp;
2281 MonoAssemblyBindingInfo *info_tmp;
2282 MonoDomain *domain = (MonoDomain*)user_data;
2284 if (!domain)
2285 return;
2287 for (tmp = domain->assembly_bindings; tmp; tmp = tmp->next) {
2288 info_tmp = tmp->data;
2289 if (strcmp (info->name, info_tmp->name) == 0 && info_versions_equal (info, info_tmp))
2290 return;
2293 info_copy = mono_mempool_alloc0 (domain->mp, sizeof (MonoAssemblyBindingInfo));
2294 memcpy (info_copy, info, sizeof (MonoAssemblyBindingInfo));
2295 if (info->name)
2296 info_copy->name = mono_mempool_strdup (domain->mp, info->name);
2297 if (info->culture)
2298 info_copy->culture = mono_mempool_strdup (domain->mp, info->culture);
2300 domain->assembly_bindings = g_slist_append_mempool (domain->mp, domain->assembly_bindings, info_copy);
2303 static inline gboolean
2304 info_major_minor_in_range (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
2306 if (!info->has_old_version_bottom)
2307 return FALSE;
2309 if (info->old_version_bottom.major > aname->major || info->old_version_bottom.minor > aname->minor)
2310 return FALSE;
2312 if (info->has_old_version_top && (info->old_version_top.major < aname->major || info->old_version_top.minor < aname->minor))
2313 return FALSE;
2315 /* This is not the nicest way to do it, but it's a by-product of the way parsing is done */
2316 info->major = aname->major;
2317 info->minor = aname->minor;
2319 return TRUE;
2322 /* LOCKING: Assumes that we are already locked - both loader and domain locks */
2323 static MonoAssemblyBindingInfo*
2324 get_per_domain_assembly_binding_info (MonoDomain *domain, MonoAssemblyName *aname)
2326 MonoAssemblyBindingInfo *info;
2327 GSList *list;
2329 if (!domain->assembly_bindings)
2330 return NULL;
2332 info = NULL;
2333 for (list = domain->assembly_bindings; list; list = list->next) {
2334 info = list->data;
2335 if (info && !strcmp (aname->name, info->name) && info_major_minor_in_range (info, aname))
2336 break;
2337 info = NULL;
2340 if (info) {
2341 if (info->name && info->public_key_token [0] && info->has_old_version_bottom &&
2342 info->has_new_version && assembly_binding_maps_name (info, aname))
2343 info->is_valid = TRUE;
2344 else
2345 info->is_valid = FALSE;
2348 return info;
2351 static MonoAssemblyName*
2352 mono_assembly_apply_binding (MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2354 MonoAssemblyBindingInfo *info, *info2;
2355 MonoImage *ppimage;
2356 MonoDomain *domain;
2358 if (aname->public_key_token [0] == 0)
2359 return aname;
2361 domain = mono_domain_get ();
2362 mono_loader_lock ();
2363 info = search_binding_loaded (aname);
2364 if (!info) {
2365 mono_domain_lock (domain);
2366 info = get_per_domain_assembly_binding_info (domain, aname);
2367 mono_domain_unlock (domain);
2370 mono_loader_unlock ();
2371 if (info) {
2372 if (!check_policy_versions (info, aname))
2373 return aname;
2375 mono_assembly_bind_version (info, aname, dest_name);
2376 return dest_name;
2379 if (domain && domain->setup && domain->setup->configuration_file) {
2380 mono_domain_lock (domain);
2381 if (!domain->assembly_bindings_parsed) {
2382 gchar *domain_config_file = mono_string_to_utf8 (domain->setup->configuration_file);
2384 mono_config_parse_assembly_bindings (domain_config_file, aname->major, aname->minor, domain, assembly_binding_info_parsed);
2385 domain->assembly_bindings_parsed = TRUE;
2386 g_free (domain_config_file);
2388 mono_domain_unlock (domain);
2390 mono_loader_lock ();
2391 mono_domain_lock (domain);
2392 info = get_per_domain_assembly_binding_info (domain, aname);
2393 mono_domain_unlock (domain);
2394 mono_loader_unlock ();
2397 if (!info) {
2398 info = g_new0 (MonoAssemblyBindingInfo, 1);
2399 info->major = aname->major;
2400 info->minor = aname->minor;
2403 if (!info->is_valid) {
2404 ppimage = mono_assembly_load_publisher_policy (aname);
2405 if (ppimage) {
2406 get_publisher_policy_info (ppimage, aname, info);
2407 mono_image_close (ppimage);
2411 /* Define default error value if needed */
2412 if (!info->is_valid) {
2413 info->name = g_strdup (aname->name);
2414 info->culture = g_strdup (aname->culture);
2415 g_strlcpy ((char *)info->public_key_token, (const char *)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2418 mono_loader_lock ();
2419 info2 = search_binding_loaded (aname);
2420 if (info2) {
2421 /* This binding was added by another thread
2422 * before us */
2423 mono_assembly_binding_info_free (info);
2424 g_free (info);
2426 info = info2;
2427 } else
2428 loaded_assembly_bindings = g_slist_prepend (loaded_assembly_bindings, info);
2430 mono_loader_unlock ();
2432 if (!info->is_valid || !check_policy_versions (info, aname))
2433 return aname;
2435 mono_assembly_bind_version (info, aname, dest_name);
2436 return dest_name;
2440 * mono_assembly_load_from_gac
2442 * @aname: The assembly name object
2444 static MonoAssembly*
2445 mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status, MonoBoolean refonly)
2447 MonoAssembly *result = NULL;
2448 gchar *name, *version, *culture, *fullpath, *subpath;
2449 gint32 len;
2450 gchar **paths;
2451 char *pubtok;
2453 if (aname->public_key_token [0] == 0) {
2454 return NULL;
2457 if (strstr (aname->name, ".dll")) {
2458 len = strlen (filename) - 4;
2459 name = g_malloc (len);
2460 strncpy (name, aname->name, len);
2461 } else {
2462 name = g_strdup (aname->name);
2465 if (aname->culture) {
2466 culture = g_utf8_strdown (aname->culture, -1);
2467 } else {
2468 culture = g_strdup ("");
2471 pubtok = g_ascii_strdown ((char*)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2472 version = g_strdup_printf ("%d.%d.%d.%d_%s_%s", aname->major,
2473 aname->minor, aname->build, aname->revision,
2474 culture, pubtok);
2475 g_free (pubtok);
2477 subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
2478 g_free (name);
2479 g_free (version);
2480 g_free (culture);
2482 if (extra_gac_paths) {
2483 paths = extra_gac_paths;
2484 while (!result && *paths) {
2485 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", subpath, NULL);
2486 result = mono_assembly_open_full (fullpath, status, refonly);
2487 g_free (fullpath);
2488 paths++;
2492 if (result) {
2493 result->in_gac = TRUE;
2494 g_free (subpath);
2495 return result;
2498 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
2499 "mono", "gac", subpath, NULL);
2500 result = mono_assembly_open_full (fullpath, status, refonly);
2501 g_free (fullpath);
2503 if (result)
2504 result->in_gac = TRUE;
2506 g_free (subpath);
2508 return result;
2512 MonoAssembly*
2513 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status)
2515 char *corlib_file;
2517 if (corlib) {
2518 /* g_print ("corlib already loaded\n"); */
2519 return corlib;
2522 if (assemblies_path) {
2523 corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status, FALSE);
2524 if (corlib)
2525 return corlib;
2528 /* Load corlib from mono/<version> */
2530 corlib_file = g_build_filename ("mono", runtime->framework_version, "mscorlib.dll", NULL);
2531 if (assemblies_path) {
2532 corlib = load_in_path (corlib_file, (const char**)assemblies_path, status, FALSE);
2533 if (corlib) {
2534 g_free (corlib_file);
2535 return corlib;
2538 corlib = load_in_path (corlib_file, default_path, status, FALSE);
2539 g_free (corlib_file);
2541 return corlib;
2544 MonoAssembly*
2545 mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
2546 const char *basedir,
2547 MonoImageOpenStatus *status,
2548 gboolean refonly)
2550 MonoAssembly *result;
2551 char *fullpath, *filename;
2552 MonoAssemblyName maped_aname, maped_name_pp;
2553 int ext_index;
2554 const char *ext;
2555 int len;
2557 aname = mono_assembly_remap_version (aname, &maped_aname);
2559 /* Reflection only assemblies don't get assembly binding */
2560 if (!refonly)
2561 aname = mono_assembly_apply_binding (aname, &maped_name_pp);
2563 result = mono_assembly_loaded_full (aname, refonly);
2564 if (result)
2565 return result;
2567 result = refonly ? invoke_assembly_refonly_preload_hook (aname, assemblies_path) : invoke_assembly_preload_hook (aname, assemblies_path);
2568 if (result) {
2569 result->in_gac = FALSE;
2570 return result;
2573 /* Currently we retrieve the loaded corlib for reflection
2574 * only requests, like a common reflection only assembly
2576 if (strcmp (aname->name, "mscorlib") == 0 || strcmp (aname->name, "mscorlib.dll") == 0) {
2577 return mono_assembly_load_corlib (mono_get_runtime_info (), status);
2580 len = strlen (aname->name);
2581 for (ext_index = 0; ext_index < 2; ext_index ++) {
2582 ext = ext_index == 0 ? ".dll" : ".exe";
2583 if (len > 4 && (!strcmp (aname->name + len - 4, ".dll") || !strcmp (aname->name + len - 4, ".exe"))) {
2584 filename = g_strdup (aname->name);
2585 /* Don't try appending .dll/.exe if it already has one of those extensions */
2586 ext_index++;
2587 } else {
2588 filename = g_strconcat (aname->name, ext, NULL);
2591 result = mono_assembly_load_from_gac (aname, filename, status, refonly);
2592 if (result) {
2593 g_free (filename);
2594 return result;
2597 if (basedir) {
2598 fullpath = g_build_filename (basedir, filename, NULL);
2599 result = mono_assembly_open_full (fullpath, status, refonly);
2600 g_free (fullpath);
2601 if (result) {
2602 result->in_gac = FALSE;
2603 g_free (filename);
2604 return result;
2608 result = load_in_path (filename, default_path, status, refonly);
2609 if (result)
2610 result->in_gac = FALSE;
2611 g_free (filename);
2612 if (result)
2613 return result;
2616 return result;
2620 * mono_assembly_load_full:
2621 * @aname: A MonoAssemblyName with the assembly name to load.
2622 * @basedir: A directory to look up the assembly at.
2623 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
2624 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
2626 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
2627 * attempts to load the assembly from that directory before probing the standard locations.
2629 * If the assembly is being opened in reflection-only mode (@refonly set to TRUE) then no
2630 * assembly binding takes place.
2632 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
2633 * value pointed by status is updated with an error code.
2635 MonoAssembly*
2636 mono_assembly_load_full (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
2638 MonoAssembly *result = mono_assembly_load_full_nosearch (aname, basedir, status, refonly);
2640 if (!result)
2641 /* Try a postload search hook */
2642 result = mono_assembly_invoke_search_hook_internal (aname, refonly, TRUE);
2643 return result;
2647 * mono_assembly_load:
2648 * @aname: A MonoAssemblyName with the assembly name to load.
2649 * @basedir: A directory to look up the assembly at.
2650 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
2652 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
2653 * attempts to load the assembly from that directory before probing the standard locations.
2655 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
2656 * value pointed by status is updated with an error code.
2658 MonoAssembly*
2659 mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
2661 return mono_assembly_load_full (aname, basedir, status, FALSE);
2664 MonoAssembly*
2665 mono_assembly_loaded_full (MonoAssemblyName *aname, gboolean refonly)
2667 MonoAssembly *res;
2668 MonoAssemblyName maped_aname;
2670 aname = mono_assembly_remap_version (aname, &maped_aname);
2672 res = mono_assembly_invoke_search_hook_internal (aname, refonly, FALSE);
2674 return res;
2678 * mono_assembly_loaded:
2679 * @aname: an assembly to look for.
2681 * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
2682 * a MonoAssembly that matches the MonoAssemblyName specified.
2684 MonoAssembly*
2685 mono_assembly_loaded (MonoAssemblyName *aname)
2687 return mono_assembly_loaded_full (aname, FALSE);
2691 * Returns whether mono_assembly_close_finish() must be called as
2692 * well. See comment for mono_image_close_except_pools() for why we
2693 * unload in two steps.
2695 gboolean
2696 mono_assembly_close_except_image_pools (MonoAssembly *assembly)
2698 GSList *tmp;
2699 g_return_val_if_fail (assembly != NULL, FALSE);
2701 if (assembly == REFERENCE_MISSING)
2702 return FALSE;
2704 /* Might be 0 already */
2705 if (InterlockedDecrement (&assembly->ref_count) > 0)
2706 return FALSE;
2708 mono_profiler_assembly_event (assembly, MONO_PROFILE_START_UNLOAD);
2710 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading assembly %s [%p].", assembly->aname.name, assembly);
2712 mono_debug_close_image (assembly->image);
2714 mono_assemblies_lock ();
2715 loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
2716 mono_assemblies_unlock ();
2718 assembly->image->assembly = NULL;
2720 if (!mono_image_close_except_pools (assembly->image))
2721 assembly->image = NULL;
2723 for (tmp = assembly->friend_assembly_names; tmp; tmp = tmp->next) {
2724 MonoAssemblyName *fname = tmp->data;
2725 mono_assembly_name_free (fname);
2726 g_free (fname);
2728 g_slist_free (assembly->friend_assembly_names);
2729 g_free (assembly->basedir);
2731 mono_profiler_assembly_event (assembly, MONO_PROFILE_END_UNLOAD);
2733 return TRUE;
2736 void
2737 mono_assembly_close_finish (MonoAssembly *assembly)
2739 g_assert (assembly && assembly != REFERENCE_MISSING);
2741 if (assembly->image)
2742 mono_image_close_finish (assembly->image);
2744 if (assembly->dynamic) {
2745 g_free ((char*)assembly->aname.culture);
2746 } else {
2747 g_free (assembly);
2752 * mono_assembly_close:
2753 * @assembly: the assembly to release.
2755 * This method releases a reference to the @assembly. The assembly is
2756 * only released when all the outstanding references to it are released.
2758 void
2759 mono_assembly_close (MonoAssembly *assembly)
2761 if (mono_assembly_close_except_image_pools (assembly))
2762 mono_assembly_close_finish (assembly);
2765 MonoImage*
2766 mono_assembly_load_module (MonoAssembly *assembly, guint32 idx)
2768 return mono_image_load_file_for_image (assembly->image, idx);
2771 void
2772 mono_assembly_foreach (GFunc func, gpointer user_data)
2774 GList *copy;
2777 * We make a copy of the list to avoid calling the callback inside the
2778 * lock, which could lead to deadlocks.
2780 mono_assemblies_lock ();
2781 copy = g_list_copy (loaded_assemblies);
2782 mono_assemblies_unlock ();
2784 g_list_foreach (loaded_assemblies, func, user_data);
2786 g_list_free (copy);
2790 * mono_assemblies_cleanup:
2792 * Free all resources used by this module.
2794 void
2795 mono_assemblies_cleanup (void)
2797 GSList *l;
2799 DeleteCriticalSection (&assemblies_mutex);
2801 for (l = loaded_assembly_bindings; l; l = l->next) {
2802 MonoAssemblyBindingInfo *info = l->data;
2804 mono_assembly_binding_info_free (info);
2805 g_free (info);
2807 g_slist_free (loaded_assembly_bindings);
2809 free_assembly_load_hooks ();
2810 free_assembly_search_hooks ();
2811 free_assembly_preload_hooks ();
2815 * Holds the assembly of the application, for
2816 * System.Diagnostics.Process::MainModule
2818 static MonoAssembly *main_assembly=NULL;
2820 void
2821 mono_assembly_set_main (MonoAssembly *assembly)
2823 main_assembly = assembly;
2827 * mono_assembly_get_main:
2829 * Returns: the assembly for the application, the first assembly that is loaded by the VM
2831 MonoAssembly *
2832 mono_assembly_get_main (void)
2834 return (main_assembly);
2838 * mono_assembly_get_image:
2839 * @assembly: The assembly to retrieve the image from
2841 * Returns: the MonoImage associated with this assembly.
2843 MonoImage*
2844 mono_assembly_get_image (MonoAssembly *assembly)
2846 return assembly->image;
2849 void
2850 mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies)
2852 bundles = assemblies;
2855 #define MONO_DECLSEC_FORMAT_10 0x3C
2856 #define MONO_DECLSEC_FORMAT_20 0x2E
2857 #define MONO_DECLSEC_FIELD 0x53
2858 #define MONO_DECLSEC_PROPERTY 0x54
2860 #define SKIP_VISIBILITY_XML_ATTRIBUTE ("\"SkipVerification\"")
2861 #define SKIP_VISIBILITY_ATTRIBUTE_NAME ("System.Security.Permissions.SecurityPermissionAttribute")
2862 #define SKIP_VISIBILITY_ATTRIBUTE_SIZE (sizeof (SKIP_VISIBILITY_ATTRIBUTE_NAME) - 1)
2863 #define SKIP_VISIBILITY_PROPERTY_NAME ("SkipVerification")
2864 #define SKIP_VISIBILITY_PROPERTY_SIZE (sizeof (SKIP_VISIBILITY_PROPERTY_NAME) - 1)
2866 static gboolean
2867 mono_assembly_try_decode_skip_verification_param (const char *p, const char **resp, gboolean *abort_decoding)
2869 int len;
2870 switch (*p++) {
2871 case MONO_DECLSEC_PROPERTY:
2872 break;
2873 case MONO_DECLSEC_FIELD:
2874 default:
2875 *abort_decoding = TRUE;
2876 return FALSE;
2877 break;
2880 if (*p++ != MONO_TYPE_BOOLEAN) {
2881 *abort_decoding = TRUE;
2882 return FALSE;
2885 /* property name length */
2886 len = mono_metadata_decode_value (p, &p);
2888 if (len >= SKIP_VISIBILITY_PROPERTY_SIZE && !memcmp (p, SKIP_VISIBILITY_PROPERTY_NAME, SKIP_VISIBILITY_PROPERTY_SIZE)) {
2889 p += len;
2890 return *p;
2892 p += len + 1;
2894 *resp = p;
2895 return FALSE;
2898 static gboolean
2899 mono_assembly_try_decode_skip_verification (const char *p, const char *endn)
2901 int i, j, num, len, params_len;
2903 if (*p == MONO_DECLSEC_FORMAT_10) {
2904 gsize read, written;
2905 char *res = g_convert (p, endn - p, "UTF-8", "UTF-16LE", &read, &written, NULL);
2906 if (res) {
2907 gboolean found = strstr (res, SKIP_VISIBILITY_XML_ATTRIBUTE) != NULL;
2908 g_free (res);
2909 return found;
2911 return FALSE;
2913 if (*p++ != MONO_DECLSEC_FORMAT_20)
2914 return FALSE;
2916 /* number of encoded permission attributes */
2917 num = mono_metadata_decode_value (p, &p);
2918 for (i = 0; i < num; ++i) {
2919 gboolean is_valid = FALSE;
2920 gboolean abort_decoding = FALSE;
2922 /* attribute name length */
2923 len = mono_metadata_decode_value (p, &p);
2925 /* We don't really need to fully decode the type. Comparing the name is enough */
2926 is_valid = len >= SKIP_VISIBILITY_ATTRIBUTE_SIZE && !memcmp (p, SKIP_VISIBILITY_ATTRIBUTE_NAME, SKIP_VISIBILITY_ATTRIBUTE_SIZE);
2928 p += len;
2930 /*size of the params table*/
2931 params_len = mono_metadata_decode_value (p, &p);
2932 if (is_valid) {
2933 const char *params_end = p + params_len;
2935 /* number of parameters */
2936 len = mono_metadata_decode_value (p, &p);
2938 for (j = 0; j < len; ++j) {
2939 if (mono_assembly_try_decode_skip_verification_param (p, &p, &abort_decoding))
2940 return TRUE;
2941 if (abort_decoding)
2942 break;
2944 p = params_end;
2945 } else {
2946 p += params_len;
2950 return FALSE;
2954 gboolean
2955 mono_assembly_has_skip_verification (MonoAssembly *assembly)
2957 MonoTableInfo *t;
2958 guint32 cols [MONO_DECL_SECURITY_SIZE];
2959 const char *blob;
2960 int i, len;
2962 if (MONO_SECMAN_FLAG_INIT (assembly->skipverification))
2963 return MONO_SECMAN_FLAG_GET_VALUE (assembly->skipverification);
2965 t = &assembly->image->tables [MONO_TABLE_DECLSECURITY];
2967 for (i = 0; i < t->rows; ++i) {
2968 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2969 if ((cols [MONO_DECL_SECURITY_PARENT] & MONO_HAS_DECL_SECURITY_MASK) != MONO_HAS_DECL_SECURITY_ASSEMBLY)
2970 continue;
2971 if (cols [MONO_DECL_SECURITY_ACTION] != SECURITY_ACTION_REQMIN)
2972 continue;
2974 blob = mono_metadata_blob_heap (assembly->image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2975 len = mono_metadata_decode_blob_size (blob, &blob);
2976 if (!len)
2977 continue;
2979 if (mono_assembly_try_decode_skip_verification (blob, blob + len)) {
2980 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, TRUE);
2981 return TRUE;
2985 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, FALSE);
2986 return FALSE;