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

gollvm: can't find some C std pthread_xxx functions #45424

Open
JX-Zhang98 opened this issue Apr 7, 2021 · 8 comments
Open

gollvm: can't find some C std pthread_xxx functions #45424

JX-Zhang98 opened this issue Apr 7, 2021 · 8 comments
Assignees
Milestone

Comments

@JX-Zhang98
Copy link

@JX-Zhang98 JX-Zhang98 commented Apr 7, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16 gollvm LLVM 13.0.0git linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="off"
GOARCH="amd64"
GOBIN="/home/jxzhang/workspace/gowork/bin"
GOCACHE="/home/jxzhang/.cache/go-build"
GOENV="/home/jxzhang/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jxzhang/workspace/gowork/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jxzhang/workspace/gowork"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/jxzhang/.local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/jxzhang/.local/gollvm/tools"
GOVCS=""
GOVERSION="go1.16 gollvm LLVM 13.0.0git"
GCCGO="/home/jxzhang/.local/gollvm/bin/llvm-goc"
AR="ar"
CC="/home/jxzhang/workspace/llvm_area/llvm-project/build/bin/clang"
CXX="/home/jxzhang/workspace/llvm_area/llvm-project/build/bin/clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4207555597=/tmp/go-build -gno-record-gcc-switches -funwind-tables"

What did you do?

I wrote a cgo demo which call pthread std functions and tried to compile it with gollvm
Here is a link on play.golang- https://play.golang.org/p/PiaY0AIGprl
Though the code can't run on play.golang for some reason...

What did you expect to see?

Compile succeed with gollvm

What did you see instead?

An error caused by an undefined reference occured

ccode.c:11: error: undefined reference to 'pthread_join'

In addition to this function, there are some other functions that also have this error:
pthread_once, pthread_mutexattr_init, pthread_mutexattr_settype, pthread_key_create, pthread_getspecific, pthread_mutexattr_destroy ...

Some extra observations

These functions above seems different between pthread_create which can be referenced normally

jx $  nm /home/jx/.local/gollvm/lib64/libgo.so.13git | grep pthread_create           
00000000010831e0 t __wrap_pthread_create
                 w pthread_create
jx $ nm /home/jx/.local/gollvm/lib64/libgo.so.13git | grep pthread_join
jx $ nm /home/jx/.local/gollvm/lib64/libgo.so.13git | grep pthread_once  
                 w pthread_once
@thanm thanm self-assigned this Apr 7, 2021
@thanm
Copy link
Member

@thanm thanm commented Apr 7, 2021

Is there a reason why you don't add "-lpthread" to your cgo linker flags? e.g.

#cgo LDFLAGS: -L./ -lpthread

That is the normal thing to do when linking C code that uses functions from the pthread library.

@dmitshur dmitshur changed the title Gollvm: can't find some C std pthread_xxx functions gollvm: can't find some C std pthread_xxx functions Apr 7, 2021
@dmitshur dmitshur added this to the gollvm milestone Apr 7, 2021
@JX-Zhang98
Copy link
Author

@JX-Zhang98 JX-Zhang98 commented Apr 8, 2021

The main reason I dropped -lpthread" here is that the code in issue can be compiled successfully by original Go compiler.
And gollvm can also succeed when only the pthread_create function is called.

In fact, I have tried to add "-lpthread" to #cgo,and add it to args when compiling middle files with llvm-goc manually
But another error appeared:

# pthread
/home/jxzhang/.local/gollvm/bin/../lib64/libgo.so: error: undefined reference to 'ffi_tramp_is_supported'
/home/jxzhang/.local/gollvm/bin/../lib64/libgo.so: error: undefined reference to 'ffi_tramp_alloc'
/home/jxzhang/.local/gollvm/bin/../lib64/libgo.so: error: undefined reference to 'ffi_tramp_get_addr'
/home/jxzhang/.local/gollvm/bin/../lib64/libgo.so: error: undefined reference to 'ffi_tramp_free'

It seems more difficult...
This error appeared even if I didn't call pthread_join, but I have pulled the latest libffi.
I got nothing about this error on google..

@thanm
Copy link
Member

@thanm thanm commented Apr 8, 2021

Ok, this part looks actionable. I'll send a CL, thanks.

For the pthread issue, I think this is arising due to differences in the cgo implementations for the main go compiler and gccgo/gollvm. In particular it looks as though we get "-lpthread" vua

https://go.googlesource.com/go/+/1c9e587b90172e7654db897d8c938ffc665e1673/src/runtime/cgo/cgo.go#19

which is not part of the gofrontend version of cgo. I'll ask around about this.

@gopherbot
Copy link

@gopherbot gopherbot commented Apr 8, 2021

Change https://golang.org/cl/308450 mentions this issue: gollvm: update libgo libffi cmake rules

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 8, 2021

The gccgo driver program automatically adds -lpthread when linking statically against libgo. When libgo is linked dynamically, it depends on libpthread.so anyhow. So with gccgo programs always have an implicit -lpthread.

@JX-Zhang98
Copy link
Author

@JX-Zhang98 JX-Zhang98 commented Apr 9, 2021

Appreciate your help. The latest version works well with such scenario.
I think this issue can be closed, right?

@thanm
Copy link
Member

@thanm thanm commented Apr 9, 2021

The gccgo driver program automatically adds -lpthread when linking
statically against libgo. When libgo is linked dynamically, it depends
on libpthread.so anyhow. So with gccgo programs always have an
implicit -lpthread.

From what I am seeing gccgo shows the same behavior:

$ which go
/ssd/gcc-trunk/cross/bin/go
# ptgollvm
ccode.c:11: error: undefined reference to 'pthread_join'
collect2: error: ld returned 1 exit status
$ go build -x -gccgoflags=-v . |& fgrep lpthread
$ ldd `which go` | fgrep libgo
	libgo.so.19 => /ssd/gcc-trunk/cross/lib64/libgo.so.19 (0x00007f0d5dcd3000)
$ nm /ssd/gcc-trunk/cross/lib64/libgo.so.19 | fgrep pthread_create
                 w pthread_create
00000000011c0860 t __wrap_pthread_create
$ nm /ssd/gcc-trunk/cross/lib64/libgo.so.19 | fgrep pthread_join
$

It's true that libgo.so depends on libpthread, but this doesn't help resolve the reference to pthread_join at link time.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 10, 2021

Ah, interesting. Thanks.

So perhaps the question is whether we should always link against -lpthread, not because we normally need to, but because that is in effect what happens with the gc compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants