[go] cmd/link: don't update offset of existing ELF section name

1 view
Skip to first unread message

Ian Lance Taylor (Gerrit)

unread,
Dec 2, 2025, 8:03:35 PM (8 hours ago) Dec 2
to goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com

Ian Lance Taylor has uploaded the change for review

Commit message

cmd/link: don't update offset of existing ELF section name

Fixes #76656
Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1

Change diff

diff --git a/src/cmd/link/elf_test.go b/src/cmd/link/elf_test.go
index f11cc4b..adf255a 100644
--- a/src/cmd/link/elf_test.go
+++ b/src/cmd/link/elf_test.go
@@ -11,6 +11,7 @@
"cmd/internal/hash"
"cmd/link/internal/ld"
"debug/elf"
+ "encoding/binary"
"fmt"
"internal/platform"
"internal/testenv"
@@ -22,6 +23,7 @@
"sync"
"testing"
"text/template"
+ "unsafe"
)

func getCCAndCCFLAGS(t *testing.T, env []string) (string, []string) {
@@ -677,12 +679,23 @@
}

func TestELFHeadersSorted(t *testing.T) {
+ for _, buildmode := range []string{"exe", "pie"} {
+ t.Run(buildmode, func(t *testing.T) {
+ testELFHeadersSorted(t, buildmode)
+ })
+ }
+}
+
+func testELFHeadersSorted(t *testing.T, buildmode string) {
testenv.MustHaveGoBuild(t)

// We can only test this for internal linking mode.
// For external linking the external linker will
// decide how to sort the sections.
testenv.MustInternalLink(t, testenv.NoSpecialBuildTypes)
+ if buildmode == "pie" {
+ testenv.MustInternalLinkPIE(t)
+ }

t.Parallel()

@@ -693,12 +706,71 @@
}

exe := filepath.Join(tmpdir, "x.exe")
- cmd := goCmd(t, "build", "-ldflags=-linkmode=internal", "-o", exe, src)
+ cmd := goCmd(t, "build", "-buildmode="+buildmode, "-ldflags=-linkmode=internal", "-o", exe, src)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("build failed: %v, output:\n%s", err, out)
}

- ef, err := elf.Open(exe)
+ // Check that the first section header is all zeroes.
+ f, err := os.Open(exe)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+
+ var ident [elf.EI_NIDENT]byte
+ if _, err := f.Read(ident[:]); err != nil {
+ t.Fatal(err)
+ }
+
+ var bo binary.ByteOrder
+ switch elf.Data(ident[elf.EI_DATA]) {
+ case elf.ELFDATA2LSB:
+ bo = binary.LittleEndian
+ case elf.ELFDATA2MSB:
+ bo = binary.BigEndian
+ default:
+ t.Fatalf("unrecognized data encoding %d", ident[elf.EI_DATA])
+ }
+
+ var shoff int64
+ var shsize int
+ switch elf.Class(ident[elf.EI_CLASS]) {
+ case elf.ELFCLASS32:
+ var hdr elf.Header32
+ data := make([]byte, unsafe.Sizeof(hdr))
+ if _, err := f.ReadAt(data, 0); err != nil {
+ t.Fatal(err)
+ }
+ shoff = int64(bo.Uint32(data[unsafe.Offsetof(hdr.Shoff):]))
+ shsize = int(unsafe.Sizeof(elf.Section32{}))
+
+ case elf.ELFCLASS64:
+ var hdr elf.Header64
+ data := make([]byte, unsafe.Sizeof(hdr))
+ if _, err := f.ReadAt(data, 0); err != nil {
+ t.Fatal(err)
+ }
+ shoff = int64(bo.Uint64(data[unsafe.Offsetof(hdr.Shoff):]))
+ shsize = int(unsafe.Sizeof(elf.Section64{}))
+
+ default:
+ t.Fatalf("unrecognized class %d", ident[elf.EI_CLASS])
+ }
+
+ if shoff > 0 {
+ data := make([]byte, shsize)
+ if _, err := f.ReadAt(data, shoff); err != nil {
+ t.Fatal(err)
+ }
+ for i, c := range data {
+ if c != 0 {
+ t.Errorf("section header 0 byte %d is %d, should be zero", i, c)
+ }
+ }
+ }
+
+ ef, err := elf.NewFile(f)
if err != nil {
t.Fatal(err)
}
diff --git a/src/cmd/link/internal/ld/elf.go b/src/cmd/link/internal/ld/elf.go
index c948022..12218fe 100644
--- a/src/cmd/link/internal/ld/elf.go
+++ b/src/cmd/link/internal/ld/elf.go
@@ -434,7 +434,9 @@
// are likely to be the only match.
for _, sh := range shdr {
if suffix, ok := strings.CutPrefix(sh.nameString, elfRelType); ok {
- m[suffix] = off + uint32(len(elfRelType))
+ if _, found := m[suffix]; !found {
+ m[suffix] = off + uint32(len(elfRelType))
+ }
writeString(sh.nameString)
}
}

Change information

Files:
  • M src/cmd/link/elf_test.go
  • M src/cmd/link/internal/ld/elf.go
Change size: M
Delta: 2 files changed, 77 insertions(+), 3 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
Gerrit-Change-Number: 726100
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Dec 2, 2025, 8:16:38 PM (7 hours ago) Dec 2
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Keith Randall, Cherry Mui, Russ Cox, Than McIntosh, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
Attention needed from Cherry Mui, Ian Lance Taylor, Russ Cox and Than McIntosh

Keith Randall voted Code-Review+2

Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Cherry Mui
  • Ian Lance Taylor
  • Russ Cox
  • Than McIntosh
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
Gerrit-Change-Number: 726100
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-Reviewer: Than McIntosh <th...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Attention: Cherry Mui <cher...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Than McIntosh <th...@golang.org>
Gerrit-Comment-Date: Wed, 03 Dec 2025 01:16:33 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Dec 2, 2025, 8:16:56 PM (7 hours ago) Dec 2
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Keith Randall, Cherry Mui, Russ Cox, Than McIntosh, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
Attention needed from Cherry Mui, Ian Lance Taylor, Russ Cox and Than McIntosh

Keith Randall voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Cherry Mui
  • Ian Lance Taylor
  • Russ Cox
  • Than McIntosh
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
Gerrit-Change-Number: 726100
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@google.com>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-Reviewer: Than McIntosh <th...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Attention: Cherry Mui <cher...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Than McIntosh <th...@golang.org>
Gerrit-Comment-Date: Wed, 03 Dec 2025 01:16:51 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Michael Stapelberg (Gerrit)

unread,
3:21 AM (24 minutes ago) 3:21 AM
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Go LUCI, Keith Randall, Keith Randall, Cherry Mui, Russ Cox, Than McIntosh, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Cherry Mui, Ian Lance Taylor, Russ Cox and Than McIntosh

Michael Stapelberg voted Code-Review+2

Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Cherry Mui
  • Ian Lance Taylor
  • Russ Cox
  • Than McIntosh
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
    Gerrit-Change-Number: 726100
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@google.com>
    Gerrit-Reviewer: Michael Stapelberg <stape...@google.com>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-Reviewer: Than McIntosh <th...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Than McIntosh <th...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Dec 2025 08:20:59 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    3:22 AM (22 minutes ago) 3:22 AM
    to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Michael Stapelberg, Go LUCI, Keith Randall, Keith Randall, Cherry Mui, Russ Cox, Than McIntosh, golang-co...@googlegroups.com

    Gopher Robot submitted the change

    Change information

    Commit message:
    cmd/link: don't update offset of existing ELF section name

    Fixes #76656
    Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
    Reviewed-by: Keith Randall <k...@golang.org>
    Reviewed-by: Keith Randall <k...@google.com>
    Reviewed-by: Michael Stapelberg <stape...@google.com>
    Auto-Submit: Ian Lance Taylor <ia...@golang.org>
    Files:
    • M src/cmd/link/elf_test.go
    • M src/cmd/link/internal/ld/elf.go
    Change size: M
    Delta: 2 files changed, 77 insertions(+), 3 deletions(-)
    Branch: refs/heads/master
    Submit Requirements:
    • requirement satisfiedCode-Review: +1 by Keith Randall, +2 by Michael Stapelberg, +2 by Keith Randall
    • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: merged
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: If2e823ba1577700af00f5883e4ea5c139e4749c1
    Gerrit-Change-Number: 726100
    Gerrit-PatchSet: 2
    Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    open
    diffy
    satisfied_requirement
    Reply all
    Reply to author
    Forward
    0 new messages