Skip to content

Commit 089cc68

Browse files
pks-tcherrymui
authored andcommitted
cmd/link: allow deriving GNU build ID from Go build ID ID
While it is possible to embed a GNU build ID into the linked executable by passing `-B 0xBUILDID` to the linker, the build ID will need to be precomputed by the build system somehow. This makes it unnecessarily complex to generate a deterministic build ID as it either requires the build system to hash all inputs manually or to build the binary twice, once to compute its hash and once with the GNU build ID derived from that hash. Despite being complex, it is also inefficient as it requires the build system to duplicate some of the work that the Go linker already performs anyway. Introduce a new argument "gobuildid" that can be passed to `-B` that causes the linker to automatically derive the GNU build ID from the Go build ID. Given that the Go build ID is deterministically computed from all of its inputs, the resulting GNU build ID should be deterministic in the same way, which is the desired behaviour. Furthermore, given that the `-B` flag currently requires a "0x" prefix for all values passed to it, using "gobuildid" as value is a backwards compatible change. An alternative would be to unconditionally calculate the GNU build ID unless otherwise specified. This would require some larger rework though because building the Go toolchain would not converge anymore due the GNU build ID changing on every stage, which in turn would cause the Go build ID to change as well. Fixes #41004 Change-Id: I707c5fc321749c00761643d6cc79d44bf2cd744d GitHub-Last-Rev: 5483305 GitHub-Pull-Request: #61469 Reviewed-on: https://go-review.googlesource.com/c/go/+/511475 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent d516aa6 commit 089cc68

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/cmd/link/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Flags:
1818
-B note
1919
Add an ELF_NT_GNU_BUILD_ID note when using ELF.
2020
The value should start with 0x and be an even number of hex digits.
21+
Alternatively, you can pass "gobuildid" in order to derive the
22+
GNU build ID from the Go build ID.
2123
-E entry
2224
Set entry symbol name.
2325
-H type

src/cmd/link/elf_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
package main
88

99
import (
10+
"bytes"
11+
"cmd/internal/buildid"
12+
"cmd/internal/notsha256"
13+
"cmd/link/internal/ld"
1014
"debug/elf"
1115
"fmt"
1216
"internal/platform"
@@ -199,6 +203,39 @@ func TestMinusRSymsWithSameName(t *testing.T) {
199203
}
200204
}
201205

206+
func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) {
207+
testenv.MustHaveGoBuild(t)
208+
209+
t.Parallel()
210+
211+
goFile := filepath.Join(t.TempDir(), "notes.go")
212+
if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
213+
t.Fatal(err)
214+
}
215+
outFile := filepath.Join(t.TempDir(), "notes.exe")
216+
goTool := testenv.GoToolPath(t)
217+
218+
cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", "-buildid 0x1234 -B gobuildid", goFile)
219+
cmd.Dir = t.TempDir()
220+
221+
out, err := cmd.CombinedOutput()
222+
if err != nil {
223+
t.Logf("%s", out)
224+
t.Fatal(err)
225+
}
226+
227+
expectedGoBuildID := notsha256.Sum256([]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")
232+
}
233+
234+
if !bytes.Equal(gnuBuildID, expectedGoBuildID[:20]) {
235+
t.Fatalf("build id not matching")
236+
}
237+
}
238+
202239
func TestMergeNoteSections(t *testing.T) {
203240
testenv.MustHaveGoBuild(t)
204241
expected := 1

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

+12
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,18 @@ func elfwritefreebsdsig(out *OutBuf) int {
806806
}
807807

808808
func addbuildinfo(val string) {
809+
if val == "gobuildid" {
810+
buildID := *flagBuildid
811+
if buildID == "" {
812+
Exitf("-B gobuildid requires a Go build ID supplied via -buildid")
813+
}
814+
815+
hashedBuildID := notsha256.Sum256([]byte(buildID))
816+
buildinfo = hashedBuildID[:20]
817+
818+
return
819+
}
820+
809821
if !strings.HasPrefix(val, "0x") {
810822
Exitf("-B argument must start with 0x: %s", val)
811823
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func Main(arch *sys.Arch, theArch Arch) {
190190
flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
191191
flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
192192
flag.BoolVar(&ctxt.compressDWARF, "compressdwarf", true, "compress DWARF if possible")
193-
objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
193+
objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF; use \"gobuildid\" to generate it from the Go build ID", addbuildinfo)
194194
objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
195195
objabi.AddVersionFlag() // -V
196196
objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })

0 commit comments

Comments
 (0)