From 740851bacafd8e47b9a6ce0cd8fa8e05506a7382 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Mon, 14 Dec 2020 13:03:06 -0500 Subject: [PATCH] cmd/link: avoid use of -T when linking with lld When doing external linking on Windows, auto-detect the linker flavor (bfd vs gold vs lld) and when linking with "lld", avoid the use of "-T" (linker script), since this option is not supported by lld. [Note: the Go linker currently employs -T to ensure proper placement of the .debug_gdb_scripts section, to work around issues in older versions of binutils; LLD recognizes this section and does place it properly]. Updates #39326. Change-Id: I3ea79cdceef2316bf86eccdb60188ac3655264ed Reviewed-on: https://go-review.googlesource.com/c/go/+/278932 Trust: Than McIntosh Run-TryBot: Than McIntosh TryBot-Result: Go Bot Reviewed-by: Jeremy Faller Reviewed-by: Cherry Zhang --- src/cmd/link/internal/ld/lib.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index f3c301cc9b..833b3eb9db 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1560,10 +1560,22 @@ func (ctxt *Link) hostlink() { checkStatic(p) } if ctxt.HeadType == objabi.Hwindows { + // Determine which linker we're using. Add in the extldflags in + // case used has specified "-fuse-ld=...". + cmd := exec.Command(*flagExtld, *flagExtldflags, "-Wl,--version") + usingLLD := false + if out, err := cmd.CombinedOutput(); err == nil { + if bytes.Contains(out, []byte("LLD ")) { + usingLLD = true + } + } + // use gcc linker script to work around gcc bug // (see https://golang.org/issue/20183 for details). - p := writeGDBLinkerScript() - argv = append(argv, "-Wl,-T,"+p) + if !usingLLD { + p := writeGDBLinkerScript() + argv = append(argv, "-Wl,-T,"+p) + } // libmingw32 and libmingwex have some inter-dependencies, // so must use linker groups. argv = append(argv, "-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group")