/// <summary>
        /// Interprets a PE image as a .NET module.
        /// </summary>
        /// <param name="peImage">The image to interpret as a .NET module.</param>
        /// <param name="readerParameters">The parameters to use while reading the module.</param>
        public SerializedModuleDefinition(IPEImage peImage, ModuleReaderParameters readerParameters)
            : base(new MetadataToken(TableIndex.Module, 1))
        {
            if (peImage is null)
            {
                throw new ArgumentNullException(nameof(peImage));
            }
            if (readerParameters is null)
            {
                throw new ArgumentNullException(nameof(readerParameters));
            }

            var metadata = peImage.DotNetDirectory?.Metadata;

            if (metadata is null)
            {
                throw new BadImageFormatException("Input PE image does not contain a .NET metadata directory.");
            }

            var tablesStream = metadata.GetStream <TablesStream>();

            if (tablesStream is null)
            {
                throw new BadImageFormatException(".NET metadata directory does not define a tables stream.");
            }

            var moduleTable = tablesStream.GetTable <ModuleDefinitionRow>(TableIndex.Module);

            if (!moduleTable.TryGetByRid(1, out _row))
            {
                throw new BadImageFormatException("Module definition table does not contain any rows.");
            }

            // Store parameters in fields.
            ReaderContext = new ModuleReaderContext(peImage, this, readerParameters);

            // Copy over PE header fields.
            FilePath            = peImage.FilePath;
            MachineType         = peImage.MachineType;
            FileCharacteristics = peImage.Characteristics;
            PEKind             = peImage.PEKind;
            SubSystem          = peImage.SubSystem;
            DllCharacteristics = peImage.DllCharacteristics;
            TimeDateStamp      = peImage.TimeDateStamp;

            // Copy over "simple" columns.
            Generation = _row.Generation;
            Attributes = peImage.DotNetDirectory.Flags;

            // Initialize member factory.
            _memberFactory = new CachedSerializedMemberFactory(ReaderContext);

            // Find assembly definition and corlib assembly.
            Assembly = FindParentAssembly();
            var corLib = FindMostRecentCorLib();

            if (corLib is {})
Beispiel #2
0
 /// <summary>
 /// Creates a new net module resolver that searches for the module in a directory.
 /// </summary>
 /// <param name="directory">The path to the search directory.</param>
 /// <param name="readerParameters">The parameters to use for reading a module.</param>
 public DirectoryNetModuleResolver(string directory, ModuleReaderParameters readerParameters)
 {
     Directory        = directory ?? throw new ArgumentNullException(nameof(directory));
     ReaderParameters = readerParameters ?? throw new ArgumentNullException(nameof(readerParameters));
 }
Beispiel #3
0
 /// <summary>
 /// Creates a new instance of the <see cref="ModuleReaderContext"/> class.
 /// </summary>
 /// <param name="image">The original PE image to read from.</param>
 /// <param name="parentModule">The root module object.</param>
 /// <param name="parameters">The module reader parameters.</param>
 public ModuleReaderContext(IPEImage image, SerializedModuleDefinition parentModule, ModuleReaderParameters parameters)
 {
     Image        = image ?? throw new ArgumentNullException(nameof(image));
     ParentModule = parentModule ?? throw new ArgumentNullException(nameof(parentModule));
     Parameters   = parameters ?? throw new ArgumentNullException(nameof(parameters));
 }
Beispiel #4
0
        /// <summary>
        /// Interprets a PE image as a .NET module.
        /// </summary>
        /// <param name="peImage">The image to interpret as a .NET module.</param>
        /// <param name="readerParameters">The parameters to use while reading the module.</param>
        public SerializedModuleDefinition(IPEImage peImage, ModuleReaderParameters readerParameters)
            : base(new MetadataToken(TableIndex.Module, 1))
        {
            if (peImage is null)
            {
                throw new ArgumentNullException(nameof(peImage));
            }
            if (readerParameters is null)
            {
                throw new ArgumentNullException(nameof(readerParameters));
            }

            var metadata = peImage.DotNetDirectory?.Metadata;

            if (metadata is null)
            {
                throw new BadImageFormatException("Input PE image does not contain a .NET metadata directory.");
            }

            var tablesStream = metadata.GetStream <TablesStream>();

            if (tablesStream is null)
            {
                throw new BadImageFormatException(".NET metadata directory does not define a tables stream.");
            }

            var moduleTable = tablesStream.GetTable <ModuleDefinitionRow>(TableIndex.Module);

            if (!moduleTable.TryGetByRid(1, out _row))
            {
                throw new BadImageFormatException("Module definition table does not contain any rows.");
            }

            // Store parameters in fields.
            ReaderContext = new ModuleReaderContext(peImage, this, readerParameters);

            // Copy over PE header fields.
            FilePath            = peImage.FilePath;
            MachineType         = peImage.MachineType;
            FileCharacteristics = peImage.Characteristics;
            PEKind             = peImage.PEKind;
            SubSystem          = peImage.SubSystem;
            DllCharacteristics = peImage.DllCharacteristics;
            TimeDateStamp      = peImage.TimeDateStamp;

            // Copy over "simple" columns.
            Generation = _row.Generation;
            Attributes = peImage.DotNetDirectory.Flags;

            // Initialize member factory.
            _memberFactory = new CachedSerializedMemberFactory(ReaderContext);

            // Find assembly definition and corlib assembly.
            Assembly          = FindParentAssembly();
            CorLibTypeFactory = CreateCorLibTypeFactory();

            OriginalTargetRuntime = DetectTargetRuntime();
            MetadataResolver      = new DefaultMetadataResolver(CreateAssemblyResolver(
                                                                    readerParameters.PEReaderParameters.FileService));

            // Prepare lazy RID lists.
            _fieldLists = new LazyRidListRelation <TypeDefinitionRow>(metadata, TableIndex.TypeDef,
                                                                      (rid, _) => rid, tablesStream.GetFieldRange);
            _methodLists = new LazyRidListRelation <TypeDefinitionRow>(metadata, TableIndex.TypeDef,
                                                                       (rid, _) => rid, tablesStream.GetMethodRange);
            _paramLists = new LazyRidListRelation <MethodDefinitionRow>(metadata, TableIndex.Method,
                                                                        (rid, _) => rid, tablesStream.GetParameterRange);
            _propertyLists = new LazyRidListRelation <PropertyMapRow>(metadata, TableIndex.PropertyMap,
                                                                      (_, map) => map.Parent, tablesStream.GetPropertyRange);
            _eventLists = new LazyRidListRelation <EventMapRow>(metadata, TableIndex.EventMap,
                                                                (_, map) => map.Parent, tablesStream.GetEventRange);
        }