From 89ca1ce9a8fbb70800fe24cc13aa267c3c90aadb Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Wed, 7 Apr 2021 09:27:11 -0400 Subject: [PATCH] cmd/compile,cmd/internal/objabi: abstract out object header string Change-Id: Idbbb4cb7127b93afa34a8aa18bbdaad1f206ab6a Reviewed-on: https://go-review.googlesource.com/c/go/+/308090 Trust: Austin Clements Run-TryBot: Austin Clements TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/obj.go | 2 +- src/cmd/compile/internal/noder/import.go | 6 +++--- src/cmd/internal/objabi/util.go | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/cmd/compile/internal/gc/obj.go b/src/cmd/compile/internal/gc/obj.go index d18011a9e5..f87a3c6b48 100644 --- a/src/cmd/compile/internal/gc/obj.go +++ b/src/cmd/compile/internal/gc/obj.go @@ -70,7 +70,7 @@ func dumpobj1(outfile string, mode int) { } func printObjHeader(bout *bio.Writer) { - fmt.Fprintf(bout, "go object %s %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring()) + bout.WriteString(objabi.HeaderString()) if base.Flag.BuildID != "" { fmt.Fprintf(bout, "build id %q\n", base.Flag.BuildID) } diff --git a/src/cmd/compile/internal/noder/import.go b/src/cmd/compile/internal/noder/import.go index 89a2598833..e6e3fe1834 100644 --- a/src/cmd/compile/internal/noder/import.go +++ b/src/cmd/compile/internal/noder/import.go @@ -243,9 +243,9 @@ func importfile(decl *syntax.ImportDecl) *types.Pkg { base.Errorf("import %s: not a go object file: %s", file, p) base.ErrorExit() } - q := fmt.Sprintf("%s %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring()) - if p[10:] != q { - base.Errorf("import %s: object is [%s] expected [%s]", file, p[10:], q) + q := objabi.HeaderString() + if p != q { + base.Errorf("import %s: object is [%s] expected [%s]", file, p, q) base.ErrorExit() } diff --git a/src/cmd/internal/objabi/util.go b/src/cmd/internal/objabi/util.go index 76c56dab27..c2c05bd1b2 100644 --- a/src/cmd/internal/objabi/util.go +++ b/src/cmd/internal/objabi/util.go @@ -5,6 +5,7 @@ package objabi import ( + "fmt" "log" "os" "strings" @@ -121,3 +122,11 @@ func gowasm() (f gowasmFeatures) { func Getgoextlinkenabled() string { return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED) } + +// HeaderString returns the toolchain configuration string written in +// Go object headers. This string ensures we don't attempt to import +// 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 %s\n", GOOS, GOARCH, Version, Expstring()) +}