]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/objabi: record GO$GOARCH setting in object header
authorRuss Cox <rsc@golang.org>
Wed, 31 Aug 2022 18:37:11 +0000 (14:37 -0400)
committerGopher Robot <gobot@golang.org>
Fri, 16 Sep 2022 12:40:45 +0000 (12:40 +0000)
The object header string is meant to record the relevant toolchain
configuration, so that we don't import or link object files that are
incompatible with each other. One important part of compatibility
is the sub-architecture version (GOARM for GOARCH=arm, and so on).

Add the sub-architecture info to the object header line so that
binaries cannot be built that have inconsistent sub-architecture
configurations across the build.

This check is only important when the build system makes a mistake.
Builds using the go command don't make this kind of mistake anymore,
but we just debugged a difficult problem inside Google where a custom
build system had built part of a program with GOARM=5 and part of
a program with GOARM=7, resulting in corrupted execution when
signal-based preemption was attempted. Updating the check will avoid
this kind of problem in the future, in any custom build system, or if the
go command makes a mistake.

After this change:

% sed 3q pkg/darwin_amd64/runtime.a
!<arch>
__.PKGDEF       0           0     0     644     30525     `
go object darwin amd64 devel go1.20-102ebe10b7 Wed Aug 17 14:31:01 2022 -0400 GOAMD64=v1 X:regabiwrappers,regabiargs
%

Change-Id: I901e0758f1002dd2c58292dc65e2d06da86e4495
Reviewed-on: https://go-review.googlesource.com/c/go/+/427174
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/internal/objabi/util.go
src/internal/buildcfg/cfg.go

index c2f1b204b980f21595bd594ba9aed58ec6fbef8c..a3e1242f590580f3ae2c63dc4f9890432e8d2aef 100644 (file)
@@ -22,5 +22,12 @@ const (
 // or link object files that are incompatible with each other. This
 // string always starts with "go object ".
 func HeaderString() string {
-       return fmt.Sprintf("go object %s %s %s X:%s\n", buildcfg.GOOS, buildcfg.GOARCH, buildcfg.Version, strings.Join(buildcfg.Experiment.Enabled(), ","))
+       archExtra := ""
+       if k, v := buildcfg.GOGOARCH(); k != "" && v != "" {
+               archExtra = " " + k + "=" + v
+       }
+       return fmt.Sprintf("go object %s %s %s%s X:%s\n",
+               buildcfg.GOOS, buildcfg.GOARCH,
+               buildcfg.Version, archExtra,
+               strings.Join(buildcfg.Experiment.Enabled(), ","))
 }
index e8553e85008231fde04e5f7ba6d89d2582ba24a7..a0736aaf742cdd25c497e0b92b42133e17e7fa86 100644 (file)
@@ -172,6 +172,28 @@ func experimentTags() []string {
        return list
 }
 
+// GOGOARCH returns the name and value of the GO$GOARCH setting.
+// For example, if GOARCH is "amd64" it might return "GOAMD64", "v2".
+func GOGOARCH() (name, value string) {
+       switch GOARCH {
+       case "386":
+               return "GO386", GO386
+       case "amd64":
+               return "GOAMD64", fmt.Sprintf("v%d", GOAMD64)
+       case "arm":
+               return "GOARM", fmt.Sprintf("%d", GOARM)
+       case "mips", "mipsle":
+               return "GOMIPS", GOMIPS
+       case "mips64", "mips64le":
+               return "GOMIPS64", GOMIPS64
+       case "ppc64", "ppc64le":
+               return "GOPPC64", fmt.Sprintf("power%d", GOPPC64)
+       case "wasm":
+               return "GOWASM", GOWASM.String()
+       }
+       return "", ""
+}
+
 func gogoarchTags() []string {
        switch GOARCH {
        case "386":