Skip to content

Commit 427d1a2

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 00034fa commit 427d1a2

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/cmd/link/elf_test.go

+34-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package main
88

99
import (
10-
"bytes"
1110
"cmd/internal/buildid"
1211
"cmd/internal/hash"
1312
"cmd/link/internal/ld"
@@ -203,36 +202,51 @@ func TestMinusRSymsWithSameName(t *testing.T) {
203202
}
204203
}
205204

206-
func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) {
205+
func TestGNUBuildID(t *testing.T) {
207206
testenv.MustHaveGoBuild(t)
208207

209208
t.Parallel()
210209

211-
goFile := filepath.Join(t.TempDir(), "notes.go")
210+
tmpdir := t.TempDir()
211+
goFile := filepath.Join(tmpdir, "notes.go")
212212
if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
213213
t.Fatal(err)
214214
}
215-
outFile := filepath.Join(t.TempDir(), "notes.exe")
216-
goTool := testenv.GoToolPath(t)
217215

218-
cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", "-buildid 0x1234 -B gobuildid", goFile)
219-
cmd.Dir = t.TempDir()
216+
// Use a specific Go buildid for testing.
217+
const gobuildid = "testbuildid"
218+
h := hash.Sum32([]byte(gobuildid))
219+
gobuildidHash := string(h[:20])
220220

221-
out, err := cmd.CombinedOutput()
222-
if err != nil {
223-
t.Logf("%s", out)
224-
t.Fatal(err)
221+
tests := []struct{ name, ldflags, expect string }{
222+
{"default", "", gobuildidHash},
223+
{"gobuildid", "-B=gobuildid", gobuildidHash},
224+
{"specific", "-B=0x0123456789abcdef", "\x01\x23\x45\x67\x89\xab\xcd\xef"},
225+
{"none", "-B=none", ""},
225226
}
226-
227-
expectedGoBuildID := hash.Sum32([]byte("0x1234"))
228-
229-
gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
230-
if err != nil || gnuBuildID == nil {
231-
t.Fatalf("can't read GNU build ID")
227+
if testenv.HasCGO() {
228+
for _, test := range tests {
229+
t1 := test
230+
t1.name += "_external"
231+
t1.ldflags += " -linkmode=external"
232+
tests = append(tests, t1)
233+
}
232234
}
233-
234-
if !bytes.Equal(gnuBuildID, expectedGoBuildID[:20]) {
235-
t.Fatalf("build id not matching")
235+
for _, test := range tests {
236+
t.Run(test.name, func(t *testing.T) {
237+
exe := filepath.Join(tmpdir, test.name)
238+
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-buildid="+gobuildid+" "+test.ldflags, "-o", exe, goFile)
239+
if out, err := cmd.CombinedOutput(); err != nil {
240+
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
241+
}
242+
gnuBuildID, err := buildid.ReadELFNote(exe, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
243+
if err != nil {
244+
t.Fatalf("can't read GNU build ID")
245+
}
246+
if string(gnuBuildID) != test.expect {
247+
t.Errorf("build id mismatch: got %x, want %x", gnuBuildID, test.expect)
248+
}
249+
})
236250
}
237251
}
238252

src/cmd/link/internal/ld/lib.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,12 @@ func (ctxt *Link) hostlink() {
17011701
argv = append(argv, "-fuse-ld="+altLinker)
17021702
}
17031703

1704-
if ctxt.IsELF && len(buildinfo) > 0 {
1705-
argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
1704+
if ctxt.IsELF {
1705+
if len(buildinfo) > 0 {
1706+
argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
1707+
} else if *flagHostBuildid == "none" {
1708+
argv = append(argv, "-Wl,--build-id=none")
1709+
}
17061710
}
17071711

17081712
// On Windows, given -o foo, GCC will append ".exe" to produce

src/cmd/link/internal/ld/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func Main(arch *sys.Arch, theArch Arch) {
294294
*flagBuildid = "go-openbsd"
295295
}
296296

297-
if *flagHostBuildid == "" && *flagBuildid != "" && ctxt.IsDarwin() {
297+
if *flagHostBuildid == "" && *flagBuildid != "" {
298298
*flagHostBuildid = "gobuildid"
299299
}
300300
addbuildinfo(ctxt)

0 commit comments

Comments
 (0)