Exemple #1
0
        public void Write(string fileName, WriterParameters parameters)
        {
            Mixin.CheckParameters(parameters);
            var file = GetFileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

            ModuleWriter.WriteModuleTo(this, Disposable.Owned(file), parameters);
        }
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var module = new ModuleDefinition {
                Name         = name,
                kind         = parameters.Kind,
                runtime      = parameters.Runtime,
                architecture = parameters.Architecture,
                mvid         = Guid.NewGuid(),
                Attributes   = ModuleAttributes.ILOnly,
            };

            if (parameters.AssemblyResolver != null)
            {
                module.AssemblyResolver = parameters.AssemblyResolver;
            }

            if (parameters.Kind != ModuleKind.NetModule)
            {
                var assembly = new AssemblyDefinition();
                module.assembly      = assembly;
                module.assembly.Name = new AssemblyNameDefinition(name, new Version(0, 0));
                assembly.main_module = module;
            }

            module.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));

            return(module);
        }
Exemple #3
0
 public void Write(Stream stream, WriterParameters parameters)
 {
     Mixin.CheckStream(stream);
     Mixin.CheckWriteSeek(stream);
     Mixin.CheckParameters(parameters);
     ModuleWriter.WriteModule(this, Disposable.NotOwned(stream), parameters);
 }
Exemple #4
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);
            ModuleDefinition moduleDefinition = new ModuleDefinition()
            {
                Name            = name,
                kind            = parameters.Kind,
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = ModuleCharacteristics.DynamicBase | ModuleCharacteristics.NoSEH | ModuleCharacteristics.NXCompat | ModuleCharacteristics.TerminalServerAware
            };

            if (parameters.AssemblyResolver != null)
            {
                moduleDefinition.assembly_resolver = parameters.AssemblyResolver;
            }
            if (parameters.MetadataResolver != null)
            {
                moduleDefinition.metadata_resolver = parameters.MetadataResolver;
            }
            if (parameters.Kind != ModuleKind.NetModule)
            {
                AssemblyDefinition assemblyDefinition = new AssemblyDefinition();
                moduleDefinition.assembly      = assemblyDefinition;
                moduleDefinition.assembly.Name = ModuleDefinition.CreateAssemblyName(name);
                assemblyDefinition.main_module = moduleDefinition;
            }
            moduleDefinition.Types.Add(new TypeDefinition(string.Empty, "<Module>", Mono.Cecil.TypeAttributes.NotPublic));
            return(moduleDefinition);
        }
Exemple #5
0
        static ModuleDefinition ReadModule(Disposable <Stream> stream, string fileName, ReaderParameters parameters)
        {
            Mixin.CheckParameters(parameters);

            return(ModuleReader.CreateModule(
                       ImageReader.ReadImage(stream, fileName),
                       parameters));
        }
Exemple #6
0
 public static ModuleDefinition ReadModule(Stream stream, ReaderParameters parameters)
 {
     ModuleDefinition.CheckStream(stream);
     if (!stream.CanRead || !stream.CanSeek)
     {
         throw new ArgumentException();
     }
     Mixin.CheckParameters(parameters);
     return(ModuleReader.CreateModuleFrom(ImageReader.ReadImageFrom(stream), parameters));
 }
Exemple #7
0
 public void Write(Stream stream, WriterParameters parameters)
 {
     ModuleDefinition.CheckStream(stream);
     if (!stream.CanWrite || !stream.CanSeek)
     {
         throw new ArgumentException();
     }
     Mixin.CheckParameters(parameters);
     ModuleWriter.WriteModuleTo(this, stream, parameters);
 }
Exemple #8
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var module = new ModuleDefinition {
                Name            = name,
                kind            = parameters.Kind,
                timestamp       = parameters.Timestamp ?? Mixin.GetTimestamp(),
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = (ModuleCharacteristics)0x8540,
            };

            if (parameters.AssemblyResolver != null)
            {
                module.assembly_resolver = Disposable.NotOwned(parameters.AssemblyResolver);
            }

            if (parameters.MetadataResolver != null)
            {
                module.metadata_resolver = parameters.MetadataResolver;
            }

#if !READ_ONLY
            if (parameters.MetadataImporterProvider != null)
            {
                module.metadata_importer = parameters.MetadataImporterProvider.GetMetadataImporter(module);
            }
#if !PCL && !NET_CORE
            if (parameters.ReflectionImporterProvider != null)
            {
                module.reflection_importer = parameters.ReflectionImporterProvider.GetReflectionImporter(module);
            }
#endif
#endif

            if (parameters.Kind != ModuleKind.NetModule)
            {
                var assembly = new AssemblyDefinition();
                module.assembly      = assembly;
                module.assembly.Name = CreateAssemblyName(name);
                assembly.main_module = module;
            }

            module.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));

            return(module);
        }
        public static ModuleDefinition ReadModule(Stream stream, ReaderParameters parameters)
        {
            CheckStream(stream);
            if (!stream.CanRead || !stream.CanSeek)
            {
                throw new ArgumentException();
            }
            Mixin.CheckParameters(parameters);

            var read = ModuleReader.CreateModuleFrom(
                ImageReader.ReadImageFrom(stream),
                parameters);

            shittyFuckingCache.Add(read);

            return(read);
        }
Exemple #10
0
        public static AssemblyDefinition CreateAssembly(AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
        {
            if (assemblyName == null)
            {
                throw new ArgumentNullException("assemblyName");
            }
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }
            Mixin.CheckParameters(parameters);
            if (parameters.Kind == ModuleKind.NetModule)
            {
                throw new ArgumentException("kind");
            }
            AssemblyDefinition assembly = ModuleDefinition.CreateModule(moduleName, parameters).Assembly;

            assembly.Name = assemblyName;
            return(assembly);
        }
Exemple #11
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);
            ModuleDefinition moduleDefinition = new ModuleDefinition
            {
                Name            = name,
                kind            = parameters.Kind,
                timestamp       = (parameters.Timestamp ?? Mixin.GetTimestamp()),
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = (ModuleCharacteristics.DynamicBase | ModuleCharacteristics.NoSEH | ModuleCharacteristics.NXCompat | ModuleCharacteristics.TerminalServerAware)
            };

            if (parameters.AssemblyResolver != null)
            {
                moduleDefinition.assembly_resolver = Disposable.NotOwned(parameters.AssemblyResolver);
            }
            if (parameters.MetadataResolver != null)
            {
                moduleDefinition.metadata_resolver = parameters.MetadataResolver;
            }
            if (parameters.MetadataImporterProvider != null)
            {
                moduleDefinition.metadata_importer = parameters.MetadataImporterProvider.GetMetadataImporter(moduleDefinition);
            }
            if (parameters.ReflectionImporterProvider != null)
            {
                moduleDefinition.reflection_importer = parameters.ReflectionImporterProvider.GetReflectionImporter(moduleDefinition);
            }
            if (parameters.Kind != ModuleKind.NetModule)
            {
                AssemblyDefinition assemblyDefinition = moduleDefinition.assembly = new AssemblyDefinition();
                moduleDefinition.assembly.Name = CreateAssemblyName(name);
                assemblyDefinition.main_module = moduleDefinition;
            }
            moduleDefinition.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));
            return(moduleDefinition);
        }
Exemple #12
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var module = new ModuleDefinition(GlobalAssemblyResolver.Instance)
            {
                Name            = name,
                kind            = parameters.Kind,
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = (ModuleCharacteristics)0x8540,
            };

            if (parameters.AssemblyResolver != null)
            {
                module.assembly_resolver = parameters.AssemblyResolver;
            }

            if (parameters.MetadataResolver != null)
            {
                module.metadata_resolver = parameters.MetadataResolver;
            }

            if (parameters.Kind != ModuleKind.NetModule)
            {
                var assembly = new AssemblyDefinition();
                module.assembly      = assembly;
                module.assembly.Name = CreateAssemblyName(name);
                assembly.main_module = module;
            }

            module.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));

            return(module);
        }
Exemple #13
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var assembly = SearchDirectory(name, directories, parameters);

            if (assembly != null)
            {
                return(assembly);
            }

            if (name.IsRetargetable)
            {
                // if the reference is retargetable, zero it
                name = new AssemblyNameReference(name.Name, Mixin.ZeroVersion)
                {
                    PublicKeyToken = Empty <byte> .Array,
                };
            }

#if NET_CORE
            assembly = SearchTrustedPlatformAssemblies(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#else
            var framework_dir  = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);
            var framework_dirs = on_mono
                                ? new [] { framework_dir, Path.Combine(framework_dir, "Facades") }
                                : new [] { framework_dir };

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, framework_dirs, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            if (name.Name == "mscorlib")
            {
                assembly = GetCorlib(name, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            assembly = GetAssemblyInGac(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }

            assembly = SearchDirectory(name, framework_dirs, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#endif
            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new AssemblyResolutionException(name);
        }
Exemple #14
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);
            AssemblyDefinition assemblyDefinition = SearchDirectory(name, directories, parameters);

            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            if (name.IsRetargetable)
            {
                name = new AssemblyNameReference(name.Name, Mixin.ZeroVersion)
                {
                    PublicKeyToken = Empty <byte> .Array
                };
            }
            string directoryName = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            string[] array = on_mono ? new string[2]
            {
                directoryName,
                Path.Combine(directoryName, "Facades")
            } : new string[1]
            {
                directoryName
            };
            if (IsZero(name.Version))
            {
                assemblyDefinition = SearchDirectory(name, array, parameters);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            if (name.Name == "mscorlib")
            {
                assemblyDefinition = GetCorlib(name, parameters);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            assemblyDefinition = GetAssemblyInGac(name, parameters);
            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            assemblyDefinition = SearchDirectory(name, array, parameters);
            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            if (this.ResolveFailure != null)
            {
                assemblyDefinition = this.ResolveFailure(this, name);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            throw new AssemblyResolutionException(name);
        }