2 * assembly.c: Routines for loading assemblies.
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 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
19 #include "object-internals.h"
20 #include <mono/metadata/loader.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/class-internals.h>
25 #include <mono/metadata/domain-internals.h>
26 #include <mono/metadata/mono-endian.h>
27 #include <mono/metadata/mono-debug.h>
28 #include <mono/io-layer/io-layer.h>
29 #include <mono/utils/mono-uri.h>
30 #include <mono/metadata/mono-config.h>
31 #include <mono/metadata/mono-config-dirs.h>
32 #include <mono/utils/mono-digest.h>
33 #include <mono/utils/mono-logger-internals.h>
34 #include <mono/utils/mono-path.h>
35 #include <mono/metadata/reflection.h>
36 #include <mono/metadata/coree.h>
37 #include <mono/metadata/cil-coff.h>
38 #include <mono/utils/mono-io-portability.h>
39 #include <mono/utils/atomic.h>
40 #include <mono/utils/mono-os-mutex.h>
43 #include <sys/types.h>
48 #ifdef PLATFORM_MACOSX
49 #include <mach-o/dyld.h>
52 /* AssemblyVersionMap: an assembly name and the assembly version set on which it is based */
54 const char* assembly_name;
55 guint8 version_set_index;
58 /* the default search path is empty, the first slot is replaced with the computed value */
66 /* Contains the list of directories to be searched for assemblies (MONO_PATH) */
67 static char **assemblies_path = NULL;
69 /* Contains the list of directories that point to auxiliary GACs */
70 static char **extra_gac_paths = NULL;
72 #ifndef DISABLE_ASSEMBLY_REMAPPING
73 /* The list of system assemblies what will be remapped to the running
74 * runtime version. WARNING: this list must be sorted.
75 * The integer number is an index in the MonoRuntimeInfo structure, whose
76 * values can be found in domain.c - supported_runtimes. Look there
77 * to understand what remapping will be made.
79 * .NET version can be found at https://github.com/dotnet/coreclr/blob/master/src/inc/fxretarget.h#L99
82 static const AssemblyVersionMap framework_assemblies [] = {
84 {"Commons.Xml.Relaxng", 0},
91 {"Microsoft.Build.Engine", 2},
92 {"Microsoft.Build.Framework", 2},
93 {"Microsoft.VisualBasic", 1},
94 {"Microsoft.VisualC", 1},
96 {"Mono.CompilerServices.SymbolWriter", 0},
98 {"Mono.Data.SybaseClient", 0},
100 {"Mono.Data.TdsClient", 0},
101 {"Mono.GetOptions", 0},
104 {"Mono.Security", 0},
105 {"Mono.Security.Win32", 0},
107 {"Novell.Directory.Ldap", 0},
110 {"System.ComponentModel.Composition", 2},
111 {"System.ComponentModel.DataAnnotations", 2},
112 {"System.Configuration", 0},
113 {"System.Configuration.Install", 0},
116 {"System.Data.Linq", 2},
117 {"System.Data.OracleClient", 0},
118 {"System.Data.Services", 2},
119 {"System.Data.Services.Client", 2},
120 {"System.Data.SqlXml", 0},
121 {"System.Design", 0},
122 {"System.DirectoryServices", 0},
123 {"System.Drawing", 0},
124 {"System.Drawing.Design", 0},
125 {"System.EnterpriseServices", 0},
126 {"System.IdentityModel", 3},
127 {"System.IdentityModel.Selectors", 3},
128 {"System.Management", 0},
129 {"System.Messaging", 0},
131 {"System.Runtime.Remoting", 0},
132 {"System.Runtime.Serialization", 3},
133 {"System.Runtime.Serialization.Formatters.Soap", 0},
134 {"System.Security", 0},
135 {"System.ServiceModel", 3},
136 {"System.ServiceModel.Web", 2},
137 {"System.ServiceProcess", 0},
138 {"System.Transactions", 0},
140 {"System.Web.Abstractions", 2},
141 {"System.Web.DynamicData", 2},
142 {"System.Web.Extensions", 2},
143 {"System.Web.Mobile", 0},
144 {"System.Web.Routing", 2},
145 {"System.Web.Services", 0},
146 {"System.Windows.Forms", 0},
148 {"System.Xml.Linq", 2},
155 * keeps track of loaded assemblies
157 static GList *loaded_assemblies = NULL;
158 static MonoAssembly *corlib;
160 #if defined(__native_client__)
162 /* On Native Client, allow mscorlib to be loaded from memory */
163 /* instead of loaded off disk. If these are not set, default */
164 /* mscorlib loading will take place */
166 /* NOTE: If mscorlib data is passed to mono in this way then */
167 /* it needs to remain allocated during the use of mono. */
169 static void *corlibData = NULL;
170 static size_t corlibSize = 0;
173 mono_set_corlib_data (void *data, size_t size)
181 static char* unquote (const char *str);
183 /* This protects loaded_assemblies and image->references */
184 #define mono_assemblies_lock() mono_os_mutex_lock (&assemblies_mutex)
185 #define mono_assemblies_unlock() mono_os_mutex_unlock (&assemblies_mutex)
186 static mono_mutex_t assemblies_mutex;
188 /* If defined, points to the bundled assembly information */
189 const MonoBundledAssembly **bundles;
191 static mono_mutex_t assembly_binding_mutex;
193 /* Loaded assembly binding info */
194 static GSList *loaded_assembly_bindings = NULL;
196 /* Class lazy loading functions */
197 static GENERATE_TRY_GET_CLASS_WITH_CACHE (internals_visible, System.Runtime.CompilerServices, InternalsVisibleToAttribute)
200 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly, gboolean postload);
202 mono_assembly_load_full_internal (MonoAssemblyName *aname, MonoAssembly *requesting, const char *basedir, MonoImageOpenStatus *status, gboolean refonly);
204 mono_assembly_is_in_gac (const gchar *filanem);
207 encode_public_tok (const guchar *token, gint32 len)
209 const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
213 res = (gchar *)g_malloc (len * 2 + 1);
214 for (i = 0; i < len; i++) {
215 res [i * 2] = allowed [token [i] >> 4];
216 res [i * 2 + 1] = allowed [token [i] & 0xF];
223 * mono_public_tokens_are_equal:
224 * @pubt1: first public key token
225 * @pubt2: second public key token
227 * Compare two public key tokens and return #TRUE is they are equal and #FALSE
231 mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2)
233 return memcmp (pubt1, pubt2, 16) == 0;
237 * mono_set_assemblies_path:
238 * @path: list of paths that contain directories where Mono will look for assemblies
240 * Use this method to override the standard assembly lookup system and
241 * override any assemblies coming from the GAC. This is the method
242 * that supports the MONO_PATH variable.
244 * Notice that MONO_PATH and this method are really a very bad idea as
245 * it prevents the GAC from working and it prevents the standard
246 * resolution mechanisms from working. Nonetheless, for some debugging
247 * situations and bootstrapping setups, this is useful to have.
250 mono_set_assemblies_path (const char* path)
252 char **splitted, **dest;
254 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
256 g_strfreev (assemblies_path);
257 assemblies_path = dest = splitted;
259 char *tmp = *splitted;
261 *dest++ = mono_path_canonicalize (tmp);
267 if (g_getenv ("MONO_DEBUG") == NULL)
270 splitted = assemblies_path;
272 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
273 g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
279 /* Native Client can't get this info from an environment variable so */
280 /* it's passed in to the runtime, or set manually by embedding code. */
281 #ifdef __native_client__
282 char* nacl_mono_path = NULL;
286 check_path_env (void)
289 path = g_getenv ("MONO_PATH");
290 #ifdef __native_client__
292 path = nacl_mono_path;
294 if (!path || assemblies_path != NULL)
297 mono_set_assemblies_path(path);
301 check_extra_gac_path_env (void) {
303 char **splitted, **dest;
305 path = g_getenv ("MONO_GAC_PREFIX");
309 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
311 g_strfreev (extra_gac_paths);
312 extra_gac_paths = dest = splitted;
320 if (g_getenv ("MONO_DEBUG") == NULL)
324 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
325 g_warning ("'%s' in MONO_GAC_PREFIX doesn't exist or has wrong permissions.", *splitted);
332 assembly_binding_maps_name (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
334 if (!info || !info->name)
337 if (strcmp (info->name, aname->name))
340 if (info->major != aname->major || info->minor != aname->minor)
343 if ((info->culture != NULL && info->culture [0]) != (aname->culture != NULL && aname->culture [0]))
346 if (info->culture && aname->culture && strcmp (info->culture, aname->culture))
349 if (!mono_public_tokens_are_equal (info->public_key_token, aname->public_key_token))
356 mono_assembly_binding_info_free (MonoAssemblyBindingInfo *info)
362 g_free (info->culture);
366 get_publisher_policy_info (MonoImage *image, MonoAssemblyName *aname, MonoAssemblyBindingInfo *binding_info)
369 guint32 cols [MONO_MANIFEST_SIZE];
370 const gchar *filename;
371 gchar *subpath, *fullpath;
373 t = &image->tables [MONO_TABLE_MANIFESTRESOURCE];
374 /* MS Impl. accepts policy assemblies with more than
375 * one manifest resource, and only takes the first one */
377 binding_info->is_valid = FALSE;
381 mono_metadata_decode_row (t, 0, cols, MONO_MANIFEST_SIZE);
382 if ((cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) != MONO_IMPLEMENTATION_FILE) {
383 binding_info->is_valid = FALSE;
387 filename = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
388 g_assert (filename != NULL);
390 subpath = g_path_get_dirname (image->name);
391 fullpath = g_build_path (G_DIR_SEPARATOR_S, subpath, filename, NULL);
392 mono_config_parse_publisher_policy (fullpath, binding_info);
396 /* Define the optional elements/attributes before checking */
397 if (!binding_info->culture)
398 binding_info->culture = g_strdup ("");
400 /* Check that the most important elements/attributes exist */
401 if (!binding_info->name || !binding_info->public_key_token [0] || !binding_info->has_old_version_bottom ||
402 !binding_info->has_new_version || !assembly_binding_maps_name (binding_info, aname)) {
403 mono_assembly_binding_info_free (binding_info);
404 binding_info->is_valid = FALSE;
408 binding_info->is_valid = TRUE;
412 compare_versions (AssemblyVersionSet *v, MonoAssemblyName *aname)
414 if (v->major > aname->major)
416 else if (v->major < aname->major)
419 if (v->minor > aname->minor)
421 else if (v->minor < aname->minor)
424 if (v->build > aname->build)
426 else if (v->build < aname->build)
429 if (v->revision > aname->revision)
431 else if (v->revision < aname->revision)
438 check_policy_versions (MonoAssemblyBindingInfo *info, MonoAssemblyName *name)
443 /* If has_old_version_top doesn't exist, we don't have an interval */
444 if (!info->has_old_version_top) {
445 if (compare_versions (&info->old_version_bottom, name) == 0)
451 /* Check that the version defined by name is valid for the interval */
452 if (compare_versions (&info->old_version_top, name) < 0)
455 /* We should be greater or equal than the small version */
456 if (compare_versions (&info->old_version_bottom, name) > 0)
463 * mono_assembly_names_equal:
465 * @r: second assembly.
467 * Compares two MonoAssemblyNames and returns whether they are equal.
469 * This compares the names, the cultures, the release version and their
472 * Returns: TRUE if both assembly names are equal.
475 mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
477 if (!l->name || !r->name)
480 if (strcmp (l->name, r->name))
483 if (l->culture && r->culture && strcmp (l->culture, r->culture))
486 if (l->major != r->major || l->minor != r->minor ||
487 l->build != r->build || l->revision != r->revision)
488 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)))
491 if (!l->public_key_token [0] || !r->public_key_token [0])
494 if (!mono_public_tokens_are_equal (l->public_key_token, r->public_key_token))
500 static MonoAssembly *
501 load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status, MonoBoolean refonly)
505 MonoAssembly *result;
507 for (i = 0; search_path [i]; ++i) {
508 fullpath = g_build_filename (search_path [i], basename, NULL);
509 result = mono_assembly_open_full (fullpath, status, refonly);
518 * mono_assembly_setrootdir:
519 * @root_dir: The pathname of the root directory where we will locate assemblies
521 * This routine sets the internal default root directory for looking up
524 * This is used by Windows installations to compute dynamically the
525 * place where the Mono assemblies are located.
529 mono_assembly_setrootdir (const char *root_dir)
532 * Override the MONO_ASSEMBLIES directory configured at compile time.
534 /* Leak if called more than once */
535 default_path [0] = g_strdup (root_dir);
539 * mono_assembly_getrootdir:
541 * Obtains the root directory used for looking up assemblies.
543 * Returns: a string with the directory, this string should not be freed.
545 G_CONST_RETURN gchar *
546 mono_assembly_getrootdir (void)
548 return default_path [0];
553 * @assembly_dir: the base directory for assemblies
554 * @config_dir: the base directory for configuration files
556 * This routine is used internally and by developers embedding
557 * the runtime into their own applications.
559 * There are a number of cases to consider: Mono as a system-installed
560 * package that is available on the location preconfigured or Mono in
561 * a relocated location.
563 * If you are using a system-installed Mono, you can pass NULL
564 * to both parameters. If you are not, you should compute both
565 * directory values and call this routine.
567 * The values for a given PREFIX are:
569 * assembly_dir: PREFIX/lib
570 * config_dir: PREFIX/etc
572 * Notice that embedders that use Mono in a relocated way must
573 * compute the location at runtime, as they will be in control
574 * of where Mono is installed.
577 mono_set_dirs (const char *assembly_dir, const char *config_dir)
579 if (assembly_dir == NULL)
580 assembly_dir = mono_config_get_assemblies_dir ();
581 if (config_dir == NULL)
582 config_dir = mono_config_get_cfg_dir ();
583 mono_assembly_setrootdir (assembly_dir);
584 mono_set_config_dir (config_dir);
590 compute_base (char *path)
592 char *p = strrchr (path, '/');
596 /* Not a well known Mono executable, we are embedded, cant guess the base */
597 if (strcmp (p, "/mono") && strcmp (p, "/mono-boehm") && strcmp (p, "/mono-sgen") && strcmp (p, "/pedump") && strcmp (p, "/monodis"))
601 p = strrchr (path, '/');
605 if (strcmp (p, "/bin") != 0)
614 mono_set_dirs (mono_config_get_assemblies_dir (), mono_config_get_cfg_dir ());
617 static G_GNUC_UNUSED void
621 char *config, *lib, *mono;
626 * Only /usr prefix is treated specially
628 bindir = mono_config_get_bin_dir ();
630 if (strncmp (exe, bindir, strlen (bindir)) == 0 || (base = compute_base (exe)) == NULL){
635 config = g_build_filename (base, "etc", NULL);
636 lib = g_build_filename (base, "lib", NULL);
637 mono = g_build_filename (lib, "mono/4.5", NULL); // FIXME: stop hardcoding 4.5 here
638 if (stat (mono, &buf) == -1)
641 mono_set_dirs (lib, config);
649 #endif /* HOST_WIN32 */
654 * Registers the root directory for the Mono runtime, for Linux and Solaris 10,
655 * this auto-detects the prefix where Mono was installed.
658 mono_set_rootdir (void)
660 #if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
661 gchar *bindir, *installdir, *root, *name, *resolvedname, *config;
664 name = mono_get_module_file_name ((HMODULE) &__ImageBase);
668 * _NSGetExecutablePath may return -1 to indicate buf is not large
669 * enough, but we ignore that case to avoid having to do extra dynamic
670 * allocation for the path and hope that 4096 is enough - this is
671 * ok in the Linux/Solaris case below at least...
675 guint buf_size = sizeof (buf);
678 if (_NSGetExecutablePath (buf, &buf_size) == 0)
679 name = g_strdup (buf);
688 resolvedname = mono_path_resolve_symlinks (name);
690 bindir = g_path_get_dirname (resolvedname);
691 installdir = g_path_get_dirname (bindir);
692 root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", NULL);
694 config = g_build_filename (root, "..", "etc", NULL);
696 mono_set_dirs (root, config);
698 if (g_file_test (root, G_FILE_TEST_EXISTS) && g_file_test (config, G_FILE_TEST_EXISTS))
699 mono_set_dirs (root, config);
709 g_free (resolvedname);
710 #elif defined(DISABLE_MONO_AUTODETECTION)
718 s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
726 /* Solaris 10 style */
727 str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
728 s = readlink (str, buf, sizeof (buf)-1);
740 * mono_assemblies_init:
742 * Initialize global variables used by this module.
745 mono_assemblies_init (void)
748 * Initialize our internal paths if we have not been initialized yet.
749 * This happens when embedders use Mono.
751 if (mono_assembly_getrootdir () == NULL)
755 check_extra_gac_path_env ();
757 mono_os_mutex_init_recursive (&assemblies_mutex);
758 mono_os_mutex_init (&assembly_binding_mutex);
762 mono_assembly_binding_lock (void)
764 mono_locks_os_acquire (&assembly_binding_mutex, AssemblyBindingLock);
768 mono_assembly_binding_unlock (void)
770 mono_locks_os_release (&assembly_binding_mutex, AssemblyBindingLock);
774 mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
776 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
777 guint32 cols [MONO_ASSEMBLY_SIZE];
778 gint32 machine, flags;
783 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
786 aname->hash_value = NULL;
787 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
788 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
789 aname->flags = cols [MONO_ASSEMBLY_FLAGS];
790 aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
791 aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
792 aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
793 aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
794 aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
795 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
796 guchar* token = (guchar *)g_malloc (8);
801 pkey = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
802 len = mono_metadata_decode_blob_size (pkey, &pkey);
803 aname->public_key = (guchar*)pkey;
805 mono_digest_get_public_token (token, aname->public_key, len);
806 encoded = encode_public_tok (token, 8);
807 g_strlcpy ((char*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
813 aname->public_key = NULL;
814 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
817 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
818 aname->public_key = (guchar*)mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
821 aname->public_key = 0;
823 machine = ((MonoCLIImageInfo*)(image->image_info))->cli_header.coff.coff_machine;
824 flags = ((MonoCLIImageInfo*)(image->image_info))->cli_cli_header.ch_flags;
826 case COFF_MACHINE_I386:
827 /* https://bugzilla.xamarin.com/show_bug.cgi?id=17632 */
828 if (flags & (CLI_FLAGS_32BITREQUIRED|CLI_FLAGS_PREFERRED32BIT))
829 aname->arch = MONO_PROCESSOR_ARCHITECTURE_X86;
830 else if ((flags & 0x70) == 0x70)
831 aname->arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
833 aname->arch = MONO_PROCESSOR_ARCHITECTURE_MSIL;
835 case COFF_MACHINE_IA64:
836 aname->arch = MONO_PROCESSOR_ARCHITECTURE_IA64;
838 case COFF_MACHINE_AMD64:
839 aname->arch = MONO_PROCESSOR_ARCHITECTURE_AMD64;
841 case COFF_MACHINE_ARM:
842 aname->arch = MONO_PROCESSOR_ARCHITECTURE_ARM;
852 * mono_stringify_assembly_name:
853 * @aname: the assembly name.
855 * Convert @aname into its string format. The returned string is dynamically
856 * allocated and should be freed by the caller.
858 * Returns: a newly allocated string with a string representation of
862 mono_stringify_assembly_name (MonoAssemblyName *aname)
864 const char *quote = (aname->name && g_ascii_isspace (aname->name [0])) ? "\"" : "";
866 return g_strdup_printf (
867 "%s%s%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
868 quote, aname->name, quote,
869 aname->major, aname->minor, aname->build, aname->revision,
870 aname->culture && *aname->culture? aname->culture: "neutral",
871 aname->public_key_token [0] ? (char *)aname->public_key_token : "null",
872 (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
876 assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
878 const gchar *public_tok;
881 public_tok = mono_metadata_blob_heap (image, key_index);
882 len = mono_metadata_decode_blob_size (public_tok, &public_tok);
884 if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
886 mono_digest_get_public_token (token, (guchar*)public_tok, len);
887 return encode_public_tok (token, 8);
890 return encode_public_tok ((guchar*)public_tok, len);
894 * mono_assembly_addref:
895 * @assemnly: the assembly to reference
897 * This routine increments the reference count on a MonoAssembly.
898 * The reference count is reduced every time the method mono_assembly_close() is
902 mono_assembly_addref (MonoAssembly *assembly)
904 InterlockedIncrement (&assembly->ref_count);
908 * CAUTION: This table must be kept in sync with
909 * ivkm/reflect/Fusion.cs
912 #define SILVERLIGHT_KEY "7cec85d7bea7798e"
913 #define WINFX_KEY "31bf3856ad364e35"
914 #define ECMA_KEY "b77a5c561934e089"
915 #define MSFINAL_KEY "b03f5f7f11d50a3a"
923 static KeyRemapEntry key_remap_table[] = {
924 { "Microsoft.CSharp", WINFX_KEY, MSFINAL_KEY },
925 { "System", SILVERLIGHT_KEY, ECMA_KEY },
926 { "System.ComponentModel.Composition", WINFX_KEY, ECMA_KEY },
927 { "System.ComponentModel.DataAnnotations", "ddd0da4d3e678217", WINFX_KEY },
928 { "System.Core", SILVERLIGHT_KEY, ECMA_KEY },
929 // FIXME: MS uses MSFINAL_KEY for .NET 4.5
930 { "System.Net", SILVERLIGHT_KEY, MSFINAL_KEY },
931 { "System.Numerics", WINFX_KEY, ECMA_KEY },
932 { "System.Runtime.Serialization", SILVERLIGHT_KEY, ECMA_KEY },
933 { "System.ServiceModel", WINFX_KEY, ECMA_KEY },
934 { "System.ServiceModel.Web", SILVERLIGHT_KEY, WINFX_KEY },
935 { "System.Windows", SILVERLIGHT_KEY, MSFINAL_KEY },
936 { "System.Xml", SILVERLIGHT_KEY, ECMA_KEY },
937 { "System.Xml.Linq", WINFX_KEY, ECMA_KEY },
938 { "System.Xml.Serialization", WINFX_KEY, ECMA_KEY }
942 remap_keys (MonoAssemblyName *aname)
945 for (i = 0; i < G_N_ELEMENTS (key_remap_table); i++) {
946 const KeyRemapEntry *entry = &key_remap_table [i];
948 if (strcmp (aname->name, entry->name) ||
949 !mono_public_tokens_are_equal (aname->public_key_token, (const unsigned char*) entry->from))
952 memcpy (aname->public_key_token, entry->to, MONO_PUBLIC_KEY_TOKEN_LENGTH);
954 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
955 "Remapped public key token of retargetable assembly %s from %s to %s",
956 aname->name, entry->from, entry->to);
961 static MonoAssemblyName *
962 mono_assembly_remap_version (MonoAssemblyName *aname, MonoAssemblyName *dest_aname)
964 const MonoRuntimeInfo *current_runtime;
965 int pos, first, last;
967 if (aname->name == NULL) return aname;
969 current_runtime = mono_get_runtime_info ();
971 if (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) {
972 const AssemblyVersionSet* vset;
974 /* Remap to current runtime */
975 vset = ¤t_runtime->version_sets [0];
977 memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
978 dest_aname->major = vset->major;
979 dest_aname->minor = vset->minor;
980 dest_aname->build = vset->build;
981 dest_aname->revision = vset->revision;
982 dest_aname->flags &= ~ASSEMBLYREF_RETARGETABLE_FLAG;
984 /* Remap assembly name */
985 if (!strcmp (aname->name, "System.Net"))
986 dest_aname->name = g_strdup ("System");
988 remap_keys (dest_aname);
990 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
991 "The request to load the retargetable assembly %s v%d.%d.%d.%d was remapped to %s v%d.%d.%d.%d",
993 aname->major, aname->minor, aname->build, aname->revision,
995 vset->major, vset->minor, vset->build, vset->revision
1001 #ifndef DISABLE_ASSEMBLY_REMAPPING
1003 last = G_N_ELEMENTS (framework_assemblies) - 1;
1005 while (first <= last) {
1007 pos = first + (last - first) / 2;
1008 res = strcmp (aname->name, framework_assemblies[pos].assembly_name);
1010 const AssemblyVersionSet* vset;
1011 int index = framework_assemblies[pos].version_set_index;
1012 g_assert (index < G_N_ELEMENTS (current_runtime->version_sets));
1013 vset = ¤t_runtime->version_sets [index];
1015 if (aname->major == vset->major && aname->minor == vset->minor &&
1016 aname->build == vset->build && aname->revision == vset->revision)
1019 if ((aname->major | aname->minor | aname->build | aname->revision) != 0)
1020 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
1021 "The request to load the assembly %s v%d.%d.%d.%d was remapped to v%d.%d.%d.%d",
1023 aname->major, aname->minor, aname->build, aname->revision,
1024 vset->major, vset->minor, vset->build, vset->revision
1027 memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
1028 dest_aname->major = vset->major;
1029 dest_aname->minor = vset->minor;
1030 dest_aname->build = vset->build;
1031 dest_aname->revision = vset->revision;
1033 } else if (res < 0) {
1045 * mono_assembly_get_assemblyref:
1046 * @image: pointer to the MonoImage to extract the information from.
1047 * @index: index to the assembly reference in the image.
1048 * @aname: pointer to a `MonoAssemblyName` that will hold the returned value.
1050 * Fills out the @aname with the assembly name of the @index assembly reference in @image.
1053 mono_assembly_get_assemblyref (MonoImage *image, int index, MonoAssemblyName *aname)
1056 guint32 cols [MONO_ASSEMBLYREF_SIZE];
1059 t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1061 mono_metadata_decode_row (t, index, cols, MONO_ASSEMBLYREF_SIZE);
1063 hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
1064 aname->hash_len = mono_metadata_decode_blob_size (hash, &hash);
1065 aname->hash_value = hash;
1066 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
1067 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
1068 aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
1069 aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
1070 aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
1071 aname->build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
1072 aname->revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
1074 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
1075 gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname->flags);
1076 g_strlcpy ((char*)aname->public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1079 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1084 mono_assembly_load_reference (MonoImage *image, int index)
1086 MonoAssembly *reference;
1087 MonoAssemblyName aname;
1088 MonoImageOpenStatus status;
1091 * image->references is shared between threads, so we need to access
1092 * it inside a critical section.
1094 mono_assemblies_lock ();
1095 if (!image->references) {
1096 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1098 image->references = g_new0 (MonoAssembly *, t->rows + 1);
1099 image->nreferences = t->rows;
1101 reference = image->references [index];
1102 mono_assemblies_unlock ();
1106 mono_assembly_get_assemblyref (image, index, &aname);
1108 if (image->assembly && image->assembly->ref_only) {
1109 /* We use the loaded corlib */
1110 if (!strcmp (aname.name, "mscorlib"))
1111 reference = mono_assembly_load_full_internal (&aname, image->assembly, image->assembly->basedir, &status, FALSE);
1113 reference = mono_assembly_loaded_full (&aname, TRUE);
1115 /* Try a postload search hook */
1116 reference = mono_assembly_invoke_search_hook_internal (&aname, image->assembly, TRUE, TRUE);
1120 * Here we must advice that the error was due to
1121 * a non loaded reference using the ReflectionOnly api
1124 reference = (MonoAssembly *)REFERENCE_MISSING;
1126 /* we first try without setting the basedir: this can eventually result in a ResolveAssembly
1127 * event which is the MS .net compatible behaviour (the assemblyresolve_event3.cs test has been fixed
1128 * accordingly, it would fail on the MS runtime before).
1129 * The second load attempt has the basedir set to keep compatibility with the old mono behavior, for
1130 * example bug-349190.2.cs and who knows how much more code in the wild.
1132 reference = mono_assembly_load_full_internal (&aname, image->assembly, NULL, &status, FALSE);
1133 if (!reference && image->assembly)
1134 reference = mono_assembly_load_full_internal (&aname, image->assembly, image->assembly->basedir, &status, FALSE);
1137 if (reference == NULL){
1140 if (status == MONO_IMAGE_ERROR_ERRNO && errno == ENOENT) {
1141 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 : "" );
1142 } else if (status == MONO_IMAGE_ERROR_ERRNO) {
1143 extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
1144 } else if (status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
1145 extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
1146 } else if (status == MONO_IMAGE_IMAGE_INVALID) {
1147 extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
1149 extra_msg = g_strdup ("");
1152 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "The following assembly referenced from %s could not be loaded:\n"
1153 " Assembly: %s (assemblyref_index=%d)\n"
1154 " Version: %d.%d.%d.%d\n"
1155 " Public Key: %s\n%s",
1156 image->name, aname.name, index,
1157 aname.major, aname.minor, aname.build, aname.revision,
1158 strlen ((char*)aname.public_key_token) == 0 ? "(none)" : (char*)aname.public_key_token, extra_msg);
1162 mono_assemblies_lock ();
1163 if (reference == NULL) {
1164 /* Flag as not found */
1165 reference = (MonoAssembly *)REFERENCE_MISSING;
1168 if (!image->references [index]) {
1169 if (reference != REFERENCE_MISSING){
1170 mono_assembly_addref (reference);
1171 if (image->assembly)
1172 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Ref addref %s[%p] -> %s[%p]: %d",
1173 image->assembly->aname.name, image->assembly, reference->aname.name, reference, reference->ref_count);
1175 if (image->assembly)
1176 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Failed to load assembly %s[%p]\n",
1177 image->assembly->aname.name, image->assembly);
1180 image->references [index] = reference;
1182 mono_assemblies_unlock ();
1184 if (image->references [index] != reference) {
1185 /* Somebody loaded it before us */
1186 mono_assembly_close (reference);
1191 * mono_assembly_load_references:
1194 * @deprecated: There is no reason to use this method anymore, it does nothing
1196 * This method is now a no-op, it does nothing other than setting the @status to #MONO_IMAGE_OK
1199 mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
1201 /* This is a no-op now but it is part of the embedding API so we can't remove it */
1202 *status = MONO_IMAGE_OK;
1205 typedef struct AssemblyLoadHook AssemblyLoadHook;
1206 struct AssemblyLoadHook {
1207 AssemblyLoadHook *next;
1208 MonoAssemblyLoadFunc func;
1212 AssemblyLoadHook *assembly_load_hook = NULL;
1215 mono_assembly_invoke_load_hook (MonoAssembly *ass)
1217 AssemblyLoadHook *hook;
1219 for (hook = assembly_load_hook; hook; hook = hook->next) {
1220 hook->func (ass, hook->user_data);
1225 mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
1227 AssemblyLoadHook *hook;
1229 g_return_if_fail (func != NULL);
1231 hook = g_new0 (AssemblyLoadHook, 1);
1233 hook->user_data = user_data;
1234 hook->next = assembly_load_hook;
1235 assembly_load_hook = hook;
1239 free_assembly_load_hooks (void)
1241 AssemblyLoadHook *hook, *next;
1243 for (hook = assembly_load_hook; hook; hook = next) {
1249 typedef struct AssemblySearchHook AssemblySearchHook;
1250 struct AssemblySearchHook {
1251 AssemblySearchHook *next;
1252 MonoAssemblySearchFunc func;
1258 AssemblySearchHook *assembly_search_hook = NULL;
1260 static MonoAssembly*
1261 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly, gboolean postload)
1263 AssemblySearchHook *hook;
1265 for (hook = assembly_search_hook; hook; hook = hook->next) {
1266 if ((hook->refonly == refonly) && (hook->postload == postload)) {
1269 * A little explanation is in order here.
1271 * The default postload search hook needs to know the requesting assembly to report it to managed code.
1272 * The embedding API exposes a search hook that doesn't take such argument.
1274 * The original fix would call the default search hook before all the registered ones and pass
1275 * the requesting assembly to it. It works but broke a very suddle embedding API aspect that some users
1276 * rely on. Which is the ordering between user hooks and the default runtime hook.
1278 * Registering the hook after mono_jit_init would let your hook run before the default one and
1279 * when using it to handle non standard app layouts this could save your app from a massive amount
1280 * of syscalls that the default hook does when probing all sorts of places. Slow targets with horrible IO
1281 * are all using this trick and if we broke this assumption they would be very disapointed at us.
1283 * So what's the fix? We register the default hook using regular means and special case it when iterating
1284 * over the registered hooks. This preserves ordering and enables managed resolve hooks to get the requesting
1287 if (hook->func == (void*)mono_domain_assembly_postload_search)
1288 ass = mono_domain_assembly_postload_search (aname, requesting, refonly);
1290 ass = hook->func (aname, hook->user_data);
1300 mono_assembly_invoke_search_hook (MonoAssemblyName *aname)
1302 return mono_assembly_invoke_search_hook_internal (aname, NULL, FALSE, FALSE);
1306 mono_install_assembly_search_hook_internal (MonoAssemblySearchFunc func, gpointer user_data, gboolean refonly, gboolean postload)
1308 AssemblySearchHook *hook;
1310 g_return_if_fail (func != NULL);
1312 hook = g_new0 (AssemblySearchHook, 1);
1314 hook->user_data = user_data;
1315 hook->refonly = refonly;
1316 hook->postload = postload;
1317 hook->next = assembly_search_hook;
1318 assembly_search_hook = hook;
1322 mono_install_assembly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1324 mono_install_assembly_search_hook_internal (func, user_data, FALSE, FALSE);
1328 free_assembly_search_hooks (void)
1330 AssemblySearchHook *hook, *next;
1332 for (hook = assembly_search_hook; hook; hook = next) {
1339 mono_install_assembly_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1341 mono_install_assembly_search_hook_internal (func, user_data, TRUE, FALSE);
1345 mono_install_assembly_postload_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1347 mono_install_assembly_search_hook_internal (func, user_data, FALSE, TRUE);
1351 mono_install_assembly_postload_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1353 mono_install_assembly_search_hook_internal (func, user_data, TRUE, TRUE);
1356 typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
1357 struct AssemblyPreLoadHook {
1358 AssemblyPreLoadHook *next;
1359 MonoAssemblyPreLoadFunc func;
1363 static AssemblyPreLoadHook *assembly_preload_hook = NULL;
1364 static AssemblyPreLoadHook *assembly_refonly_preload_hook = NULL;
1366 static MonoAssembly *
1367 invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1369 AssemblyPreLoadHook *hook;
1370 MonoAssembly *assembly;
1372 for (hook = assembly_preload_hook; hook; hook = hook->next) {
1373 assembly = hook->func (aname, assemblies_path, hook->user_data);
1374 if (assembly != NULL)
1381 static MonoAssembly *
1382 invoke_assembly_refonly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1384 AssemblyPreLoadHook *hook;
1385 MonoAssembly *assembly;
1387 for (hook = assembly_refonly_preload_hook; hook; hook = hook->next) {
1388 assembly = hook->func (aname, assemblies_path, hook->user_data);
1389 if (assembly != NULL)
1397 mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1399 AssemblyPreLoadHook *hook;
1401 g_return_if_fail (func != NULL);
1403 hook = g_new0 (AssemblyPreLoadHook, 1);
1405 hook->user_data = user_data;
1406 hook->next = assembly_preload_hook;
1407 assembly_preload_hook = hook;
1411 mono_install_assembly_refonly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1413 AssemblyPreLoadHook *hook;
1415 g_return_if_fail (func != NULL);
1417 hook = g_new0 (AssemblyPreLoadHook, 1);
1419 hook->user_data = user_data;
1420 hook->next = assembly_refonly_preload_hook;
1421 assembly_refonly_preload_hook = hook;
1425 free_assembly_preload_hooks (void)
1427 AssemblyPreLoadHook *hook, *next;
1429 for (hook = assembly_preload_hook; hook; hook = next) {
1434 for (hook = assembly_refonly_preload_hook; hook; hook = next) {
1441 absolute_dir (const gchar *filename)
1452 if (g_path_is_absolute (filename)) {
1453 part = g_path_get_dirname (filename);
1454 res = g_strconcat (part, G_DIR_SEPARATOR_S, NULL);
1459 cwd = g_get_current_dir ();
1460 mixed = g_build_filename (cwd, filename, NULL);
1461 parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
1466 for (i = 0; (part = parts [i]) != NULL; i++) {
1467 if (!strcmp (part, "."))
1470 if (!strcmp (part, "..")) {
1471 if (list && list->next) /* Don't remove root */
1472 list = g_list_delete_link (list, list);
1474 list = g_list_prepend (list, part);
1478 result = g_string_new ("");
1479 list = g_list_reverse (list);
1481 /* Ignores last data pointer, which should be the filename */
1482 for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next){
1484 g_string_append_printf (result, "%s%c", (char *) tmp->data,
1489 g_string_free (result, FALSE);
1494 return g_strdup (".");
1501 * mono_assembly_open_from_bundle:
1502 * @filename: Filename requested
1503 * @status: return status code
1505 * This routine tries to open the assembly specified by `filename' from the
1506 * defined bundles, if found, returns the MonoImage for it, if not found
1510 mono_assembly_open_from_bundle (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1514 MonoImage *image = NULL;
1517 * we do a very simple search for bundled assemblies: it's not a general
1518 * purpose assembly loading mechanism.
1524 name = g_path_get_basename (filename);
1526 mono_assemblies_lock ();
1527 for (i = 0; !image && bundles [i]; ++i) {
1528 if (strcmp (bundles [i]->name, name) == 0) {
1529 image = mono_image_open_from_data_with_name ((char*)bundles [i]->data, bundles [i]->size, FALSE, status, refonly, name);
1533 mono_assemblies_unlock ();
1535 mono_image_addref (image);
1536 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Loader loaded assembly from bundle: '%s'.", name);
1545 * mono_assemblies_open_full:
1546 * @filename: the file to load
1547 * @status: return status code
1548 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
1550 * This loads an assembly from the specified @filename. The @filename allows
1551 * a local URL (starting with a file:// prefix). If a file prefix is used, the
1552 * filename is interpreted as a URL, and the filename is URL-decoded. Otherwise the file
1553 * is treated as a local path.
1555 * First, an attempt is made to load the assembly from the bundled executable (for those
1556 * deployments that have been done with the `mkbundle` tool or for scenarios where the
1557 * assembly has been registered as an embedded assembly). If this is not the case, then
1558 * the assembly is loaded from disk using `api:mono_image_open_full`.
1560 * If the pointed assembly does not live in the Global Assembly Cache, a shadow copy of
1561 * the assembly is made.
1563 * If @refonly is set to true, then the assembly is loaded purely for inspection with
1564 * the `System.Reflection` API.
1566 * Returns: NULL on error, with the @status set to an error code, or a pointer
1570 mono_assembly_open_full (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1574 MonoImageOpenStatus def_status;
1577 gboolean loaded_from_bundle;
1579 g_return_val_if_fail (filename != NULL, NULL);
1582 status = &def_status;
1583 *status = MONO_IMAGE_OK;
1585 if (strncmp (filename, "file://", 7) == 0) {
1586 GError *error = NULL;
1587 gchar *uri = (gchar *) filename;
1591 * MS allows file://c:/... and fails on file://localhost/c:/...
1592 * They also throw an IndexOutOfRangeException if "file://"
1595 uri = g_strdup_printf ("file:///%s", uri + 7);
1598 uri = mono_escape_uri_string (tmpuri);
1599 fname = g_filename_from_uri (uri, NULL, &error);
1602 if (tmpuri != filename)
1605 if (error != NULL) {
1606 g_warning ("%s\n", error->message);
1607 g_error_free (error);
1608 fname = g_strdup (filename);
1611 fname = g_strdup (filename);
1614 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1615 "Assembly Loader probing location: '%s'.", fname);
1618 if (!mono_assembly_is_in_gac (fname)) {
1620 new_fname = mono_make_shadow_copy (fname, &error);
1621 mono_error_raise_exception (&error); /* FIXME don't raise here */
1623 if (new_fname && new_fname != fname) {
1626 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1627 "Assembly Loader shadow-copied assembly to: '%s'.", fname);
1632 // If VM built with mkbundle
1633 loaded_from_bundle = FALSE;
1634 if (bundles != NULL) {
1635 image = mono_assembly_open_from_bundle (fname, status, refonly);
1636 loaded_from_bundle = image != NULL;
1640 image = mono_image_open_full (fname, status, refonly);
1643 if (*status == MONO_IMAGE_OK)
1644 *status = MONO_IMAGE_ERROR_ERRNO;
1649 if (image->assembly) {
1650 /* Already loaded by another appdomain */
1651 mono_assembly_invoke_load_hook (image->assembly);
1652 mono_image_close (image);
1654 return image->assembly;
1657 ass = mono_assembly_load_from_full (image, fname, status, refonly);
1660 if (!loaded_from_bundle)
1661 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1662 "Assembly Loader loaded assembly from location: '%s'.", filename);
1664 mono_config_for_assembly (ass->image);
1667 /* Clear the reference added by mono_image_open */
1668 mono_image_close (image);
1676 free_item (gpointer val, gpointer user_data)
1682 * mono_assembly_load_friends:
1685 * Load the list of friend assemblies that are allowed to access
1686 * the assembly's internal types and members. They are stored as assembly
1687 * names in custom attributes.
1689 * This is an internal method, we need this because when we load mscorlib
1690 * we do not have the internals visible cattr loaded yet,
1691 * so we need to load these after we initialize the runtime.
1693 * LOCKING: Acquires the assemblies lock plus the loader lock.
1696 mono_assembly_load_friends (MonoAssembly* ass)
1699 MonoCustomAttrInfo* attrs;
1702 if (ass->friend_assembly_names_inited)
1705 attrs = mono_custom_attrs_from_assembly (ass);
1707 mono_assemblies_lock ();
1708 ass->friend_assembly_names_inited = TRUE;
1709 mono_assemblies_unlock ();
1713 mono_assemblies_lock ();
1714 if (ass->friend_assembly_names_inited) {
1715 mono_assemblies_unlock ();
1718 mono_assemblies_unlock ();
1722 * We build the list outside the assemblies lock, the worse that can happen
1723 * is that we'll need to free the allocated list.
1725 for (i = 0; i < attrs->num_attrs; ++i) {
1726 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1727 MonoAssemblyName *aname;
1729 /* Do some sanity checking */
1730 if (!attr->ctor || attr->ctor->klass != mono_class_try_get_internals_visible_class ())
1732 if (attr->data_size < 4)
1734 data = (const char*)attr->data;
1735 /* 0xFF means null string, see custom attr format */
1736 if (data [0] != 1 || data [1] != 0 || (data [2] & 0xFF) == 0xFF)
1738 mono_metadata_decode_value (data + 2, &data);
1739 aname = g_new0 (MonoAssemblyName, 1);
1740 /*g_print ("friend ass: %s\n", data);*/
1741 if (mono_assembly_name_parse_full (data, aname, TRUE, NULL, NULL)) {
1742 list = g_slist_prepend (list, aname);
1747 mono_custom_attrs_free (attrs);
1749 mono_assemblies_lock ();
1750 if (ass->friend_assembly_names_inited) {
1751 mono_assemblies_unlock ();
1752 g_slist_foreach (list, free_item, NULL);
1753 g_slist_free (list);
1756 ass->friend_assembly_names = list;
1758 /* Because of the double checked locking pattern above */
1759 mono_memory_barrier ();
1760 ass->friend_assembly_names_inited = TRUE;
1761 mono_assemblies_unlock ();
1765 * mono_assembly_open:
1766 * @filename: Opens the assembly pointed out by this name
1767 * @status: return status code
1769 * This loads an assembly from the specified @filename. The @filename allows
1770 * a local URL (starting with a file:// prefix). If a file prefix is used, the
1771 * filename is interpreted as a URL, and the filename is URL-decoded. Otherwise the file
1772 * is treated as a local path.
1774 * First, an attempt is made to load the assembly from the bundled executable (for those
1775 * deployments that have been done with the `mkbundle` tool or for scenarios where the
1776 * assembly has been registered as an embedded assembly). If this is not the case, then
1777 * the assembly is loaded from disk using `api:mono_image_open_full`.
1779 * If the pointed assembly does not live in the Global Assembly Cache, a shadow copy of
1780 * the assembly is made.
1782 * Return: a pointer to the MonoAssembly if @filename contains a valid
1783 * assembly or NULL on error. Details about the error are stored in the
1787 mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
1789 return mono_assembly_open_full (filename, status, FALSE);
1793 * mono_assembly_load_from_full:
1794 * @image: Image to load the assembly from
1795 * @fname: assembly name to associate with the assembly
1796 * @status: returns the status condition
1797 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
1799 * If the provided @image has an assembly reference, it will process the given
1800 * image as an assembly with the given name.
1802 * Most likely you want to use the `api:mono_assembly_load_full` method instead.
1804 * Returns: A valid pointer to a `MonoAssembly*` on success and the @status will be
1805 * set to #MONO_IMAGE_OK; or NULL on error.
1807 * If there is an error loading the assembly the @status will indicate the
1808 * reason with @status being set to `MONO_IMAGE_INVALID` if the
1809 * image did not contain an assembly reference table.
1812 mono_assembly_load_from_full (MonoImage *image, const char*fname,
1813 MonoImageOpenStatus *status, gboolean refonly)
1815 MonoAssembly *ass, *ass2;
1818 if (!image->tables [MONO_TABLE_ASSEMBLY].rows) {
1819 /* 'image' doesn't have a manifest -- maybe someone is trying to Assembly.Load a .netmodule */
1820 *status = MONO_IMAGE_IMAGE_INVALID;
1824 #if defined (HOST_WIN32)
1829 tmp_fn = g_strdup (fname);
1830 for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
1831 if (tmp_fn [i] == '/')
1835 base_dir = absolute_dir (tmp_fn);
1839 base_dir = absolute_dir (fname);
1843 * Create assembly struct, and enter it into the assembly cache
1845 ass = g_new0 (MonoAssembly, 1);
1846 ass->basedir = base_dir;
1847 ass->ref_only = refonly;
1850 mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
1852 mono_assembly_fill_assembly_name (image, &ass->aname);
1854 if (mono_defaults.corlib && strcmp (ass->aname.name, "mscorlib") == 0) {
1855 // MS.NET doesn't support loading other mscorlibs
1858 mono_image_addref (mono_defaults.corlib);
1859 *status = MONO_IMAGE_OK;
1860 return mono_defaults.corlib->assembly;
1863 /* Add a non-temporary reference because of ass->image */
1864 mono_image_addref (image);
1866 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Image addref %s[%p] -> %s[%p]: %d", ass->aname.name, ass, image->name, image, image->ref_count);
1869 * The load hooks might take locks so we can't call them while holding the
1872 if (ass->aname.name) {
1873 ass2 = mono_assembly_invoke_search_hook_internal (&ass->aname, NULL, refonly, FALSE);
1877 mono_image_close (image);
1878 *status = MONO_IMAGE_OK;
1883 mono_assemblies_lock ();
1885 if (image->assembly) {
1887 * This means another thread has already loaded the assembly, but not yet
1888 * called the load hooks so the search hook can't find the assembly.
1890 mono_assemblies_unlock ();
1891 ass2 = image->assembly;
1894 mono_image_close (image);
1895 *status = MONO_IMAGE_OK;
1899 image->assembly = ass;
1901 loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
1902 mono_assemblies_unlock ();
1905 if (image->is_module_handle)
1906 mono_image_fixup_vtable (image);
1909 mono_assembly_invoke_load_hook (ass);
1911 mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
1917 * mono_assembly_load_from:
1918 * @image: Image to load the assembly from
1919 * @fname: assembly name to associate with the assembly
1920 * @status: return status code
1922 * If the provided @image has an assembly reference, it will process the given
1923 * image as an assembly with the given name.
1925 * Most likely you want to use the `api:mono_assembly_load_full` method instead.
1927 * This is equivalent to calling `api:mono_assembly_load_from_full` with the
1928 * @refonly parameter set to FALSE.
1929 * Returns: A valid pointer to a `MonoAssembly*` on success and the @status will be
1930 * set to #MONO_IMAGE_OK; or NULL on error.
1932 * If there is an error loading the assembly the @status will indicate the
1933 * reason with @status being set to `MONO_IMAGE_INVALID` if the
1934 * image did not contain an assembly reference table.
1938 mono_assembly_load_from (MonoImage *image, const char *fname,
1939 MonoImageOpenStatus *status)
1941 return mono_assembly_load_from_full (image, fname, status, FALSE);
1945 * mono_assembly_name_free:
1946 * @aname: assembly name to free
1948 * Frees the provided assembly name object.
1949 * (it does not frees the object itself, only the name members).
1952 mono_assembly_name_free (MonoAssemblyName *aname)
1957 g_free ((void *) aname->name);
1958 g_free ((void *) aname->culture);
1959 g_free ((void *) aname->hash_value);
1963 parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
1966 gchar header [16], val, *arr;
1967 gint i, j, offset, bitlen, keylen, pkeylen;
1969 keylen = strlen (key) >> 1;
1973 /* allow the ECMA standard key */
1974 if (strcmp (key, "00000000000000000400000000000000") == 0) {
1976 *pubkey = g_strdup (key);
1982 val = g_ascii_xdigit_value (key [0]) << 4;
1983 val |= g_ascii_xdigit_value (key [1]);
1988 val = g_ascii_xdigit_value (key [24]);
1989 val |= g_ascii_xdigit_value (key [25]);
2001 /* We need the first 16 bytes
2002 * to check whether this key is valid or not */
2003 pkeylen = strlen (pkey) >> 1;
2007 for (i = 0, j = 0; i < 16; i++) {
2008 header [i] = g_ascii_xdigit_value (pkey [j++]) << 4;
2009 header [i] |= g_ascii_xdigit_value (pkey [j++]);
2012 if (header [0] != 0x06 || /* PUBLICKEYBLOB (0x06) */
2013 header [1] != 0x02 || /* Version (0x02) */
2014 header [2] != 0x00 || /* Reserved (word) */
2015 header [3] != 0x00 ||
2016 (guint)(read32 (header + 8)) != 0x31415352) /* DWORD magic = RSA1 */
2019 /* Based on this length, we _should_ be able to know if the length is right */
2020 bitlen = read32 (header + 12) >> 3;
2021 if ((bitlen + 16 + 4) != pkeylen)
2024 /* parsing is OK and the public key itself is not requested back */
2028 /* Encode the size of the blob */
2030 if (keylen <= 127) {
2031 arr = (gchar *)g_malloc (keylen + 1);
2032 arr [offset++] = keylen;
2034 arr = (gchar *)g_malloc (keylen + 2);
2035 arr [offset++] = 0x80; /* 10bs */
2036 arr [offset++] = keylen;
2039 for (i = offset, j = 0; i < keylen + offset; i++) {
2040 arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
2041 arr [i] |= g_ascii_xdigit_value (key [j++]);
2050 build_assembly_name (const char *name, const char *version, const char *culture, const char *token, const char *key, guint32 flags, guint32 arch, MonoAssemblyName *aname, gboolean save_public_key)
2052 gint major, minor, build, revision;
2055 gchar *pkey, *pkeyptr, *encoded, tok [8];
2057 memset (aname, 0, sizeof (MonoAssemblyName));
2060 version_parts = sscanf (version, "%u.%u.%u.%u", &major, &minor, &build, &revision);
2061 if (version_parts < 2 || version_parts > 4)
2064 /* FIXME: we should set build & revision to -1 (instead of 0)
2065 if these are not set in the version string. That way, later on,
2066 we can still determine if these were specified. */
2067 aname->major = major;
2068 aname->minor = minor;
2069 if (version_parts >= 3)
2070 aname->build = build;
2073 if (version_parts == 4)
2074 aname->revision = revision;
2076 aname->revision = 0;
2079 aname->flags = flags;
2081 aname->name = g_strdup (name);
2084 if (g_ascii_strcasecmp (culture, "neutral") == 0)
2085 aname->culture = g_strdup ("");
2087 aname->culture = g_strdup (culture);
2090 if (token && strncmp (token, "null", 4) != 0) {
2093 /* the constant includes the ending NULL, hence the -1 */
2094 if (strlen (token) != (MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)) {
2095 mono_assembly_name_free (aname);
2098 lower = g_ascii_strdown (token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2099 g_strlcpy ((char*)aname->public_key_token, lower, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2105 if (strcmp (key, "null") == 0 || !parse_public_key (key, &pkey, &is_ecma)) {
2106 mono_assembly_name_free (aname);
2111 if (save_public_key)
2112 aname->public_key = (guint8*)pkey;
2115 g_strlcpy ((gchar*)aname->public_key_token, "b77a5c561934e089", MONO_PUBLIC_KEY_TOKEN_LENGTH);
2119 len = mono_metadata_decode_blob_size ((const gchar *) pkey, (const gchar **) &pkeyptr);
2120 // We also need to generate the key token
2121 mono_digest_get_public_token ((guchar*) tok, (guint8*) pkeyptr, len);
2122 encoded = encode_public_tok ((guchar*) tok, 8);
2123 g_strlcpy ((gchar*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2126 if (save_public_key)
2127 aname->public_key = (guint8*) pkey;
2136 parse_assembly_directory_name (const char *name, const char *dirname, MonoAssemblyName *aname)
2141 parts = g_strsplit (dirname, "_", 3);
2142 if (!parts || !parts[0] || !parts[1] || !parts[2]) {
2147 res = build_assembly_name (name, parts[0], parts[1], parts[2], NULL, 0, 0, aname, FALSE);
2153 split_key_value (const gchar *pair, gchar **key, guint32 *keylen, gchar **value)
2155 char *eqsign = strchr (pair, '=');
2163 *key = (gchar*)pair;
2164 *keylen = eqsign - *key;
2165 while (*keylen > 0 && g_ascii_isspace ((*key) [*keylen - 1]))
2167 *value = g_strstrip (eqsign + 1);
2172 mono_assembly_name_parse_full (const char *name, MonoAssemblyName *aname, gboolean save_public_key, gboolean *is_version_defined, gboolean *is_token_defined)
2176 gchar *version = NULL;
2178 gchar *culture = NULL;
2180 gchar *token = NULL;
2184 gchar *retargetable = NULL;
2185 gchar *retargetable_uq;
2189 gchar *value, *part_name;
2190 guint32 part_name_len;
2193 gboolean version_defined;
2194 gboolean token_defined;
2196 guint32 arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
2198 if (!is_version_defined)
2199 is_version_defined = &version_defined;
2200 *is_version_defined = FALSE;
2201 if (!is_token_defined)
2202 is_token_defined = &token_defined;
2203 *is_token_defined = FALSE;
2205 parts = tmp = g_strsplit (name, ",", 6);
2206 if (!tmp || !*tmp) {
2211 dllname = g_strstrip (*tmp);
2216 if (!split_key_value (g_strstrip (*tmp), &part_name, &part_name_len, &value))
2217 goto cleanup_and_fail;
2219 if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Version", part_name_len)) {
2220 *is_version_defined = TRUE;
2222 if (strlen (version) == 0) {
2223 goto cleanup_and_fail;
2229 if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Culture", part_name_len)) {
2231 if (strlen (culture) == 0) {
2232 goto cleanup_and_fail;
2238 if (part_name_len == 14 && !g_ascii_strncasecmp (part_name, "PublicKeyToken", part_name_len)) {
2239 *is_token_defined = TRUE;
2241 if (strlen (token) == 0) {
2242 goto cleanup_and_fail;
2248 if (part_name_len == 9 && !g_ascii_strncasecmp (part_name, "PublicKey", part_name_len)) {
2250 if (strlen (key) == 0) {
2251 goto cleanup_and_fail;
2257 if (part_name_len == 12 && !g_ascii_strncasecmp (part_name, "Retargetable", part_name_len)) {
2258 retargetable = value;
2259 retargetable_uq = unquote (retargetable);
2260 if (retargetable_uq != NULL)
2261 retargetable = retargetable_uq;
2263 if (!g_ascii_strcasecmp (retargetable, "yes")) {
2264 flags |= ASSEMBLYREF_RETARGETABLE_FLAG;
2265 } else if (g_ascii_strcasecmp (retargetable, "no")) {
2266 free (retargetable_uq);
2267 goto cleanup_and_fail;
2270 free (retargetable_uq);
2275 if (part_name_len == 21 && !g_ascii_strncasecmp (part_name, "ProcessorArchitecture", part_name_len)) {
2277 procarch_uq = unquote (procarch);
2278 if (procarch_uq != NULL)
2279 procarch = procarch_uq;
2281 if (!g_ascii_strcasecmp (procarch, "MSIL"))
2282 arch = MONO_PROCESSOR_ARCHITECTURE_MSIL;
2283 else if (!g_ascii_strcasecmp (procarch, "X86"))
2284 arch = MONO_PROCESSOR_ARCHITECTURE_X86;
2285 else if (!g_ascii_strcasecmp (procarch, "IA64"))
2286 arch = MONO_PROCESSOR_ARCHITECTURE_IA64;
2287 else if (!g_ascii_strcasecmp (procarch, "AMD64"))
2288 arch = MONO_PROCESSOR_ARCHITECTURE_AMD64;
2291 goto cleanup_and_fail;
2303 /* if retargetable flag is set, then we must have a fully qualified name */
2304 if (retargetable != NULL && (version == NULL || culture == NULL || (key == NULL && token == NULL))) {
2305 goto cleanup_and_fail;
2308 dllname_uq = unquote (dllname);
2309 version_uq = unquote (version);
2310 culture_uq = unquote (culture);
2311 token_uq = unquote (token);
2312 key_uq = unquote (key);
2314 res = build_assembly_name (
2315 dllname_uq == NULL ? dllname : dllname_uq,
2316 version_uq == NULL ? version : version_uq,
2317 culture_uq == NULL ? culture : culture_uq,
2318 token_uq == NULL ? token : token_uq,
2319 key_uq == NULL ? key : key_uq,
2320 flags, arch, aname, save_public_key);
2337 unquote (const char *str)
2345 slen = strlen (str);
2349 if (*str != '\'' && *str != '\"')
2352 end = str + slen - 1;
2356 return g_strndup (str + 1, slen - 2);
2360 * mono_assembly_name_parse:
2361 * @name: name to parse
2362 * @aname: the destination assembly name
2364 * Parses an assembly qualified type name and assigns the name,
2365 * version, culture and token to the provided assembly name object.
2367 * Returns: TRUE if the name could be parsed.
2370 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname)
2372 return mono_assembly_name_parse_full (name, aname, FALSE, NULL, NULL);
2376 * mono_assembly_name_new:
2377 * @name: name to parse
2379 * Allocate a new MonoAssemblyName and fill its values from the
2382 * Returns: a newly allocated structure or NULL if there was any failure.
2385 mono_assembly_name_new (const char *name)
2387 MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
2388 if (mono_assembly_name_parse (name, aname))
2395 mono_assembly_name_get_name (MonoAssemblyName *aname)
2401 mono_assembly_name_get_culture (MonoAssemblyName *aname)
2403 return aname->culture;
2407 mono_assembly_name_get_pubkeytoken (MonoAssemblyName *aname)
2409 if (aname->public_key_token [0])
2410 return aname->public_key_token;
2415 mono_assembly_name_get_version (MonoAssemblyName *aname, uint16_t *minor, uint16_t *build, uint16_t *revision)
2418 *minor = aname->minor;
2420 *build = aname->build;
2422 *revision = aname->revision;
2423 return aname->major;
2426 static MonoAssembly*
2427 probe_for_partial_name (const char *basepath, const char *fullname, MonoAssemblyName *aname, MonoImageOpenStatus *status)
2429 gchar *fullpath = NULL;
2431 const char* direntry;
2432 MonoAssemblyName gac_aname;
2433 gint major=-1, minor=0, build=0, revision=0;
2434 gboolean exact_version;
2436 dirhandle = g_dir_open (basepath, 0, NULL);
2440 exact_version = (aname->major | aname->minor | aname->build | aname->revision) != 0;
2442 while ((direntry = g_dir_read_name (dirhandle))) {
2443 gboolean match = TRUE;
2445 if(!parse_assembly_directory_name (aname->name, direntry, &gac_aname))
2448 if (aname->culture != NULL && strcmp (aname->culture, gac_aname.culture) != 0)
2451 if (match && strlen ((char*)aname->public_key_token) > 0 &&
2452 !mono_public_tokens_are_equal (aname->public_key_token, gac_aname.public_key_token))
2456 if (exact_version) {
2457 match = (aname->major == gac_aname.major && aname->minor == gac_aname.minor &&
2458 aname->build == gac_aname.build && aname->revision == gac_aname.revision);
2460 else if (gac_aname.major < major)
2462 else if (gac_aname.major == major) {
2463 if (gac_aname.minor < minor)
2465 else if (gac_aname.minor == minor) {
2466 if (gac_aname.build < build)
2468 else if (gac_aname.build == build && gac_aname.revision <= revision)
2475 major = gac_aname.major;
2476 minor = gac_aname.minor;
2477 build = gac_aname.build;
2478 revision = gac_aname.revision;
2480 fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
2483 mono_assembly_name_free (&gac_aname);
2486 g_dir_close (dirhandle);
2488 if (fullpath == NULL)
2491 MonoAssembly *res = mono_assembly_open (fullpath, status);
2498 * mono_assembly_load_with_partial_name:
2499 * @name: an assembly name that is then parsed by `api:mono_assembly_name_parse`.
2500 * @status: return status code
2502 * Loads a Mono Assembly from a name. The name is parsed using `api:mono_assembly_name_parse`,
2503 * so it might contain a qualified type name, version, culture and token.
2505 * This will load the assembly from the file whose name is derived from the assembly name
2506 * by appending the .dll extension.
2508 * The assembly is loaded from either one of the extra Global Assembly Caches specified
2509 * by the extra GAC paths (specified by the `MONO_GAC_PREFIX` environment variable) or
2510 * if that fails from the GAC.
2512 * Returns: NULL on failure, or a pointer to a MonoAssembly on success.
2515 mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
2519 MonoAssemblyName *aname, base_name;
2520 MonoAssemblyName mapped_aname;
2521 gchar *fullname, *gacpath;
2524 memset (&base_name, 0, sizeof (MonoAssemblyName));
2527 if (!mono_assembly_name_parse (name, aname))
2531 * If no specific version has been requested, make sure we load the
2532 * correct version for system assemblies.
2534 if ((aname->major | aname->minor | aname->build | aname->revision) == 0)
2535 aname = mono_assembly_remap_version (aname, &mapped_aname);
2537 res = mono_assembly_loaded (aname);
2539 mono_assembly_name_free (aname);
2543 res = invoke_assembly_preload_hook (aname, assemblies_path);
2545 res->in_gac = FALSE;
2546 mono_assembly_name_free (aname);
2550 fullname = g_strdup_printf ("%s.dll", aname->name);
2552 if (extra_gac_paths) {
2553 paths = extra_gac_paths;
2554 while (!res && *paths) {
2555 gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", aname->name, NULL);
2556 res = probe_for_partial_name (gacpath, fullname, aname, status);
2565 mono_assembly_name_free (aname);
2569 gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", aname->name, NULL);
2570 res = probe_for_partial_name (gacpath, fullname, aname, status);
2576 MonoDomain *domain = mono_domain_get ();
2577 MonoReflectionAssembly *refasm;
2579 refasm = mono_try_assembly_resolve (domain, mono_string_new (domain, name), NULL, FALSE, &error);
2580 if (!mono_error_ok (&error)) {
2582 mono_assembly_name_free (aname);
2583 mono_error_raise_exception (&error); /* FIXME don't raise here */
2587 res = refasm->assembly;
2591 mono_assembly_name_free (aname);
2597 mono_assembly_is_in_gac (const gchar *filename)
2599 const gchar *rootdir;
2603 if (filename == NULL)
2606 for (paths = extra_gac_paths; paths && *paths; paths++) {
2607 if (strstr (*paths, filename) != *paths)
2610 gp = (gchar *) (filename + strlen (*paths));
2611 if (*gp != G_DIR_SEPARATOR)
2614 if (strncmp (gp, "lib", 3))
2617 if (*gp != G_DIR_SEPARATOR)
2620 if (strncmp (gp, "mono", 4))
2623 if (*gp != G_DIR_SEPARATOR)
2626 if (strncmp (gp, "gac", 3))
2629 if (*gp != G_DIR_SEPARATOR)
2635 rootdir = mono_assembly_getrootdir ();
2636 if (strstr (filename, rootdir) != filename)
2639 gp = (gchar *) (filename + strlen (rootdir));
2640 if (*gp != G_DIR_SEPARATOR)
2643 if (strncmp (gp, "mono", 4))
2646 if (*gp != G_DIR_SEPARATOR)
2649 if (strncmp (gp, "gac", 3))
2652 if (*gp != G_DIR_SEPARATOR)
2658 mono_assembly_load_publisher_policy (MonoAssemblyName *aname)
2661 gchar *filename, *pname, *name, *culture, *version, *fullpath, *subpath;
2665 if (strstr (aname->name, ".dll")) {
2666 len = strlen (aname->name) - 4;
2667 name = (gchar *)g_malloc (len);
2668 strncpy (name, aname->name, len);
2670 name = g_strdup (aname->name);
2673 culture = g_utf8_strdown (aname->culture, -1);
2675 culture = g_strdup ("");
2677 pname = g_strdup_printf ("policy.%d.%d.%s", aname->major, aname->minor, name);
2678 version = g_strdup_printf ("0.0.0.0_%s_%s", culture, aname->public_key_token);
2682 filename = g_strconcat (pname, ".dll", NULL);
2683 subpath = g_build_path (G_DIR_SEPARATOR_S, pname, version, filename, NULL);
2689 if (extra_gac_paths) {
2690 paths = extra_gac_paths;
2691 while (!image && *paths) {
2692 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths,
2693 "lib", "mono", "gac", subpath, NULL);
2694 image = mono_image_open (fullpath, NULL);
2705 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
2706 "mono", "gac", subpath, NULL);
2707 image = mono_image_open (fullpath, NULL);
2714 static MonoAssemblyName*
2715 mono_assembly_bind_version (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2717 memcpy (dest_name, aname, sizeof (MonoAssemblyName));
2718 dest_name->major = info->new_version.major;
2719 dest_name->minor = info->new_version.minor;
2720 dest_name->build = info->new_version.build;
2721 dest_name->revision = info->new_version.revision;
2726 /* LOCKING: assembly_binding lock must be held */
2727 static MonoAssemblyBindingInfo*
2728 search_binding_loaded (MonoAssemblyName *aname)
2732 for (tmp = loaded_assembly_bindings; tmp; tmp = tmp->next) {
2733 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)tmp->data;
2734 if (assembly_binding_maps_name (info, aname))
2741 static inline gboolean
2742 info_compare_versions (AssemblyVersionSet *left, AssemblyVersionSet *right)
2744 if (left->major != right->major || left->minor != right->minor ||
2745 left->build != right->build || left->revision != right->revision)
2751 static inline gboolean
2752 info_versions_equal (MonoAssemblyBindingInfo *left, MonoAssemblyBindingInfo *right)
2754 if (left->has_old_version_bottom != right->has_old_version_bottom)
2757 if (left->has_old_version_top != right->has_old_version_top)
2760 if (left->has_new_version != right->has_new_version)
2763 if (left->has_old_version_bottom && !info_compare_versions (&left->old_version_bottom, &right->old_version_bottom))
2766 if (left->has_old_version_top && !info_compare_versions (&left->old_version_top, &right->old_version_top))
2769 if (left->has_new_version && !info_compare_versions (&left->new_version, &right->new_version))
2775 /* LOCKING: assumes all the necessary locks are held */
2777 assembly_binding_info_parsed (MonoAssemblyBindingInfo *info, void *user_data)
2779 MonoAssemblyBindingInfo *info_copy;
2781 MonoAssemblyBindingInfo *info_tmp;
2782 MonoDomain *domain = (MonoDomain*)user_data;
2787 for (tmp = domain->assembly_bindings; tmp; tmp = tmp->next) {
2788 info_tmp = (MonoAssemblyBindingInfo *)tmp->data;
2789 if (strcmp (info->name, info_tmp->name) == 0 && info_versions_equal (info, info_tmp))
2793 info_copy = (MonoAssemblyBindingInfo *)mono_mempool_alloc0 (domain->mp, sizeof (MonoAssemblyBindingInfo));
2794 memcpy (info_copy, info, sizeof (MonoAssemblyBindingInfo));
2796 info_copy->name = mono_mempool_strdup (domain->mp, info->name);
2798 info_copy->culture = mono_mempool_strdup (domain->mp, info->culture);
2800 domain->assembly_bindings = g_slist_append_mempool (domain->mp, domain->assembly_bindings, info_copy);
2804 get_version_number (int major, int minor)
2806 return major * 256 + minor;
2809 static inline gboolean
2810 info_major_minor_in_range (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
2812 int aname_version_number = get_version_number (aname->major, aname->minor);
2813 if (!info->has_old_version_bottom)
2816 if (get_version_number (info->old_version_bottom.major, info->old_version_bottom.minor) > aname_version_number)
2819 if (info->has_old_version_top && get_version_number (info->old_version_top.major, info->old_version_top.minor) < aname_version_number)
2822 /* This is not the nicest way to do it, but it's a by-product of the way parsing is done */
2823 info->major = aname->major;
2824 info->minor = aname->minor;
2829 /* LOCKING: Assumes that we are already locked - both loader and domain locks */
2830 static MonoAssemblyBindingInfo*
2831 get_per_domain_assembly_binding_info (MonoDomain *domain, MonoAssemblyName *aname)
2833 MonoAssemblyBindingInfo *info;
2836 if (!domain->assembly_bindings)
2840 for (list = domain->assembly_bindings; list; list = list->next) {
2841 info = (MonoAssemblyBindingInfo *)list->data;
2842 if (info && !strcmp (aname->name, info->name) && info_major_minor_in_range (info, aname))
2848 if (info->name && info->public_key_token [0] && info->has_old_version_bottom &&
2849 info->has_new_version && assembly_binding_maps_name (info, aname))
2850 info->is_valid = TRUE;
2852 info->is_valid = FALSE;
2858 static MonoAssemblyName*
2859 mono_assembly_apply_binding (MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2861 MonoAssemblyBindingInfo *info, *info2;
2865 if (aname->public_key_token [0] == 0)
2868 domain = mono_domain_get ();
2870 mono_assembly_binding_lock ();
2871 info = search_binding_loaded (aname);
2872 mono_assembly_binding_unlock ();
2875 mono_domain_lock (domain);
2876 info = get_per_domain_assembly_binding_info (domain, aname);
2877 mono_domain_unlock (domain);
2881 if (!check_policy_versions (info, aname))
2884 mono_assembly_bind_version (info, aname, dest_name);
2888 if (domain && domain->setup && domain->setup->configuration_file) {
2889 mono_domain_lock (domain);
2890 if (!domain->assembly_bindings_parsed) {
2891 gchar *domain_config_file_name = mono_string_to_utf8 (domain->setup->configuration_file);
2892 gchar *domain_config_file_path = mono_portability_find_file (domain_config_file_name, TRUE);
2894 if (!domain_config_file_path)
2895 domain_config_file_path = domain_config_file_name;
2897 mono_config_parse_assembly_bindings (domain_config_file_path, aname->major, aname->minor, domain, assembly_binding_info_parsed);
2898 domain->assembly_bindings_parsed = TRUE;
2899 if (domain_config_file_name != domain_config_file_path)
2900 g_free (domain_config_file_name);
2901 g_free (domain_config_file_path);
2904 info2 = get_per_domain_assembly_binding_info (domain, aname);
2907 info = (MonoAssemblyBindingInfo *)g_memdup (info2, sizeof (MonoAssemblyBindingInfo));
2908 info->name = g_strdup (info2->name);
2909 info->culture = g_strdup (info2->culture);
2910 info->domain_id = domain->domain_id;
2913 mono_domain_unlock (domain);
2917 info = g_new0 (MonoAssemblyBindingInfo, 1);
2918 info->major = aname->major;
2919 info->minor = aname->minor;
2922 if (!info->is_valid) {
2923 ppimage = mono_assembly_load_publisher_policy (aname);
2925 get_publisher_policy_info (ppimage, aname, info);
2926 mono_image_close (ppimage);
2930 /* Define default error value if needed */
2931 if (!info->is_valid) {
2932 info->name = g_strdup (aname->name);
2933 info->culture = g_strdup (aname->culture);
2934 g_strlcpy ((char *)info->public_key_token, (const char *)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2937 mono_assembly_binding_lock ();
2938 info2 = search_binding_loaded (aname);
2940 /* This binding was added by another thread
2942 mono_assembly_binding_info_free (info);
2947 loaded_assembly_bindings = g_slist_prepend (loaded_assembly_bindings, info);
2949 mono_assembly_binding_unlock ();
2951 if (!info->is_valid || !check_policy_versions (info, aname))
2954 mono_assembly_bind_version (info, aname, dest_name);
2959 * mono_assembly_load_from_gac
2961 * @aname: The assembly name object
2963 static MonoAssembly*
2964 mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status, MonoBoolean refonly)
2966 MonoAssembly *result = NULL;
2967 gchar *name, *version, *culture, *fullpath, *subpath;
2972 if (aname->public_key_token [0] == 0) {
2976 if (strstr (aname->name, ".dll")) {
2977 len = strlen (filename) - 4;
2978 name = (gchar *)g_malloc (len);
2979 strncpy (name, aname->name, len);
2981 name = g_strdup (aname->name);
2984 if (aname->culture) {
2985 culture = g_utf8_strdown (aname->culture, -1);
2987 culture = g_strdup ("");
2990 pubtok = g_ascii_strdown ((char*)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2991 version = g_strdup_printf ("%d.%d.%d.%d_%s_%s", aname->major,
2992 aname->minor, aname->build, aname->revision,
2996 subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
3001 if (extra_gac_paths) {
3002 paths = extra_gac_paths;
3003 while (!result && *paths) {
3004 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", subpath, NULL);
3005 result = mono_assembly_open_full (fullpath, status, refonly);
3012 result->in_gac = TRUE;
3017 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
3018 "mono", "gac", subpath, NULL);
3019 result = mono_assembly_open_full (fullpath, status, refonly);
3023 result->in_gac = TRUE;
3031 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status)
3034 MonoAssemblyName *aname;
3037 /* g_print ("corlib already loaded\n"); */
3041 // In native client, Corlib is embedded in the executable as static variable corlibData
3042 #if defined(__native_client__)
3043 if (corlibData != NULL && corlibSize != 0) {
3045 /* First "FALSE" instructs mono not to make a copy. */
3046 /* Second "FALSE" says this is not just a ref. */
3047 MonoImage* image = mono_image_open_from_data_full (corlibData, corlibSize, FALSE, &status, FALSE);
3048 if (image == NULL || status != 0)
3049 g_print("mono_image_open_from_data_full failed: %d\n", status);
3050 corlib = mono_assembly_load_from_full (image, "mscorlib", &status, FALSE);
3051 if (corlib == NULL || status != 0)
3052 g_print ("mono_assembly_load_from_full failed: %d\n", status);
3058 // A nonstandard preload hook may provide a special mscorlib assembly
3059 aname = mono_assembly_name_new ("mscorlib.dll");
3060 corlib = invoke_assembly_preload_hook (aname, assemblies_path);
3061 mono_assembly_name_free (aname);
3064 goto return_corlib_and_facades;
3066 // This unusual directory layout can occur if mono is being built and run out of its own source repo
3067 if (assemblies_path) { // Custom assemblies path set via MONO_PATH or mono_set_assemblies_path
3068 corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status, FALSE);
3070 goto return_corlib_and_facades;
3073 /* Normal case: Load corlib from mono/<version> */
3074 corlib_file = g_build_filename ("mono", runtime->framework_version, "mscorlib.dll", NULL);
3075 if (assemblies_path) { // Custom assemblies path
3076 corlib = load_in_path (corlib_file, (const char**)assemblies_path, status, FALSE);
3078 g_free (corlib_file);
3079 goto return_corlib_and_facades;
3082 corlib = load_in_path (corlib_file, default_path, status, FALSE);
3083 g_free (corlib_file);
3085 return_corlib_and_facades:
3086 if (corlib && !strcmp (runtime->framework_version, "4.5")) // FIXME: stop hardcoding 4.5 here
3087 default_path [1] = g_strdup_printf ("%s/Facades", corlib->basedir);
3093 mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
3094 const char *basedir,
3095 MonoImageOpenStatus *status,
3098 MonoAssembly *result;
3099 char *fullpath, *filename;
3100 MonoAssemblyName maped_aname;
3101 MonoAssemblyName maped_name_pp;
3106 aname = mono_assembly_remap_version (aname, &maped_aname);
3108 /* Reflection only assemblies don't get assembly binding */
3110 aname = mono_assembly_apply_binding (aname, &maped_name_pp);
3112 result = mono_assembly_loaded_full (aname, refonly);
3116 result = refonly ? invoke_assembly_refonly_preload_hook (aname, assemblies_path) : invoke_assembly_preload_hook (aname, assemblies_path);
3118 result->in_gac = FALSE;
3122 /* Currently we retrieve the loaded corlib for reflection
3123 * only requests, like a common reflection only assembly
3125 if (strcmp (aname->name, "mscorlib") == 0 || strcmp (aname->name, "mscorlib.dll") == 0) {
3126 return mono_assembly_load_corlib (mono_get_runtime_info (), status);
3129 len = strlen (aname->name);
3130 for (ext_index = 0; ext_index < 2; ext_index ++) {
3131 ext = ext_index == 0 ? ".dll" : ".exe";
3132 if (len > 4 && (!strcmp (aname->name + len - 4, ".dll") || !strcmp (aname->name + len - 4, ".exe"))) {
3133 filename = g_strdup (aname->name);
3134 /* Don't try appending .dll/.exe if it already has one of those extensions */
3137 filename = g_strconcat (aname->name, ext, NULL);
3140 result = mono_assembly_load_from_gac (aname, filename, status, refonly);
3147 fullpath = g_build_filename (basedir, filename, NULL);
3148 result = mono_assembly_open_full (fullpath, status, refonly);
3151 result->in_gac = FALSE;
3157 result = load_in_path (filename, default_path, status, refonly);
3159 result->in_gac = FALSE;
3169 mono_assembly_load_full_internal (MonoAssemblyName *aname, MonoAssembly *requesting, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
3171 MonoAssembly *result = mono_assembly_load_full_nosearch (aname, basedir, status, refonly);
3174 /* Try a postload search hook */
3175 result = mono_assembly_invoke_search_hook_internal (aname, requesting, refonly, TRUE);
3180 * mono_assembly_load_full:
3181 * @aname: A MonoAssemblyName with the assembly name to load.
3182 * @basedir: A directory to look up the assembly at.
3183 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
3184 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
3186 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
3187 * attempts to load the assembly from that directory before probing the standard locations.
3189 * If the assembly is being opened in reflection-only mode (@refonly set to TRUE) then no
3190 * assembly binding takes place.
3192 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
3193 * value pointed by status is updated with an error code.
3196 mono_assembly_load_full (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
3198 return mono_assembly_load_full_internal (aname, NULL, basedir, status, refonly);
3202 * mono_assembly_load:
3203 * @aname: A MonoAssemblyName with the assembly name to load.
3204 * @basedir: A directory to look up the assembly at.
3205 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
3207 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
3208 * attempts to load the assembly from that directory before probing the standard locations.
3210 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
3211 * value pointed by status is updated with an error code.
3214 mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
3216 return mono_assembly_load_full_internal (aname, NULL, basedir, status, FALSE);
3220 * mono_assembly_loaded_full:
3221 * @aname: an assembly to look for.
3222 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
3224 * This is used to determine if the specified assembly has been loaded
3225 * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
3226 * a `MonoAssembly` that matches the `MonoAssemblyName` specified.
3229 mono_assembly_loaded_full (MonoAssemblyName *aname, gboolean refonly)
3232 MonoAssemblyName maped_aname;
3234 aname = mono_assembly_remap_version (aname, &maped_aname);
3236 res = mono_assembly_invoke_search_hook_internal (aname, NULL, refonly, FALSE);
3242 * mono_assembly_loaded:
3243 * @aname: an assembly to look for.
3245 * This is used to determine if the specified assembly has been loaded
3247 * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
3248 * a `MonoAssembly` that matches the `MonoAssemblyName` specified.
3251 mono_assembly_loaded (MonoAssemblyName *aname)
3253 return mono_assembly_loaded_full (aname, FALSE);
3257 mono_assembly_release_gc_roots (MonoAssembly *assembly)
3259 if (assembly == NULL || assembly == REFERENCE_MISSING)
3262 if (assembly_is_dynamic (assembly)) {
3264 MonoDynamicImage *dynimg = (MonoDynamicImage *)assembly->image;
3265 for (i = 0; i < dynimg->image.module_count; ++i)
3266 mono_dynamic_image_release_gc_roots ((MonoDynamicImage *)dynimg->image.modules [i]);
3267 mono_dynamic_image_release_gc_roots (dynimg);
3272 * Returns whether mono_assembly_close_finish() must be called as
3273 * well. See comment for mono_image_close_except_pools() for why we
3274 * unload in two steps.
3277 mono_assembly_close_except_image_pools (MonoAssembly *assembly)
3280 g_return_val_if_fail (assembly != NULL, FALSE);
3282 if (assembly == REFERENCE_MISSING)
3285 /* Might be 0 already */
3286 if (InterlockedDecrement (&assembly->ref_count) > 0)
3289 mono_profiler_assembly_event (assembly, MONO_PROFILE_START_UNLOAD);
3291 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading assembly %s [%p].", assembly->aname.name, assembly);
3293 mono_debug_close_image (assembly->image);
3295 mono_assemblies_lock ();
3296 loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
3297 mono_assemblies_unlock ();
3299 assembly->image->assembly = NULL;
3301 if (!mono_image_close_except_pools (assembly->image))
3302 assembly->image = NULL;
3304 for (tmp = assembly->friend_assembly_names; tmp; tmp = tmp->next) {
3305 MonoAssemblyName *fname = (MonoAssemblyName *)tmp->data;
3306 mono_assembly_name_free (fname);
3309 g_slist_free (assembly->friend_assembly_names);
3310 g_free (assembly->basedir);
3312 mono_profiler_assembly_event (assembly, MONO_PROFILE_END_UNLOAD);
3318 mono_assembly_close_finish (MonoAssembly *assembly)
3320 g_assert (assembly && assembly != REFERENCE_MISSING);
3322 if (assembly->image)
3323 mono_image_close_finish (assembly->image);
3325 if (assembly_is_dynamic (assembly)) {
3326 g_free ((char*)assembly->aname.culture);
3333 * mono_assembly_close:
3334 * @assembly: the assembly to release.
3336 * This method releases a reference to the @assembly. The assembly is
3337 * only released when all the outstanding references to it are released.
3340 mono_assembly_close (MonoAssembly *assembly)
3342 if (mono_assembly_close_except_image_pools (assembly))
3343 mono_assembly_close_finish (assembly);
3347 mono_assembly_load_module (MonoAssembly *assembly, guint32 idx)
3349 return mono_image_load_file_for_image (assembly->image, idx);
3353 * mono_assembly_foreach:
3354 * @func: function to invoke for each assembly loaded
3355 * @user_data: data passed to the callback
3357 * Invokes the provided @func callback for each assembly loaded into
3358 * the runtime. The first parameter passed to the callback is the
3359 * `MonoAssembly*`, and the second parameter is the @user_data.
3361 * This is done for all assemblies loaded in the runtime, not just
3362 * those loaded in the current application domain.
3365 mono_assembly_foreach (GFunc func, gpointer user_data)
3370 * We make a copy of the list to avoid calling the callback inside the
3371 * lock, which could lead to deadlocks.
3373 mono_assemblies_lock ();
3374 copy = g_list_copy (loaded_assemblies);
3375 mono_assemblies_unlock ();
3377 g_list_foreach (loaded_assemblies, func, user_data);
3383 * mono_assemblies_cleanup:
3385 * Free all resources used by this module.
3388 mono_assemblies_cleanup (void)
3392 mono_os_mutex_destroy (&assemblies_mutex);
3393 mono_os_mutex_destroy (&assembly_binding_mutex);
3395 for (l = loaded_assembly_bindings; l; l = l->next) {
3396 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)l->data;
3398 mono_assembly_binding_info_free (info);
3401 g_slist_free (loaded_assembly_bindings);
3403 free_assembly_load_hooks ();
3404 free_assembly_search_hooks ();
3405 free_assembly_preload_hooks ();
3408 /*LOCKING takes the assembly_binding lock*/
3410 mono_assembly_cleanup_domain_bindings (guint32 domain_id)
3414 mono_assembly_binding_lock ();
3415 iter = &loaded_assembly_bindings;
3418 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)l->data;
3420 if (info->domain_id == domain_id) {
3422 mono_assembly_binding_info_free (info);
3429 mono_assembly_binding_unlock ();
3433 * Holds the assembly of the application, for
3434 * System.Diagnostics.Process::MainModule
3436 static MonoAssembly *main_assembly=NULL;
3439 mono_assembly_set_main (MonoAssembly *assembly)
3441 main_assembly = assembly;
3445 * mono_assembly_get_main:
3447 * Returns: the assembly for the application, the first assembly that is loaded by the VM
3450 mono_assembly_get_main (void)
3452 return (main_assembly);
3456 * mono_assembly_get_image:
3457 * @assembly: The assembly to retrieve the image from
3459 * Returns: the MonoImage associated with this assembly.
3462 mono_assembly_get_image (MonoAssembly *assembly)
3464 return assembly->image;
3468 * mono_assembly_get_name:
3469 * @assembly: The assembly to retrieve the name from
3471 * The returned name's lifetime is the same as @assembly's.
3473 * Returns: the MonoAssemblyName associated with this assembly.
3476 mono_assembly_get_name (MonoAssembly *assembly)
3478 return &assembly->aname;
3482 mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies)
3484 bundles = assemblies;
3487 #define MONO_DECLSEC_FORMAT_10 0x3C
3488 #define MONO_DECLSEC_FORMAT_20 0x2E
3489 #define MONO_DECLSEC_FIELD 0x53
3490 #define MONO_DECLSEC_PROPERTY 0x54
3492 #define SKIP_VISIBILITY_XML_ATTRIBUTE ("\"SkipVerification\"")
3493 #define SKIP_VISIBILITY_ATTRIBUTE_NAME ("System.Security.Permissions.SecurityPermissionAttribute")
3494 #define SKIP_VISIBILITY_ATTRIBUTE_SIZE (sizeof (SKIP_VISIBILITY_ATTRIBUTE_NAME) - 1)
3495 #define SKIP_VISIBILITY_PROPERTY_NAME ("SkipVerification")
3496 #define SKIP_VISIBILITY_PROPERTY_SIZE (sizeof (SKIP_VISIBILITY_PROPERTY_NAME) - 1)
3499 mono_assembly_try_decode_skip_verification_param (const char *p, const char **resp, gboolean *abort_decoding)
3503 case MONO_DECLSEC_PROPERTY:
3505 case MONO_DECLSEC_FIELD:
3507 *abort_decoding = TRUE;
3512 if (*p++ != MONO_TYPE_BOOLEAN) {
3513 *abort_decoding = TRUE;
3517 /* property name length */
3518 len = mono_metadata_decode_value (p, &p);
3520 if (len >= SKIP_VISIBILITY_PROPERTY_SIZE && !memcmp (p, SKIP_VISIBILITY_PROPERTY_NAME, SKIP_VISIBILITY_PROPERTY_SIZE)) {
3531 mono_assembly_try_decode_skip_verification (const char *p, const char *endn)
3533 int i, j, num, len, params_len;
3535 if (*p == MONO_DECLSEC_FORMAT_10) {
3536 gsize read, written;
3537 char *res = g_convert (p, endn - p, "UTF-8", "UTF-16LE", &read, &written, NULL);
3539 gboolean found = strstr (res, SKIP_VISIBILITY_XML_ATTRIBUTE) != NULL;
3545 if (*p++ != MONO_DECLSEC_FORMAT_20)
3548 /* number of encoded permission attributes */
3549 num = mono_metadata_decode_value (p, &p);
3550 for (i = 0; i < num; ++i) {
3551 gboolean is_valid = FALSE;
3552 gboolean abort_decoding = FALSE;
3554 /* attribute name length */
3555 len = mono_metadata_decode_value (p, &p);
3557 /* We don't really need to fully decode the type. Comparing the name is enough */
3558 is_valid = len >= SKIP_VISIBILITY_ATTRIBUTE_SIZE && !memcmp (p, SKIP_VISIBILITY_ATTRIBUTE_NAME, SKIP_VISIBILITY_ATTRIBUTE_SIZE);
3562 /*size of the params table*/
3563 params_len = mono_metadata_decode_value (p, &p);
3565 const char *params_end = p + params_len;
3567 /* number of parameters */
3568 len = mono_metadata_decode_value (p, &p);
3570 for (j = 0; j < len; ++j) {
3571 if (mono_assembly_try_decode_skip_verification_param (p, &p, &abort_decoding))
3587 mono_assembly_has_skip_verification (MonoAssembly *assembly)
3590 guint32 cols [MONO_DECL_SECURITY_SIZE];
3594 if (MONO_SECMAN_FLAG_INIT (assembly->skipverification))
3595 return MONO_SECMAN_FLAG_GET_VALUE (assembly->skipverification);
3597 t = &assembly->image->tables [MONO_TABLE_DECLSECURITY];
3599 for (i = 0; i < t->rows; ++i) {
3600 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
3601 if ((cols [MONO_DECL_SECURITY_PARENT] & MONO_HAS_DECL_SECURITY_MASK) != MONO_HAS_DECL_SECURITY_ASSEMBLY)
3603 if (cols [MONO_DECL_SECURITY_ACTION] != SECURITY_ACTION_REQMIN)
3606 blob = mono_metadata_blob_heap (assembly->image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
3607 len = mono_metadata_decode_blob_size (blob, &blob);
3611 if (mono_assembly_try_decode_skip_verification (blob, blob + len)) {
3612 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, TRUE);
3617 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, FALSE);