Skip to content

Commit 422f448

Browse files
committed
cmd/link: use path from "cc --print-prog-name ar" for c-archive buildmode
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: I9de66e98947c42633b16fde7208c2958d62fe7cc Reviewed-on: https://go-review.googlesource.com/c/go/+/479775 Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c02e1bf commit 422f448

File tree

1 file changed

+23
-16
lines changed
  • src/cmd/link/internal/ld

1 file changed

+23
-16
lines changed

src/cmd/link/internal/ld/lib.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,12 @@ func (ctxt *Link) archive() {
13161316
exitIfErrors()
13171317

13181318
if *flagExtar == "" {
1319+
const printProgName = "--print-prog-name=ar"
1320+
cc := ctxt.extld()
13191321
*flagExtar = "ar"
1322+
if linkerFlagSupported(ctxt.Arch, cc[0], "", printProgName) {
1323+
*flagExtar = ctxt.findExtLinkTool("ar")
1324+
}
13201325
}
13211326

13221327
mayberemoveoutfile()
@@ -1875,22 +1880,8 @@ func (ctxt *Link) hostlink() {
18751880

18761881
if combineDwarf {
18771882
// Find "dsymutils" and "strip" tools using CC --print-prog-name.
1878-
var cc []string
1879-
cc = append(cc, ctxt.extld()...)
1880-
cc = append(cc, hostlinkArchArgs(ctxt.Arch)...)
1881-
cc = append(cc, "--print-prog-name", "dsymutil")
1882-
out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput()
1883-
if err != nil {
1884-
Exitf("%s: finding dsymutil failed: %v\n%s", os.Args[0], err, out)
1885-
}
1886-
dsymutilCmd := strings.TrimSuffix(string(out), "\n")
1887-
1888-
cc[len(cc)-1] = "strip"
1889-
out, err = exec.Command(cc[0], cc[1:]...).CombinedOutput()
1890-
if err != nil {
1891-
Exitf("%s: finding strip failed: %v\n%s", os.Args[0], err, out)
1892-
}
1893-
stripCmd := strings.TrimSuffix(string(out), "\n")
1883+
dsymutilCmd := ctxt.findExtLinkTool("dsymutil")
1884+
stripCmd := ctxt.findExtLinkTool("dsymutil")
18941885

18951886
dsym := filepath.Join(*flagTmpdir, "go.dwarf")
18961887
if out, err := exec.Command(dsymutilCmd, "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
@@ -2763,3 +2754,19 @@ func captureHostObj(h *Hostobj) {
27632754
fmt.Fprintf(os.Stderr, "link: info: captured host object %s to %s\n",
27642755
h.file, opath)
27652756
}
2757+
2758+
// findExtLinkTool invokes the external linker CC with --print-prog-name
2759+
// passing the name of the tool we're interested in, such as "strip",
2760+
// "ar", or "dsymutil", and returns the path passed back from the command.
2761+
func (ctxt *Link) findExtLinkTool(toolname string) string {
2762+
var cc []string
2763+
cc = append(cc, ctxt.extld()...)
2764+
cc = append(cc, hostlinkArchArgs(ctxt.Arch)...)
2765+
cc = append(cc, "--print-prog-name", toolname)
2766+
out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput()
2767+
if err != nil {
2768+
Exitf("%s: finding %s failed: %v\n%s", os.Args[0], toolname, err, out)
2769+
}
2770+
cmdpath := strings.TrimSuffix(string(out), "\n")
2771+
return cmdpath
2772+
}

0 commit comments

Comments
 (0)