Skip to content

Commit a3b092d

Browse files
committed
[release-branch.go1.21] cmd/link: use symbol-targeted relocation for initializers on Mach-O
Apple's new linker, ld-prime from Xcode 15 beta, when handling initializers in __mod_init_func, drops the offset in the data, resolving the relocation to the beginning of the section. The latest version of ld-prime rejects non-zero addend. We need to use symbol-targeted "external" relocations, so that it doesn't need an addend and can be resolved correctly. This also works fine with ld64. Fixes #60694. For #61229. Change-Id: Ida2be6aa4c91bfcd142b755e2ec63aabfbbd77a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/502616 Run-TryBot: Cherry Mui <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> (cherry picked from commit bad9ca8) Reviewed-on: https://go-review.googlesource.com/c/go/+/514535
1 parent 07c72a0 commit a3b092d

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/cmd/link/internal/amd64/asm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func machoreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sy
446446
rs := r.Xsym
447447
rt := r.Type
448448

449-
if ldr.SymType(rs) == sym.SHOSTOBJ || rt == objabi.R_PCREL || rt == objabi.R_GOTPCREL || rt == objabi.R_CALL {
449+
if rt == objabi.R_PCREL || rt == objabi.R_GOTPCREL || rt == objabi.R_CALL || ldr.SymType(rs) == sym.SHOSTOBJ || ldr.SymType(s) == sym.SINITARR {
450450
if ldr.SymDynid(rs) < 0 {
451451
ldr.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs))
452452
return false

src/cmd/link/internal/arm64/asm.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,11 @@ func machoreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sy
545545
}
546546
}
547547

548-
if ldr.SymType(rs) == sym.SHOSTOBJ || rt == objabi.R_CALLARM64 ||
548+
if rt == objabi.R_CALLARM64 ||
549549
rt == objabi.R_ARM64_PCREL_LDST8 || rt == objabi.R_ARM64_PCREL_LDST16 ||
550550
rt == objabi.R_ARM64_PCREL_LDST32 || rt == objabi.R_ARM64_PCREL_LDST64 ||
551-
rt == objabi.R_ADDRARM64 || rt == objabi.R_ARM64_GOTPCREL {
551+
rt == objabi.R_ADDRARM64 || rt == objabi.R_ARM64_GOTPCREL ||
552+
ldr.SymType(rs) == sym.SHOSTOBJ || ldr.SymType(s) == sym.SINITARR {
552553
if ldr.SymDynid(rs) < 0 {
553554
ldr.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs))
554555
return false

src/cmd/link/internal/ld/data.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ func (st *relocSymState) relocsym(s loader.Sym, P []byte) {
368368
o = 0
369369
}
370370
} else if target.IsDarwin() {
371-
if ldr.SymType(rs) != sym.SHOSTOBJ {
371+
if ldr.SymType(rs) != sym.SHOSTOBJ && ldr.SymType(s) != sym.SINITARR {
372+
// ld-prime drops the offset in data for SINITARR. We need to use
373+
// symbol-targeted relocation. See also machoreloc1.
372374
o += ldr.SymValue(rs)
373375
}
374376
} else if target.IsWindows() {

0 commit comments

Comments
 (0)