From 9112d296e84315a07d76a24874037448e2affdd7 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Wed, 29 Sep 2021 16:47:12 -0400 Subject: [PATCH] cmd/link: add runtime.text.N symbols to Mach-O symbol table On Darwin/ARM64 when external linking, for very large text we split it into multiple sections. For each section (other than the first) we create runtime.text.N marker symbols. In CL 316050 I forgot to add those symbols to the symbol table. This CL does it. It doesn't actually matter for program execution. But we add them on ELF when splitting text sections, so we do it here as well. Also, this makes it easier to tell if we split text sections. Change-Id: Ida7f8e9431867881e5ee2bc1a2129eeaf83cb878 Reviewed-on: https://go-review.googlesource.com/c/go/+/353209 Trust: Cherry Mui Run-TryBot: Cherry Mui Reviewed-by: Than McIntosh --- src/cmd/link/internal/ld/ld_test.go | 18 ++++++++++++++---- src/cmd/link/internal/ld/macho.go | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cmd/link/internal/ld/ld_test.go b/src/cmd/link/internal/ld/ld_test.go index 3702a4d08f5..2d5a7add9d2 100644 --- a/src/cmd/link/internal/ld/ld_test.go +++ b/src/cmd/link/internal/ld/ld_test.go @@ -5,6 +5,7 @@ package ld import ( + "bytes" "debug/pe" "fmt" "internal/testenv" @@ -154,13 +155,22 @@ func TestLargeTextSectionSplitting(t *testing.T) { // is arbitrary; we just need something sufficiently large that uses // external linking. exe := filepath.Join(dir, "go.exe") - out, eerr := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "-ldflags=-linkmode=external -debugtextsize=1048576", "cmd/go").CombinedOutput() - if eerr != nil { - t.Fatalf("build failure: %s\n%s\n", eerr, string(out)) + out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "-ldflags=-linkmode=external -debugtextsize=1048576", "cmd/go").CombinedOutput() + if err != nil { + t.Fatalf("build failure: %s\n%s\n", err, string(out)) + } + + // Check that we did split text sections. + out, err = exec.Command(testenv.GoToolPath(t), "tool", "nm", exe).CombinedOutput() + if err != nil { + t.Fatalf("nm failure: %s\n%s\n", err, string(out)) + } + if !bytes.Contains(out, []byte("runtime.text.1")) { + t.Errorf("runtime.text.1 not found, text section not split?") } // Result should be runnable. - _, err := exec.Command(exe, "version").CombinedOutput() + _, err = exec.Command(exe, "version").CombinedOutput() if err != nil { t.Fatal(err) } diff --git a/src/cmd/link/internal/ld/macho.go b/src/cmd/link/internal/ld/macho.go index a577a5308d5..8633222ee33 100644 --- a/src/cmd/link/internal/ld/macho.go +++ b/src/cmd/link/internal/ld/macho.go @@ -897,6 +897,14 @@ func collectmachosyms(ctxt *Link) { if ldr.SymType(s) == sym.STEXT { addsym(s) } + for n := range Segtext.Sections[1:] { + s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n+1), 0) + if s != 0 { + addsym(s) + } else { + break + } + } s = ldr.Lookup("runtime.etext", 0) if ldr.SymType(s) == sym.STEXT { addsym(s)