[go] cmd/link: use "ar" path derived from querying CC for -buildmode=c-archive

5 views
Skip to first unread message

Than McIntosh (Gerrit)

unread,
Mar 28, 2023, 1:52:24 PM3/28/23
to Than McIntosh, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gopher Robot, Cherry Mui, golang-co...@googlegroups.com

Than McIntosh submitted this change.

View Change



4 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: src/cmd/link/internal/ld/lib.go
Insertions: 1, Deletions: 0.

@@ -1318,6 +1318,7 @@
if *flagExtar == "" {
const printProgName = "--print-prog-name=ar"
cc := ctxt.extld()
+ *flagExtar = "ar"
if linkerFlagSupported(ctxt.Arch, cc[0], "", printProgName) {
*flagExtar = ctxt.findExtLinkTool("ar")
}
```

Approvals: Than McIntosh: Run TryBots Gopher Robot: TryBots succeeded Cherry Mui: Looks good to me, approved
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 <cher...@google.com>
Run-TryBot: Than McIntosh <th...@google.com>
TryBot-Result: Gopher Robot <go...@golang.org>
---
M src/cmd/link/internal/ld/lib.go
1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go
index 02c6908..6500c81 100644
--- a/src/cmd/link/internal/ld/lib.go
+++ b/src/cmd/link/internal/ld/lib.go
@@ -1316,7 +1316,12 @@
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()
@@ -1875,22 +1880,8 @@

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("dsymutil")

dsym := filepath.Join(*flagTmpdir, "go.dwarf")
if out, err := exec.Command(dsymutilCmd, "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
@@ -2763,3 +2754,19 @@
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
+}

To view, visit change 479775. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I9de66e98947c42633b16fde7208c2958d62fe7cc
Gerrit-Change-Number: 479775
Gerrit-PatchSet: 6
Gerrit-Owner: Than McIntosh <th...@google.com>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Than McIntosh <th...@google.com>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages