Skip to content

Commit b65e34f

Browse files
committed
cmd/link: don't mangle string symbol names
String symbol names could contain weird characters as we put the string literal into the symbol name. So it may appear to need mangling. However, as string symbols are grouped into a single "go:string.*" symbol, the individual symbol names actually don't matter. So don't mangle them. Also make the mangling code more defensive in case of weird symbol names. Fixes #62098. Change-Id: I533012567a9fffab69debda934f426421c7abb04 Reviewed-on: https://go-review.googlesource.com/c/go/+/520856 Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Cherry Mui <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d63c88d commit b65e34f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,11 @@ func TestForkExec(t *testing.T) {
380380
}
381381
}
382382

383-
func TestGeneric(t *testing.T) {
383+
func TestSymbolNameMangle(t *testing.T) {
384384
// Issue 58800: generic function name may contain weird characters
385385
// that confuse the external linker.
386+
// Issue 62098: the name mangling code doesn't handle some string
387+
// symbols correctly.
386388
globalSkip(t)
387-
goCmd(t, "build", "-buildmode=plugin", "-o", "generic.so", "./generic/plugin.go")
389+
goCmd(t, "build", "-buildmode=plugin", "-o", "mangle.so", "./mangle/plugin.go")
388390
}

src/cmd/cgo/internal/testplugin/testdata/generic/plugin.go renamed to src/cmd/cgo/internal/testplugin/testdata/mangle/plugin.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,37 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Instantiated function name may contain weird characters
6-
// that confuse the external linker, so it needs to be
7-
// mangled.
5+
// Test cases for symbol name mangling.
86

97
package main
108

11-
//go:noinline
12-
func F[T any]() {}
9+
import (
10+
"fmt"
11+
"strings"
12+
)
1313

14+
// Issue 58800:
15+
// Instantiated function name may contain weird characters
16+
// that confuse the external linker, so it needs to be
17+
// mangled.
1418
type S struct {
1519
X int `parser:"|@@)"`
1620
}
1721

22+
//go:noinline
23+
func F[T any]() {}
24+
1825
func P() {
1926
F[S]()
2027
}
2128

29+
// Issue 62098: the name mangling code doesn't handle some string
30+
// symbols correctly.
31+
func G(id string) error {
32+
if strings.ContainsAny(id, "&$@;/:+,?\\{^}%`]\">[~<#|") {
33+
return fmt.Errorf("invalid")
34+
}
35+
return nil
36+
}
37+
2238
func main() {}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ func typeSymbolMangle(name string) string {
993993
if strings.HasPrefix(name, "type:runtime.") {
994994
return name
995995
}
996+
if strings.HasPrefix(name, "go:string.") {
997+
// String symbols will be grouped to a single go:string.* symbol.
998+
// No need to mangle individual symbol names.
999+
return name
1000+
}
9961001
if len(name) <= 14 && !strings.Contains(name, "@") { // Issue 19529
9971002
return name
9981003
}
@@ -1007,7 +1012,7 @@ func typeSymbolMangle(name string) string {
10071012
// instantiated symbol, replace type name in []
10081013
i := strings.IndexByte(name, '[')
10091014
j := strings.LastIndexByte(name, ']')
1010-
if j == -1 {
1015+
if j == -1 || j <= i {
10111016
j = len(name)
10121017
}
10131018
hash := notsha256.Sum256([]byte(name[i+1 : j]))

0 commit comments

Comments
 (0)