cmd/link: propagate Mach-O section alignment to symbol in loadmacho

The Mach-O object file loader reads the section alignment from the
section header into ldMachoSect.align, but never calls SetAlign on
the symbol builder when converting sections to linker symbols. This
causes all Mach-O .syso sections to fall back to Funcalign (16 bytes
on ARM64) regardless of the alignment declared in the section header.

For .syso files containing C-compiled code with ADRP instructions on
ARM64, the lack of page alignment (4096 bytes) leads to incorrect
PC-relative address computation and runtime crashes.

The ELF loader already correctly propagates section alignment via
sb.SetAlign (ldelf.go:543). Apply the same treatment to the Mach-O
loader. Note that Mach-O stores alignment as log2 (e.g. 12 for 4096),
so we use 1 << sect.align.

Fixes #78192

Change-Id: Icae22be2dc726d56eaa35825b484d04ed18566f2
GitHub-Last-Rev: 85aa42b59a
GitHub-Pull-Request: golang/go#78194
Reviewed-on: https://go-review.googlesource.com/c/go/+/756000
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
penglei
2026-03-17 14:33:28 +00:00
committed by Cherry Mui
parent 0520d3f352
commit fdd38337f0

View File

@@ -570,6 +570,9 @@ func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader,
bld.SetData(dat[sect.addr-c.seg.vmaddr:][:sect.size])
}
bld.SetSize(int64(len(bld.Data())))
if sect.align != 0 {
bld.SetAlign(1 << sect.align)
}
if sect.segname == "__TEXT" {
if sect.name == "__text" {