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 <alex.brainman@gmail.com>
This commit is contained in:
Jason A. Donenfeld 2019-08-26 07:21:36 -06:00
parent 8fc35238a7
commit c96d794f66
2 changed files with 18 additions and 8 deletions

View file

@ -1175,6 +1175,11 @@ func (ctxt *Link) hostlink() {
// Mark as having awareness of terminal services, to avoid // Mark as having awareness of terminal services, to avoid
// ancient compatibility hacks. // ancient compatibility hacks.
argv = append(argv, "-Wl,--tsaware") 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: case objabi.Haix:
argv = append(argv, "-pthread") argv = append(argv, "-pthread")
// prevent ld to reorder .text functions to keep the same // prevent ld to reorder .text functions to keep the same

View file

@ -128,6 +128,11 @@ const (
IMAGE_REL_BASED_HIGHLOW = 3 IMAGE_REL_BASED_HIGHLOW = 3
) )
const (
PeMinimumTargetMajorVersion = 6
PeMinimumTargetMinorVersion = 1
)
// DOS stub that prints out // DOS stub that prints out
// "This program cannot be run in DOS mode." // "This program cannot be run in DOS mode."
var dosstub = []uint8{ var dosstub = []uint8{
@ -830,18 +835,18 @@ func (f *peFile) writeOptionalHeader(ctxt *Link) {
oh.SectionAlignment = uint32(PESECTALIGN) oh.SectionAlignment = uint32(PESECTALIGN)
oh64.FileAlignment = uint32(PEFILEALIGN) oh64.FileAlignment = uint32(PEFILEALIGN)
oh.FileAlignment = uint32(PEFILEALIGN) oh.FileAlignment = uint32(PEFILEALIGN)
oh64.MajorOperatingSystemVersion = 6 oh64.MajorOperatingSystemVersion = PeMinimumTargetMajorVersion
oh.MajorOperatingSystemVersion = 6 oh.MajorOperatingSystemVersion = PeMinimumTargetMajorVersion
oh64.MinorOperatingSystemVersion = 1 oh64.MinorOperatingSystemVersion = PeMinimumTargetMinorVersion
oh.MinorOperatingSystemVersion = 1 oh.MinorOperatingSystemVersion = PeMinimumTargetMinorVersion
oh64.MajorImageVersion = 1 oh64.MajorImageVersion = 1
oh.MajorImageVersion = 1 oh.MajorImageVersion = 1
oh64.MinorImageVersion = 0 oh64.MinorImageVersion = 0
oh.MinorImageVersion = 0 oh.MinorImageVersion = 0
oh64.MajorSubsystemVersion = 6 oh64.MajorSubsystemVersion = PeMinimumTargetMajorVersion
oh.MajorSubsystemVersion = 6 oh.MajorSubsystemVersion = PeMinimumTargetMajorVersion
oh64.MinorSubsystemVersion = 1 oh64.MinorSubsystemVersion = PeMinimumTargetMinorVersion
oh.MinorSubsystemVersion = 1 oh.MinorSubsystemVersion = PeMinimumTargetMinorVersion
oh64.SizeOfImage = f.nextSectOffset oh64.SizeOfImage = f.nextSectOffset
oh.SizeOfImage = f.nextSectOffset oh.SizeOfImage = f.nextSectOffset
oh64.SizeOfHeaders = uint32(PEFILEHEADR) oh64.SizeOfHeaders = uint32(PEFILEHEADR)