diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs index 3454d4ebbd270..91143e55dc8b1 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs @@ -481,12 +481,21 @@ public IReadOnlyDictionary GetCustomMethodToRuntimeFu private bool TryLocateNativeReadyToRunHeader() { - PEExportTable exportTable = CompositeReader.GetExportTable(); - if (exportTable.TryGetValue("RTR_HEADER", out _readyToRunHeaderRVA)) + try { - _composite = true; - return true; + PEExportTable exportTable = CompositeReader.GetExportTable(); + if (exportTable.TryGetValue("RTR_HEADER", out _readyToRunHeaderRVA)) + { + _composite = true; + return true; + } } + catch (BadImageFormatException) + { + // MSIL assemblies with no ready-to-run payload typically have no export table + return false; + } + return false; } diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs index 938103f3fa6ca..96d440b3bc12d 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs @@ -458,13 +458,15 @@ protected void UpdateOffset(int offset) /// Signature offset within the PE file byte array public R2RSignatureDecoder(IR2RSignatureTypeProvider provider, TGenericContext context, MetadataReader metadataReader, ReadyToRunReader r2rReader, int offset) { - _metadataReader = metadataReader; - _outerReader = metadataReader; Context = context; _provider = provider; _image = r2rReader.Image; _originalOffset = _offset = offset; _contextReader = r2rReader; + MetadataReader moduleOverrideMetadataReader = TryGetModuleOverrideMetadataReader(); + _metadataReader = moduleOverrideMetadataReader ?? metadataReader; + _outerReader = moduleOverrideMetadataReader ?? metadataReader; + Reset(); } /// @@ -480,11 +482,27 @@ public R2RSignatureDecoder(IR2RSignatureTypeProvider @@ -1098,17 +1116,15 @@ private ReadyToRunSignature ParseSignature(StringBuilder builder) bool moduleOverride = (fixupType & (byte)ReadyToRunFixupKind.ModuleOverride) != 0; SignatureDecoder moduleDecoder = this; - // Check first byte for a module override being encoded + // Check first byte for a module override being encoded. The metadata reader for the module + // override is configured in the R2RSignatureDecoder constructor. if (moduleOverride) { fixupType &= ~(uint)ReadyToRunFixupKind.ModuleOverride; - int moduleIndex = (int)ReadUIntAndEmitInlineSignatureBinary(builder); - IAssemblyMetadata refAsmEcmaReader = _contextReader.OpenReferenceAssembly(moduleIndex); - moduleDecoder = new SignatureDecoder(Context.AssemblyResolver, Context.Options, refAsmEcmaReader.MetadataReader, _image, Offset, refAsmEcmaReader.MetadataReader, _contextReader); + ReadUIntAndEmitInlineSignatureBinary(builder); } - ReadyToRunSignature result = moduleDecoder.ParseSignature((ReadyToRunFixupKind)fixupType, builder); - UpdateOffset(moduleDecoder.Offset); + ReadyToRunSignature result = ParseSignature((ReadyToRunFixupKind)fixupType, builder); return result; }