Search code examples
goelfmemory-alignment

ELF go binaries default byte alignment


I empirically see that go ELF binaries use 16 bytes alignment. For example:

$ wget https://github.com/gardener/gardenctl/releases/download/v0.24.2/gardenctl-linux-amd64
$ readelf -W -s gardenctl-linux-amd64 | grep -E "FUNC" | wc -l
44746
$ readelf -W -s gardenctl-linux-amd64 | grep -E "0[ ]+[0-9]* FUNC" | wc -l
44744

so vast majority have 0 in their least significant byte. Is it always like that in go binaries?


Solution

  • This depends on the platform. If you have a source repo checked out:

    % cd go/src/cmd/link/internal
    % grep "funcAlign =" */*.go
    amd64/l.go:     funcAlign = 32
    arm/l.go:       funcAlign = 4 // single-instruction alignment
    arm64/l.go:     funcAlign = 16
    mips64/l.go:    funcAlign = 8
    ppc64/l.go:     funcAlign = 16
    riscv64/l.go:   funcAlign = 8
    s390x/l.go:     funcAlign = 16
    x86/l.go:       funcAlign = 16
    

    the alignment for amd64 may go back down to 16 in the future; it is 32 for a while because of https://github.com/golang/go/issues/35881