Skip to content

Commit cb55d1a

Browse files
cherrymuicagedmantis
authored andcommitted
[release-branch.go1.22] cmd/link: add runtime.text.N symbols to macho symbol table in dynlink mode
In dynamic linking mode (e.g. when using plugins) on darwin, the marker symbols runtime.text and runtime.etext are added to Textp in an early stage, so when adding symbols to the symbol table we don't need to explicitly add them. However, when splitting text sections, the runtime.text.N marker symbols for the addtional sections are not added to Textp. So we do need to add them explicitly to the symbol table. Updates #66993. Fixes #67527. Change-Id: Ic718d03cd71fc0bfb931cff82640b1f4c53b89be Reviewed-on: https://go-review.googlesource.com/c/go/+/586555 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Than McIntosh <[email protected]> (cherry picked from commit 9a9dd72) Reviewed-on: https://go-review.googlesource.com/c/go/+/586081
1 parent 3c96ae0 commit cb55d1a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/cmd/cgo/internal/testplugin/plugin_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func testMain(m *testing.M) int {
7474
}
7575
defer os.RemoveAll(GOPATH)
7676
tmpDir = GOPATH
77+
fmt.Printf("TMPDIR=%s\n", tmpDir)
7778

7879
modRoot := filepath.Join(GOPATH, "src", "testplugin")
7980
altRoot := filepath.Join(GOPATH, "alt", "src", "testplugin")
@@ -395,3 +396,21 @@ func TestIssue62430(t *testing.T) {
395396
goCmd(t, "build", "-o", "issue62430.exe", "./issue62430/main.go")
396397
run(t, "./issue62430.exe")
397398
}
399+
400+
func TestTextSectionSplit(t *testing.T) {
401+
globalSkip(t)
402+
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
403+
t.Skipf("text section splitting is not done in %s/%s", runtime.GOOS, runtime.GOARCH)
404+
}
405+
406+
// Use -ldflags=-debugtextsize=262144 to let the linker split text section
407+
// at a smaller size threshold, so it actually splits for the test binary.
408+
goCmd(nil, "build", "-ldflags=-debugtextsize=262144", "-o", "host-split.exe", "./host")
409+
run(t, "./host-split.exe")
410+
411+
// Check that we did split text sections.
412+
syms := goCmd(nil, "tool", "nm", "host-split.exe")
413+
if !strings.Contains(syms, "runtime.text.1") {
414+
t.Errorf("runtime.text.1 not found, text section not split?")
415+
}
416+
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -901,21 +901,25 @@ func collectmachosyms(ctxt *Link) {
901901
// Add special runtime.text and runtime.etext symbols (which are local).
902902
// We've already included this symbol in Textp on darwin if ctxt.DynlinkingGo().
903903
// See data.go:/textaddress
904+
// NOTE: runtime.text.N symbols (if we split text sections) are not added, though,
905+
// so we handle them here.
904906
if !*FlagS {
905907
if !ctxt.DynlinkingGo() {
906908
s := ldr.Lookup("runtime.text", 0)
907909
if ldr.SymType(s) == sym.STEXT {
908910
addsym(s)
909911
}
910-
for n := range Segtext.Sections[1:] {
911-
s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n+1), 0)
912-
if s != 0 {
913-
addsym(s)
914-
} else {
915-
break
916-
}
912+
}
913+
for n := range Segtext.Sections[1:] {
914+
s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n+1), 0)
915+
if s != 0 {
916+
addsym(s)
917+
} else {
918+
break
917919
}
918-
s = ldr.Lookup("runtime.etext", 0)
920+
}
921+
if !ctxt.DynlinkingGo() {
922+
s := ldr.Lookup("runtime.etext", 0)
919923
if ldr.SymType(s) == sym.STEXT {
920924
addsym(s)
921925
}

0 commit comments

Comments
 (0)