diff --git a/src/DotNet/Writer/NativeModuleWriter.cs b/src/DotNet/Writer/NativeModuleWriter.cs index d02678733..04eb42f80 100644 --- a/src/DotNet/Writer/NativeModuleWriter.cs +++ b/src/DotNet/Writer/NativeModuleWriter.cs @@ -74,9 +74,11 @@ public sealed class NativeModuleWriter : ModuleWriterBase { List origSections; readonly struct ReusedChunkInfo { + public PESection Section { get; } public IReuseChunk Chunk { get; } public RVA RVA { get; } - public ReusedChunkInfo(IReuseChunk chunk, RVA rva) { + public ReusedChunkInfo(PESection section, IReuseChunk chunk, RVA rva) { + Section = section; Chunk = chunk; RVA = rva; } @@ -354,9 +356,7 @@ void ReuseIfPossible(PESection section, IReuseChunk chunk, RVA origRva, uint ori if (((uint)origRva & (requiredAlignment - 1)) != 0) return; - if (section.Remove(chunk) is null) - throw new InvalidOperationException(); - reusedChunks.Add(new ReusedChunkInfo(chunk, origRva)); + reusedChunks.Add(new ReusedChunkInfo(section,chunk, origRva)); } FileOffset GetNewFileOffset(RVA rva) { @@ -412,6 +412,15 @@ long WriteFile() { ReuseIfPossible(textSection, debugDirectory, debugDataDir.VirtualAddress, realDebugDirSize, DebugDirectory.DEFAULT_DEBUGDIRECTORY_ALIGNMENT); } + if (Overlaps()) + reusedChunks.Clear(); + else { + foreach (var info in reusedChunks) { + if (info.Section.Remove(info.Chunk) is null) + throw new InvalidOperationException(); + } + } + if (constants.IsEmpty) textSection.Remove(constants); if (netResources.IsEmpty) @@ -451,8 +460,6 @@ long WriteFile() { var offset = GetNewFileOffset(info.RVA); info.Chunk.SetOffset(offset, info.RVA); } - if (Overlaps()) - reusedChunks.Clear(); } metadata.UpdateMethodAndFieldRvas(); foreach (var section in origSections) { @@ -845,11 +852,10 @@ bool GetEntryPoint(out uint ep) { bool Overlaps() { for (int i = 0; i < reusedChunks.Count; i++) { - uint start = (uint)reusedChunks[i].Chunk.RVA; + uint start = (uint)reusedChunks[i].RVA; uint end = start + reusedChunks[i].Chunk.GetFileLength(); for (int j = 0; j < reusedChunks.Count; j++) { - var chunk = reusedChunks[j].Chunk; - if (i != j && start < (uint)chunk.RVA + chunk.GetFileLength() && end > (uint)chunk.RVA) + if (i != j && start < (uint)reusedChunks[j].RVA + reusedChunks[j].Chunk.GetFileLength() && end > (uint)reusedChunks[j].RVA) return true; } }