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/link: linking with -linkshared fails if a package depends on a method in another pkg indirectly #22998

Open
yunabe opened this Issue Dec 5, 2017 · 6 comments

Comments

Projects
None yet
4 participants
@yunabe

yunabe commented Dec 5, 2017

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

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes. go1.9.2 is the latest release as of 2017-12-04.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/yunabe/local/gocode"
GORACE=""
GOROOT="/usr/lib/go-1.9"
GOTOOLDIR="/usr/lib/go-1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build706131046=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

  1. Write a package (lib0) that defines a type and a method and uses a standard library (e.g. "fmt")
  2. Write another package (lib1) that defines an instance of the type above
  3. Write another package (lib2) that calls a method from the instance defined in (2).
  4. Build these three packages respectively with -buildmode=shared -linkshared with the same -pkgdir.

I created a git repository to reproduce this bug (yunabe/gobug22998). You can reproduce this bug by running

go get -d github.com/yunabe/gobug22998
$GOPATH/src/github.com/yunabe/gobug22998/run.sh

What did you expect to see?

libgithub.com-yunabe-gobug22998-lib2.so is created corretly and it depends on lib0.so and lib1.so

What did you see instead?

Got errors from the linker

# /tmp/go-build121402299/libgithub.com-yunabe-gobug22998-lib2.so
github.com/yunabe/gobug22998/lib2.HelloAlice: missing section for relocation target github.com/yunabe/gobug22998/lib0.(*Person).GetName
github.com/yunabe/gobug22998/lib2.HelloAlice: reloc 8 to non-elf symbol github.com/yunabe/gobug22998/lib0.(*Person).GetName (outer=github.com/yunabe/gobug22998/lib0.(*Person).GetName) 0
github.com/yunabe/gobug22998/lib2.HelloAlice: undefined: "github.com/yunabe/gobug22998/lib0.(*Person).GetName"

Notes

There is a workaround for this bug. If I add an dependency to lib0 explicitly by importing _ "github.com/yunabe/linkbug/lib0" in lib2/src.go, everything works fine. Thus, I guess link with -linkshared fails to resolve indirect dependencies to methods.

Also, if you remove import "fmt" from lib0/src.go, you can build lib2.so without hitting this bug.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Dec 5, 2017

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Dec 5, 2017

@bradfitz bradfitz changed the title from Linking with -linkshared fails if a package depends on a method in another pkg indirectly to cmd/link: linking with -linkshared fails if a package depends on a method in another pkg indirectly May 29, 2018

@bradfitz

This comment has been minimized.

Member

bradfitz commented May 29, 2018

Ping @mwhudson

@mwhudson

This comment has been minimized.

Contributor

mwhudson commented May 30, 2018

Oops, sorry for missing this. I can reproduce the problem with the supplied script.

I guess the linker needs to be fixed to load any go shared libraries that are dependencies of ones it loads. This suuuper simple patch seems to fix it:

diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go
index edf3922980..37e3086ae3 100644
--- a/src/cmd/link/internal/ld/lib.go
+++ b/src/cmd/link/internal/ld/lib.go
@@ -1646,6 +1646,7 @@ func ldshlibsyms(ctxt *Link, shlib string) {
                                dep = abs
                        }
                }
+               ldshlibsyms(ctxt, dep)
                deps = append(deps, dep)
        }
 

but that almost too easy...

@yunabe

This comment has been minimized.

yunabe commented May 30, 2018

I found a similar but different bug in cmd/link with -linkshared (#25065). The bug might be related to this bug and we may be able to fix it with this bug.

Thanks,
yunabe

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Jun 23, 2018

@mwhudson Do you want to try sending in your patch?

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 29, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Dec 7, 2018

I can't recreate this problem, though I don't know what would have fixed it. Is still an issue on tip?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment