From 5d54e73135acccef16c5100f8f68331af759a87a Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Fri, 20 Dec 2019 13:25:36 -0800 Subject: [PATCH] Allow R2RReader to reuse existing MetadataReader/PEReader objects (#1077) --- .../ILCompiler.Reflection.ReadyToRun/R2RReader.cs | 49 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/coreclr/src/tools/crossgen2/ILCompiler.Reflection.ReadyToRun/R2RReader.cs b/src/coreclr/src/tools/crossgen2/ILCompiler.Reflection.ReadyToRun/R2RReader.cs index d7ae54d..2d2f882 100644 --- a/src/coreclr/src/tools/crossgen2/ILCompiler.Reflection.ReadyToRun/R2RReader.cs +++ b/src/coreclr/src/tools/crossgen2/ILCompiler.Reflection.ReadyToRun/R2RReader.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Reflection.Metadata; using System.Reflection.Metadata.Ecma335; @@ -124,6 +125,20 @@ namespace ILCompiler.Reflection.ReadyToRun { } + public EcmaMetadataReader(IAssemblyResolver assemblyResolver, MetadataReader metadata, PEReader peReader, string filename, List manifestReferenceAssemblies) + { + _assemblyResolver = assemblyResolver; + _assemblyCache = new Dictionary(); + MetadataReader = metadata; + PEReader = peReader; + ImmutableArray content = peReader.GetEntireImage().GetContent(); + // TODO: Avoid copying + Image = new byte[content.Length]; + content.CopyTo(Image); + Filename = filename; + ManifestReferenceAssemblies = manifestReferenceAssemblies; + } + /// /// Open an MSIL binary and locate the metadata blob. /// @@ -228,42 +243,42 @@ namespace ILCompiler.Reflection.ReadyToRun /// /// The ReadyToRun header /// - public R2RHeader R2RHeader { get; } + public R2RHeader R2RHeader { get; private set; } /// /// The runtime functions and method signatures of each method /// - public IList R2RMethods { get; } + public IList R2RMethods { get; private set; } /// /// Parsed instance entrypoint table entries. /// - public IList InstanceMethods { get; } + public IList InstanceMethods { get; private set; } /// /// The available types from READYTORUN_SECTION_AVAILABLE_TYPES /// - public IList AvailableTypes { get; } + public IList AvailableTypes { get; private set; } /// /// The compiler identifier string from READYTORUN_SECTION_COMPILER_IDENTIFIER /// - public string CompilerIdentifier { get; } + public string CompilerIdentifier { get; private set; } /// /// Exception lookup table is used to map runtime function addresses to EH clauses. /// - public EHLookupTable EHLookupTable { get; } + public EHLookupTable EHLookupTable { get; private set; } /// /// List of import sections present in the R2R executable. /// - public IList ImportSections { get; } + public IList ImportSections { get; private set; } /// /// Map from import cell addresses to their symbolic names. /// - public Dictionary ImportCellNames { get; } + public Dictionary ImportCellNames { get; private set; } private Dictionary _runtimeFunctionToDebugInfo = new Dictionary(); @@ -274,9 +289,25 @@ namespace ILCompiler.Reflection.ReadyToRun /// /// PE image /// The Cor header flag must be ILLibrary - public unsafe R2RReader(IAssemblyResolver assemblyResolver, string filename) + public R2RReader(IAssemblyResolver assemblyResolver, MetadataReader metadata, PEReader peReader, string filename) + : base(assemblyResolver, metadata, peReader, filename, new List()) + { + Initialize(); + } + + /// + /// Initializes the fields of the R2RHeader and R2RMethods + /// + /// PE image + /// The Cor header flag must be ILLibrary + public R2RReader(IAssemblyResolver assemblyResolver, string filename) : base(assemblyResolver, filename, new List()) { + Initialize(); + } + + private unsafe void Initialize() + { IsR2R = ((PEReader.PEHeaders.CorHeader.Flags & CorFlags.ILLibrary) != 0); if (!IsR2R) { -- 2.7.4