Skip to content

Commit 819e339

Browse files
rscgopherbot
authored andcommitted
cmd/internal/objabi: record GO$GOARCH setting in object header
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 <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent e0e1ce9 commit 819e339

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/cmd/internal/objabi/util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@ const (
2222
// or link object files that are incompatible with each other. This
2323
// string always starts with "go object ".
2424
func HeaderString() string {
25-
return fmt.Sprintf("go object %s %s %s X:%s\n", buildcfg.GOOS, buildcfg.GOARCH, buildcfg.Version, strings.Join(buildcfg.Experiment.Enabled(), ","))
25+
archExtra := ""
26+
if k, v := buildcfg.GOGOARCH(); k != "" && v != "" {
27+
archExtra = " " + k + "=" + v
28+
}
29+
return fmt.Sprintf("go object %s %s %s%s X:%s\n",
30+
buildcfg.GOOS, buildcfg.GOARCH,
31+
buildcfg.Version, archExtra,
32+
strings.Join(buildcfg.Experiment.Enabled(), ","))
2633
}

src/internal/buildcfg/cfg.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,28 @@ func experimentTags() []string {
172172
return list
173173
}
174174

175+
// GOGOARCH returns the name and value of the GO$GOARCH setting.
176+
// For example, if GOARCH is "amd64" it might return "GOAMD64", "v2".
177+
func GOGOARCH() (name, value string) {
178+
switch GOARCH {
179+
case "386":
180+
return "GO386", GO386
181+
case "amd64":
182+
return "GOAMD64", fmt.Sprintf("v%d", GOAMD64)
183+
case "arm":
184+
return "GOARM", fmt.Sprintf("%d", GOARM)
185+
case "mips", "mipsle":
186+
return "GOMIPS", GOMIPS
187+
case "mips64", "mips64le":
188+
return "GOMIPS64", GOMIPS64
189+
case "ppc64", "ppc64le":
190+
return "GOPPC64", fmt.Sprintf("power%d", GOPPC64)
191+
case "wasm":
192+
return "GOWASM", GOWASM.String()
193+
}
194+
return "", ""
195+
}
196+
175197
func gogoarchTags() []string {
176198
switch GOARCH {
177199
case "386":

0 commit comments

Comments
 (0)