From c96d794f66e952f216e415d0305f2555a3bfada9 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 26 Aug 2019 07:21:36 -0600 Subject: [PATCH] ld: ensure that PE versions sync for internal and external linkage Previously users who opted into cgo might have received a bit of a behavior surprise when their mingw installation defaulted to a potentially older and different set of compatibility hacks. Since Go is explicitly targeting >=6.1 for internal linkage, propagate these changes to external linkage too. While we're at it, we move these values into constant variables so that they don't become out of sync and allow for easy updating as Go gradually drops compatibility for older operating systems. Change-Id: I41e654d135be6e3db9088e73efeb414933e36caa Reviewed-on: https://go-review.googlesource.com/c/go/+/191842 Reviewed-by: Alex Brainman --- src/cmd/link/internal/ld/lib.go | 5 +++++ src/cmd/link/internal/ld/pe.go | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 0564501b45..92dc9ba061 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1175,6 +1175,11 @@ func (ctxt *Link) hostlink() { // Mark as having awareness of terminal services, to avoid // ancient compatibility hacks. argv = append(argv, "-Wl,--tsaware") + + argv = append(argv, fmt.Sprintf("-Wl,--major-os-version=%d", PeMinimumTargetMajorVersion)) + argv = append(argv, fmt.Sprintf("-Wl,--minor-os-version=%d", PeMinimumTargetMinorVersion)) + argv = append(argv, fmt.Sprintf("-Wl,--major-subsystem-version=%d", PeMinimumTargetMajorVersion)) + argv = append(argv, fmt.Sprintf("-Wl,--minor-subsystem-version=%d", PeMinimumTargetMinorVersion)) case objabi.Haix: argv = append(argv, "-pthread") // prevent ld to reorder .text functions to keep the same diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index e3fe2f9ce5..ab51c874ce 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -128,6 +128,11 @@ const ( IMAGE_REL_BASED_HIGHLOW = 3 ) +const ( + PeMinimumTargetMajorVersion = 6 + PeMinimumTargetMinorVersion = 1 +) + // DOS stub that prints out // "This program cannot be run in DOS mode." var dosstub = []uint8{ @@ -830,18 +835,18 @@ func (f *peFile) writeOptionalHeader(ctxt *Link) { oh.SectionAlignment = uint32(PESECTALIGN) oh64.FileAlignment = uint32(PEFILEALIGN) oh.FileAlignment = uint32(PEFILEALIGN) - oh64.MajorOperatingSystemVersion = 6 - oh.MajorOperatingSystemVersion = 6 - oh64.MinorOperatingSystemVersion = 1 - oh.MinorOperatingSystemVersion = 1 + oh64.MajorOperatingSystemVersion = PeMinimumTargetMajorVersion + oh.MajorOperatingSystemVersion = PeMinimumTargetMajorVersion + oh64.MinorOperatingSystemVersion = PeMinimumTargetMinorVersion + oh.MinorOperatingSystemVersion = PeMinimumTargetMinorVersion oh64.MajorImageVersion = 1 oh.MajorImageVersion = 1 oh64.MinorImageVersion = 0 oh.MinorImageVersion = 0 - oh64.MajorSubsystemVersion = 6 - oh.MajorSubsystemVersion = 6 - oh64.MinorSubsystemVersion = 1 - oh.MinorSubsystemVersion = 1 + oh64.MajorSubsystemVersion = PeMinimumTargetMajorVersion + oh.MajorSubsystemVersion = PeMinimumTargetMajorVersion + oh64.MinorSubsystemVersion = PeMinimumTargetMinorVersion + oh.MinorSubsystemVersion = PeMinimumTargetMinorVersion oh64.SizeOfImage = f.nextSectOffset oh.SizeOfImage = f.nextSectOffset oh64.SizeOfHeaders = uint32(PEFILEHEADR) -- 2.50.0