Skip to content

Commit 98f8454

Browse files
committed
cmd/link: don't decode type symbol in shared library in deadcode
In the linker's deadcode pass we decode type symbols for interface satisfaction analysis. When linking against Go shared libraries, the type symbol may come from a shared library, so it doesn't have data in the current module being linked, so we cannot decode it. We already have code to skip DYNIMPORT symbols. However, this doesn't actually work, because at that point the type symbols' names haven't been mangled, whereas they may be mangled in the shared library. So the symbol definition (in shared library) and reference (in current module) haven't been connected. Skip decoding type symbols of type Sxxx (along with DYNIMPORT) when linkShared. Note: we cannot skip all type symbols, as we still need to mark unexported methods defined in the current module. Fixes #44031. Change-Id: I833d19a060c94edbd6fc448172358f9a7d760657 Reviewed-on: https://go-review.googlesource.com/c/go/+/288496 Trust: Cherry Zhang <[email protected]> Trust: Than McIntosh <[email protected]> Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 1426a57 commit 98f8454

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

misc/cgo/testshared/shared_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,11 @@ func TestGCData(t *testing.T) {
10631063
goCmd(t, "build", "-linkshared", "./gcdata/main")
10641064
runWithEnv(t, "running gcdata/main", []string{"GODEBUG=clobberfree=1"}, "./main")
10651065
}
1066+
1067+
// Test that we don't decode type symbols from shared libraries (which has no data,
1068+
// causing panic). See issue 44031.
1069+
func TestIssue44031(t *testing.T) {
1070+
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/a")
1071+
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/b")
1072+
goCmd(t, "run", "-linkshared", "./issue44031/main")
1073+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type ATypeWithALoooooongName interface { // a long name, so the type descriptor symbol name is mangled
8+
M()
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import "testshared/issue44031/a"
8+
9+
type T int
10+
11+
func (T) M() {}
12+
13+
var i = a.ATypeWithALoooooongName(T(0))
14+
15+
func F() {
16+
i.M()
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import "testshared/issue44031/b"
8+
9+
type t int
10+
11+
func (t) m() {}
12+
13+
type i interface{ m() } // test that unexported method is correctly marked
14+
15+
var v interface{} = t(0)
16+
17+
func main() {
18+
b.F()
19+
v.(i).m()
20+
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,17 @@ func (d *deadcodePass) flood() {
165165
// R_USEIFACEMETHOD is a marker relocation that marks an interface
166166
// method as used.
167167
rs := r.Sym()
168-
if d.ldr.SymType(rs) != sym.SDYNIMPORT { // don't decode DYNIMPORT symbol (we'll mark all exported methods anyway)
169-
m := d.decodeIfaceMethod(d.ldr, d.ctxt.Arch, rs, r.Add())
170-
if d.ctxt.Debugvlog > 1 {
171-
d.ctxt.Logf("reached iface method: %v\n", m)
172-
}
173-
d.ifaceMethod[m] = true
168+
if d.ctxt.linkShared && (d.ldr.SymType(rs) == sym.SDYNIMPORT || d.ldr.SymType(rs) == sym.Sxxx) {
169+
// Don't decode symbol from shared library (we'll mark all exported methods anyway).
170+
// We check for both SDYNIMPORT and Sxxx because name-mangled symbols haven't
171+
// been resolved at this point.
172+
continue
173+
}
174+
m := d.decodeIfaceMethod(d.ldr, d.ctxt.Arch, rs, r.Add())
175+
if d.ctxt.Debugvlog > 1 {
176+
d.ctxt.Logf("reached iface method: %v\n", m)
174177
}
178+
d.ifaceMethod[m] = true
175179
continue
176180
}
177181
rs := r.Sym()

0 commit comments

Comments
 (0)