From be910bf2fd6dee75bfb15aab567dc94b126bde29 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Thu, 15 Jun 2023 17:25:08 -0400 Subject: cmd/link: always use symbol-targeted relocations on Mach-O In Mach-O object files, there are two kinds of relocations: "external" relocation, which targets a symbol, and "non-external" relocation, which targets a section. For targeting symbols not in the current object, we must use symbol-targeted relocations. For targeting symbols defined in the current object, for some relocation types, both kinds can be used. We currently use section-targeted relocations for R_ADDR targeting locally defined symbols. Modern Apple toolchain seems to prefer symbol-targeted relocations. Also, Apple's new linker, ld-prime, seems to not handle section- targeted relocations well in some cases. So this CL switches to always generate symbol-targeted relocations. This also simplifies the code. One exception is that DWARF tools seem to handle only section- targeted relocations. So generate those in DWARF sections. This CL supersedes CL 502616. Fixes #60694. For #61229. Change-Id: I3b74df64f21114635061bcd89114392b3a2d588b Reviewed-on: https://go-review.googlesource.com/c/go/+/503935 Reviewed-by: Than McIntosh Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot --- src/cmd/link/internal/ld/data.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/cmd/link/internal/ld/data.go') diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index 21b2e9a9d4..95fc8b5a42 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -368,9 +368,11 @@ func (st *relocSymState) relocsym(s loader.Sym, P []byte) { o = 0 } } else if target.IsDarwin() { - if ldr.SymType(rs) != sym.SHOSTOBJ && ldr.SymType(s) != sym.SINITARR { - // ld-prime drops the offset in data for SINITARR. We need to use - // symbol-targeted relocation. See also machoreloc1. + if ldr.SymType(s).IsDWARF() { + // We generally use symbol-targeted relocations. + // DWARF tools seem to only handle section-targeted relocations, + // so generate section-targeted relocations in DWARF sections. + // See also machoreloc1. o += ldr.SymValue(rs) } } else if target.IsWindows() { -- cgit v1.3-6-g1900