Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: functions implemented in assembly do not automatically get a funcsym, causing problems with shared libraries #11480

Closed
mwhudson opened this issue Jun 30, 2015 · 4 comments
Milestone

Comments

@mwhudson
Copy link
Contributor

This is quite a little tower of details, but it's possible for an executable using shared libraries to crash with a "runtime: unknown pc in defer" message. Here's a diff adding a test case that fails this way:

diff --git a/misc/cgo/testshared/src/dep/asm.s b/misc/cgo/testshared/src/dep/asm.s
new file mode 100644
index 0000000..b332fe2
--- /dev/null
+++ b/misc/cgo/testshared/src/dep/asm.s
@@ -0,0 +1,10 @@
+// Copyright 2014 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.
+
+//+build !gccgo
+
+#include "textflag.h"
+
+TEXT ·ImplementedInAsm(SB),NOSPLIT,$0-0
+       RET
diff --git a/misc/cgo/testshared/src/dep/gccgo.go b/misc/cgo/testshared/src/dep/gccgo.go
new file mode 100644
index 0000000..552ec30
--- /dev/null
+++ b/misc/cgo/testshared/src/dep/gccgo.go
@@ -0,0 +1,5 @@
+//+build gccgo
+
+package dep
+
+func ImplementedInAsm() {}
diff --git a/misc/cgo/testshared/src/dep/stubs.go b/misc/cgo/testshared/src/dep/stubs.go
new file mode 100644
index 0000000..036296a
--- /dev/null
+++ b/misc/cgo/testshared/src/dep/stubs.go
@@ -0,0 +1,5 @@
+//+build !gccgo
+
+package dep
+
+func ImplementedInAsm()
diff --git a/misc/cgo/testshared/src/exe/exe.go b/misc/cgo/testshared/src/exe/exe.go
index 34fd144..f644776 100644
--- a/misc/cgo/testshared/src/exe/exe.go
+++ b/misc/cgo/testshared/src/exe/exe.go
@@ -1,7 +1,12 @@
 package main

-import "dep"
+import (
+       "dep"
+       "runtime"
+)

 func main() {
+       defer dep.ImplementedInAsm()
+       runtime.GC()
        dep.V = dep.F() + 1
 }

The problem seems to be that when dep is built into libdep2.so there is no ImplementedInAsm·f symbol created in libdep2.so. One is created when building exe but in the .o file it is created by a reloc against an undefined STT_FUNC symbol, which the system linker reacts to by creating a plt and putting the address of the plt stub in the ImplementedInAsm·f symbol, so the address of the PLT stub ends up in the defer slot and the runtime chokes on it.

I guess it's not exactly common to defer and assembly-implemented function implemented in another packge, but this does happens in the standard library: sync.Once defers a call to sync/atomic.StoreUint32, which is how I found the problem.

Not sure what to do about this. Maybe nothing for 1.5. But maybe even functions implemented in assembly should get a funcsym?

@bradfitz bradfitz added this to the Go1.5Maybe milestone Jun 30, 2015
@bradfitz
Copy link
Contributor

/cc @rsc for triage.

@gopherbot
Copy link

CL https://golang.org/cl/12145 mentions this issue.

rsc added a commit that referenced this issue Jul 15, 2015
These used to be defined at use, but that breaks when shared libraries
are involved.

For #11480.

Change-Id: I416a848754fb615c0d75f9f0ccc00723d07f7f01
Reviewed-on: https://go-review.googlesource.com/12145
Reviewed-by: Rob Pike <r@golang.org>
@gopherbot
Copy link

CL https://golang.org/cl/12340 mentions this issue.

@mikioh mikioh modified the milestones: Go1.5, Go1.5Maybe Jul 20, 2015
mwhudson added a commit to mwhudson/go that referenced this issue Sep 2, 2015
This is mostly Russ's https://golang.org/cl/12145 but with some extra fixes to
account for the fact that function declarations without implementations now
break shared libraries, and including my test case.

Fixes golang#11480.

Change-Id: Iabdc2934a0378e5025e4e7affadb535eaef2c8f1
@golang golang locked and limited conversation to collaborators Jul 20, 2016
@gopherbot
Copy link

CL https://golang.org/cl/31031 mentions this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants