]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/buildid: skip over GNU build ID from buildid computation
authorCherry Mui <cherryyz@google.com>
Tue, 8 Oct 2024 21:46:22 +0000 (17:46 -0400)
committerCherry Mui <cherryyz@google.com>
Mon, 21 Oct 2024 17:56:51 +0000 (17:56 +0000)
This is similar to CL 618597, but for GNU build ID on ELF. This
makes it possible to enable "-B gobuildid" by default on ELF.

Updates #41004.
For #63934.

Change-Id: I4e663a27a2f7824bce994c783fe6d9ce8d1a395a
Reviewed-on: https://go-review.googlesource.com/c/go/+/618600
Reviewed-by: Than McIntosh <thanm@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/internal/buildid/rewrite.go

index 26720afef4c9dbe6c5c9f7f8341b13357c9b11e2..5300f7f9b883caba05d95f2db82186fd07ad2c0c 100644 (file)
@@ -9,6 +9,7 @@ import (
        "cmd/internal/codesign"
        imacho "cmd/internal/macho"
        "crypto/sha256"
+       "debug/elf"
        "debug/macho"
        "fmt"
        "io"
@@ -185,14 +186,26 @@ func findHostBuildID(r io.Reader) (offset int64, size int64, ok bool) {
        if !ok {
                return 0, 0, false
        }
-       // TODO: handle ELF GNU build ID.
-       f, err := macho.NewFile(ra)
+
+       ef, err := elf.NewFile(ra)
+       if err == nil {
+               // ELF file. Find GNU build ID section.
+               sect := ef.Section(".note.gnu.build-id")
+               if sect == nil {
+                       return 0, 0, false
+               }
+               // Skip over the 3-word note "header" and "GNU\x00".
+               return int64(sect.Offset + 16), int64(sect.Size - 16), true
+       }
+
+       mf, err := macho.NewFile(ra)
        if err != nil {
                return 0, 0, false
        }
 
-       reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), f.ByteOrder, imacho.FileHeaderSize(f))
-       for i := uint32(0); i < f.Ncmd; i++ {
+       // Mach-O file. Find LC_UUID load command.
+       reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), mf.ByteOrder, imacho.FileHeaderSize(mf))
+       for i := uint32(0); i < mf.Ncmd; i++ {
                cmd, err := reader.Next()
                if err != nil {
                        break