Skip to content

cmd/link: produces invalid ELF sections (zeroth section has nonzero name) #76656

@stapelberg

Description

@stapelberg

Go version

go version go1.26-devel_f22d37d574 Mon Dec 1 14:59:40 2025 -0800 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/usr/local/google/home/stapelberg/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/usr/local/google/home/stapelberg/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1577916995=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/usr/local/google/home/stapelberg/upstream-go/src/go.mod'
GOMODCACHE='/usr/local/google/home/stapelberg/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/usr/local/google/home/stapelberg/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/google/home/stapelberg/upstream-go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/usr/local/google/home/stapelberg/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/google/home/stapelberg/upstream-go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26-devel_f22d37d574 Mon Dec 1 14:59:40 2025 -0800'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I noticed a Google-internal test related to ELF parsing started failing (b/464515354) after commit http://go.dev/cl/718840 (cmd/link: build shstrtab from ELF sections).

Digging deeper, it seems like the triggering condition is building with -buildmode=pie, and the issue is that the Go linker no longer ensures the zeroth ELF section contains all zeros.

What did you see happen?

Here’s a reproducer script that Gemini 3 produced:

#!/bin/bash
set -e

# Create a minimal Go program
cat > main.go <<EOF
package main
import "fmt"
func main() { fmt.Println("hello") }
EOF

# Build with -buildmode=pie to trigger the issue
go build -buildmode=pie -o main main.go

# Find the offset of section headers
SHOFF=$(readelf -h main | grep "Start of section headers:" | awk '{print $5}')

# Dump the first 64 bytes (the null section header)
echo "Checking first section header at offset $SHOFF:"
od -t x1 -j $SHOFF -N 64 main

# Check if sh_name (first 4 bytes) is non-zero
FIRST_BYTE=$(od -t u1 -j $SHOFF -N 1 -An main | tr -d ' ')
if [ "$FIRST_BYTE" != "0" ]; then
  echo "FAIL: First section header has non-zero sh_name: $FIRST_BYTE"
  echo "This violates the ELF specification for SHT_NULL section."
else
  echo "PASS: First section header is zero."
fi

We can also use eu-elflint from the elfutils package to confirm. With Go 1.24, I get:

% eu-elflint --gnu-ld main
phdr[2]: unknown object file note type 4 with owner name 'Go' at offset 100
section [ 6] '.gnu.version_r' has wrong type: expected GNU_verneed, is PROGBITS
section [ 8] '.gnu.version' has wrong type: expected GNU_versym, is PROGBITS
section [35] '.note.go.buildid': unknown object file note type 4 with owner name 'Go' at offset 100

And with Go at commit f22d37d, I get an extra error about the zeroth section:

% eu-elflint --gnu-ld main                      
phdr[2]: unknown object file note type 4 with owner name 'Go' at offset 100
zeroth section has nonzero name
section [ 2] '.note.go.buildid': unknown object file note type 4 with owner name 'Go' at offset 100
section [11] '.gnu.version_r' has wrong type: expected GNU_verneed, is PROGBITS
section [13] '.gnu.version' has wrong type: expected GNU_versym, is PROGBITS

What did you expect to see?

The linker should keep the zeroth section at all-zero to conform to the ELF spec.

See e.g. https://www.sco.com/developers/devspecs/gabi41.pdf, page 59:

Other section type values are reserved. As mentioned before, the section header for index 0 (S H N _ U N D E F) exists, even though the index marks undefined section references. This entry holds the following.

[table containing all 0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions