Skip to content

Commit 00034fa

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent c15e589 commit 00034fa

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/cmd/internal/buildid/rewrite.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"cmd/internal/codesign"
1010
imacho "cmd/internal/macho"
1111
"crypto/sha256"
12+
"debug/elf"
1213
"debug/macho"
1314
"fmt"
1415
"io"
@@ -185,14 +186,26 @@ func findHostBuildID(r io.Reader) (offset int64, size int64, ok bool) {
185186
if !ok {
186187
return 0, 0, false
187188
}
188-
// TODO: handle ELF GNU build ID.
189-
f, err := macho.NewFile(ra)
189+
190+
ef, err := elf.NewFile(ra)
191+
if err == nil {
192+
// ELF file. Find GNU build ID section.
193+
sect := ef.Section(".note.gnu.build-id")
194+
if sect == nil {
195+
return 0, 0, false
196+
}
197+
// Skip over the 3-word note "header" and "GNU\x00".
198+
return int64(sect.Offset + 16), int64(sect.Size - 16), true
199+
}
200+
201+
mf, err := macho.NewFile(ra)
190202
if err != nil {
191203
return 0, 0, false
192204
}
193205

194-
reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), f.ByteOrder, imacho.FileHeaderSize(f))
195-
for i := uint32(0); i < f.Ncmd; i++ {
206+
// Mach-O file. Find LC_UUID load command.
207+
reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), mf.ByteOrder, imacho.FileHeaderSize(mf))
208+
for i := uint32(0); i < mf.Ncmd; i++ {
196209
cmd, err := reader.Next()
197210
if err != nil {
198211
break

0 commit comments

Comments
 (0)