mirror of
https://github.com/golang/go
synced 2024-11-14 05:40:29 -07:00
cmd/link: on ELF, generate GNU build ID by default
On ELF, default to "-B gobuildid", so it generates GNU build ID based on Go buildid by default. Updates #41004. Fixes #63934. Fixes #68652. Change-Id: I5619dfaa4eeb6575c52922ae1de3430b46e31db6 Reviewed-on: https://go-review.googlesource.com/c/go/+/618601 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Than McIntosh <thanm@golang.org>
This commit is contained in:
parent
00034fa796
commit
427d1a23ef
@ -7,7 +7,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"cmd/internal/buildid"
|
"cmd/internal/buildid"
|
||||||
"cmd/internal/hash"
|
"cmd/internal/hash"
|
||||||
"cmd/link/internal/ld"
|
"cmd/link/internal/ld"
|
||||||
@ -203,36 +202,51 @@ func TestMinusRSymsWithSameName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) {
|
func TestGNUBuildID(t *testing.T) {
|
||||||
testenv.MustHaveGoBuild(t)
|
testenv.MustHaveGoBuild(t)
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
goFile := filepath.Join(t.TempDir(), "notes.go")
|
tmpdir := t.TempDir()
|
||||||
|
goFile := filepath.Join(tmpdir, "notes.go")
|
||||||
if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
|
if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
outFile := filepath.Join(t.TempDir(), "notes.exe")
|
|
||||||
goTool := testenv.GoToolPath(t)
|
|
||||||
|
|
||||||
cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", "-buildid 0x1234 -B gobuildid", goFile)
|
// Use a specific Go buildid for testing.
|
||||||
cmd.Dir = t.TempDir()
|
const gobuildid = "testbuildid"
|
||||||
|
h := hash.Sum32([]byte(gobuildid))
|
||||||
|
gobuildidHash := string(h[:20])
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
tests := []struct{ name, ldflags, expect string }{
|
||||||
if err != nil {
|
{"default", "", gobuildidHash},
|
||||||
t.Logf("%s", out)
|
{"gobuildid", "-B=gobuildid", gobuildidHash},
|
||||||
t.Fatal(err)
|
{"specific", "-B=0x0123456789abcdef", "\x01\x23\x45\x67\x89\xab\xcd\xef"},
|
||||||
|
{"none", "-B=none", ""},
|
||||||
}
|
}
|
||||||
|
if testenv.HasCGO() {
|
||||||
expectedGoBuildID := hash.Sum32([]byte("0x1234"))
|
for _, test := range tests {
|
||||||
|
t1 := test
|
||||||
gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
|
t1.name += "_external"
|
||||||
if err != nil || gnuBuildID == nil {
|
t1.ldflags += " -linkmode=external"
|
||||||
t.Fatalf("can't read GNU build ID")
|
tests = append(tests, t1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
for _, test := range tests {
|
||||||
if !bytes.Equal(gnuBuildID, expectedGoBuildID[:20]) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
t.Fatalf("build id not matching")
|
exe := filepath.Join(tmpdir, test.name)
|
||||||
|
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-buildid="+gobuildid+" "+test.ldflags, "-o", exe, goFile)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
|
||||||
|
}
|
||||||
|
gnuBuildID, err := buildid.ReadELFNote(exe, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("can't read GNU build ID")
|
||||||
|
}
|
||||||
|
if string(gnuBuildID) != test.expect {
|
||||||
|
t.Errorf("build id mismatch: got %x, want %x", gnuBuildID, test.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1701,8 +1701,12 @@ func (ctxt *Link) hostlink() {
|
|||||||
argv = append(argv, "-fuse-ld="+altLinker)
|
argv = append(argv, "-fuse-ld="+altLinker)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctxt.IsELF && len(buildinfo) > 0 {
|
if ctxt.IsELF {
|
||||||
argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
|
if len(buildinfo) > 0 {
|
||||||
|
argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
|
||||||
|
} else if *flagHostBuildid == "none" {
|
||||||
|
argv = append(argv, "-Wl,--build-id=none")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// On Windows, given -o foo, GCC will append ".exe" to produce
|
// On Windows, given -o foo, GCC will append ".exe" to produce
|
||||||
|
@ -294,7 +294,7 @@ func Main(arch *sys.Arch, theArch Arch) {
|
|||||||
*flagBuildid = "go-openbsd"
|
*flagBuildid = "go-openbsd"
|
||||||
}
|
}
|
||||||
|
|
||||||
if *flagHostBuildid == "" && *flagBuildid != "" && ctxt.IsDarwin() {
|
if *flagHostBuildid == "" && *flagBuildid != "" {
|
||||||
*flagHostBuildid = "gobuildid"
|
*flagHostBuildid = "gobuildid"
|
||||||
}
|
}
|
||||||
addbuildinfo(ctxt)
|
addbuildinfo(ctxt)
|
||||||
|
Loading…
Reference in New Issue
Block a user