ELF可执行文件默认的字节对齐方式是什么?

huangapple go评论81阅读模式
英文:

ELF go binaries default byte alignment

问题

我经验性地观察到go ELF二进制文件使用16字节对齐。例如:

$ 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

因此,绝大多数的最低有效字节都是0。在go二进制文件中,这种情况总是如此吗?

英文:

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?

答案1

得分: 2

这取决于平台。如果你已经检出了源代码库:

% 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

amd64的对齐方式在将来可能会降至16;目前是32,因为 https://github.com/golang/go/issues/35881

英文:

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

huangapple
  • 本文由 发表于 2021年7月26日 18:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/68528333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定