]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: avoid crash on undefined func sym with external linking
authorThan McIntosh <thanm@google.com>
Thu, 26 Aug 2021 19:59:36 +0000 (15:59 -0400)
committerThan McIntosh <thanm@google.com>
Thu, 2 Sep 2021 14:44:50 +0000 (14:44 +0000)
Fix a buglet in relocation processing that crops up with external
linking when you have an undefined function symbol that also has a
prototype (as if it were being defined in assembly src).

Fixes #47993.

Change-Id: Ib655492a63b205ffdc124cfd0cb7f7b731571821
Reviewed-on: https://go-review.googlesource.com/c/go/+/345473
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
src/cmd/go/testdata/script/link_external_undef.txt [new file with mode: 0644]
src/cmd/link/internal/ld/data.go

diff --git a/src/cmd/go/testdata/script/link_external_undef.txt b/src/cmd/go/testdata/script/link_external_undef.txt
new file mode 100644 (file)
index 0000000..d86b3a3
--- /dev/null
@@ -0,0 +1,48 @@
+
+# Test case for issue 47993, in which the linker crashes
+# on a bad input instead of issuing an error and exiting.
+
+# This test requires external linking, so use cgo as a proxy 
+[!cgo] skip
+
+! go build -ldflags='-linkmode=external' .
+! stderr 'panic'
+stderr '^.*unreachable sym in relocation.*'
+
+-- go.mod --
+
+module issue47993
+
+go 1.16
+
+-- main.go --
+
+package main
+
+type M struct {
+       b bool
+}
+
+// Note the body-less func def here. This is what causes the problems.
+func (m *M) run(fp func())
+
+func doit(m *M) {
+        InAsm()
+       m.run(func() {
+       })
+}
+
+func main() {
+     m := &M{true}
+     doit(m)
+}
+
+func InAsm() 
+
+-- main.s --
+
+// Add an assembly function so as to leave open the possibility
+// that body-less functions in Go might be defined in assembly.
+
+// Currently we just need an empty file here.
+
index 70fbb9dc4e24cf2d47c78c89b072684625735784..e0591c3959496a12dfa3e0b2e9462ae2da5a3c1f 100644 (file)
@@ -436,6 +436,11 @@ func (st *relocSymState) relocsym(s loader.Sym, P []byte) {
                        if weak && !ldr.AttrReachable(rs) {
                                continue
                        }
+                       if ldr.SymSect(rs) == nil {
+                               st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs))
+                               continue
+                       }
+
                        // The method offset tables using this relocation expect the offset to be relative
                        // to the start of the first text section, even if there are multiple.
                        if ldr.SymSect(rs).Name == ".text" {