Skip to content

Commit a49faea

Browse files
committed
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
1 parent 5fe3f0a commit a49faea

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-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

+48
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,50 @@ 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", "-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+
goBuildID, err := buildid.ReadFile(outFile)
228+
if err != nil {
229+
t.Fatalf("can't read Go build ID")
230+
}
231+
232+
// The linker does not yet know about the content ID, which is computed
233+
// at a later stage, and instead reuses the action ID as content ID. We
234+
// thus need to adapt the build ID to be `actionID/.../actionID` instead
235+
// of `actionID/.../contentID`.
236+
actionID := goBuildID[:strings.Index(goBuildID, "/")]
237+
goBuildID = goBuildID[:strings.LastIndex(goBuildID, "/")] + "/" + actionID
238+
hashedGoBuildID := notsha256.Sum256([]byte(goBuildID))
239+
240+
gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
241+
if err != nil || gnuBuildID == nil {
242+
t.Fatalf("can't read GNU build ID")
243+
}
244+
245+
if !bytes.Equal(gnuBuildID, hashedGoBuildID[:20]) {
246+
t.Fatalf("build id not matching")
247+
}
248+
}
249+
202250
func TestMergeNoteSections(t *testing.T) {
203251
testenv.MustHaveGoBuild(t)
204252
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
@@ -149,7 +149,7 @@ func Main(arch *sys.Arch, theArch Arch) {
149149
flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
150150
flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
151151
flag.BoolVar(&ctxt.compressDWARF, "compressdwarf", true, "compress DWARF if possible")
152-
objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
152+
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)
153153
objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
154154
objabi.AddVersionFlag() // -V
155155
objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })

0 commit comments

Comments
 (0)