Skip to content

Commit cd5c10f

Browse files
cmd/link: set correct alignment of ELF note section
Otherwise the default computation in symalign kicked in, setting the alignment to be too high. This didn't matter with GNU ld, which put each loadable note into a separate PT_NOTE segment, but it did matter with gold which accumulated them all into a single PT_NOTE segment, respecting the requested alignment. In the single PT_NOTE segment generated by gold, the incorrect section alignment made the notes unreadable. Fixes #21564 Change-Id: I15eb408bb04a2566c9fdfb6828e14188d9ef2280 Reviewed-on: https://go-review.googlesource.com/58290 Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 744ebfd commit cd5c10f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/cmd/go/note_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,23 @@ func testNoteReading(t *testing.T) {
6262
if id != buildID {
6363
t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID)
6464
}
65+
66+
switch runtime.GOOS {
67+
case "dragonfly", "freebsd", "linux", "netbsd", "openbsd":
68+
// Test while forcing use of the gold linker, since in the past
69+
// we've had trouble reading the notes generated by gold.
70+
err := tg.doRun([]string{"build", "-ldflags", "-buildid=" + buildID + " -linkmode=external -extldflags=-fuse-ld=gold", "-o", tg.path("hello.exe"), tg.path("hello.go")})
71+
if err != nil && (tg.grepCountBoth("invalid linker") > 0 || tg.grepCountBoth("gold") > 0) {
72+
// It's not an error if gold isn't there.
73+
t.Log("skipping gold test")
74+
break
75+
}
76+
id, err = buildid.ReadBuildIDFromBinary(tg.path("hello.exe"))
77+
if err != nil {
78+
t.Fatalf("reading build ID from hello binary (linkmode=external -extldflags=-fuse-ld=gold): %v", err)
79+
}
80+
if id != buildID {
81+
t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external -extldflags=-fuse-ld=gold)", id, buildID)
82+
}
83+
}
6584
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,7 @@ func addgonote(ctxt *Link, sectionName string, tag uint32, desc []byte) {
18741874
s.P = append(s.P, 0)
18751875
}
18761876
s.Size = int64(len(s.P))
1877+
s.Align = 4
18771878
}
18781879

18791880
func (ctxt *Link) doelf() {

0 commit comments

Comments
 (0)