Skip to content

cmd/link: support LLVM/Clang for MSVC #54811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,18 @@ func (ctxt *Link) archive() {
}

func (ctxt *Link) hostlink() {
// Determine if we are using Clang for MSVC.
// Some args for the linker of Mingw can not be passed to the linker of MSVC,
// and some args for the linker of MSVC are defferent from that of Mingw.
extld := ctxt.extld()
name := extld[0]
usingClangForMSVC := false
if out, err := exec.Command(name, "--version").CombinedOutput(); err == nil {
if bytes.Contains(out, []byte("clang ")) && bytes.Contains(out, []byte("msvc")) {
usingClangForMSVC = true
}
}

if ctxt.LinkMode != LinkExternal || nerrors > 0 {
return
}
Expand Down Expand Up @@ -1344,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
Expand Down Expand Up @@ -1392,6 +1418,13 @@ func (ctxt *Link) hostlink() {
heon := "--high-entropy-va"
dboff := "--disable-dynamicbase"
heoff := "--disable-high-entropy-va"
// These flags for MSVC are defferent.
if usingClangForMSVC {
dbon = "/DYNAMICBASE"
heon = "/HIGHENTROPYVA"
dboff = "/DYNAMICBASE:NO"
heoff = "/HIGHENTROPYVA:NO"
}
if val {
dbopt = dbon
heopt = heon
Expand Down Expand Up @@ -1589,7 +1622,7 @@ func (ctxt *Link) hostlink() {
}

const compressDWARF = "-Wl,--compress-debug-sections=zlib"
if ctxt.compressDWARF && linkerFlagSupported(ctxt.Arch, argv[0], altLinker, compressDWARF) {
if !usingClangForMSVC && ctxt.compressDWARF && linkerFlagSupported(ctxt.Arch, argv[0], altLinker, compressDWARF) {
argv = append(argv, compressDWARF)
}

Expand Down Expand Up @@ -1720,7 +1753,7 @@ func (ctxt *Link) hostlink() {

// use gcc linker script to work around gcc bug
// (see https://golang.org/issue/20183 for details).
if !usingLLD {
if !usingLLD && !usingClangForMSVC {
p := writeGDBLinkerScript()
argv = append(argv, "-Wl,-T,"+p)
}
Expand All @@ -1729,9 +1762,13 @@ func (ctxt *Link) hostlink() {
argv = append(argv, "-lsynchronization")
}
}
// libmingw32 and libmingwex have some inter-dependencies,
// so must use linker groups.
argv = append(argv, "-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group")

// MSVC don't have these libs.
if !usingClangForMSVC {
// libmingw32 and libmingwex have some inter-dependencies,
// so must use linker groups.
argv = append(argv, "-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group")
}
argv = append(argv, peimporteddlls()...)
}

Expand Down