From: Than McIntosh Date: Tue, 28 Mar 2023 13:17:27 +0000 (-0400) Subject: cmd/link: use path from "cc --print-prog-name ar" for c-archive buildmode X-Git-Tag: go1.23rc1~29 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7bfc82429c4067664d9645c70323cd76ee0496c0;p=gostls13.git cmd/link: use path from "cc --print-prog-name ar" for c-archive buildmode [This is a roll-forward of CL 479775, which had to be rolled back due to bad interactions with the wrappers used by the ios-arm64-corellium builder. ios-arm64-corellium is no longer being maintained AFAICT, meaning that it should be ok to move ahead with this patch again]. When external linking with -buildmode=c-archive, the Go linker eventually invokes the "ar" tool to create the final archive library. Prior to this patch, if the '-extar' flag was not in use, we would just run "ar". This works well in most cases but breaks down if we're doing cross-compilation targeting Windows (macos system "ar" apparently doesn't create the windows symdef section correctly). To fix the problem, capture the output of "cc --print-prog-name ar" and invoke "ar" using the path returned by that command. Fixes #59221. Change-Id: Ie367541b23641266a6f48ac68adf971501bff9fb Reviewed-on: https://go-review.googlesource.com/c/go/+/592375 LUCI-TryBot-Result: Go LUCI Reviewed-by: Cherry Mui --- diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 755c889585..3bec04e0b8 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1367,7 +1367,12 @@ func (ctxt *Link) archive() { exitIfErrors() if *flagExtar == "" { + const printProgName = "--print-prog-name=ar" + cc := ctxt.extld() *flagExtar = "ar" + if linkerFlagSupported(ctxt.Arch, cc[0], "", printProgName) { + *flagExtar = ctxt.findExtLinkTool("ar") + } } mayberemoveoutfile() @@ -1997,22 +2002,8 @@ func (ctxt *Link) hostlink() { uuidUpdated := false if combineDwarf { // Find "dsymutils" and "strip" tools using CC --print-prog-name. - var cc []string - cc = append(cc, ctxt.extld()...) - cc = append(cc, hostlinkArchArgs(ctxt.Arch)...) - cc = append(cc, "--print-prog-name", "dsymutil") - out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput() - if err != nil { - Exitf("%s: finding dsymutil failed: %v\n%s", os.Args[0], err, out) - } - dsymutilCmd := strings.TrimSuffix(string(out), "\n") - - cc[len(cc)-1] = "strip" - out, err = exec.Command(cc[0], cc[1:]...).CombinedOutput() - if err != nil { - Exitf("%s: finding strip failed: %v\n%s", os.Args[0], err, out) - } - stripCmd := strings.TrimSuffix(string(out), "\n") + dsymutilCmd := ctxt.findExtLinkTool("dsymutil") + stripCmd := ctxt.findExtLinkTool("strip") dsym := filepath.Join(*flagTmpdir, "go.dwarf") cmd := exec.Command(dsymutilCmd, "-f", *flagOutfile, "-o", dsym) @@ -2913,3 +2904,19 @@ func captureHostObj(h *Hostobj) { fmt.Fprintf(os.Stderr, "link: info: captured host object %s to %s\n", h.file, opath) } + +// findExtLinkTool invokes the external linker CC with --print-prog-name +// passing the name of the tool we're interested in, such as "strip", +// "ar", or "dsymutil", and returns the path passed back from the command. +func (ctxt *Link) findExtLinkTool(toolname string) string { + var cc []string + cc = append(cc, ctxt.extld()...) + cc = append(cc, hostlinkArchArgs(ctxt.Arch)...) + cc = append(cc, "--print-prog-name", toolname) + out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput() + if err != nil { + Exitf("%s: finding %s failed: %v\n%s", os.Args[0], toolname, err, out) + } + cmdpath := strings.TrimSuffix(string(out), "\n") + return cmdpath +}