Go 1.14: how to -linkshared to a library produced by -buildmode=shared?

864 views
Skip to first unread message

mi...@ridge.co

unread,
Feb 28, 2020, 8:08:05 AM2/28/20
to golang-nuts
Hello.

I'm trying to build a shared library using `-buildmode=shared` and make a binary link to this shared library.

The packages are:


I'd like to package `a` and `c` into the shared library, and put `b` and `splitlib` into the binary.

I've tried the following:

$ go install -buildmode=shared std
$ go install -buildmode=shared -linkshared  -gcflags='-N -l' ./a

This produced `libgithub.com-misha-ridge-splitlib-a.so` in the current directory

Then

$ go build -o main -linkshared  -gcflags='-N -l' .

This produced `main` in the current directory that only links to a shared libstd, but not to
the freshly built lib...splitlib-a.so:

$ ldd main
linux-vdso.so.1 (0x00007ffea3bfe000)
libstd.so => /usr/local/go/pkg/linux_amd64_dynlink/libstd.so (0x00007f7ea27f6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7ea2631000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7ea262c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7ea260b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7ea4f23000)
$

The shared library does not contain symbols from splitlib/a or splitlib/b:


Only the final binary does:

$ go tool nm main  | grep splitlib

Am I missing some step?

The repository with reproducer is at https://github.com/misha-ridge/splitlib

-- 
Best,
Misha.

Bryan C. Mills

unread,
Mar 3, 2020, 8:27:06 PM3/3/20
to golang-nuts
Did this work in 1.13?

https://golang.org/issue/35759 is probably related; see especially the TODO here.
Please file an issue so we don't lose track of it.

Mikhail Gusarov

unread,
Mar 4, 2020, 6:48:57 PM3/4/20
to Bryan C. Mills, golang-nuts
Hi Bryan,

On 4 Mar 2020, at 2:27, 'Bryan C. Mills' via golang-nuts wrote:

> Did this work in 1.13?

Actually, no. In 1.13 it triggers the panic as described in this bug:

> https://golang.org/issue/35759 is probably related; see especially the
> TODO
> here
> <https://github.com/golang/go/blob/5a61de3fe160cc8b327ee893cd74c4d0ce9dc13d/src/cmd/go/internal/load/pkg.go#L1612-L1616>
> .

> Please file an issue <https://golang.org/issue/new> so we don't lose
> track
> of it.

Sure filed as #37675.

--
Misha
Reply all
Reply to author
Forward
0 new messages