From 6d927dab139a8b27386dadee68f185b4180f6da3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Feb 2026 19:18:17 +0000 Subject: [PATCH 1/2] Make sure that segment vmsize is never smaller than segment file size --- .../tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs b/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs index df4546469bd01a..5370f89f0416cb 100644 --- a/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs +++ b/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs @@ -165,7 +165,7 @@ private void LayoutSections(bool recordFinalLayout, ref uint fileOffset, out uin section.SectionIndex = sectionIndex; sectionIndex++; - segmentSize = Math.Max(segmentSize, virtualAddress); + segmentSize = Math.Max(Math.Max(segmentSize, virtualAddress), segmentFileSize); if (recordFinalLayout) { From 6b21a40967681ec4cb792600d80e0f0943cb1928 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Feb 2026 22:26:29 +0000 Subject: [PATCH 2/2] Apply filip's fix --- .../tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs b/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs index 5370f89f0416cb..898c4b31fad652 100644 --- a/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs +++ b/src/coreclr/tools/Common/Compiler/ObjectWriter/MachObjectWriter.cs @@ -137,6 +137,7 @@ private void LayoutSections(bool recordFinalLayout, ref uint fileOffset, out uin { ulong virtualAddress = 0; byte sectionIndex = 1; + uint initialFileOffset = uint.MaxValue; segmentFileSize = 0; segmentSize = 0; @@ -149,9 +150,10 @@ private void LayoutSections(bool recordFinalLayout, ref uint fileOffset, out uin if (section.IsInFile) { + initialFileOffset = Math.Min(initialFileOffset, fileOffset); section.FileOffset = fileOffset; fileOffset += (uint)section.Size; - segmentFileSize = Math.Max(segmentFileSize, fileOffset); + segmentFileSize = Math.Max(segmentFileSize, fileOffset - initialFileOffset); } else { @@ -165,7 +167,7 @@ private void LayoutSections(bool recordFinalLayout, ref uint fileOffset, out uin section.SectionIndex = sectionIndex; sectionIndex++; - segmentSize = Math.Max(Math.Max(segmentSize, virtualAddress), segmentFileSize); + segmentSize = Math.Max(segmentSize, virtualAddress); if (recordFinalLayout) {