Skip to content

Commit 7bfc824

Browse files
committed
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 <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent a2026cf commit 7bfc824

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
@@ -1367,7 +1367,12 @@ func (ctxt *Link) archive() {
13671367
exitIfErrors()
13681368

13691369
if *flagExtar == "" {
1370+
const printProgName = "--print-prog-name=ar"
1371+
cc := ctxt.extld()
13701372
*flagExtar = "ar"
1373+
if linkerFlagSupported(ctxt.Arch, cc[0], "", printProgName) {
1374+
*flagExtar = ctxt.findExtLinkTool("ar")
1375+
}
13711376
}
13721377

13731378
mayberemoveoutfile()
@@ -1997,22 +2002,8 @@ func (ctxt *Link) hostlink() {
19972002
uuidUpdated := false
19982003
if combineDwarf {
19992004
// Find "dsymutils" and "strip" tools using CC --print-prog-name.
2000-
var cc []string
2001-
cc = append(cc, ctxt.extld()...)
2002-
cc = append(cc, hostlinkArchArgs(ctxt.Arch)...)
2003-
cc = append(cc, "--print-prog-name", "dsymutil")
2004-
out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput()
2005-
if err != nil {
2006-
Exitf("%s: finding dsymutil failed: %v\n%s", os.Args[0], err, out)
2007-
}
2008-
dsymutilCmd := strings.TrimSuffix(string(out), "\n")
2009-
2010-
cc[len(cc)-1] = "strip"
2011-
out, err = exec.Command(cc[0], cc[1:]...).CombinedOutput()
2012-
if err != nil {
2013-
Exitf("%s: finding strip failed: %v\n%s", os.Args[0], err, out)
2014-
}
2015-
stripCmd := strings.TrimSuffix(string(out), "\n")
2005+
dsymutilCmd := ctxt.findExtLinkTool("dsymutil")
2006+
stripCmd := ctxt.findExtLinkTool("strip")
20162007

20172008
dsym := filepath.Join(*flagTmpdir, "go.dwarf")
20182009
cmd := exec.Command(dsymutilCmd, "-f", *flagOutfile, "-o", dsym)
@@ -2913,3 +2904,19 @@ func captureHostObj(h *Hostobj) {
29132904
fmt.Fprintf(os.Stderr, "link: info: captured host object %s to %s\n",
29142905
h.file, opath)
29152906
}
2907+
2908+
// findExtLinkTool invokes the external linker CC with --print-prog-name
2909+
// passing the name of the tool we're interested in, such as "strip",
2910+
// "ar", or "dsymutil", and returns the path passed back from the command.
2911+
func (ctxt *Link) findExtLinkTool(toolname string) string {
2912+
var cc []string
2913+
cc = append(cc, ctxt.extld()...)
2914+
cc = append(cc, hostlinkArchArgs(ctxt.Arch)...)
2915+
cc = append(cc, "--print-prog-name", toolname)
2916+
out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput()
2917+
if err != nil {
2918+
Exitf("%s: finding %s failed: %v\n%s", os.Args[0], toolname, err, out)
2919+
}
2920+
cmdpath := strings.TrimSuffix(string(out), "\n")
2921+
return cmdpath
2922+
}

0 commit comments

Comments
 (0)