英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论