Example #1
0
        /// <summary>
        /// Creates an <see cref="AssemblyDef"/> instance from a stream
        /// </summary>
        /// <remarks>This will read all bytes from the stream and call <see cref="Load(byte[],ModuleContext)"/>.
        /// It's better to use one of the other Load() methods.</remarks>
        /// <param name="stream">The stream</param>
        /// <param name="options">Module creation options or <c>null</c></param>
        /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <c>null</c></exception>
        /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
        public static AssemblyDef Load(Stream stream, ModuleCreationOptions options)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            ModuleDef module = null;

            try
            {
                module = ModuleDefMD.Load(stream, options);
                var asm = module.Assembly;
                if (asm == null)
                {
                    throw new BadImageFormatException(string.Format("{0} is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString()));
                }
                return(asm);
            }
            catch
            {
                if (module != null)
                {
                    module.Dispose();
                }
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Creates an <see cref="AssemblyDef"/> instance from a memory location
        /// </summary>
        /// <param name="addr">Address of a .NET assembly</param>
        /// <param name="options">Module creation options or <c>null</c></param>
        /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="addr"/> is <c>null</c></exception>
        /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
        public static AssemblyDef Load(IntPtr addr, ModuleCreationOptions options)
        {
            if (addr == IntPtr.Zero)
            {
                throw new ArgumentNullException("addr");
            }
            ModuleDef module = null;

            try
            {
                module = ModuleDefMD.Load(addr, options);
                var asm = module.Assembly;
                if (asm == null)
                {
                    throw new BadImageFormatException(string.Format("{0} (addr: {1:X8}) is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString(), addr.ToInt64()));
                }
                return(asm);
            }
            catch
            {
                if (module != null)
                {
                    module.Dispose();
                }
                throw;
            }
        }
Example #3
0
		/// <summary>
		/// Creates an <see cref="AssemblyDef"/> instance from a file
		/// </summary>
		/// <param name="fileName">File name of an existing .NET assembly</param>
		/// <param name="options">Module creation options or <c>null</c></param>
		/// <returns>A new <see cref="AssemblyDef"/> instance</returns>
		/// <exception cref="ArgumentNullException">If <paramref name="fileName"/> is <c>null</c></exception>
		/// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
		public static AssemblyDef Load(string fileName, ModuleCreationOptions options) {
			if (fileName == null)
				throw new ArgumentNullException("fileName");
			ModuleDef module = null;
			try {
				module = ModuleDefMD.Load(fileName, options);
				var asm = module.Assembly;
				if (asm == null)
					throw new BadImageFormatException(string.Format("{0} is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", fileName));
				return asm;
			}
			catch {
				if (module != null)
					module.Dispose();
				throw;
			}
		}
Example #4
0
		/// <summary>
		/// Creates an <see cref="AssemblyDef"/> instance from a byte[]
		/// </summary>
		/// <param name="data">Contents of a .NET assembly</param>
		/// <param name="options">Module creation options or <c>null</c></param>
		/// <returns>A new <see cref="AssemblyDef"/> instance</returns>
		/// <exception cref="ArgumentNullException">If <paramref name="data"/> is <c>null</c></exception>
		/// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
		public static AssemblyDef Load(byte[] data, ModuleCreationOptions options) {
			if (data == null)
				throw new ArgumentNullException("data");
			ModuleDef module = null;
			try {
				module = ModuleDefMD.Load(data, options);
				var asm = module.Assembly;
				if (asm == null)
					throw new BadImageFormatException(string.Format("{0} is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString()));
				return asm;
			}
			catch {
				if (module != null)
					module.Dispose();
				throw;
			}
		}
Example #5
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a memory location
 /// </summary>
 /// <param name="addr">Address of a .NET module/assembly</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <param name="imageLayout">Image layout of the file in memory</param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(IntPtr addr, ModuleCreationOptions options, ImageLayout imageLayout)
 {
     return Load(MetaDataCreator.Load(addr, imageLayout), options);
 }
Example #6
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance
 /// </summary>
 /// <param name="peImage">PE image</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(IPEImage peImage, ModuleCreationOptions options)
 {
     return Load(MetaDataCreator.Load(peImage), options);
 }
Example #7
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a memory location
 /// </summary>
 /// <param name="addr">Address of a .NET module/assembly</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(IntPtr addr, ModuleCreationOptions options)
 {
     return Load(MetaDataCreator.Load(addr), options);
 }
Example #8
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a reflection module
 /// </summary>
 /// <param name="mod">An existing reflection module</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <param name="imageLayout">Image layout of the module in memory</param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(System.Reflection.Module mod, ModuleCreationOptions options, ImageLayout imageLayout)
 {
     IntPtr addr = Marshal.GetHINSTANCE(mod);
     if (addr == new IntPtr(-1))
         throw new InvalidOperationException(string.Format("Module {0} has no HINSTANCE", mod));
     return Load(addr, options, imageLayout);
 }
Example #9
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a reflection module
 /// </summary>
 /// <param name="mod">An existing reflection module</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(System.Reflection.Module mod, ModuleCreationOptions options)
 {
     return Load(mod, options, GetImageLayout(mod));
 }
Example #10
0
		/// <summary>
		/// Creates a <see cref="ModuleDefMD"/> instance from a reflection module
		/// </summary>
		/// <param name="mod">An existing reflection module</param>
		/// <param name="options">Module creation options or <c>null</c></param>
		/// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
		public static ModuleDefMD Load(System.Reflection.Module mod, ModuleCreationOptions options) {
			return Load(mod, options, GetImageLayout(mod));
		}
Example #11
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a file
 /// </summary>
 /// <param name="fileName">File name of an existing .NET module/assembly</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(string fileName, ModuleCreationOptions options = null)
 {
     return Load(MetaDataCreator.Load(fileName), options);
 }
Example #12
0
        ISymbolReader CreateSymbolReader(ModuleCreationOptions options)
        {
            if (options.CreateSymbolReader != null) {
                var symReader = options.CreateSymbolReader(this);
                if (symReader != null)
                    return symReader;
            }

            if (options.PdbFileOrData != null) {
                var pdbFileName = options.PdbFileOrData as string;
                if (!string.IsNullOrEmpty(pdbFileName)) {
                    var symReader = SymbolReaderCreator.Create(options.PdbImplementation, metaData, pdbFileName);
                    if (symReader != null)
                        return symReader;
                }

                var pdbData = options.PdbFileOrData as byte[];
                if (pdbData != null)
                    return SymbolReaderCreator.Create(options.PdbImplementation, metaData, pdbData);

                var pdbStream = options.PdbFileOrData as IImageStream;
                if (pdbStream != null)
                    return SymbolReaderCreator.Create(options.PdbImplementation, metaData, pdbStream);
            }

            if (options.TryToLoadPdbFromDisk && !string.IsNullOrEmpty(location))
                return SymbolReaderCreator.Create(options.PdbImplementation, location);

            return null;
        }
Example #13
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a <see cref="MetaData"/>
 /// </summary>
 /// <param name="metaData">The metadata</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance that now owns <paramref name="metaData"/></returns>
 internal static ModuleDefMD Load(MetaData metaData, ModuleCreationOptions options)
 {
     return new ModuleDefMD(metaData, options);
 }
Example #14
0
        LoadedFile LoadAssembly(object state)
        {
            IPEImage peImage;

            if (OtherSettings.Instance.UseMemoryMappedIO)
                peImage = new PEImage(fileName);
            else
                peImage = new PEImage(File.ReadAllBytes(fileName));

            var dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
            bool isDotNet = dotNetDir.VirtualAddress != 0 && dotNetDir.Size >= 0x48;

            if (isDotNet) {
                try {
                    ModuleDef module;
                    var opts = new ModuleCreationOptions(CreateModuleContext());
                    if (OtherSettings.Instance.UseMemoryMappedIO)
                        module = ModuleDefMD.Load(peImage, opts);
                    else
                        module = ModuleDefMD.Load(peImage, opts);
                    return InitializeModule(module);
                }
                catch {
                }
            }

            return new LoadedFile(peImage, null);
        }
Example #15
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a stream
 /// </summary>
 /// <remarks>This will read all bytes from the stream and call <see cref="Load(byte[],ModuleContext)"/>.
 /// It's better to use one of the other Load() methods.</remarks>
 /// <param name="stream">The stream (owned by caller)</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <c>null</c></exception>
 public static ModuleDefMD Load(Stream stream, ModuleCreationOptions options)
 {
     if (stream == null)
         throw new ArgumentNullException("stream");
     if (stream.Length > int.MaxValue)
         throw new ArgumentException("Stream is too big");
     var data = new byte[(int)stream.Length];
     stream.Position = 0;
     if (stream.Read(data, 0, data.Length) != data.Length)
         throw new IOException("Could not read all bytes from the stream");
     return Load(data, options);
 }
Example #16
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="metaData">The metadata</param>
        /// <param name="options">Module creation options or <c>null</c></param>
        /// <exception cref="ArgumentNullException">If <paramref name="metaData"/> is <c>null</c></exception>
        ModuleDefMD(MetaData metaData, ModuleCreationOptions options)
            : base(null, 1)
        {
            #if DEBUG
            if (metaData == null)
                throw new ArgumentNullException("metaData");
            #endif
            if (options == null)
                options = ModuleCreationOptions.Default;
            this.metaData = metaData;
            this.context = options.Context;
            Initialize();
            InitializeFromRawRow();
            location = metaData.PEImage.FileName ?? string.Empty;

            this.Kind = GetKind();
            this.Characteristics = MetaData.PEImage.ImageNTHeaders.FileHeader.Characteristics;
            this.DllCharacteristics = MetaData.PEImage.ImageNTHeaders.OptionalHeader.DllCharacteristics;
            this.RuntimeVersion = MetaData.VersionString;
            this.Machine = MetaData.PEImage.ImageNTHeaders.FileHeader.Machine;
            this.Cor20HeaderFlags = MetaData.ImageCor20Header.Flags;
            this.Cor20HeaderRuntimeVersion = (uint)(MetaData.ImageCor20Header.MajorRuntimeVersion << 16) | MetaData.ImageCor20Header.MinorRuntimeVersion;
            this.TablesHeaderVersion = MetaData.TablesStream.Version;
            corLibTypes = new CorLibTypes(this, options.CorLibAssemblyRef ?? FindCorLibAssemblyRef() ?? CreateDefaultCorLibAssemblyRef());
            InitializePdb(options);
        }
Example #17
0
 void InitializePdb(ModuleCreationOptions options)
 {
     if (options == null)
         return;
     LoadPdb(CreateSymbolReader(options));
 }
Example #18
0
 /// <summary>
 /// Creates a <see cref="ModuleDefMD"/> instance from a byte[]
 /// </summary>
 /// <param name="data">Contents of a .NET module/assembly</param>
 /// <param name="options">Module creation options or <c>null</c></param>
 /// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
 public static ModuleDefMD Load(byte[] data, ModuleCreationOptions options = null)
 {
     return Load(MetaDataCreator.Load(data), options);
 }
Example #19
0
		/// <summary>
		/// Creates a <see cref="ModuleDefMD"/> instance from a reflection module
		/// </summary>
		/// <param name="mod">An existing reflection module</param>
		/// <param name="options">Module creation options or <c>null</c></param>
		/// <param name="imageLayout">Image layout of the module in memory</param>
		/// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
		public static ModuleDefMD Load(System.Reflection.Module mod, ModuleCreationOptions options, ImageLayout imageLayout) {
			IntPtr addr = Marshal.GetHINSTANCE(mod);
			if (addr == new IntPtr(-1))
				throw new InvalidOperationException(string.Format("Module {0} has no HINSTANCE", mod));
			return Load(addr, options, imageLayout);
		}