Beispiel #1
0
        public Module __AddModule(RawModule module)
        {
            Module mod = module.ToModule(this);

            addedModules.Add(mod);
            return(mod);
        }
Beispiel #2
0
        private Assembly LoadFile(string path)
        {
            string ex = null;

            try {
                using (RawModule module = _universe.OpenRawModule(path)) {
                    if (_mscorlibVersion != null)
                    {
                        // to avoid problems (i.e. weird exceptions), we don't allow assemblies to load that reference a newer version of mscorlib
                        foreach (AssemblyName asmref in module.GetReferencedAssemblies())
                        {
                            if (asmref.Name == "mscorlib" && asmref.Version > _mscorlibVersion)
                            {
                                ConsoleOps.Error(true, "unable to load assembly '{0}' as it depends on a higher version of mscorlib than the one currently loaded", path);
                            }
                        }
                    }
                    Assembly asm = _universe.LoadAssembly(module);
                    if (asm.Location != module.Location && CanonicalizePath(asm.Location) != CanonicalizePath(module.Location))
                    {
                        ConsoleOps.Warning("assembly '{0}' is ignored as previously loaded assembly '{1}' has the same identity '{2}'", path, asm.Location, asm.FullName);
                    }
                    return(asm);
                }
            } catch (IOException x) {
                ex = x.Message;
            } catch (UnauthorizedAccessException x) {
                ex = x.Message;
            } catch (IKVM.Reflection.BadImageFormatException x) {
                ex = x.Message;
            }

            ConsoleOps.Error(true, "unable to load assembly '{0}'" + Environment.NewLine + "    ({1})", path, ex);
            return(null);
        }
Beispiel #3
0
        internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, IEnumerable <CustomAttributeBuilder> customAttributes)
            : base(universe)
        {
            this.name = name.Name;
            SetVersionHelper(name.Version);
            if (!string.IsNullOrEmpty(name.CultureName))
            {
                this.culture = name.CultureName;
            }
            this.flags         = name.RawFlags;
            this.hashAlgorithm = name.HashAlgorithm;
            if (this.hashAlgorithm == AssemblyHashAlgorithm.None)
            {
                this.hashAlgorithm = AssemblyHashAlgorithm.SHA1;
            }
            this.keyPair = name.KeyPair;
            if (this.keyPair != null)
            {
                this.publicKey = this.keyPair.PublicKey;
            }
            else
            {
                byte[] publicKey = name.GetPublicKey();
                if (publicKey != null && publicKey.Length != 0)
                {
                    this.publicKey = (byte[])publicKey.Clone();
                }
            }
            this.dir = dir ?? ".";
            if (customAttributes != null)
            {
                this.customAttributes.AddRange(customAttributes);
            }
            if (universe.HasMscorlib && !universe.Mscorlib.__IsMissing && universe.Mscorlib.ImageRuntimeVersion != null)
            {
                this.imageRuntimeVersion = universe.Mscorlib.ImageRuntimeVersion;
            }
            else
            {
#if NETSTANDARD1_3 || NETSTANDARD1_4
                using (Universe temp = new Universe(UniverseOptions.MetadataOnly))
                    using (RawModule mscorlib = temp.OpenRawModule(TypeUtil.GetAssembly(typeof(object)).ManifestModule.FullyQualifiedName))
                    {
                        this.imageRuntimeVersion = mscorlib.ImageRuntimeVersion;
                    }
#else
                this.imageRuntimeVersion = TypeUtil.GetAssembly(typeof(object)).ImageRuntimeVersion;
#endif
            }
            universe.RegisterDynamicAssembly(this);
        }
Beispiel #4
0
        internal Assembly LoadFile(string path)
        {
            string ex = null;

            try
            {
                using (RawModule module = universe.OpenRawModule(path))
                {
                    if (mscorlibVersion != null)
                    {
                        // to avoid problems (i.e. weird exceptions), we don't allow assemblies to load that reference a newer version of mscorlib
                        foreach (AssemblyName asmref in module.GetReferencedAssemblies())
                        {
                            /*if (asmref.Name.IsReferenceCoreLib() && asmref.Version > mscorlibVersion)
                             * {
                             *  Console.Error.WriteLine(asmref.FullName + " > " + mscorlibVersion);
                             *  Console.Error.WriteLine("Error: unable to load assembly '{0}' as it depends on a higher version of mscorlib than the one currently loaded", path);
                             *  Environment.Exit(1);
                             * }*/
                        }
                    }
                    Assembly asm = universe.LoadAssembly(module);
                    if (asm.Location != module.Location && CanonicalizePath(asm.Location) != CanonicalizePath(module.Location))
                    {
                        EmitWarning(WarningId.LocationIgnored, "assembly \"{0}\" is ignored as previously loaded assembly \"{1}\" has the same identity \"{2}\"", path, asm.Location, asm.FullName);
                    }
                    return(asm);
                }
            }
            catch (IOException x)
            {
                ex = x.Message;
            }
            catch (UnauthorizedAccessException x)
            {
                ex = x.Message;
            }
            catch (IKVM.Reflection.BadImageFormatException x)
            {
                ex = x.Message;
            }
            Console.Error.WriteLine("Error: unable to load assembly '{0}'" + Environment.NewLine + "    ({1})", path, ex);
            Environment.Exit(1);
            return(null);
        }
Beispiel #5
0
        public override Assembly LoadAssemblyFile(string fileName, bool isImplicitReference)
        {
            bool?has_extension = null;

            foreach (var path in paths)
            {
                var file = Path.Combine(path, fileName);
                if (compiler.Settings.DebugFlags > 0)
                {
                    Console.WriteLine("Probing assembly location `{0}'", file);
                }

                if (!File.Exists(file))
                {
                    if (!has_extension.HasValue)
                    {
                        has_extension = fileName.EndsWith(".dll", StringComparison.Ordinal) || fileName.EndsWith(".exe", StringComparison.Ordinal);
                    }

                    if (has_extension.Value)
                    {
                        continue;
                    }

                    file += ".dll";
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                }

                try {
                    using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                        using (RawModule module = domain.OpenRawModule(stream, file)) {
                            if (!module.IsManifestModule)
                            {
                                Error_AssemblyIsModule(fileName);
                                return(null);
                            }

                            //
                            // check whether the assembly can be actually imported without
                            // collision
                            //
                            var an = module.GetAssemblyName();
                            foreach (var entry in loaded_names)
                            {
                                var loaded_name = entry.Item1;
                                if (an.Name != loaded_name.Name)
                                {
                                    continue;
                                }

                                if (module.ModuleVersionId == entry.Item3.ManifestModule.ModuleVersionId)
                                {
                                    return(entry.Item3);
                                }

                                if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0)
                                {
                                    compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                    compiler.Report.SymbolRelatedToPreviousError(fileName);
                                    compiler.Report.Error(1704,
                                                          "An assembly with the same name `{0}' has already been imported. Consider removing one of the references or sign the assembly",
                                                          an.Name);
                                    return(null);
                                }

                                if ((an.Flags & AssemblyNameFlags.PublicKey) == (loaded_name.Flags & AssemblyNameFlags.PublicKey))
                                {
                                    compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                    compiler.Report.SymbolRelatedToPreviousError(fileName);
                                    compiler.Report.Error(1703,
                                                          "An assembly `{0}' with the same identity has already been imported. Consider removing one of the references",
                                                          an.Name);
                                    return(null);
                                }
                            }

                            if (compiler.Settings.DebugFlags > 0)
                            {
                                Console.WriteLine("Loading assembly `{0}'", fileName);
                            }

                            var assembly = domain.LoadAssembly(module);
                            if (assembly != null)
                            {
                                loaded_names.Add(Tuple.Create(an, fileName, assembly));
                            }

                            return(assembly);
                        }
                    }
                } catch (Exception e) {
                    if (compiler.Settings.DebugFlags > 0)
                    {
                        Console.WriteLine("Exception during loading: {0}'", e.ToString());
                    }

                    if (!isImplicitReference)
                    {
                        Error_FileCorrupted(file);
                    }

                    return(null);
                }
            }

            if (!isImplicitReference)
            {
                Error_FileNotFound(fileName);
            }

            return(null);
        }
Beispiel #6
0
 public Module IncludeModule(RawModule moduleFile)
 {
     return(Builder.__AddModule(moduleFile));
 }
Beispiel #7
0
		public Module __AddModule(RawModule module)
		{
			Module mod = module.ToModule(this);
			addedModules.Add(mod);
			return mod;
		}
Beispiel #8
0
        public override Assembly LoadAssemblyFile(string fileName)
        {
            bool?has_extension = null;

            foreach (var path in paths)
            {
                var file = Path.Combine(path, fileName);
                if (!File.Exists(file))
                {
                    if (!has_extension.HasValue)
                    {
                        has_extension = fileName.EndsWith(".dll", StringComparison.Ordinal) || fileName.EndsWith(".exe", StringComparison.Ordinal);
                    }

                    if (has_extension.Value)
                    {
                        continue;
                    }

                    file += ".dll";
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                }

                try
                {
                    using (RawModule module = domain.OpenRawModule(file))
                    {
                        if (!module.IsManifestModule)
                        {
                            Error_AssemblyIsModule(fileName);
                            return(null);
                        }

                        //
                        // check whether the assembly can be actually imported without
                        // collision
                        //
                        var an = module.GetAssemblyName();
                        foreach (var entry in loaded_names)
                        {
                            var loaded_name = entry.Item1;
                            if (an.Name != loaded_name.Name)
                            {
                                continue;
                            }

                            if (an.CodeBase == loaded_name.CodeBase)
                            {
                                return(null);
                            }

                            if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0)
                            {
                                compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                compiler.Report.SymbolRelatedToPreviousError(fileName);
                                compiler.Report.Error(1704,
                                                      "An assembly with the same name `{0}' has already been imported. Consider removing one of the references or sign the assembly",
                                                      an.Name);
                                return(null);
                            }

                            if ((an.Flags & AssemblyNameFlags.PublicKey) == (loaded_name.Flags & AssemblyNameFlags.PublicKey) && an.Version.Equals(loaded_name.Version))
                            {
                                compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                compiler.Report.SymbolRelatedToPreviousError(fileName);
                                compiler.Report.Error(1703,
                                                      "An assembly with the same identity `{0}' has already been imported. Consider removing one of the references",
                                                      an.FullName);
                                return(null);
                            }
                        }

                        if (Report.DebugFlags > 0)
                        {
                            Console.WriteLine("Loading assembly `{0}'", fileName);
                        }

                        loaded_names.Add(Tuple.Create(an, fileName));
                        return(domain.LoadAssembly(module));
                    }
                }
                catch
                {
                    Error_FileCorrupted(file);
                    return(null);
                }
            }

            Error_FileNotFound(fileName);
            return(null);
        }