Exemplo n.º 1
0
        public unsafe EmitResult Emit(
            Stream rebuildPeStream,
            Compilation rebuildCompilation,
            ImmutableArray <EmbeddedText> embeddedTexts,
            CancellationToken cancellationToken)
        {
            var peHeader       = OptionsReader.PeReader.PEHeaders.PEHeader !;
            var win32Resources = OptionsReader.PeReader.GetSectionData(peHeader.ResourceTableDirectory.RelativeVirtualAddress);

            using var win32ResourceStream = win32Resources.Pointer != null
                ? new UnmanagedMemoryStream(win32Resources.Pointer, win32Resources.Length)
                : null;

            var sourceLink = OptionsReader.GetSourceLinkUTF8();

            var debugEntryPoint = getDebugEntryPoint();

            var emitResult = rebuildCompilation.Emit(
                peStream: rebuildPeStream,
                pdbStream: null,
                xmlDocumentationStream: null,
                win32Resources: win32ResourceStream,
                useRawWin32Resources: true,
                manifestResources: OptionsReader.GetManifestResources(),
                options: new EmitOptions(
                    debugInformationFormat: DebugInformationFormat.Embedded,
                    highEntropyVirtualAddressSpace: (peHeader.DllCharacteristics & DllCharacteristics.HighEntropyVirtualAddressSpace) != 0,
                    subsystemVersion: SubsystemVersion.Create(peHeader.MajorSubsystemVersion, peHeader.MinorSubsystemVersion)),
                debugEntryPoint: debugEntryPoint,
                metadataPEStream: null,
                pdbOptionsBlobReader: OptionsReader.GetMetadataCompilationOptionsBlobReader(),
                sourceLinkStream: sourceLink != null ? new MemoryStream(sourceLink) : null,
                embeddedTexts: embeddedTexts,
                cancellationToken: cancellationToken);

            return(emitResult);

            IMethodSymbol?getDebugEntryPoint()
            {
                if (OptionsReader.GetMainMethodInfo() is (string mainTypeName, string mainMethodName))
                {
                    var typeSymbol = rebuildCompilation.GetTypeByMetadataName(mainTypeName);
                    if (typeSymbol is object)
                    {
                        var methodSymbols = typeSymbol
                                            .GetMembers(mainMethodName)
                                            .OfType <IMethodSymbol>();
                        return(methodSymbols.FirstOrDefault());
                    }
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        public unsafe EmitResult Emit(
            Stream rebuildPeStream,
            Stream?rebuildPdbStream,
            Compilation rebuildCompilation,
            ImmutableArray <EmbeddedText> embeddedTexts,
            CancellationToken cancellationToken)
        {
            var peHeader       = OptionsReader.PeReader.PEHeaders.PEHeader !;
            var win32Resources = OptionsReader.PeReader.GetSectionData(peHeader.ResourceTableDirectory.RelativeVirtualAddress);

            using var win32ResourceStream = win32Resources.Pointer != null
                ? new UnmanagedMemoryStream(win32Resources.Pointer, win32Resources.Length)
                : null;

            var sourceLink = OptionsReader.GetSourceLinkUTF8();

            var    debugEntryPoint = getDebugEntryPoint();
            string?pdbFilePath;
            DebugInformationFormat debugInformationFormat;

            if (OptionsReader.HasEmbeddedPdb)
            {
                if (rebuildPdbStream is object)
                {
                    throw new ArgumentException("PDB stream must be null because the compilation has an embedded PDB", nameof(rebuildPdbStream));
                }

                debugInformationFormat = DebugInformationFormat.Embedded;
                pdbFilePath            = null;
            }
            else
            {
                if (rebuildPdbStream is null)
                {
                    throw new ArgumentException("A non-null PDB stream must be provided because the compilation does not have an embedded PDB", nameof(rebuildPdbStream));
                }

                debugInformationFormat = DebugInformationFormat.PortablePdb;
                var codeViewEntry = OptionsReader.PeReader.ReadDebugDirectory().Single(entry => entry.Type == DebugDirectoryEntryType.CodeView);
                var codeView      = OptionsReader.PeReader.ReadCodeViewDebugDirectoryData(codeViewEntry);
                pdbFilePath = codeView.Path ?? throw new InvalidOperationException("Could not get PDB file path");
            }

            var emitResult = rebuildCompilation.Emit(
                peStream: rebuildPeStream,
                pdbStream: rebuildPdbStream,
                xmlDocumentationStream: null,
                win32Resources: win32ResourceStream,
                useRawWin32Resources: true,
                manifestResources: OptionsReader.GetManifestResources(),
                options: new EmitOptions(
                    debugInformationFormat: debugInformationFormat,
                    pdbFilePath: pdbFilePath,
                    highEntropyVirtualAddressSpace: (peHeader.DllCharacteristics & DllCharacteristics.HighEntropyVirtualAddressSpace) != 0,
                    subsystemVersion: SubsystemVersion.Create(peHeader.MajorSubsystemVersion, peHeader.MinorSubsystemVersion)),
                debugEntryPoint: debugEntryPoint,
                metadataPEStream: null,
                pdbOptionsBlobReader: OptionsReader.GetMetadataCompilationOptionsBlobReader(),
                sourceLinkStream: sourceLink != null ? new MemoryStream(sourceLink) : null,
                embeddedTexts: embeddedTexts,
                cancellationToken: cancellationToken);

            return(emitResult);

            IMethodSymbol?getDebugEntryPoint()
            {
                if (OptionsReader.GetMainMethodInfo() is (string mainTypeName, string mainMethodName))
                {
                    var typeSymbol = rebuildCompilation.GetTypeByMetadataName(mainTypeName);
                    if (typeSymbol is object)
                    {
                        var methodSymbols = typeSymbol
                                            .GetMembers(mainMethodName)
                                            .OfType <IMethodSymbol>();
                        return(methodSymbols.FirstOrDefault());
                    }
                }

                return(null);
            }
        }
Exemplo n.º 3
0
        public unsafe EmitResult Emit(
            Stream rebuildPeStream,
            Stream?rebuildPdbStream,
            Compilation rebuildCompilation,
            ImmutableArray <EmbeddedText> embeddedTexts,
            CancellationToken cancellationToken)
        {
            var peHeader       = OptionsReader.PeReader.PEHeaders.PEHeader !;
            var win32Resources = OptionsReader.PeReader.GetSectionData(peHeader.ResourceTableDirectory.RelativeVirtualAddress);

            using var win32ResourceStream = win32Resources.Pointer != null
                ? new UnmanagedMemoryStream(win32Resources.Pointer, win32Resources.Length)
                : null;

            var sourceLink = OptionsReader.GetSourceLinkUtf8();

            var    debugEntryPoint = getDebugEntryPoint();
            string?pdbFilePath;
            DebugInformationFormat debugInformationFormat;

            if (OptionsReader.HasEmbeddedPdb)
            {
                if (rebuildPdbStream is object)
                {
                    throw new ArgumentException(RebuildResources.PDB_stream_must_be_null_because_the_compilation_has_an_embedded_PDB, nameof(rebuildPdbStream));
                }

                debugInformationFormat = DebugInformationFormat.Embedded;
                pdbFilePath            = null;
            }
            else
            {
                if (rebuildPdbStream is null)
                {
                    throw new ArgumentException(RebuildResources.A_non_null_PDB_stream_must_be_provided_because_the_compilation_does_not_have_an_embedded_PDB, nameof(rebuildPdbStream));
                }

                debugInformationFormat = DebugInformationFormat.PortablePdb;
                var codeViewEntry = OptionsReader.PeReader.ReadDebugDirectory().Single(entry => entry.Type == DebugDirectoryEntryType.CodeView);
                var codeView      = OptionsReader.PeReader.ReadCodeViewDebugDirectoryData(codeViewEntry);
                pdbFilePath = codeView.Path ?? throw new InvalidOperationException(RebuildResources.Could_not_get_PDB_file_path);
            }

            var rebuildData = new RebuildData(
                OptionsReader.GetMetadataCompilationOptionsBlobReader(),
                getNonSourceFileDocumentNames(OptionsReader.PdbReader, OptionsReader.GetSourceFileCount()));
            var emitResult = rebuildCompilation.Emit(
                peStream: rebuildPeStream,
                pdbStream: rebuildPdbStream,
                xmlDocumentationStream: null,
                win32Resources: win32ResourceStream,
                manifestResources: OptionsReader.GetManifestResources(),
                options: new EmitOptions(
                    debugInformationFormat: debugInformationFormat,
                    pdbFilePath: pdbFilePath,
                    highEntropyVirtualAddressSpace: (peHeader.DllCharacteristics & DllCharacteristics.HighEntropyVirtualAddressSpace) != 0,
                    subsystemVersion: SubsystemVersion.Create(peHeader.MajorSubsystemVersion, peHeader.MinorSubsystemVersion)),
                debugEntryPoint: debugEntryPoint,
                metadataPEStream: null,
                rebuildData: rebuildData,
                sourceLinkStream: sourceLink != null ? new MemoryStream(sourceLink) : null,
                embeddedTexts: embeddedTexts,
                cancellationToken: cancellationToken);

            return(emitResult);