Skip to content

Commit 61e1cae

Browse files
binarycrusaderrsc
authored andcommitted
[release-branch.go1.5] cmd/go: fix Go buildid reading on Solaris
TestNoteReading fails on Solaris with linkmode=external due to some assumptions made about how ELF .note sections are written by some linkers. On current versions of Solaris and older derivatives, SHF_ALLOC is intentionally ignored for .note sections unless the .note section is assigned to the text segment via a mapfile. Also, if .note sections are assigned to the text segment, no PT_NOTE program header will be created thwarting Go's attempts at attempting to quickly find the .note. Furthermore, Go assumes that the relevant note segment will be placed early in the file while the Solaris linker currently places the note segment last in the file, additionally thwarting Go's optimisation attempts that read only the first 16KB of the file to find the buildid. The fix is to detect when the note section is outside of the first 16KB of the file and then fallback to additionally reading that section of the file. This way, in future versions of Solaris when this linking behaviour is changed, the fast path will always succeed and we'll only be slower if it fails; likewise, any other linker that does this will also just work. Fixes #12178 Change-Id: I61c1dc3f744ae3ad63938386d2ace8a432c0efe1 Reviewed-on: https://go-review.googlesource.com/14210 Run-TryBot: Aram Hăvărneanu <[email protected]> Reviewed-by: Aram Hăvărneanu <[email protected]> Reviewed-on: https://go-review.googlesource.com/17142 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 497e4a8 commit 61e1cae

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/cmd/go/note.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func readELFNote(filename, name string, typ int32) ([]byte, error) {
7070

7171
var elfGoNote = []byte("Go\x00\x00")
7272

73-
// readELFGoBuildID the Go build ID string from an ELF binary.
74-
// The Go build ID is stored in a note described by an ELF PT_NOTE prog header.
75-
// The caller has already opened filename, to get f, and read the first 4 kB out, in data.
73+
// The Go build ID is stored in a note described by an ELF PT_NOTE prog
74+
// header. The caller has already opened filename, to get f, and read
75+
// at least 4 kB out, in data.
7676
func readELFGoBuildID(filename string, f *os.File, data []byte) (buildid string, err error) {
77-
// Assume the note content is in the first 4 kB, already read.
77+
// Assume the note content is in the data, already read.
7878
// Rewrite the ELF header to set shnum to 0, so that we can pass
7979
// the data to elf.NewFile and it will decode the Prog list but not
8080
// try to read the section headers and the string table from disk.
@@ -96,11 +96,31 @@ func readELFGoBuildID(filename string, f *os.File, data []byte) (buildid string,
9696
return "", &os.PathError{Path: filename, Op: "parse", Err: err}
9797
}
9898
for _, p := range ef.Progs {
99-
if p.Type != elf.PT_NOTE || p.Off >= uint64(len(data)) || p.Off+p.Filesz >= uint64(len(data)) || p.Filesz < 16 {
99+
if p.Type != elf.PT_NOTE || p.Filesz < 16 {
100100
continue
101101
}
102102

103-
note := data[p.Off : p.Off+p.Filesz]
103+
var note []byte
104+
if p.Off+p.Filesz < uint64(len(data)) {
105+
note = data[p.Off : p.Off+p.Filesz]
106+
} else {
107+
// For some linkers, such as the Solaris linker,
108+
// the buildid may not be found in data (which
109+
// likely contains the first 16kB of the file)
110+
// or even the first few megabytes of the file
111+
// due to differences in note segment placement;
112+
// in that case, extract the note data manually.
113+
_, err = f.Seek(int64(p.Off), 0)
114+
if err != nil {
115+
return "", err
116+
}
117+
118+
note = make([]byte, p.Filesz)
119+
_, err = io.ReadFull(f, note)
120+
if err != nil {
121+
return "", err
122+
}
123+
}
104124
nameSize := ef.ByteOrder.Uint32(note)
105125
valSize := ef.ByteOrder.Uint32(note[4:])
106126
tag := ef.ByteOrder.Uint32(note[8:])

src/cmd/go/note_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ func testNoteReading(t *testing.T) {
5050
// no external linking
5151
t.Logf("no external linking - skipping linkmode=external test")
5252

53-
case "solaris":
54-
t.Logf("skipping - golang.org/issue/12178")
55-
5653
default:
5754
tg.run("build", "-ldflags", "-buildid="+buildID+" -linkmode=external", "-o", tg.path("hello.exe"), tg.path("hello.go"))
5855
id, err := main.ReadBuildIDFromBinary(tg.path("hello.exe"))

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,10 +1739,6 @@ func doelf() {
17391739
Addstring(shstrtab, ".note.go.pkg-list")
17401740
Addstring(shstrtab, ".note.go.deps")
17411741
}
1742-
1743-
if buildid != "" {
1744-
Addstring(shstrtab, ".note.go.buildid")
1745-
}
17461742
}
17471743

17481744
hasinitarr := Linkshared

0 commit comments

Comments
 (0)