1
0
mirror of https://github.com/golang/go synced 2024-11-14 13:20:30 -07:00

cmd/internal/buildid: skip over GNU build ID from buildid computation

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>
This commit is contained in:
Cherry Mui 2024-10-08 17:46:22 -04:00
parent c15e589733
commit 00034fa796

View File

@ -9,6 +9,7 @@ import (
"cmd/internal/codesign" "cmd/internal/codesign"
imacho "cmd/internal/macho" imacho "cmd/internal/macho"
"crypto/sha256" "crypto/sha256"
"debug/elf"
"debug/macho" "debug/macho"
"fmt" "fmt"
"io" "io"
@ -185,14 +186,26 @@ func findHostBuildID(r io.Reader) (offset int64, size int64, ok bool) {
if !ok { if !ok {
return 0, 0, false 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 { if err != nil {
return 0, 0, false return 0, 0, false
} }
reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), f.ByteOrder, imacho.FileHeaderSize(f)) // Mach-O file. Find LC_UUID load command.
for i := uint32(0); i < f.Ncmd; i++ { 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() cmd, err := reader.Next()
if err != nil { if err != nil {
break break