]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make sure linkname'd symbol is non-package
authorCherry Zhang <cherryyz@google.com>
Sat, 7 Nov 2020 03:46:18 +0000 (22:46 -0500)
committerCherry Zhang <cherryyz@google.com>
Mon, 9 Nov 2020 16:09:16 +0000 (16:09 +0000)
When a variable symbol is both imported (possibly through
inlining) and linkname'd, make sure its LSym is marked as
non-package for symbol indexing in the object file, so it is
resolved by name and dedup'd with the original definition.

Fixes #42401.

Change-Id: I8e90c0418c6f46a048945c5fdc06c022b77ed68d
Reviewed-on: https://go-review.googlesource.com/c/go/+/268178
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
src/cmd/compile/internal/gc/gsubr.go
test/fixedbugs/issue42401.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue42401.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue42401.go [new file with mode: 0644]

index 864ada1d3cd09d1022e64e68a6ddb557b689b577..d599a383e7b884da0e0bdb2d22732a4d29ea87bc 100644 (file)
@@ -302,6 +302,12 @@ func ggloblnod(nam *Node) {
        if nam.Name.LibfuzzerExtraCounter() {
                s.Type = objabi.SLIBFUZZER_EXTRA_COUNTER
        }
+       if nam.Sym.Linkname != "" {
+               // Make sure linkname'd symbol is non-package. When a symbol is
+               // both imported and linkname'd, s.Pkg may not set to "_" in
+               // types.Sym.Linksym because LSym already exists. Set it here.
+               s.Pkg = "_"
+       }
 }
 
 func ggloblsym(s *obj.LSym, width int32, flags int16) {
diff --git a/test/fixedbugs/issue42401.dir/a.go b/test/fixedbugs/issue42401.dir/a.go
new file mode 100644 (file)
index 0000000..75f8e7f
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+var s string
+
+func init() { s = "a" }
+
+func Get() string { return s }
diff --git a/test/fixedbugs/issue42401.dir/b.go b/test/fixedbugs/issue42401.dir/b.go
new file mode 100644 (file)
index 0000000..a834f4e
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+       "./a"
+       _ "unsafe"
+)
+
+//go:linkname s a.s
+var s string
+
+func main() {
+       if a.Get() != "a" {
+               panic("FAIL")
+       }
+
+       s = "b"
+       if a.Get() != "b" {
+               panic("FAIL")
+       }
+}
diff --git a/test/fixedbugs/issue42401.go b/test/fixedbugs/issue42401.go
new file mode 100644 (file)
index 0000000..794d5b0
--- /dev/null
@@ -0,0 +1,10 @@
+// rundir
+
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 42401: linkname doesn't work correctly when a variable symbol
+// is both imported (possibly through inlining) and linkname'd.
+
+package ignored