From cd5c10f040282b1c90109feb155527fe4a2ff4a0 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 23 Aug 2017 17:44:51 -0700 Subject: [PATCH] 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 --- src/cmd/go/note_test.go | 19 +++++++++++++++++++ src/cmd/link/internal/ld/elf.go | 1 + 2 files changed, 20 insertions(+) diff --git a/src/cmd/go/note_test.go b/src/cmd/go/note_test.go index 289af9908a..1bbbd0d8a0 100644 --- a/src/cmd/go/note_test.go +++ b/src/cmd/go/note_test.go @@ -62,4 +62,23 @@ func testNoteReading(t *testing.T) { if id != buildID { t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID) } + + switch runtime.GOOS { + case "dragonfly", "freebsd", "linux", "netbsd", "openbsd": + // Test while forcing use of the gold linker, since in the past + // we've had trouble reading the notes generated by gold. + err := tg.doRun([]string{"build", "-ldflags", "-buildid=" + buildID + " -linkmode=external -extldflags=-fuse-ld=gold", "-o", tg.path("hello.exe"), tg.path("hello.go")}) + if err != nil && (tg.grepCountBoth("invalid linker") > 0 || tg.grepCountBoth("gold") > 0) { + // It's not an error if gold isn't there. + t.Log("skipping gold test") + break + } + id, err = buildid.ReadBuildIDFromBinary(tg.path("hello.exe")) + if err != nil { + t.Fatalf("reading build ID from hello binary (linkmode=external -extldflags=-fuse-ld=gold): %v", err) + } + if id != buildID { + t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external -extldflags=-fuse-ld=gold)", id, buildID) + } + } } diff --git a/src/cmd/link/internal/ld/elf.go b/src/cmd/link/internal/ld/elf.go index 2c3f99b756..d6b3cb0927 100644 --- a/src/cmd/link/internal/ld/elf.go +++ b/src/cmd/link/internal/ld/elf.go @@ -1874,6 +1874,7 @@ func addgonote(ctxt *Link, sectionName string, tag uint32, desc []byte) { s.P = append(s.P, 0) } s.Size = int64(len(s.P)) + s.Align = 4 } func (ctxt *Link) doelf() { -- 2.48.1