From 364c5796934fbd35900c2a81b39107edc5963674 Mon Sep 17 00:00:00 2001 From: Zhihao Lin Date: Thu, 1 Sep 2022 20:56:57 +0800 Subject: [PATCH] patch some flags for Clang/MSVC --- src/cmd/link/internal/ld/lib.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 658e8bcab41526..c3e8c20bc8f339 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1356,20 +1356,34 @@ func (ctxt *Link) hostlink() { case objabi.Hwindows: if windowsgui { argv = append(argv, "-mwindows") + if usingClangForMSVC { + argv = append(argv, "-Wl,/SUBSYSTEM:WINDOWS") + } } else { argv = append(argv, "-mconsole") + if usingClangForMSVC { + argv = append(argv, "-Wl,/SUBSYSTEM:CONSOLE") + } } - // Mark as having awareness of terminal services, to avoid - // ancient compatibility hacks. - argv = append(argv, "-Wl,--tsaware") + // The linker of MSVC have defferent flags from the linker of Mingw/GCC, + // and some flags for Mingw/GCC are not supported by MSVC. + if usingClangForMSVC { + argv = append(argv, "-Wl,/TSAWARE") + argv = append(argv, "-Wl,/NXCOMPAT") + } else { + // Mark as having awareness of terminal services, to avoid + // ancient compatibility hacks. + argv = append(argv, "-Wl,--tsaware") + + // Enable DEP + argv = append(argv, "-Wl,--nxcompat") - // Enable DEP - argv = append(argv, "-Wl,--nxcompat") + 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)) + } - 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