Exemple #1
0
        /// <summary>
        /// Creates an assembly definition from an assembly metadata row.
        /// </summary>
        /// <param name="dotNetDirectory">The object providing access to the underlying metadata streams.</param>
        /// <param name="token">The token to initialize the assembly for.</param>
        /// <param name="row">The metadata table row to base the assembly definition on.</param>
        /// <param name="manifestModule">The instance containing the manifest module definition.</param>
        /// <param name="readParameters">The parameters to use for reading modules.</param>
        public SerializedAssemblyDefinition(IDotNetDirectory dotNetDirectory, MetadataToken token, AssemblyDefinitionRow row,
                                            SerializedModuleDefinition manifestModule, ModuleReadParameters readParameters)
            : base(token)
        {
            _dotNetDirectory = dotNetDirectory ?? throw new ArgumentNullException(nameof(dotNetDirectory));
            _row             = row;
            _manifestModule  = manifestModule ?? throw new ArgumentNullException(nameof(manifestModule));
            _readParameters  = readParameters ?? throw new ArgumentNullException(nameof(readParameters));
            _parentModule    = manifestModule;

            Attributes    = row.Attributes;
            Version       = new Version(row.MajorVersion, row.MinorVersion, row.BuildNumber, row.RevisionNumber);
            HashAlgorithm = row.HashAlgorithm;
        }
        /// <summary>
        /// Interprets a PE image as a .NET module.
        /// </summary>
        /// <param name="peImage">The image to interpret as a .NET module.</param>
        /// <param name="readParameters">The parameters to use while reading the module.</param>
        public SerializedModuleDefinition(IPEImage peImage, ModuleReadParameters readParameters)
            : base(new MetadataToken(TableIndex.Module, 1))
        {
            _peImage = peImage ?? throw new ArgumentNullException(nameof(peImage));

            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.
            ReadParameters = readParameters ?? throw new ArgumentNullException(nameof(readParameters));

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

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

            // Initialize member factory.
            _memberFactory = new CachedSerializedMemberFactory(metadata, this);

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

            if (corLib is {})
Exemple #3
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="readParameters">The parameters to use for reading a module.</param>
 public DirectoryNetModuleResolver(string directory, ModuleReadParameters readParameters)
 {
     Directory      = directory ?? throw new ArgumentNullException(nameof(directory));
     ReadParameters = readParameters;
 }