/// <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 {})
 /// <summary>
 /// Creates a method semantics object from a method semantics row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the semantics for.</param>
 /// <param name="row">The metadata table row to base the semantics on.</param>
 public SerializedMethodSemantics(ModuleReaderContext context, MetadataToken token, in MethodSemanticsRow row)
Пример #3
0
 /// <summary>
 /// Creates a stand-alone signature from a stand-alone sig metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the signature for.</param>
 /// <param name="row">The metadata table row to base the signature on.</param>
 public SerializedStandAloneSignature(
     ModuleReaderContext context,
     MetadataToken token,
     in StandAloneSignatureRow row)
 /// <summary>
 /// Creates an assembly definition from an assembly metadata row.
 /// </summary>
 /// <param name="context">The reader context.</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>
 public SerializedAssemblyDefinition(
     ModuleReaderContext context,
     MetadataToken token,
     in AssemblyDefinitionRow row,
Пример #5
0
 internal CachedSerializedMemberFactory(ModuleReaderContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
Пример #6
0
 /// <summary>
 /// Creates a constant from a constant metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the constant for.</param>
 /// <param name="row">The metadata table row to base the constant on.</param>
 public SerializedConstant(
     ModuleReaderContext context,
     MetadataToken token,
     in ConstantRow row)
Пример #7
0
 /// <summary>
 /// Creates a exported type from a exported type metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the exported type for.</param>
 /// <param name="row">The metadata table row to base the exported type on.</param>
 public SerializedExportedType(ModuleReaderContext context, MetadataToken token, in ExportedTypeRow row)
 /// <summary>
 /// Creates a property definition from a property metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the property for.</param>
 /// <param name="row">The metadata table row to base the property definition on.</param>
 public SerializedPropertyDefinition(ModuleReaderContext context, MetadataToken token, in PropertyDefinitionRow row)
Пример #9
0
 /// <summary>
 /// Creates a member reference from a member reference metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the reference for.</param>
 /// <param name="row">The metadata table row to base the member reference on.</param>
 public SerializedMemberReference(
     ModuleReaderContext context,
     MetadataToken token,
     in MemberReferenceRow row)
 /// <summary>
 /// Creates a security declaration from a declaration metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the declaration for.</param>
 /// <param name="row">The metadata table row to base the security declaration on.</param>
 public SerializedSecurityDeclaration(
     ModuleReaderContext context,
     MetadataToken token,
     in SecurityDeclarationRow row)
Пример #11
0
 /// <inheritdoc />
 public MethodBody ReadMethodBody(ModuleReaderContext context, MethodDefinition owner, in MethodDefinitionRow row)
Пример #12
0
 /// <summary>
 /// Creates a class layout from a class layout metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the class layout for.</param>
 /// <param name="row">The metadata table row to base the class layout on.</param>
 public SerializedClassLayout(
     ModuleReaderContext context,
     MetadataToken token,
     in ClassLayoutRow row)
 /// <summary>
 /// Creates an assembly reference from an assembly reference metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the reference for.</param>
 /// <param name="row">The metadata table row to base the assembly reference on.</param>
 public SerializedAssemblyReference(ModuleReaderContext context, MetadataToken token, in AssemblyReferenceRow row)
 /// <summary>
 /// Creates a member reference from an implementation map metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the mapping for.</param>
 /// <param name="row">The metadata table row to base the mapping on.</param>
 public SerializedImplementationMap(
     ModuleReaderContext context,
     MetadataToken token,
     in ImplementationMapRow row)
Пример #15
0
 /// <summary>
 /// Creates a generic parameter constraint from a generic parameter constraint metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the constraint for.</param>
 /// <param name="row">The metadata table row to base the constraint on.</param>
 public SerializedGenericParameterConstraint(
     ModuleReaderContext context,
     MetadataToken token,
     in GenericParameterConstraintRow row)
 /// <summary>
 /// Creates a manifest resource from a manifest resource metadata row.
 /// </summary>
 /// <param name="context">The module that contains the resource.</param>
 /// <param name="token">The token to initialize the resource for.</param>
 /// <param name="row">The metadata table row to base the resource \on.</param>
 public SerializedManifestResource(ModuleReaderContext context, MetadataToken token, in ManifestResourceRow row)
 /// <summary>
 /// Creates a interface implementation from an interface implementation metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the interface implementation for.</param>
 /// <param name="row">The metadata table row to base the interface implementation on.</param>
 public SerializedInterfaceImplementation(
     ModuleReaderContext context,
     MetadataToken token,
     in InterfaceImplementationRow row)
 /// <summary>
 /// Creates a parameter definition from a parameter metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the parameter for.</param>
 /// <param name="row">The metadata table row to base the parameter definition on.</param>
 public SerializedParameterDefinition(ModuleReaderContext context, MetadataToken token, in ParameterDefinitionRow row)
Пример #19
0
 /// <summary>
 /// Creates a type specification from a type metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the type for.</param>
 /// <param name="row">The metadata table row to base the type specification on.</param>
 public SerializedTypeSpecification(ModuleReaderContext context, MetadataToken token, in TypeSpecificationRow row)
Пример #20
0
 /// <summary>
 /// Creates a event definition from a event metadata row.
 /// </summary>
 /// <param name="context">The reader context.</param>
 /// <param name="token">The token to initialize the event for.</param>
 /// <param name="row">The metadata table row to base the event definition on.</param>
 public SerializedEventDefinition(ModuleReaderContext context, MetadataToken token, in EventDefinitionRow row)
 /// <summary>
 /// Creates a custom attribute from a custom attribute metadata row.
 /// </summary>
 /// <param name="context">The reader context..</param>
 /// <param name="token">The token to initialize the custom attribute for.</param>
 /// <param name="row">The metadata table row to base the custom attribute on.</param>
 public SerializedCustomAttribute(ModuleReaderContext context, MetadataToken token, in CustomAttributeRow row)
Пример #22
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);
        }